Advertisement
Rakibul_Ahasan

Stack_Linked_List(v.V.I)

Aug 22nd, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.49 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct node
  5. {
  6.     int val;
  7.     struct node *next;
  8. };
  9.  
  10. struct node *head=NULL,*last=NULL;
  11.  
  12. void insert(int value)
  13. {
  14.     struct node *tmp;
  15.  
  16.     tmp=(struct node *)malloc(sizeof(node));
  17.  
  18.     tmp->val=value;
  19.     tmp->next=NULL;
  20.  
  21.     ///for the first element in the list
  22.     if(head==NULL)
  23.     {
  24.         head=tmp;
  25.         last=tmp;
  26.     }
  27.     else
  28.     {
  29.         last->next=tmp;
  30.         last=tmp;
  31.     }
  32. }
  33.  
  34. void printlist()
  35. {
  36.     struct node *tmp=head;
  37.     while(tmp!=NULL)
  38.     {
  39.         printf("The list Value:%d\n\n",tmp->val);
  40.         tmp=tmp->next;
  41.     }
  42. }
  43.  
  44. int search(int value)
  45. {
  46.     struct node* tmp=head;
  47.  
  48.     while(tmp!=NULL)
  49.     {
  50.         if(tmp->val==value)
  51.             return 1;
  52.  
  53.         tmp=tmp->next;
  54.     }
  55.  
  56.     return 0;
  57. }
  58.  
  59. void deletenode(int value)
  60. {
  61.     struct node* tmp=head;
  62.     struct node* prev=NULL;
  63.  
  64.     while(tmp!=NULL)
  65.     {
  66.         if(tmp->val==value)
  67.         {
  68.             if(prev==NULL)
  69.             {
  70.                 head=tmp->next;
  71.                 printf("The number is delete\n\n");
  72.             }
  73.             else{
  74.                 prev->next=tmp->next;
  75.                 printf("The number is delete\n\n");
  76.             }
  77.  
  78.             break;
  79.         }
  80.  
  81.         prev=tmp;
  82.         tmp=tmp->next;
  83.     }
  84. }
  85.  
  86.  
  87. void menu () {
  88.      while (1) {
  89.         printf("Press 1 to insert.\n");
  90.         printf("Press 2 to search.\n");
  91.         printf("Press 3 to delete node.\n");
  92.         printf("Press 4 to print list.\n");
  93.         printf("Press 0 to Exit.\n");
  94.         printf("\n");
  95.  
  96.         int n;
  97.         scanf("%d", &n);
  98.  
  99.         if (n==0) break;
  100.  
  101.         else if (n==1) {
  102.             printf("Enter the number: ");
  103.             int num;
  104.             scanf("%d", &num);
  105.             printf("\n");
  106.  
  107.             insert(num);
  108.         }
  109.  
  110.         else if (n==2) {
  111.             printf("Enter the number: ");
  112.             int num;
  113.             scanf("%d", &num);
  114.             printf("\n");
  115.  
  116.             if (search(num)) printf("The Number is found.\n\n");
  117.             else printf("The Number is Not found.\n\n");
  118.         }
  119.  
  120.         else if (n==3) {
  121.             printf("Enter the number: ");
  122.             int num;
  123.             scanf("%d", &num);
  124.             printf("\n");
  125.  
  126.             deletenode(num) ;
  127.         }
  128.  
  129.         else if (n==4) printlist();
  130.  
  131.         else {
  132.             printf("Invalid choice! Try again.\n\n");
  133.         }
  134.     }
  135. }
  136.  
  137. int main () {
  138.     menu();
  139.     return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement