Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.57 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <limits.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. typedef struct Node {
  8.     char *data;
  9.     struct Node* next;
  10. } Node;
  11.  
  12. Node *add_first(Node *a, char* data)
  13. {
  14.     Node *c = (Node*)malloc(1 * sizeof(Node));
  15.     *c = { data, a };
  16.     return c;
  17. }
  18.  
  19. Node *reverse(Node* a)
  20. {
  21.     Node *b = a, *c = (Node*)malloc(1 * sizeof(Node));
  22.     *c = { a->data, NULL };
  23.     while (b->next != NULL)
  24.     {
  25.         b = b->next;
  26.         c = add_first(c, b->data);
  27.     }
  28.     return c;
  29. }
  30.  
  31. Node *add_kvadrat(Node *a, char* data)
  32. {
  33.     Node *c = (Node*)malloc(1 * sizeof(Node));
  34.     *c = { data, a };
  35.     if (c->next)
  36.         c->next = reverse(c->next);
  37.     c = reverse(c);
  38.     return c;
  39. }
  40.  
  41. void print_Node(Node *a)
  42. {
  43.     if (strlen(a->data))
  44.         printf("%s ", a->data);
  45.     if (a->next)
  46.         print_Node(a->next);
  47.     else
  48.         printf("\n");
  49. }
  50.  
  51. Node *init(char* b) {
  52.     Node *a = (Node*)malloc(sizeof(Node));
  53.     a->data = b;
  54.     a->next = NULL;
  55.     return(a);
  56. }
  57.  
  58. Node* getLast(Node *head) {
  59.     if (head == NULL) {
  60.         return NULL;
  61.     }
  62.     while (head->next) {
  63.         head = head->next;
  64.     }
  65.     return head;
  66. }
  67.  
  68. void add(Node *head, char* value) {
  69.     Node *last = getLast(head);
  70.     Node *tmp = (Node*)malloc(sizeof(Node));
  71.     tmp->data = value;
  72.     tmp->next = NULL;
  73.     last->next = tmp;
  74. }
  75.  
  76. void seminar_list() {
  77.     FILE* f1 = freopen("in.txt", "r", stdin);
  78.     FILE* f2 = freopen("out.txt", "w", stdout);
  79.     Node *head;
  80.     head = init("A");
  81.     add(head, "B");
  82.     add(head, "C");
  83.     add(head, "D");
  84.     print_Node(head);
  85.     head = reverse(head);
  86.     print_Node(head);
  87.     fclose(f1);
  88.     fclose(f2);
  89. }
  90.  
  91. int main(){
  92.     seminar_list();
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement