Advertisement
Guest User

c is fun c is life, but please use python

a guest
May 21st, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.14 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. typedef struct node {
  6.   int data;
  7.   struct node* next;
  8.   struct node* prev;
  9. } Node;
  10.  
  11.  
  12. Node* push_back(Node** first_ref, Node** last_ref, int data) {
  13.   Node* new = malloc(sizeof(Node));
  14.   new->data = data;
  15.   Node* current;
  16.  
  17.   if (*first_ref == NULL) {
  18.     new->next = NULL;
  19.     new->prev = NULL;
  20.     *first_ref = new;
  21.     *last_ref = new;
  22.   } else {
  23.     current = *first_ref;
  24.     // Traverse to the end of the list
  25.     while (current->next != NULL)
  26.       current = current->next;
  27.     current->next = new;
  28.     new->prev = current;
  29.     *last_ref = new;
  30.   }
  31.  
  32.   return new;
  33. }
  34.  
  35.  
  36. void print_list(Node** first_ref, Node** last_ref) {
  37.   Node* current = *first_ref;
  38.  
  39.   printf("List forward: [ ");
  40.   while (current != NULL) {
  41.     printf("%d, ", current->data);
  42.     current = current->next;
  43.   }
  44.   printf("]\n");
  45.  
  46.   current = *last_ref;
  47.   printf("List backwards: [ ");
  48.   while (current != NULL) {
  49.     printf("%d, ", current->data);
  50.     current = current->prev;
  51.   }
  52.   printf("]\n");
  53. }
  54.  
  55.  
  56. void substitute(Node** first_ref, Node** last_ref, int n, int new_data) {
  57.   Node* current = *first_ref;
  58.  
  59.   for (int i = 0; i < n; ++i) {
  60.     if (current != NULL)
  61.       current = current->next;
  62.     else {
  63.       printf("[E] Index number out of range\n");
  64.       return;
  65.     }
  66.   }
  67.  
  68.   Node* new = malloc(sizeof(Node));
  69.  
  70.   new->data = new_data;
  71.  
  72.   new->next = current->next;
  73.   new->prev = current->prev;
  74.  
  75.   if (current->prev != NULL)
  76.     current->prev->next = new;
  77.   else
  78.     *first_ref = new;
  79.  
  80.   if (current->next != NULL)
  81.     current->next->prev = new;
  82.   else
  83.     *last_ref = new;
  84. }
  85.  
  86.  
  87. int main() {
  88.   Node* first = NULL;
  89.   Node* last = NULL;
  90.  
  91.   push_back(&first, &last, 0);
  92.   push_back(&first, &last, 1);
  93.   push_back(&first, &last, 2);
  94.   push_back(&first, &last, 3);
  95.   push_back(&first, &last, 4);
  96.   push_back(&first, &last, 5);
  97.  
  98.   print_list(&first, &last);
  99.  
  100.   substitute(&first, &last, 2, 99);
  101.   substitute(&first, &last, 4, 120);
  102.  
  103.   print_list(&first, &last);
  104.  
  105.   substitute(&first, &last, 0, 32);
  106.   substitute(&first, &last, 5, 64);
  107.  
  108.   print_list(&first, &last);
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement