Linked List
This problem discusses all the implementation of the linked list functions
Algorithm
Getnode()//creates node 1. [allocate memory for node] Set temp:=(node *)malloc(sizeof(node)); 2. If (temp==NULL) then: return “error†and exit 3. Else return temp; 4. Exit Insert_beg(x)//inserts element x at beginning of list 1. Call getnode(); 2. [store value in newly created node] Set temp-gt;data := x and temp-gt;next := NULL; 3. If start==NULL then: Set start := temp; 4. Else a. Set temp-gt;next := start; b. Set start := temp; 5. Exit Insert_end(x)//inserts x at the end of the list 1. Call getnode(); 2. [store value in newly created node] Set temp-gt;data := x and temp-gt;next := NULL; 3. If start==NULL then: Set start := temp; 4. Else a. Set ptr:=start; b. Repeat step c and d while ptr!=NULL c. Set save := ptr; d. Set ptr:= ptr-gt;next; e. Set save-gt;next:= temp; 5. Exit Insert_mid(index,x)//Inserts node with value x after index no. of nodes 1. [initialize ct] ct := 0; 2. Call getnode(); 3. [store value in newly created node] Set temp-gt;data := x and temp-gt;next := NULL; 4. Set ptr:= start; 5. Repeat while(ctlt;index) a. Set save := ptr; b. Set ptr := ptr -gt; next; c. [increment ct] ct++; 6. If start==NULL then: set start :=temp; 7. Else if (ptr==start) a. Set temp-gt;next := ptr; b. Set start := temp; 8. Else a. Set temp-gt;next := save-gt;next; b. Save-gt;next := temp; 9. exit reverse()//reverses the given linked list 1. Set ptr := save := start; 2. If (start==NULL) then print “list is emptyâ€; 3. Else a. Set save := NULL b. Repeat steps c,d and e while (ptr!=NULL) c. Set temp := save; d. Set save := ptr and save-gt;next:= temp; e. Set ptr := ptr-gt;next; f. Set start := save; 4. Exit Delete_node(v)//deletes node with value v from list 1. Set ptr := start; 2. If start := NULL then print “list is emptyâ€; 3. Else a. Repeat while ((ptr != NULL ) && (ptr-gt;data != v)) b. Set save := ptr; c. Set ptr := ptr -gt; next; d. [end of while] e. Repeat steps f,g,and h if(ptr-gt;data == v) then: f. If ptr==start then: i. Set del := ptr-gt;data and ptr := ptr-gt;next; ii. Free(start); iii. Set start := ptr; g. Else i. Set del:=ptr-gt;data and save-gt;next := ptr-gt;next; ii. Free ptr; h. Return del; 4. Else print “Element to be deleted not found†and exit; 5. [End] Search(v)//searches element v in list 1. Initialize ptr := start; 2. If start == NULL then : print “list is emptyâ€; 3. Else a. Do ptr := ptr-gt;next, while (ptr !=NULL && ptr-gt;data !=v) b. If ptr-gt;data == v then return ptr; c. Else print “Element not foundâ€; 4. [End] Count()//counts number of nodes in the list 1. Initialize ct := 0 and ptr := start; 2. Repeat step 3 while (ptr!=NULL) 3. [increase ct] ct++ and set ptr := ptr-gt;next; 4. Return ct and exit;
Algorithm Description
C Code
//Implement Linked List (All Basic operations)
#include<stdio.h>
#include<conio.h>
#define null 0
struct node
{
int data;
struct node *next;
};
typedef struct node Node;
Node *start=null;
int main()
{
int d,k,a,b,count;
char ch;
Node *n,*ptr,*prev,*pprev,*start1=null;
while(1)
{
k=showlist();
if(k==1)
{
while(1)
{
printf("Enter data ");
scanf("%d",&d);
n=(Node *)malloc(sizeof(Node));
n->data=d;
n->next=start;
start=n;
printf("want to enter more data (y or n) ");
fflush(stdin);
scanf("%c",&ch);
if(ch=='n')
break;
}
}
else if(k==2)
{
while(1)
{
printf("Enter data ");
scanf("%d",&d);
n=(Node *)malloc(sizeof(Node));
n->data=d;
n->next=null;
if(start!=null)
{
ptr=start;
while(ptr!=null)
{
prev=ptr;
ptr=ptr->next;
}
prev->next=n;
}
else
{
start=n;
}
printf("want to enter more data (y or n) ");
fflush(stdin);
scanf("%c",&ch);
if(ch=='n')
break;
}
}
else if(k==3)
{
printf("Enter data ");
scanf("%d",&d);
n=(Node *)malloc(sizeof(Node));
n->data=d;
printf("Before what element you want to insert ");
scanf("%d",&b);
ptr=start;
prev=start;
count=0;
while(ptr!=null)
{
if(ptr->data==b&&count==0)
{
n->next=start;
start=n;
printf("Insert Successfully ");
goto l2;
}
else if(ptr->data==b)
{
prev->next=n;
n->next=ptr;
printf("Insert Successfully ");
goto l2;
}
prev=ptr;
ptr=ptr->next;
count++;
}
l2: if(ptr==null)
{
printf("Data not found");
}
}
else if(k==4)
{
printf("Enter data ");
scanf("%d",&d);
n=(Node *)malloc(sizeof(Node));
n->data=d;
printf("After what element you want to insert ");
scanf("%d",&a);
ptr=start;
while(ptr!=null)
{
prev=ptr;
if(ptr->data==a)
{
n->next=ptr->next;
prev->next=n;
printf("Insert Successfully ");
goto l3;
}
ptr=ptr->next;
}
l3: if(ptr==null)
{
printf("Data not found");
}
}
else if(k==5)
{
count=0;
ptr=start;
while(ptr!=null)
{
count++;
ptr=ptr->next;
}
printf("\nTotal no. of nodes in LList is : %d",count);
}
else if(k==6)
{
if(start==null)
printf("Delete unsuccessful becoz LList is empty");
else
{
ptr=start;
count=ptr->data;
start=start->next;
printf("First node that is %d deleted successfully",count);
}
}
else if(k==7)
{
if(start==null)
printf("Delete unsuccessful becoz LList is empty");
else
{
ptr=start;
while(ptr->next!=null)
{
prev=ptr;
ptr=ptr->next;
}
prev->next=null;
printf("Last node that is %d deleted successfully",ptr->data);
}
}
else if(k==8)
{
if(start==null)
printf("Delete unsuccessful becoz LList is empty");
else
{
printf("Before what node u want to delete ");
scanf("%d",&b);
pprev=null;
ptr=start;
prev=start;
count=0;
while(ptr!=null)
{
if(ptr->data==b&&count==0)
{
printf("no delete");
goto l4;
}
else if(ptr->data==b&&count==1)
{
start=ptr;
printf("Delete");
goto l4;
}
else if(ptr->data==b)
{
prev->next=ptr;
printf("Delete");
goto l4;
}
else
{
pprev=prev;
prev=ptr;
ptr=ptr->next;
count++;
}
}
l4: }
}
else if(k==9)
{
if(start==null)
printf("Delete unsuccessful becoz LList is empty");
else
{
printf("After what node u want to delete ");
scanf("%d",&a);
ptr=start;
prev=null;
pprev=null;
count=0;
while(ptr!=null)
{
pprev=ptr->next;
if(ptr->data==a&&count==0)
{
ptr->next=pprev->next;
printf("Node deleted successfully ");
goto l5;
}
else if(ptr->data==a)
{
ptr->next=pprev->next;
printf("Node deleted successfully ");
goto l5;
}
else
{
prev=ptr;
ptr=ptr->next;
count++;
}
}
l5: }
}
else if(k==10)
{
printf("From what element you want to split L.List ");
scanf("%d",&a);
ptr=start;
while(ptr!=null&&ptr->data!=a)
{
ptr=ptr->next;
}
prev=ptr->next;
ptr->next=null;
start1=prev;
l6: printf("\nElements of First LL are : ");
ptr=start;
while(ptr!=null)
{
printf("%d ->",ptr->data);
ptr=ptr->next;
}
printf("/");
printf("\nElements of Second LL are : ");
ptr=start1;
while(ptr!=null)
{
printf("%d ->",ptr->data);
ptr=ptr->next;
}
printf("/");
}
else if(k==11)
{
ptr=start;
while(ptr!=null)
{
prev=ptr;
ptr=ptr->next;
}
prev->next=start1;
start1=null;
goto l7;
}
else if(k==12)
{
if(start1==null)
{
l7: printf("\nElements of LL are : ");
ptr=start;
while(ptr!=null)
{
printf("%d ->",ptr->data);
ptr=ptr->next;
}
printf("/");
}
else
goto l6;
getch();
}
else
exit(0);
}
}
int showlist()
{
int k;
printf("\nChoose your option :\n ");
printf("1. Insert in the begining\n ");
printf("2. Insert in the end\n ");
printf("3. Insert before any data\n ");
printf("4. Insert after any data\n ");
printf("5. Count the no. of nodes\n ");
printf("6. Delete the first node\n ");
printf("7. Delete the last node\n ");
printf("8. Delete before any node\n ");
printf("9. Delete after any node\n ");
printf("10. Split the L.List\n ");
printf("11. Merge the L.List\n ");
printf("12. Show Content of L. List \n ");
printf("13. Exit\n");
printf("Enter choice : ");
scanf("%d",&k);
return k;
}