Advertisement
Shailrshah

Operations on a Linked List

Sep 26th, 2013
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.72 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct node{
  4.     int data;
  5.     struct node* next;
  6. }*headA = NULL;
  7. void display(struct node* help){
  8.     if(!help) printf(" Empty!");
  9.     while(help){
  10.         printf(" %d ", help->data);
  11.         help = help->next;
  12.     }
  13. }
  14. struct node* insert(int value, struct node*help){
  15.     struct node *newNode = (struct node*) malloc(sizeof(newNode));
  16.     newNode->data = value; newNode->next = NULL;
  17.     if(help) newNode->next = help;
  18.     help = newNode;
  19.     display(help);
  20.     return help;
  21. }
  22. struct node* input(struct node *head){
  23.     int n;
  24.     while(1){
  25.         printf("\nInput(-1 to stop): ");
  26.         scanf("%d",&n);
  27.         if(n!=-1) head = insert(n, head);
  28.         else return head;
  29.     }
  30. }
  31. void copy(struct node* help){
  32.     struct node *headB = NULL, *last = NULL;
  33.     while(help){
  34.         struct node *newNode = (struct node*) malloc(sizeof(struct node));
  35.         newNode->data = help->data; newNode->next = NULL;
  36.         if(!headB){
  37.             headB = newNode;
  38.             last = newNode;
  39.         } else{
  40.             last->next = newNode;
  41.             last = last->next;
  42.         }
  43.         help = help->next;
  44.     }
  45.     help = last = NULL;
  46.     free(help); free(last);
  47.     printf("\nThe copied linked list is:");
  48.     display(headB); free(headB);
  49. }
  50. void concatenate(struct node *help){
  51.     struct node *headB = NULL;
  52.     printf("\nEnter elements for the second Linked List\n");
  53.     headB = input(headB);
  54.     if(!help) headA = headB;
  55.     while(help->next) help = help->next;
  56.     help->next = headB;
  57.     printf("\nTwo linked lists have been concatenated:");
  58.     display(headA);
  59.     headB = NULL; free(headB);
  60. }
  61. void split(struct node *help, int index){
  62.     for(int i = 0; i < index; i++) help = help->next;
  63.     struct node *headB = help->next;
  64.     help->next = NULL;
  65.     printf("\nThe first linked list:"); display(headA);
  66.     printf("\nThe second linked list:"); display(headB); free(headB);
  67. }
  68. void reverse(struct node* help){
  69.     struct node *forward = headA, *back = NULL;
  70.     while (forward){
  71.         help = forward;
  72.         forward = help->next;
  73.         help->next = back;
  74.         back = help;
  75.     }
  76.     headA = back;
  77.     back = forward = help = NULL;
  78.     free(back); free(forward); free(help);
  79.     printf("\nThe linked list has been reversed:");
  80.     display(headA);
  81. }
  82. int count(struct node* help){
  83.     int count = 0;
  84.     while(help){
  85.         count++;
  86.         help = help->next;
  87.     }
  88.     return count;
  89. }
  90. int main(){
  91.     int n;
  92.     printf("\nEnter elements for first Linked List.\n");
  93.     headA = input(headA);
  94.     while(1){
  95.         printf("\n1.Copy 2.Concatenate 3.Split 4.Reverse 5.Count 6.Exit: ");
  96.         scanf("%d",&n);
  97.         switch(n){
  98.             case 1: copy(headA); break;
  99.             case 2: concatenate(headA); break;
  100.             case 3: split(headA,count(headA)/2-1); break;
  101.             case 4: reverse(headA); break;
  102.             case 5: printf("\nThe number of nodes are %d.", count(headA)); break;
  103.             case 6: return 0;
  104.             default: printf("\nEnter a number between 1 and 6.");
  105.         }
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement