Advertisement
apl-mhd

Bappy sir 2 final

Mar 17th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.11 KB | None | 0 0
  1. #include <cstring>
  2. #include <iostream>
  3. #include <cstdio>
  4. using namespace std;
  5.  
  6. struct list{
  7.  
  8.         int data;
  9.         struct list *next;
  10.         struct list *prev;
  11.  
  12. };
  13.  
  14. typedef struct list node;
  15.  
  16. node *Insert(node *head, int y,int x){
  17.  
  18.         node *temp, *temp2,*temp3, *prev;
  19.  
  20.  
  21.               temp2 = head;
  22.  
  23.         if(head == NULL){
  24.             head= new node();
  25.             temp= new node();
  26.  
  27.             head->next = temp;
  28.             head->prev = NULL;
  29.             temp->data = x;
  30.             temp->next = NULL;
  31.             temp->prev = head;
  32.  
  33.             printf("INSERT after dummy node\n");
  34.  
  35.         return head;
  36.  
  37.         }
  38.  
  39.  
  40.         temp2=head->next;
  41.         while(temp2->data != y  && temp2->next !=NULL)
  42.  
  43.                 temp2= temp2->next;
  44.  
  45.          if(temp2->data == y &&temp2->next == NULL){
  46.  
  47.             temp3 = new node();
  48.  
  49.             temp3->data = x;
  50.             temp3->next = NULL;
  51.             temp2->next=temp3;
  52.             temp3->prev = temp2;
  53.  
  54.             printf("INSERT after %d \n",y);
  55.  
  56.             return head;
  57.         }
  58.  
  59.         else  if(temp2->next == NULL){
  60.                 //printf("orko %d\n",head->next->data);
  61.  
  62.                 temp3 = new node();
  63.                 temp3->data = x;
  64.                 temp3->next= head->next;
  65.                 head->next = temp3;
  66.                 temp3->prev = head;
  67.  
  68.                 temp3->next->prev = temp3;
  69.                 printf("INSERT after dummy node\n");
  70.  
  71.                     return head;
  72.  
  73.         }
  74.  
  75.         else if(temp2->data == y){
  76.                 temp3 = new node();
  77.  
  78.                 temp3->data = x;
  79.                 temp3->next=temp2->next;
  80.                 temp3->next->prev = temp3;
  81.                 temp2->next=temp3;
  82.                 temp3->prev = temp;
  83.  
  84.             printf("INSERT after %d \n",y);
  85.  
  86.  
  87.             return head;
  88.         }
  89.  
  90.  
  91.     }
  92.  
  93.  
  94. void shw(node *head){
  95.  
  96.     if(head->next == NULL){
  97.         printf("no data found");
  98.         return;
  99.     }
  100.  
  101.     while(head->next !=NULL){
  102.  
  103.         printf("%d ", head->next->data);
  104.         head= head->next;
  105.     }
  106.     cout<<endl;
  107. }
  108.  
  109. node *delItem(node *head, int value){
  110.  
  111.             node *temp;
  112.             temp = head->next;
  113.  
  114.         while(temp->data !=value && temp->next !=NULL)
  115.                 temp = temp->next;
  116.  
  117.  
  118.  
  119.         if(temp->data == value &&temp->next ==NULL){
  120.  
  121.             temp->prev->next = NULL;
  122.  
  123.             delete temp;
  124.            return head;
  125.         }
  126.  
  127.         else if(temp->data == value){
  128.  
  129.                 temp->prev->next = temp->next;
  130.                 temp->next->prev = temp->prev;
  131.                
  132.                 printf("Node with key value %d is DELETED\n", value);
  133.  
  134.                 delete temp;
  135.  
  136.             return head;
  137.  
  138.         }
  139.  
  140.         else{
  141.  
  142.             printf("delete not possible \n");
  143.             return head;
  144.         }
  145. }
  146.  
  147. void searchData(node *head, int value){
  148.  
  149.         node *temp;
  150.         temp = head->next;
  151.         while(temp->next !=NULL && temp->data == value)
  152.             temp  = temp->next;
  153.         if(temp->data = value)
  154.             printf("Node with key value %d is FOUNDED\n",temp->data);
  155.         else
  156.         {
  157.              printf("Node found\n",temp->data);
  158.         }
  159. }
  160.  
  161.  
  162. int main(int argc, char **argv)
  163. {
  164.  
  165.  
  166.     int flag=1;
  167.     int y, x,del, value;
  168.     char function[20];
  169.  
  170.     node *head;
  171.  
  172.     head = NULL;
  173.  
  174.  
  175.  
  176.     while(flag){
  177.  
  178.         scanf("%s",function);
  179.  
  180.         if(strcmp(function, "ins")==0){
  181.  
  182.  
  183.                 scanf("%d%d", &y,&x);
  184.  
  185.                  head= Insert(head,y,x);
  186.  
  187.  
  188.             }
  189.  
  190.  
  191.  
  192.         if(strcmp(function, "del")==0){
  193.  
  194.             cin>>del;
  195.             head = delItem(head, del);
  196.  
  197.             }
  198.  
  199.         if(strcmp(function, "sch")==0){
  200.  
  201.             cin>>value;
  202.             searchData(head,value);
  203.  
  204.         }
  205.  
  206.          if(strcmp(function, "shw")==0){
  207.  
  208.                 shw(head);
  209.  
  210.  
  211.             }
  212.  
  213.  
  214.         if(strcmp(function, "ext")==0){
  215.  
  216.             flag =0;
  217.  
  218.             }
  219.  
  220.  
  221.         }
  222.  
  223.     return 0;
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement