Skip to content

Instantly share code, notes, and snippets.

@SanowerTamjit
Created March 20, 2019 03:30
Show Gist options
  • Save SanowerTamjit/44c34fdb2128f6ce6b71b7c7258c0693 to your computer and use it in GitHub Desktop.
Save SanowerTamjit/44c34fdb2128f6ce6b71b7c7258c0693 to your computer and use it in GitHub Desktop.
#define NULL_VALUE -99999
#define SUCCESS_VALUE 99999
struct nodeList
{
int item ; //for store the value
struct node *next ; //indicate next node
} ;
struct nodeList *list ;
void initializeList()
{
list = 0 ;
}
//insert item on linked list
int insertItem(int item)
{
struct nodeList *newNode ;
newNode = (struct nodeList*) malloc (sizeof(struct nodeList)) ;
newNode->item = item ;
newNode->next = list ;
list = newNode ;
return SUCCESS_VALUE ;
}
//Search an item in linkedList
struct nodeList *searchItem(int item){
struct nodeList *tempList; //declaring a tempListNode;
tempList = list; // move all node to tempList
while(tempList != 0){
if(tempList->item == item) //Is item found
return tempList;
tempList = tempList->next; //assign next node to tempList
}
return 0;
};
//Delete node from linked list
int deleteItem(int item){
struct nodeList *tempList, *prevList;
tempList = list;
while(tempList != 0){
//printf("\n%d",tempList->item);
if(tempList->item == item)
break;
prevList = tempList; // act templist as prevlist
tempList = tempList->next; // templist move to next node
//printf("\n\n%d",tempList->item);
}
if(tempList == 0)
return NULL_VALUE;
if(tempList == list) //first Node delte
list = list->next;
else{
//struct nodeList *tempList1 = prevList->next;
//struct nodeList *tempList2 = tempList->next;
//printf("\n\n%d\n",tempList1->item);
//printf("%d\n",tempList2->item);
prevList->next = tempList->next;
}
free(tempList);
return SUCCESS_VALUE;
}
// Show all node values
void showListNodeValues(){
printf("\n");
struct nodeList *tempList ;
tempList = list;
while(tempList != 0){
printf("%d\t",tempList->item);
tempList = tempList->next;
}
printf("\n");
}
int main(){
initializeList();
insertItem(2);
insertItem(5);
insertItem(10);
showListNodeValues();
/*struct nodeList *tempList;
tempList = searchItem(2);
printf(tempList->item);
*/
deleteItem(5);
showListNodeValues();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment