Created
March 19, 2019 16:47
-
-
Save SanowerTamjit/51e03ac76edb9f6a15fead9bfa647e1a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define ListInitSize 5 | |
#define NullValue -99999 | |
#define SuccessValue 99999 | |
int listMaxSize; | |
int *list; | |
int length; | |
//Initialize List | |
void initializer_list(){ | |
listMaxSize = ListInitSize; | |
list = (int*)malloc(sizeof(int)*listMaxSize); | |
length = 0; | |
} | |
//Show All Value Of List | |
void showListValues(){ | |
printf("\n"); | |
if(length > 0){ | |
printf("-------------> List items are <-------------\n"); | |
for(int i = 0; i < length; i++) | |
printf("%d \t",list[i]); | |
} | |
else{ | |
printf("-------------> List is empty <-------------"); | |
} | |
} | |
//Dynamic Main Block | |
// Dynamic ArrayList Memory Extend Block. | |
void memoryExtend(){ | |
printf("\nMemory extending..."); | |
int *tempList; | |
//Extend memory space for the tempList | |
listMaxSize *= 2; | |
tempList = (int*)malloc(sizeof(int)*listMaxSize); | |
for(int i = 0; i < length; i++) | |
tempList[i] = list[i]; | |
free(list); //free memory of list; | |
list = tempList; | |
printf("\nMemory extended."); | |
} | |
// Dynamic ArrayList Memory Decrease Block. | |
void memoryDecrease(){ | |
printf("\nMemory decreasing..."); | |
int *tempList; | |
//Extend memory space for the tempList | |
listMaxSize /= 2; | |
tempList = (int*)malloc(sizeof(int)*listMaxSize); | |
for(int i = 0; i < length; i++) | |
tempList[i] = list[i]; | |
free(list); //free memory of list; | |
list = tempList; | |
printf("\nMemory decreased..."); | |
} | |
//Dynamic Main Block | |
//Insert Item on last in ArrayList | |
int insertItem(int item) | |
{ | |
if (length == listMaxSize) | |
memoryExtend(); | |
list[length] = item ; | |
length++ ; | |
return SuccessValue ; | |
} | |
//Insert item on first in ArrayList | |
int insertFirst(int item) | |
{ | |
if (length == listMaxSize) | |
memoryExtend(); | |
for(int i = length; i > 0; i--) | |
list[i] = list[i-1]; | |
list[0] = item ; | |
length++ ; | |
return SuccessValue ; | |
} | |
//Insert before an item | |
int insertBefore(int srcItem, int item) | |
{ | |
int srcIndex = searchItem(srcItem); | |
if(srcIndex == NullValue) | |
return NullValue; | |
else{ | |
if (length == listMaxSize) | |
memoryExtend(); | |
for(int i = length; i > srcIndex; i--) | |
list[i] = list[i-1]; | |
list[srcIndex] = item; | |
length++ ; | |
return SuccessValue ; | |
} | |
} | |
//Insert after an item | |
int insertAfter(int srcItem, int item) | |
{ | |
int srcIndex = searchItem(srcItem); | |
if(srcIndex == NullValue) | |
return NullValue; | |
else{ | |
if (length == listMaxSize) | |
memoryExtend(); | |
for(int i = length; i > srcIndex; i--) | |
list[i] = list[i-1]; | |
list[srcIndex+1] = item; | |
length++ ; | |
return SuccessValue ; | |
} | |
} | |
// Item insert item with sort | |
int insertSort(int item) | |
{ | |
if (length == listMaxSize) | |
memoryExtend(); | |
int i; | |
for (i=length-1; ( i >= 0 && list[i] > item); i--) | |
list[i+1] = list[i]; | |
list[i+1] = item; | |
length++ ; | |
return SuccessValue ; | |
} | |
//Search an item | |
int searchItem(int item) | |
{ | |
for(int i = 0; i < length; i++) | |
if(list[i] == item) | |
return i; | |
return NullValue; | |
} | |
//Search Item with multi position | |
void searchItemMultiPos(int item) | |
{ | |
int c = 0; | |
for(int i = 0; i < length; i++){ | |
if(list[i] == item){ | |
if(c > 0) | |
printf(" [%d] ", i); | |
else | |
printf("\nItem(%d) found at position [%d]",item, i); | |
c++; | |
} | |
} | |
if(c == 0) | |
printf("\n\n-------------> Requested data not found <-------------"); | |
} | |
// Delete Item by index with preserve order | |
int itemDeleteAt(int pos){ | |
if ( pos < 0 || pos >= length ) | |
return NullValue; | |
for (int i = pos + 1; i < length; i++) | |
list[i-1] = list[i] ; //move item left by one | |
length-- ; | |
if(length < listMaxSize/2) | |
memoryDecrease(); | |
return SuccessValue ; | |
} | |
// Delete Item by index with not preserve order | |
int deleteItemAt1(int pos) | |
{ | |
if (pos < 0 || pos >= length) | |
return NullValue; | |
list[pos] = list[length-1] ; | |
length-- ; | |
if(length < listMaxSize/2) | |
memoryDecrease(); | |
return SuccessValue ; | |
} | |
int main(){ | |
initializer_list(); | |
showListValues(); | |
insertItem(1); | |
insertItem(2); | |
insertItem(3); | |
insertItem(4); | |
insertItem(5); | |
showListValues(); | |
insertItem(6); | |
insertItem(1); | |
insertItem(2); | |
showListValues(); | |
insertFirst(100); | |
insertFirst(400); | |
showListValues(); | |
/*int srcItem = 2; | |
int srcIndex = searchItem(srcItem); | |
if(srcIndex == NullValue) | |
printf("\n\n-------------> Requested data not found <-------------"); | |
else | |
printf("\n\nItem(%d) found at position [%d]",srcItem, srcIndex); | |
searchItemMultiPos(1); | |
*/ | |
if(insertAfter(6, 300) == NullValue){ | |
printf("\n\n-------------> Requested data not found <-------------"); | |
} | |
if(insertBefore(5, 110) == NullValue){ | |
printf("\n\n-------------> Requested data not found <-------------"); | |
} | |
showListValues(); | |
itemDeleteAt(searchItem(4)); | |
itemDeleteAt(searchItem(100)); | |
itemDeleteAt(searchItem(110)); | |
itemDeleteAt(searchItem(1)); | |
itemDeleteAt(searchItem(2)); | |
itemDeleteAt(searchItem(1)); | |
itemDeleteAt(searchItem(2)); | |
itemDeleteAt(searchItem(4)); | |
showListValues(); | |
sortList(); | |
showListValues(); | |
initializer_list(); | |
insertSort(100); | |
insertSort(20); | |
insertSort(33); | |
insertSort(14); | |
insertSort(5); | |
showListValues(); | |
printf("\n\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment