Advertisement
rafid_shad

exm 6

Oct 24th, 2018
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.38 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4.  
  5. struct node
  6. {
  7.     int data;
  8.     int phone;
  9.     char name [20];
  10.     struct node *next;
  11. };
  12.  
  13. struct node *head=NULL,*temp,*p,*store;
  14.  
  15.  
  16. void display()
  17. {
  18.     temp=head;
  19.     cout<<endl;
  20.     while(temp!=NULL)
  21.     {
  22.         cout<<temp->data<<endl;
  23.         cout<<temp->phone<<endl;
  24.         cout<<temp->name<<endl;
  25.         temp=temp->next;
  26.     }
  27. }
  28.  
  29.  
  30. void findata(int d)
  31. {
  32.     temp=head;
  33.     int count=0;
  34.     cout<<"Give new node information: "<<endl;
  35.     p=(struct node*) malloc (sizeof(struct node));
  36.     store =(struct node*) malloc (sizeof(struct node));
  37.     cin>>p->data;
  38.     cin>>p->phone;
  39.     cin>>p->name;
  40.     p->next=NULL;
  41.  
  42.     while(temp->data!=d && temp->next!=NULL)
  43.     {
  44.         count++;
  45.         temp=temp->next;
  46.  
  47.     }
  48.  
  49.     if(temp->data==d)
  50.     {
  51.         cout<<endl<<"**Data found at position: "<<count+1 <<endl;
  52.  
  53.         store->next=temp->next;
  54.         temp->next=p;
  55.         p->next=store->next;
  56.  
  57.     }
  58.     else
  59.     {
  60.         cout<<endl<<"**Data was not found: "<<endl;
  61.     }
  62. }
  63.  
  64. void edit(int ha)
  65. {
  66.     temp=head;
  67.     int count=0;
  68.  
  69.     while(temp->data!=ha&& temp->next!=NULL)
  70.     {
  71.         count++;
  72.         temp=temp->next;
  73.  
  74.     }
  75.  
  76.     if(temp->data==ha)
  77.     {
  78.         cout<<endl<<"**Data found at position: "<<count+1 <<endl;
  79.  
  80.         cin>>temp->data;
  81.         cin>>temp->phone;
  82.         cin>>temp->name;
  83.     }
  84.     else
  85.     {
  86.         cout<<endl<<"**Data was not found: "<<endl;
  87.     }
  88. }
  89.  
  90.  
  91. void delete_data(int g)
  92. {
  93.     struct node *l,*m;
  94.  
  95.     l=head;
  96.     m=head->next;
  97.     if(l->data==g)
  98.     {
  99.         head=m;
  100.         delete(l);
  101.     }
  102.     else
  103.     {
  104.         while(m->data!=g)
  105.         {
  106.             m=m->next;
  107.             l=l->next;
  108.  
  109.         }
  110.         if(m->next==NULL)
  111.         {
  112.             l->next=NULL;
  113.             delete(m);
  114.         }
  115.  
  116.         else
  117.         {
  118.             l->next=m->next;
  119.             delete(m);
  120.         }
  121.     }
  122.  
  123. }
  124.  
  125.  
  126.  
  127. int main()
  128. {
  129.     int n,i,x;
  130.  
  131.     cout<<"Enter how many link list u want:"<<endl;
  132.     cin>>n;
  133.     cout<<"Where u want to insert:"<<endl<<endl;
  134.     cout<<"For inserting first press 1:"<<endl;
  135.     cout<<"For inserting in last press 2:"<<endl;
  136.     cin>>x;
  137.  
  138.  
  139.     for(i=1; i<=n; i++)
  140.     {
  141.         struct node *newnode,*s;
  142.         newnode=(struct node*) malloc (sizeof(struct node));
  143.         cout<<"Enter information: "<<endl;
  144.         cin>>newnode->data;
  145.         cin>>newnode->phone;
  146.         cin>>newnode->name;
  147.         newnode->next=NULL;
  148.  
  149.         if(x==1)
  150.         {
  151.             if(head==NULL)
  152.             {
  153.                 head=newnode;
  154.             }
  155.             else
  156.             {
  157.                 newnode->next=head;
  158.                 head=newnode;
  159.             }
  160.         }
  161.         if(x==2)
  162.         {
  163.             if(head==NULL)
  164.             {
  165.                 head=newnode;
  166.                 s=head;
  167.             }
  168.  
  169.             else
  170.             {
  171.                 s->next=newnode;
  172.                 s=newnode;
  173.             }
  174.  
  175.         }
  176.     }
  177.     display();
  178.     cout<<"Enter which data your searching for:"<<endl;
  179.     int d;
  180.     cin>>d;
  181.     findata(d);
  182.     display();
  183.  
  184.     cout<<"Enter a data of a note which u want to delete:"<<endl;
  185.     int g;
  186.     cin>>g;
  187.     delete_data(g);
  188.     display();
  189.  
  190.     int ha;
  191.     cout<<"Enter where you want to edit"<<endl;
  192.     cin>>ha;
  193.     edit(ha);
  194.     display();
  195.  
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement