Advertisement
Guest User

Untitled

a guest
Dec 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.86 KB | None | 0 0
  1. // ConsoleApplication1.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <stdlib.h>
  6.  
  7. typedef struct item {
  8.     int value;
  9.     item* next;
  10. } item;
  11.  
  12. typedef struct item_2 {
  13.     int value;
  14.     item_2* next;
  15.     item_2* prev;
  16. } item_2;
  17.  
  18. /*typedef struct queue {
  19.     item* list;
  20. } queue;
  21.  
  22. void add_in_q(queue* q, int value) {
  23.     add_in_tail(q->list, value);
  24. }
  25.  
  26. int remove_from_q(queue* q) {
  27.     int temp = get_tail(q->list);
  28.     remove_head(q->list);
  29.     return temp;
  30. }*/
  31.  
  32. item* add_in_head(item* head, int new_value) {
  33.     item* new_item = (item*)malloc(sizeof(item));
  34.     new_item->value = new_value;
  35.     new_item->next = head;
  36.     return new_item;
  37. }
  38.  
  39. item* add_in_tail(item* head, int new_value) {
  40.     item* new_item = (item*)malloc(sizeof(item));
  41.     new_item->value = new_value;
  42.     new_item->next = NULL;
  43.     if (!head)
  44.         return new_item;
  45.  
  46.     item* current = head;
  47.     while (current->next) {
  48.         current = current->next;
  49.     }
  50.     current->next = new_item;
  51.     return head;
  52. }
  53.  
  54. item* remove_head(item* head) {
  55.     if (!head)
  56.         return NULL;
  57.     item* temp = head;
  58.     head = head->next;
  59.     free(temp);
  60.     return head;
  61. }
  62.  
  63. item* remove_tail(item* head) {
  64.     if (!head)
  65.         return NULL;
  66.     if (!head->next) {
  67.         free(head);
  68.         return NULL;
  69.     }
  70.     item* current = head;
  71.     while (current->next->next) {
  72.         current = current->next;
  73.     }
  74.     free(current->next);
  75.     current->next = NULL;
  76.     return head;
  77. }
  78.  
  79. void print_list(item* head) {
  80.     item* current = head;
  81.     while (current) {
  82.         printf("%4d", current->value);
  83.         current = current->next;
  84.     }
  85.     printf("\n");
  86.     return;
  87. }
  88.  
  89. void reverse_print(item* head) {
  90.     if (head->next)
  91.         reverse_print(head->next);
  92.     printf("%4d", head->value);
  93.     return;
  94. }
  95.  
  96. item* remove_by_value(item* head, int value) {
  97.     if (!head)
  98.         return NULL;
  99.     if (head->value == value) {
  100.         return remove_head(head);
  101.     }
  102.     item* current = head;
  103.     while (current->next && current->next->value != value) {
  104.         current = current->next;
  105.     }
  106.     if (!current->next)
  107.         return head;
  108.     item* temp = current->next;
  109.     current->next = current->next->next;
  110.     free(temp);
  111.     return head;
  112. }
  113.  
  114. item* clear_list(item* head) {
  115.     item* current = head;
  116.     item* temp;
  117.     unsigned len;
  118.     unsigned j;
  119.     // get_len();
  120.     while (j < len) {//(current) {
  121.         /*temp = current;
  122.         current = current->next;
  123.         free(temp);*/
  124.         current = remove_head(current);
  125.         j++;
  126.     }
  127.     return NULL;
  128. }
  129.  
  130. int main()
  131. {
  132.     item* head = NULL;
  133.     head = add_in_head(head, 0);
  134.     head = add_in_head(head, 2);
  135.     head = add_in_head(head, 4);
  136.     head = add_in_head(head, 6);
  137.     head = add_in_tail(head, 3);
  138.     head = add_in_tail(head, 3);
  139.     head = add_in_tail(head, 8);
  140.     head = add_in_tail(head, 3);
  141.     head = add_in_tail(head, 8);
  142.     print_list(head);
  143.     head = remove_by_value(head, 8);
  144.     print_list(head);
  145.  
  146.     reverse_print(head);
  147.     head = clear_list(head);
  148.     print_list(head);
  149.     getchar();
  150.     return 0;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement