rafid_shad

Linked List (all in one)

Nov 12th, 2018 (edited)
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.08 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. struct node
  4. {
  5.     int data;
  6.     struct node *next;
  7. };
  8.  
  9.  
  10. struct node *head=NULL,*temp;
  11.  
  12.  
  13. void insert_position(int v)
  14. {
  15.     struct node *newnode;
  16.     int i=2;
  17.     newnode=(struct node*) malloc (sizeof(struct node));
  18.     cout<<"Enter information for the newnode: "<<endl;
  19.     cin>>newnode->data;
  20.     newnode->next=NULL;
  21.  
  22.     struct node *cur;
  23.  
  24.     cur=head;
  25.     if(v==1)
  26.     {
  27.         newnode->next=head;
  28.         head=newnode;
  29.  
  30.     }
  31.     else
  32.     {
  33.  
  34.         while(i!=v && cur->next!=NULL)
  35.         {
  36.  
  37.             cur=cur->next;
  38.             i++;
  39.  
  40.         }
  41.         if(cur->next==NULL && i!=v)
  42.         {
  43.             cout<<"Opps!! position was not found"<<endl;
  44.         }
  45.         else if(cur->next==NULL && i==v)
  46.         {
  47.             cur->next=newnode;
  48.             newnode->next=NULL;
  49.         }
  50.         else
  51.         {
  52.             newnode->next=cur->next;
  53.             cur->next=newnode;
  54.         }
  55.  
  56.     }
  57. }
  58.  
  59.  
  60. void insert_data(int v)
  61. {
  62.     struct node *newnode,*pre,*cur;
  63.     newnode=(struct node*)malloc(sizeof(struct node));
  64.     cout<<"Give information for newnode"<<endl;
  65.     cin>>newnode->data;
  66.     newnode->next=NULL;
  67.     pre=head;
  68.     cur=head->next;
  69.     if(pre->data==v)
  70.     {
  71.         newnode->next=pre;
  72.         head=newnode;
  73.     }
  74.     else
  75.     {
  76.         while(cur->data!=v && cur->next!=NULL)
  77.         {
  78.             pre=cur;
  79.             cur=cur->next;
  80.  
  81.         }
  82.  
  83.         if(cur->data==v)
  84.         {
  85.             newnode->next=cur;
  86.             pre->next=newnode;
  87.  
  88.         }
  89.         if(cur->data!=v && cur->next==NULL)
  90.         {
  91.              cout<<"Data was not found"<<endl;
  92.         }
  93.  
  94.     }
  95.  
  96. }
  97.  
  98. void search(int d)
  99. {
  100.  
  101.     temp=head;
  102.     int count=1;
  103.  
  104.     while(temp->data!=d && temp->next!=NULL)
  105.     {
  106.         count++;
  107.         temp=temp->next;
  108.  
  109.     }
  110.  
  111.     if(temp->data==d)
  112.     {
  113.         cout<<endl<<"**Data found at position: "<<count <<endl;
  114.  
  115.     }
  116.     else
  117.     {
  118.         cout<<endl<<"**Data was not found: "<<endl;
  119.     }
  120. }
  121.  
  122.  
  123. void delete_position(int z)
  124. {
  125.     struct node *pre,*cur;
  126.     int i=2;
  127.  
  128.     pre=head;
  129.     cur=head->next;
  130.     if(z==1)
  131.     {
  132.         head=cur;
  133.         delete(pre);
  134.     }
  135.     else
  136.     {
  137.         while(i!=z && cur->next!=NULL)
  138.         {
  139.             pre=cur;
  140.             cur=cur->next;
  141.  
  142.             i++;
  143.  
  144.         }
  145.         if(i!=z && cur->next==NULL)
  146.         {
  147.             cout<<"Position was not found in the list"<<endl;
  148.         }
  149.         else
  150.         {
  151.             pre->next=cur->next;
  152.             delete(cur);
  153.         }
  154.     }
  155.  
  156. }
  157.  
  158.  
  159. void delete_data(int g)
  160. {
  161.     int i;
  162.     i=g;
  163.     struct node *pre,*cur;
  164.  
  165.     pre=head;
  166.     cur=head->next;
  167.     if(pre->data==g)
  168.     {
  169.         head=cur;
  170.         delete(pre);
  171.     }
  172.     else
  173.     {
  174.         while(cur->data!=g && cur->next!=NULL)
  175.         {
  176.            pre=cur;
  177.            cur=cur->next;
  178.  
  179.         }
  180.         if(cur->next==NULL && cur->data==g)
  181.         {
  182.             pre->next=NULL;
  183.             delete(cur);
  184.         }
  185.         else if(cur->data==g)
  186.         {
  187.             pre->next=cur->next;
  188.             delete(cur);
  189.         }
  190.         else
  191.         {
  192.             cout<<"Data was not found"<<endl;
  193.         }
  194.     }
  195.  
  196. }
  197.  
  198.  
  199. void display()
  200. {
  201.     temp=head;
  202.     cout<<endl;
  203.     while(temp!=NULL)
  204.     {
  205.         cout<<temp->data<<endl;
  206.         temp=temp->next;
  207.     }
  208. }
  209.  
  210.  
  211. int main()
  212. {
  213.     int n,i,x;
  214.     struct node *s;
  215.     s=(struct node*)malloc(sizeof(struct node));
  216.  
  217.     cout<<"Enter how many link list u want:"<<endl;
  218.     cin>>n;
  219.     cout<<"For inserting first press 1:"<<endl<<endl;;
  220.     cout<<"For inserting in last press 2:"<<endl;
  221.     cin>>x;
  222.     cout<<endl;
  223.     cout<<"Enter information for newnode"<<endl;
  224.  
  225.     for(i=1; i<=n; i++)
  226.     {
  227.         struct node *newnode;
  228.         newnode=(struct node*) malloc (sizeof(struct node));
  229.         cin>>newnode->data;
  230.         newnode->next=NULL;
  231.  
  232.         if(x==1)
  233.         {
  234.             if(head==NULL)
  235.             {
  236.                 head=newnode;
  237.             }
  238.             else
  239.             {
  240.                 newnode->next=head;
  241.                 head=newnode;
  242.             }
  243.         }
  244.         if(x==2)
  245.         {
  246.             if(head==NULL)
  247.             {
  248.                 head=newnode;
  249.                 s=head;
  250.             }
  251.  
  252.             else
  253.             {
  254.                 s->next=newnode;
  255.                 s=newnode;
  256.             }
  257.  
  258.         }
  259.     }
  260.     cout<<endl;
  261.     cout<<"Enter which data your searching for :"<<endl;
  262.     int d;
  263.     cin>>d;
  264.     search(d);
  265.     display();
  266.  
  267.     cout<<"Enter a position of a note which u want to delete:"<<endl;
  268.     int z;
  269.     cin>>z;
  270.     delete_position(z);
  271.     display();
  272.  
  273.     cout<<"Enter a data of a note which u want to delete:"<<endl;
  274.     int g;
  275.     cin>>g;
  276.     delete_data(g);
  277.     display();
  278.  
  279.     cout<<"Enter a position where u want to insert: "<<endl;
  280.     int v;
  281.     cin>>v;
  282.     insert_position(v);
  283.     display();
  284.  
  285.     cout<<"Enter a data where u want to insert: "<<endl;
  286.     cin>>v;
  287.     insert_data(v);
  288.     display();
  289.     return 0;
  290.  
  291. }
Add Comment
Please, Sign In to add comment