mdgaziur001

Linked List and Stack in one file

Sep 21st, 2025 (edited)
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.18 KB | None | 0 0
  1. #include <stdbool.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <limits.h>
  5.  
  6. struct __LLNode {
  7.     int val;
  8.     struct __LLNode *next;
  9. };
  10.  
  11. typedef struct __LLNode LLNode;
  12.  
  13. size_t LL_length(LLNode *root);
  14. void LL_free(LLNode **root);
  15.  
  16. LLNode *LL_create_node(int val) {
  17.     LLNode *node = (LLNode*)malloc(sizeof(LLNode));
  18.     node->val = val;
  19.     node->next = NULL;
  20.     return node;
  21. }
  22.  
  23. void LL_push_back(LLNode **root, LLNode *val) {
  24.     if (root == NULL) {
  25.         *root = val;
  26.         return;
  27.     }
  28.  
  29.     LLNode *current = *root;
  30.  
  31.     while (current->next != NULL) {
  32.         current = current->next;
  33.     }
  34.  
  35.     current->next = val;
  36. }
  37.  
  38. bool LL_insert_at(LLNode **root, LLNode *val, size_t index) {
  39.     if (index == 0) {
  40.         LLNode *tmp = *root;
  41.         *root = val;
  42.         val->next = tmp;
  43.         return true;
  44.     }
  45.  
  46.     if (LL_length(*root) < index) {
  47.         return false;
  48.     }
  49.  
  50.     LLNode *current = *root;
  51.     index--;
  52.  
  53.     while (index--) {
  54.         current = current->next;
  55.     }
  56.  
  57.  
  58.     LLNode *tmp = current->next;
  59.     current->next = val;
  60.     val->next = tmp;
  61.  
  62.     return false;
  63. }
  64.  
  65. bool LL_delete_at(LLNode **root, size_t index) {
  66.     if (index == 0 && root != NULL) {
  67.         *root = (*root)->next;
  68.         return true;
  69.     }
  70.  
  71.     if (LL_length(*root) < index) {
  72.         return false;
  73.     }
  74.  
  75.     LLNode *current = *root;
  76.     index--;
  77.  
  78.     while (index--) {
  79.         current = current->next;
  80.     }
  81.  
  82.  
  83.     LLNode *tmp = current->next->next;
  84.     LLNode *del = current->next;
  85.     del->next = NULL;
  86.     LL_free(&del);
  87.  
  88.     current->next = tmp;
  89.  
  90.     return true;
  91. }
  92.  
  93. size_t LL_length(LLNode *root) {
  94.     if (root == NULL) return 0;
  95.  
  96.     LLNode *current = root;
  97.     size_t len = 1;
  98.  
  99.     while (current->next != NULL) {
  100.         current = current->next;
  101.         len++;
  102.     }
  103.  
  104.     return len;
  105. }
  106.  
  107. void LL_display(LLNode *root) {
  108.     if (root == NULL) {
  109.         printf("<empty LinkedList>\n");
  110.         return;
  111.     }
  112.  
  113.     printf("%d", root->val);
  114.     LLNode *current = root->next;
  115.     while (current) {
  116.         printf(" -> %d", current->val);
  117.         current = current->next;
  118.     }
  119.  
  120.     printf("\n");
  121. }
  122.  
  123. void LL_free(LLNode **node) {
  124.     LLNode *current = *node;
  125.     while (current->next) {
  126.         LLNode *tmp = current;
  127.         current = current->next;
  128.  
  129.         free(tmp);
  130.     }
  131.  
  132.     free(current);
  133.  
  134.     *node = NULL;
  135. }
  136.  
  137. typedef struct {
  138.     LLNode *root;
  139.     size_t sz;
  140. } Stack;
  141.  
  142. Stack *Stack_new() {
  143.     Stack *stack = (Stack *)malloc(sizeof(Stack));
  144.     if (!stack) return NULL;
  145.  
  146.     stack->root = LL_create_node(INT_MAX);
  147.     stack->sz = 0;
  148.  
  149.     return stack;
  150. }
  151.  
  152. int Stack_top(Stack *stack) {
  153.     LLNode *cur = stack->root;
  154.    
  155.     while (cur->next) {
  156.         cur = cur->next;
  157.     }
  158.  
  159.     return cur->val;
  160. }
  161.  
  162. void Stack_push(Stack *stack, int val) {
  163.     LLNode *cur = stack->root;
  164.  
  165.     while (cur->next) {
  166.         cur = cur->next;
  167.     }
  168.  
  169.     cur->next = LL_create_node(val);
  170. }
  171.  
  172. int Stack_pop(Stack *stack) {
  173.     LLNode *prev = stack->root;
  174.     LLNode *cur = stack->root->next;
  175.  
  176.     while (cur->next) {
  177.         prev = cur;
  178.         cur = cur->next;
  179.     }
  180.  
  181.     prev->next = NULL;
  182.     return cur->val;
  183. }
  184.  
  185. bool Stack_is_empty(Stack *stack) {
  186.     return stack->root->next == NULL;
  187. }
  188.  
  189. int main() {
  190.     Stack *st = Stack_new();
  191.  
  192.     for (int i = 1; i <= 10; i++) {
  193.         Stack_push(st, i);
  194.     }
  195.  
  196.     printf("Stack is %s\n", Stack_is_empty(st) ? "empty" : "not empty");
  197.  
  198.     while (!Stack_is_empty(st)) {
  199.         printf("%d ", Stack_pop(st));
  200.     }
  201.     printf("\n");
  202.  
  203.     return 0;
  204. }
  205.  
Advertisement
Add Comment
Please, Sign In to add comment