Advertisement
NB52053

Nadui_LAB4(LinkList)

Nov 2nd, 2016
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.93 KB | None | 0 0
  1. \\LinkList
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. struct node
  8. {
  9.     int value;
  10.     struct node *next;
  11. };
  12. struct node *head = NULL,*tail = NULL;
  13. void insertfirst(int x);
  14. void insertlast(int x);
  15. void print();
  16. int search(int x);
  17. void remove(int x);
  18. int length();
  19.  
  20. int main()
  21. {
  22.     while(1)
  23.     {
  24.         printf("...MENU...\n");
  25.         printf("(1) Insert First\n");
  26.         printf("(2) Insert Last\n");
  27.         printf("(3) Show All\n");
  28.         printf("(4) Search\n");
  29.         printf("(5) Remove\n");
  30.         printf("(6) Length\n");
  31.         printf("(7) Exit\n");
  32.         printf("------------\n");
  33.         printf("Enter Your Choice:");
  34.         int choice;
  35.         scanf("%d",&choice);
  36.        
  37.         int x;
  38.         switch(choice)
  39.         {
  40.             case 1:
  41.                     printf("Enter Number: ");
  42.                     scanf("%d",&x);
  43.                     insertfirst(x);
  44.                     print();
  45.                     break;
  46.             case 2:
  47.                     printf("Enter Number: ");
  48.                     scanf("%d",&x);
  49.                     insertlast(x);
  50.                     print();
  51.                     break;
  52.             case 3:
  53.                     print();
  54.                     break;
  55.             case 4:
  56.                     printf("Search value : ");
  57.                     scanf("%d",&x);
  58.                     if(search(x))
  59.                         printf("Found\n");
  60.                     else
  61.                         printf("Not Found\n");
  62.                     break;
  63.             case 5:
  64.                     printf("Which value you want to remove : ");
  65.                     scanf("%d",&x);
  66.                     remove(x);
  67.                     print();
  68.                     break;
  69.             case 6:
  70.                     printf("length = %d\n",length());
  71.                     break;
  72.             case 7:
  73.                     exit(0);
  74.         }
  75.         getchar();
  76.     }
  77. }
  78. void insertfirst(int x)
  79. {
  80.     node *temp= (node*)malloc(sizeof(node));
  81.     if(head==NULL)
  82.     {
  83.         temp->value=x;
  84.         temp->next=NULL;
  85.         head=temp;
  86.         tail=head;
  87.     }
  88.     else
  89.     {
  90.         temp->value=x;
  91.         temp->next=head;
  92.         head=temp;
  93.     }
  94.  
  95. }
  96. void print()
  97. {
  98.     node *currentnode=head;
  99.     printf("\nYour List is: ");
  100.     while(currentnode!=NULL)
  101.     {
  102.         printf("%d ",currentnode->value);
  103.         currentnode= currentnode->next;
  104.     }
  105.     printf("\n\n\n");
  106.  
  107. }
  108. void insertlast(int x)
  109. {
  110.     node *temp=(node*)malloc(sizeof(node));
  111.     if(head==NULL)
  112.     {
  113.         temp->value=x;
  114.         temp->next=NULL;
  115.         head=temp;
  116.         tail=temp;
  117.     }
  118.     else
  119.     {
  120.         temp->value=x;
  121.         temp->next=NULL;
  122.         tail->next=temp;
  123.         tail=temp;
  124.     }
  125. }
  126. int search(int x)
  127. {
  128.     node *currentnode=head;
  129.     while(currentnode!=NULL)
  130.     {
  131.         if(currentnode->value==x)
  132.             return 1;
  133.         currentnode= currentnode->next;
  134.     }
  135.     return 0;
  136. }
  137. int length()
  138. {
  139.     int l=0;
  140.     node *currentnode=head;
  141.     while(currentnode!=NULL)
  142.     {
  143.         l++;
  144.         currentnode= currentnode->next;
  145.     }
  146.     return l;
  147. }
  148. void remove(int x)
  149. {
  150.     node *temp=NULL,*prev=NULL,*currentnode =head;
  151.     if(search(x))
  152.     {
  153.         if(head->value==x)
  154.         {
  155.             temp=head;
  156.             head=head->next;
  157.             free(temp);
  158.         }
  159.         else
  160.         {
  161.             while(currentnode->next!=NULL)
  162.             {
  163.                 if(currentnode->next->value==x)
  164.                 {
  165.                     prev=currentnode;
  166.                     temp=currentnode->next;
  167.                     break;
  168.                 }
  169.                 else
  170.                     currentnode=currentnode->next;
  171.             }
  172.             if(temp->next==NULL)
  173.             {
  174.                 prev->next=NULL;
  175.                 tail=prev;
  176.                 free(temp);
  177.             }
  178.             else
  179.             {
  180.                 prev->next=temp->next;
  181.                 free(temp);
  182.             }
  183.         }
  184.     }
  185.     else
  186.         printf("Not found for remove");
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement