Advertisement
rafid_shad

exm 4

Oct 24th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.15 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. struct node
  5. {
  6.     int data;
  7.     struct node *next;
  8.  
  9. };
  10.  
  11. struct node *head=NULL,*temp,*p,*store;
  12.  
  13.  
  14. void insert_position(int v)
  15. {
  16.     struct node *newnode;
  17.     int i;
  18.     newnode=(struct node*) malloc (sizeof(struct node));
  19.     cout<<"Enter information for the newnode: "<<endl;
  20.     cin>>newnode->data;
  21.     newnode->next=NULL;
  22.  
  23.     struct node *cur,*pre;
  24.  
  25.     cur=head;
  26.     if(v==1)
  27.     {
  28.         newnode->next=head;
  29.         head=newnode;
  30.  
  31.     }
  32.     else
  33.     {
  34.  
  35.         for(i=1; i<v-1; i++)
  36.         {
  37.             pre=cur;
  38.             cur=cur->next;
  39.         }
  40.  
  41.         newnode->next=cur->next;
  42.         cur->next=newnode;
  43.  
  44.     }
  45.  
  46.  
  47.  
  48.  
  49. }
  50. void display()
  51. {
  52.     temp=head;
  53.     cout<<endl;
  54.     int h=0;
  55.     while(temp!=NULL)
  56.     {
  57.         cout<<temp->data<<endl;
  58.         temp=temp->next;
  59.         h++;
  60.     }
  61.     cout<<"Total nodes "<<h<<endl;
  62. }
  63.  
  64. int main()
  65. {
  66.     int n,i,x;
  67.  
  68.     cout<<"Enter how many link list u want:"<<endl;
  69.     cin>>n;
  70.     cout<<"Where u want to insert:"<<endl<<endl;
  71.     cout<<"For inserting first press 1:"<<endl;
  72.     cout<<"For inserting in last press 2:"<<endl;
  73.     cin>>x;
  74.  
  75.  
  76.     for(i=1; i<=n; i++)
  77.     {
  78.         struct node *newnode,*s;
  79.         newnode=(struct node*) malloc (sizeof(struct node));
  80.         cout<<"Enter information: "<<endl;
  81.         cin>>newnode->data;
  82.         newnode->next=NULL;
  83.  
  84.         if(x==1)
  85.         {
  86.             if(head==NULL)
  87.             {
  88.                 head=newnode;
  89.             }
  90.             else
  91.             {
  92.                 newnode->next=head;
  93.                 head=newnode;
  94.             }
  95.         }
  96.         if(x==2)
  97.         {
  98.             if(head==NULL)
  99.             {
  100.                 head=newnode;
  101.                 s=head;
  102.             }
  103.  
  104.             else
  105.             {
  106.                 s->next=newnode;
  107.                 s=newnode;
  108.             }
  109.  
  110.         }
  111.  
  112.  
  113.  
  114.     }
  115.     cout<<"Enter a position where u want to insert: "<<endl;
  116.     int v;
  117.     cin>>v;
  118.     insert_position(v);
  119.  
  120.     cout<<"Enter a second position where u want to insert"<<endl;
  121.     cin>>v;
  122.     insert_position(v);
  123.     display();
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement