sp1d3o

Untitled

Jan 1st, 2022
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.86 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct Node {
  5.     int data;
  6.     struct Node *next;
  7. } node_t;
  8.  
  9. typedef struct List {
  10.     node_t *b_node;
  11.     node_t *c_node;
  12. } list_t;
  13.  
  14. void print_list(list_t *list);
  15. void add_start(list_t *list, int new_data);
  16. void add_end(list_t *list, int new_data);
  17.  
  18. int main(int argc, char *argv[])
  19. {
  20.     list_t *List = (list_t *)malloc(sizeof(list_t));
  21.  
  22.     node_t *head = (node_t *)malloc(sizeof(node_t));
  23.     node_t *second = (node_t *)malloc(sizeof(node_t));
  24.     node_t *third = (node_t *)malloc(sizeof(node_t));
  25.     node_t *fourth = (node_t *)malloc(sizeof(node_t));
  26.     node_t *fifth = (node_t *)malloc(sizeof(node_t));
  27.  
  28.     head->data = 1;
  29.     head->next = NULL;
  30.  
  31.     List->b_node = head;
  32.     List->c_node = NULL;
  33.  
  34.     /*
  35.     second->data = 2;
  36.     second->next = third;
  37.  
  38.     third->data = 3;
  39.     third->next = fourth;
  40.  
  41.     fourth->data = 4;
  42.     fourth->next = fifth;
  43.  
  44.     fifth->data = 5;
  45.     fifth->next = NULL;
  46.     */
  47.  
  48.     return 0;
  49. }
  50.  
  51. void add_start(list_t *list, int new_data)
  52. {
  53.     if(NULL == list) {
  54.         fprintf(stderr, "You fucked up!\n");
  55.         fflush(stderr);
  56.         return;
  57.     }
  58.  
  59.     node_t *new = (node_t *)malloc(sizeof(node_t));
  60.  
  61.     new->data = new_data;
  62.  
  63.     new->next = list->b_node;
  64.     list->b_node = new;
  65. }
  66.  
  67. void print_list(list_t *list)
  68. {
  69.     node_t  *current = list->b_node;
  70.  
  71.     while(current != NULL) {
  72.         printf("%d\n", current->data);
  73.         current = current->next;
  74.     }
  75. }
  76.  
  77. void add_end(list_t *list, int new_data)
  78. {
  79.     if(NULL == list) {
  80.         fprintf(stderr, "You fucked up!\n");
  81.         fflush(stderr);
  82.         return;
  83.     }
  84.  
  85.     node_t *new = (node_t *)malloc(sizeof(node_t));
  86.  
  87.     new->data = new_data;
  88.  
  89.     if(list->c_node->next == NULL) {
  90.         list->c_node->next = new;
  91.         new->next = NULL;
  92.         return;
  93.     }
  94. }
Add Comment
Please, Sign In to add comment