Created
September 6, 2017 18:58
-
-
Save lsiddiqsunny/2ee6aa72b1628ae04a50f29a9cd0e730 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
#include<stdio.h> | |
#include<stdlib.h> | |
#define NULL_VALUE -99999 | |
#define SUCCESS_VALUE 99999 | |
struct listNode | |
{ | |
int item; | |
struct listNode * next; | |
}; | |
struct listNode * list; | |
void initializeList() | |
{ | |
list = 0; //initially set to NULL | |
} | |
int insertItem(int item) //insert at the beginning of the linked list | |
{ | |
struct listNode * newNode ; | |
newNode = (struct listNode*) malloc (sizeof(struct listNode)) ; | |
newNode->item = item ; | |
newNode->next = list ; //point to previous first node | |
list = newNode ; //set list to point to newnode as this is now the first node | |
return SUCCESS_VALUE ; | |
} | |
int deleteItem(int item) | |
{ | |
struct listNode *temp, *prev ; | |
temp = list ; //start at the beginning | |
while (temp != 0) | |
{ | |
if (temp->item == item) break ; | |
prev = temp; | |
temp = temp->next ; //move to next node | |
} | |
if (temp == 0) return NULL_VALUE ; //item not found to delete | |
if (temp == list) //delete the first node | |
{ | |
list = list->next ; | |
free(temp) ; | |
} | |
else | |
{ | |
prev->next = temp->next ; | |
free(temp); | |
} | |
return SUCCESS_VALUE ; | |
} | |
struct listNode * searchItem(int item) | |
{ | |
struct listNode * temp ; | |
temp = list ; //start at the beginning | |
while (temp != 0) | |
{ | |
if (temp->item == item) return temp ; | |
temp = temp->next ; //move to next node | |
} | |
return 0 ; //0 means invalid pointer in C, also called NULL value in C | |
} | |
void printList() | |
{ | |
if(list==0){ | |
printf("NO ELEMENT\n"); | |
return; | |
} | |
struct listNode * temp; | |
temp = list; | |
while(temp!=0) | |
{ | |
printf("%d->", temp->item); | |
temp = temp->next; | |
} | |
printf("\n"); | |
} | |
int insertLast(int item) | |
{ | |
struct listNode *current_node=list; | |
while(current_node->next!=0) | |
{ | |
current_node=current_node->next; | |
} | |
struct listNode * temp ; | |
temp = (struct listNode*) malloc (sizeof(struct listNode)) ; | |
temp->item=item; | |
temp->next=0; | |
current_node->next=temp; | |
return SUCCESS_VALUE; | |
} | |
int insertBefore(int oldItem, int newItem) | |
{ | |
struct listNode *temp, *prev ; | |
temp = list ; //start at the beginning | |
while (temp != 0) | |
{ | |
if (temp->item == oldItem) break ; | |
prev = temp; | |
temp = temp->next ; //move to next node | |
} | |
if (temp == 0) | |
{ | |
insertItem(newItem) ; | |
return SUCCESS_VALUE; | |
} //item not found to delete | |
if (temp == list) | |
{ | |
insertItem(newItem) ; | |
return SUCCESS_VALUE; | |
} | |
else | |
{ | |
struct listNode * newNode ; | |
newNode = (struct listNode*) malloc (sizeof(struct listNode)) ; | |
newNode->item=newItem; | |
newNode->next=temp; | |
prev->next=newNode; | |
return SUCCESS_VALUE; | |
} | |
return SUCCESS_VALUE ; | |
} | |
int deleteAfter(int item) | |
{ | |
struct listNode *newNode=searchItem(item); | |
if(newNode==0||newNode->next==0){ | |
return NULL_VALUE; | |
} | |
else { | |
struct listNode *temp=newNode->next; | |
newNode->next=temp->next; | |
free(temp); | |
} | |
} | |
int deleteLast() | |
{ | |
if(list==0) return NULL_VALUE; | |
struct listNode *temp, *prev ; | |
temp = list ; //start at the beginning | |
while (temp->next != 0) | |
{ | |
prev = temp; | |
temp = temp->next ; //move to next node | |
} | |
if (temp == 0) return NULL_VALUE ; //item not found to delete | |
if (temp == list) //delete the first node | |
{ | |
list = 0; | |
free(temp) ; | |
} | |
else | |
{ | |
prev->next = 0 ; | |
free(temp); | |
} | |
return SUCCESS_VALUE ; | |
} | |
int main(void) | |
{ | |
initializeList(); | |
while(1) | |
{ | |
printf("1. Insert new item. \n2. Delete item. \n3. Search item. \n"); | |
printf("4. Insert at Last . \n5.Insert before an old Value \n6.DeleteAfter \n7.Delete Last \n8. Print. \n9. exit.\n"); | |
int ch; | |
scanf("%d",&ch); | |
if(ch==1) | |
{ | |
int item; | |
scanf("%d", &item); | |
insertItem(item); | |
} | |
else if(ch==2) | |
{ | |
int item; | |
scanf("%d", &item); | |
deleteItem(item); | |
} | |
else if(ch==3) | |
{ | |
int item; | |
scanf("%d", &item); | |
struct listNode * res = searchItem(item); | |
if(res!=0) printf("Found.\n"); | |
else printf("Not found.\n"); | |
} | |
else if(ch==4) | |
{ | |
int item; | |
scanf("%d",&item); | |
insertLast(item); | |
} | |
else if(ch==5) | |
{ | |
int old,New; | |
printf("Insert oldItem:"); | |
scanf("%d",&old); | |
printf("Insert newItem:"); | |
scanf("%d",&New); | |
insertBefore(old,New ); | |
}else if(ch==6){ | |
int item; | |
scanf("%d",&item); | |
deleteAfter(item); | |
} | |
else if(ch==7){ | |
deleteLast(); | |
} | |
else if(ch==8) | |
{ | |
printList(); | |
} | |
else if(ch==9) | |
{ | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment