Advertisement
Misbah_Uddin_Tareq

5.3

Dec 2nd, 2019
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.80 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<iostream>
  3. #include<malloc.h>
  4. #include<string.h>
  5. using namespace std;
  6.  
  7. void display(void);
  8. void insert_loc(void);
  9. void insert_first(void);
  10. char item[10];
  11.  
  12. struct node{
  13.     char info[10];
  14.     struct node* link;
  15. }*start=NULL;
  16.  
  17. int main()
  18. {
  19.     int choice;
  20.     do{
  21.         printf("\n\n 1)insert first\n 3)insert loc\n 4)display\n 8)exit\n");
  22.         printf("enter choice:");
  23.         scanf("%d",&choice);
  24.         switch(choice)
  25.         {
  26.  
  27.             case 1:
  28.                 insert_first();
  29.                 break;
  30.             case 3:
  31.                 insert_loc();
  32.                 break;
  33.             case 4:
  34.                 display();
  35.                 break;
  36.             case 8:
  37.                 printf("\nBYE BYE!!!!");
  38.                 return 0;
  39.             default :
  40.                 printf("invalid key...please try again!!!");
  41.  
  42.         }
  43.     }while(1);
  44.  
  45. }
  46.  
  47.  
  48. void insert_loc(void)
  49. {
  50.  
  51.     struct node *ptr,*newn;
  52.     ptr=start;
  53.     newn=start;
  54.     if(start==NULL)
  55.     cout<<"\nthe list is empty!!!,insert atleast one node...\n";
  56.     else
  57. {
  58.     cout<<"enter an item after which a node to be inserted:";
  59.     cin>>item;
  60.     cout<<"\nitem to be inserted:";
  61.     while(ptr!=NULL)
  62.     {
  63.         if(strcmp(ptr->info,item)==0)
  64.     {
  65.         newn=(struct node *)malloc(sizeof(struct node));
  66.         cin>>newn->info;
  67.         newn->link=ptr->link;
  68.         ptr->link=newn;
  69.         break;
  70.     }
  71.     else
  72.     ptr=ptr->link;
  73.  
  74.     }
  75. }
  76. cout<<"item inserted:"<<newn->info;
  77. return;
  78. }
  79. void display(void)
  80. {
  81.  
  82.     struct node *ptr=start;
  83.     int n=1;
  84.     if(ptr!=NULL)
  85. {
  86.     while(ptr!=NULL)
  87.     {
  88.         printf("no %d:%s\n",n++,ptr->info);
  89.         ptr=ptr->link;
  90.     }
  91. }
  92.     else
  93.     {
  94.         printf("List is empty!!!\n");
  95.     }
  96.  
  97. }
  98.  
  99. void insert_first(void)
  100. {
  101.     int n=1;
  102. char item1[10];
  103.     struct node *ptr;
  104.     printf("item to be inserted:");
  105.      cin>>item1;
  106.     if(start==NULL)
  107.     {
  108.     start=(struct node *)malloc(sizeof(struct node));
  109.     strcpy(start->info,item1);
  110.     start->link=NULL;
  111.  
  112.     }
  113.     else
  114.     {
  115.     ptr=start;
  116.     start=(struct node *)malloc(sizeof(struct node));
  117.     strcpy(start->info,item1);
  118.     start->link=ptr;
  119.     }
  120.     cout<<"item inserted:"<<item1;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement