Advertisement
niamulhasan

Assignment 2 for test

Oct 17th, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.57 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<malloc.h>
  3.  
  4.     struct node{
  5.         struct node *prev;
  6.         int data;
  7.         struct node *next;
  8.     } *a, *b, *c, *d, *head, *tail, *current;
  9.  
  10. //1. Create the linked list with 4 nodes.
  11.     void create(){
  12.         a = (struct node*)malloc(sizeof(struct node));
  13.         b = (struct node*)malloc(sizeof(struct node));
  14.         c = (struct node*)malloc(sizeof(struct node));
  15.         d = (struct node*)malloc(sizeof(struct node));
  16.  
  17.         a->prev = NULL;
  18.         a->data = 10;
  19.         a->next = b;
  20.  
  21.         b->prev = a;
  22.         b->data = 20;
  23.         b->next = c;
  24.  
  25.         c->prev = b;
  26.         c->data = 30;
  27.         c->next = d;
  28.  
  29.         d->prev = c;
  30.         d->data = 40;
  31.         d->next = NULL;
  32.  
  33.         head = a;
  34.         tail = d;
  35.     }
  36.  
  37.     void display(){
  38.         current = head;
  39.         while(current != NULL){
  40.             printf("%d ", current->data);
  41.             current = current->next;
  42.         }
  43.     }
  44.  
  45. // 2. Insert a new node at the beginning.
  46.     void appendFirst(int key){
  47.         struct node *newNode;
  48.         current = head;
  49.         newNode = (struct node*)malloc(sizeof(struct node));
  50.  
  51.         newNode->prev = NULL;
  52.         newNode->data = key;
  53.         newNode->next = head;
  54.  
  55.         head = newNode;
  56.     }
  57.  
  58.  
  59. // 3. Insert a new node at the end.
  60.     void appendLast(int key){
  61.         current = tail;
  62.         struct node *newNode;
  63.         newNode = (struct node*)malloc(sizeof(struct node));
  64.         newNode->data = key;
  65.         newNode->next = NULL;
  66.  
  67.         tail->next = newNode;
  68.         newNode->prev = tail;
  69.     }
  70.  
  71. // 4. Insert a new node after the 3rd node.
  72.     void appendAtThird(int data){
  73.         current = head;
  74.         struct node *newNode;
  75.         newNode = (struct node*)malloc(sizeof(struct node));
  76.         newNode->data = data;
  77.  
  78.         int i=1;
  79.         for(i=1;i<3;i++){
  80.             current = current->next;
  81.         }
  82.         newNode->next = current->next;
  83.         current->next = newNode;
  84.         newNode->prev = current;
  85.         current->next->prev = newNode;
  86.     }
  87.  
  88. // 5. Insert a new node after a node by searching the data with a specific key.
  89.     void appendAfterKey(int key){
  90.         current = head;
  91.         while(current->data != key){
  92.             current = current->next;
  93.         }
  94.  
  95.         struct node *newNode;
  96.         newNode = (struct node*)malloc(sizeof(struct node));
  97.         newNode->data = 666;
  98.  
  99.         newNode->next = current->next;
  100.         current->next = newNode;
  101.         current->next->prev = newNode;
  102.         newNode->prev = current;
  103.     }
  104.  
  105. //6.Insert a new node before a node by searching the data with a specific key.
  106.     void appendBeforeKey(int key){
  107.         current = head;
  108.         while(current->next->data != key){
  109.             current = current->next;
  110.         }
  111.  
  112.         struct node *newNode;
  113.         newNode = (struct node*)malloc(sizeof(struct node));
  114.         newNode->data = 444;
  115.  
  116.         newNode->next = current->next;
  117.         current->next = newNode;
  118.         current->next->prev = newNode;
  119.         newNode->prev = current;
  120.     }
  121.  
  122. // 7. Update data of a node after searching the node and replace by a inputted value.
  123.     void updateAtKey(int key){
  124.         current = head;
  125.         while(current->data != key){
  126.             current = current->next;
  127.         }
  128.         current->data = 22;
  129.     }
  130.  
  131. // 8. Delete the first node.
  132.     void deleteFirst(){
  133.         current = head;
  134.         current->next->prev = NULL;
  135.         head = current->next;
  136.     }
  137.  
  138. //9. Delete the last node.
  139.     void deleteLast(){
  140.         current = tail;
  141.         current->prev->next = NULL;
  142.         tail = tail;
  143.  
  144.     }
  145.  
  146. //10. Delete the node after searching with a specific key.
  147.     void deleteAtKey(int key){
  148.         current = head;
  149.         while(current->next->data != key){
  150.             current = current->next;
  151.         }
  152.         current->next = current->next->next;
  153.         current->next->next->prev = current;
  154.     }
  155.  
  156. //(Pro) 11. Delete the next node after searching with a specific key.
  157.     void deleteAfterKey(int key){
  158.         current = head;
  159.         while(current->data != key){
  160.                 current = current->next;
  161.         }
  162.         current->next = current->next->next;
  163.         current->next->next->prev = current;
  164.     }
  165.  
  166.  
  167. // 12. Delete the previous node after searching with a specific key.
  168.     void deleteBeforeKey(int key){
  169.         current = head;
  170.         while(current->next->data != key){
  171.             current = current->next;
  172.         }
  173.         current->prev->next = current->next;
  174.         current->next->prev = current->prev;
  175.     }
  176.  
  177.  
  178. int main(){
  179.  
  180.     printf("Creating 4 Nodes: \n");
  181.     create();
  182.     display();
  183.  
  184.     printf("\n\nAppending Node at first: \n");
  185.     appendFirst(22);
  186.     display();
  187.  
  188.     printf("\n\nAppending Node at Last: \n");
  189.     appendLast(50);
  190.     display();
  191.  
  192.     printf("\n\nAppending Node at 3rd: \n");
  193.     appendAtThird(500);
  194.     display();
  195.  
  196.     printf("\n\nAppending after key: \n");
  197.     appendAfterKey(20);
  198.     display();
  199.  
  200.     printf("\n\nAppending before key: \n");
  201.     appendBeforeKey(30);
  202.     display();
  203.  
  204.     printf("\n\nUpdating at a key: \n");
  205.     updateAtKey(444);
  206.     display();
  207.  
  208.     printf("\n\nDelete First: \n");
  209.     deleteFirst();
  210.     display();
  211.  
  212.     printf("\n\nDelete Last: \n");
  213.     deleteLast();
  214.     display();
  215.  
  216.     printf("\n\nDelete At a Key 500: \n");
  217.     deleteAtKey(500);
  218.     display();
  219.  
  220.     printf("\n\nDelete After Key 20: \n");
  221.     deleteAfterKey(20);
  222.     display();
  223.  
  224.     printf("\n\nDelete Before Key 20: \n");
  225.     //deleteAfterKey(22);
  226.     display();
  227.  
  228.  
  229. return 0;
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement