Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.95 KB | None | 0 0
  1. #include <cstdio>
  2.  
  3. class list  
  4. {  
  5.   private:  
  6.     struct node  
  7.     {  
  8.       int data;  
  9.       node *next;  
  10.     };    
  11.  
  12.   public:
  13.     node *head, *tail;
  14.  
  15.     list()  
  16.     {  
  17.       head=NULL;  
  18.       tail=NULL;  
  19.     }  
  20.     void displaylist()  
  21.     {  
  22.       node *temp=new node;  
  23.       temp=head;  
  24.       while(temp!=NULL)  
  25.       {  
  26.         printf("%d\n",temp->data);  
  27.         temp=temp->next;  
  28.       }  
  29.     }  
  30.     void createnode(int value)  
  31.     {  
  32.       node *temp=new node;  
  33.       temp->data=value;  
  34.       temp->next=NULL;  
  35.       if(head==NULL)  
  36.       {  
  37.         head=temp;  
  38.         tail=temp;  
  39.         temp=NULL;  
  40.       }  
  41.       else  
  42.       {  
  43.         tail->next=temp;  
  44.         tail=temp;  
  45.       }  
  46.     }  
  47.     int size()  
  48.     {  
  49.       int count=0;  
  50.       node *temp=new node;  
  51.       temp=head;  
  52.       while(temp!=NULL)  
  53.       {  
  54.         count=count+1;  
  55.         temp=temp->next;  
  56.       }  
  57.       return count;  
  58.     }  
  59.  
  60.     void insert_position(int pos, int value)  
  61.     {  
  62.       node *pre=new node;  
  63.       node *cur=new node;  
  64.       node *temp=new node;  
  65.       cur=head;
  66.       printf("Head is %d\n", head->data);
  67.       for(int i=1;i<pos;i++)  
  68.       {
  69.         pre=cur;  
  70.         cur=cur->next;
  71.         if(cur != NULL)
  72.           printf("next is %d\n", cur->data);
  73.       }  
  74.       temp->data=value;  
  75.       pre->next=temp;  
  76.       temp->next=cur;
  77.       if (pos == 1)
  78.         head = temp;
  79.       printf("new head is %d\n", head->data);
  80.  
  81.     }  
  82.  
  83.     void display(int pos)  
  84.     {  
  85.       node *temp=new node;  
  86.       temp=head;  
  87.       for (int i=1; i<pos; i++)  
  88.       {  
  89.         temp=temp->next;  
  90.       }  
  91.       printf("%d",temp->data);  
  92.     }  
  93.  
  94.     void delete_position(int pos)  
  95.     {  
  96.       node *current=new node;  
  97.       node *previous=new node;  
  98.       node *axr=new node;  
  99.       current=head;  
  100.       if (pos==1)  
  101.       {  
  102.         head=head->next;  
  103.         delete current;  
  104.         return;  
  105.       }  
  106.       for(int i=1;i<pos;i++)  
  107.       {  
  108.         previous=current;  
  109.         current=current->next;  
  110.       }  
  111.       axr=current;  
  112.       previous->next=current->next;  
  113.       delete axr;  
  114.  
  115.     }  
  116.     void insert_start(int value)  
  117.     {  
  118.       node *temp=new node;  
  119.       temp->data=value;  
  120.       temp->next=head;  
  121.       head=temp;  
  122.     }  
  123. };  
  124.  
  125.  
  126. int main()  
  127. {  
  128.   int k,x,m,i,n,s;  
  129.   list mylist;  
  130.   scanf("%d",&n);
  131.   scanf("%d",&k);  
  132.   scanf("%d",&k);  
  133.  
  134.   mylist.insert_start(k);  
  135.   printf("HEAD %d\n", mylist.head->data);
  136.   for (i=2; i<=n; i++)  
  137.   {  
  138.     scanf("%d",&k);  
  139.     scanf("%d",&x);
  140.     printf("okkkk\n");
  141.     mylist.insert_position(k,x);
  142.   }  
  143.   scanf("%d",&m);  
  144.   for (i=1; i<=m; i++)  
  145.   {  
  146.     scanf("%d",&k);  
  147.     mylist.delete_position(k);  
  148.   }  
  149.   scanf("%d",&k);  
  150.   s=mylist.size();  
  151.   printf("%d ",s);  
  152.   mylist.display(k);  
  153.   return 0;  
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement