Facebook
From Cream Dove, 3 Years ago, written in C.
Embed
Download Paste or View Raw
Hits: 77
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedef struct node Node;
  4. struct node {
  5.     int data;
  6.     Node *next;
  7. };
  8.  
  9. Node *create_node(int item, Node *next)
  10. {
  11.     Node *new_node = (Node *) malloc(sizeof(Node));
  12.     if(new_node == NULL) {
  13.         printf("Error! Could Not Create A New Node\n");
  14.         exit(1);
  15.     }
  16.     new_node->data = item;
  17.     new_node->next = next;
  18.     return new_node;
  19. }
  20.  
  21. Node *prepend(Node *head, int item)
  22. {
  23.     Node *new_node = create_node(item, head);
  24.     return new_node;
  25. }
  26.  
  27. Node *append(Node *head, int item)
  28. {
  29.     Node *new_node = create_node(item, NULL);
  30.     if(head == NULL) {
  31.         return new_node;
  32.     }
  33.     Node *current_node = head;
  34.     while(current_node->next != NULL) {
  35.         current_node = current_node->next;
  36.     }
  37.     current_node->next = new_node;
  38.     return head;
  39. }
  40.  
  41. void print_linked_list(Node *head)
  42. {
  43.     Node *current_node = head;
  44.     while(current_node != NULL) {
  45.         printf("%d  ", current_node->data);
  46.         current_node = current_node->next;
  47.     }
  48.     printf("\n");
  49. }
  50.  
  51. int count(Node *head)
  52. {
  53.     int count = 0;
  54.     Node *current_node = head;
  55.     while(current_node != NULL) {
  56.         count++;
  57.         current_node = current_node->next;
  58.     }
  59.    return count;
  60. }
  61.  
  62. Node *search(Node *head, int item)
  63. {
  64.     Node *current_node = head;
  65.     while(current_node != NULL) {
  66.         if(current_node->data == item)
  67.         return current_node;
  68.         else
  69.         current_node = current_node->next;
  70.     }
  71.     return current_node;
  72. }
  73.  
  74. Node *remove_node(Node *head, Node *node)
  75. {
  76.     if (node == head) {
  77.         head = node->next;
  78.         free(node);
  79.         return head;
  80.     }
  81.     Node *current_node = head;
  82.     while(current_node != NULL) {
  83.         if(current_node->next == node) {
  84.             break;
  85.         }
  86.         current_node = current_node->next;
  87.     }
  88.     if(current_node == NULL) {
  89.         return head;
  90.     }
  91.     current_node->next = node->next;
  92.     free(node);
  93.     return head;
  94. }
  95. void insert(Node *node, int item)
  96. {
  97.     Node *new_node = create_node(item, node->next);
  98.     node->next = new_node;
  99.     print_linked_list(node);
  100.  
  101. }
  102.  
  103. int main()
  104. {
  105.     Node *n1, *n2, *head, *n3;
  106.     head = NULL;
  107.     n1 = create_node(10, NULL);
  108.     head = n1;
  109.     print_linked_list(head);
  110.     head = prepend(head, 20);
  111.     print_linked_list(head);
  112.     head = append(head, 30);
  113.     print_linked_list(head);
  114.     printf("Number of Node = %d\n", count(head));
  115.     printf("Searching Node is = %d\n",(search(head, 30))->data);
  116.     head = remove_node(head, n1);
  117.     print_linked_list(head);
  118.     n2 = head;
  119.     head = remove_node(head, n2);
  120.     print_linked_list(head);
  121.     n2 = head;
  122.     head = remove_node(head, n2);
  123.     print_linked_list(head);
  124.     insert(head, 20);
  125.     return 0;
  126. }