Advertisement
AquaBlitz11

Linked List - Iterative

Oct 12th, 2018
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.37 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct Node {
  5.     int key;
  6.     struct Node *next;
  7. } Node;
  8.  
  9. Node *create_node(int key, Node *next=nullptr) {
  10.     Node *ptr = (Node*)malloc(sizeof(Node));
  11.     ptr->key = key;
  12.     ptr->next = next;
  13.     return ptr;
  14. }
  15.  
  16. Node *push_front(Node *head, int key) {
  17.     return create_node(key, head);
  18. }
  19.  
  20. Node *push_back(Node *head, int key) {
  21.     if (!head)
  22.         return create_node(key);
  23.     Node *curr = head;
  24.     while (curr->next)
  25.         curr = curr->next;
  26.     curr->next = create_node(key);
  27.     return head;
  28. }
  29.  
  30. Node *insert_in_order(Node *head, int key) {
  31.     if (!head)
  32.         return create_node(key);
  33.     Node *curr = head;
  34.     while (curr->next && curr->next->key < key)
  35.         curr = curr->next;
  36.     curr->next = create_node(key, curr->next);
  37.     return head;
  38. }
  39.  
  40. Node *find_key(Node *head, int key) {
  41.     Node *curr = head;
  42.     while (curr) {
  43.         if (curr->key == key)
  44.             return curr;
  45.         curr = curr->next;
  46.     }
  47.     return nullptr;
  48. }
  49.  
  50. Node *delete_key(Node *head, int key) {
  51.     if (!head)
  52.         return nullptr;
  53.     if (head->key == key) {
  54.         Node *tmp = head->next;
  55.         free(head);
  56.         return tmp;
  57.     } else {
  58.         Node *curr = head;
  59.         while (curr->next && curr->next->key != key)
  60.             curr = curr->next;
  61.         if (curr->next && curr->next->key == key) {
  62.             Node *tmp = curr->next->next;
  63.             free(curr->next);
  64.             curr->next = tmp;
  65.         }
  66.         return head;
  67.     }
  68. }
  69.  
  70. void print_list(Node *head) {
  71.     Node *curr = head;
  72.     while (curr) {
  73.         printf("%d -> ", curr->key);
  74.         curr = curr->next;
  75.     }
  76.     printf("NULL\n");
  77. }
  78.  
  79. Node *free_list(Node *head) {
  80.     Node *curr = head;
  81.     while (curr) {
  82.         Node *tmp = curr->next;
  83.         free(curr);
  84.         curr = tmp;
  85.     }
  86.     return nullptr;
  87. }
  88.  
  89. int main()
  90. {
  91.     Node *p = nullptr, *head = nullptr;
  92.     head = insert_in_order(head, 10);
  93.     head = insert_in_order(head, 20);
  94.     head = insert_in_order(head, 30);
  95.     print_list(head);
  96.     p = find_key(head, 50);
  97.     print_list(p);
  98.     p = find_key(head, 30);
  99.     print_list(p);
  100.     p = find_key(head, 20);
  101.     print_list(p);
  102.     head = delete_key(head,50);
  103.     head = delete_key(head,20);
  104.     print_list(head);
  105.     head = free_list(head);
  106.     print_list(head);
  107.  
  108.     return 0;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement