Advertisement
niamulhasan

C all functions of singly-linked list!

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