Advertisement
Guest User

Untitled

a guest
Apr 10th, 2020
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.06 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include <string.h>
  4. typedef struct Node
  5. {
  6.     char word[255];
  7.    struct Node*next;
  8.    struct Node*prev;
  9. } Node;
  10.  
  11. Node*new_el(char*q) //создаю новый элемент
  12. {
  13.     Node* cur=(Node*)malloc(sizeof(Node));
  14.     strcpy(cur->word, q);
  15.     cur->next=NULL;
  16.     cur->prev=NULL;
  17.     return cur;
  18. }
  19.  
  20. Node*adding(Node*head, Node*cur) //добавляю новый элемент в двусвязный список
  21. {
  22.     if(head==NULL)
  23.     {
  24.         head=cur;
  25.     }
  26.     else
  27.     {
  28.         Node*p=head;
  29.         while(p->next)
  30.         {
  31.             p=p->next;
  32.         }
  33.         p->next=cur;
  34.         cur->prev=p;
  35.     }
  36.     return head;
  37. }
  38.  
  39. Node*three_Nodes() //сразу создаю три подряд идущих узла
  40. {
  41.     char one[2]={"1"};
  42.     char two[2]={"2"};
  43.     char three[2]={"3"};
  44.     Node*first=new_el(one);
  45.     Node*second=new_el(two);
  46.     Node*third=new_el(three);
  47.     first->next=second;
  48.     second->prev=first;
  49.     second->next=third;
  50.     third->prev=second;
  51.     return first;
  52. }
  53.  
  54. Node*modification(Node*head) //удаляю элемент со звездочкой и вместо него вставляю три узла
  55. {
  56.     Node*p=head->next;
  57.     while(p->next)
  58.     {
  59.         if (strchr(p->word, '*')!=NULL)
  60.         {
  61.             Node*cur=three_Nodes();
  62.             Node*tmp=p;
  63.             cur->next->next->next=p->next;
  64.             cur->prev=p->prev;
  65.             p->prev->next=cur;
  66.             p->next->prev=cur->next->next;
  67.             free(tmp);
  68.             tmp->next=NULL;
  69.             tmp->prev=NULL;
  70.             p=cur->next->next;
  71.         }
  72.         p=p->next;
  73.     }
  74.     if(strchr(p->word,'*')!=NULL) //отдельно проверяю хвост списка
  75.     {
  76.         Node*cur=three_Nodes();
  77.         cur->prev=p->prev;
  78.         Node*q=p;
  79.         p->prev->next=cur;
  80.         free(q);
  81.         q->prev=NULL;
  82.         q->next=NULL;
  83.     }
  84.    
  85.     if(strchr(head->word, '*')!=NULL) //отдельно проверяю голову
  86.     {
  87.         Node*tmp=head;
  88.         Node*cur=three_Nodes();
  89.         cur->next->next->next=head->next;
  90.         head->next->prev=cur->next->next;
  91.         free(tmp);
  92.         tmp->next=NULL;
  93.         tmp->prev=NULL;
  94.         head=cur;
  95.     }
  96.     return head;
  97. }
  98.  
  99. void print(Node*head) //печатаю список
  100. {
  101.     Node*q=head;
  102.     while(q)
  103.     {
  104.         printf("\n%s", q->word);
  105.         q=q->next;
  106.     }
  107. }
  108. void lisr_delete(Node*head) //удаляю список
  109. {
  110.     while(head)
  111.     {
  112.         Node*tmp=head;
  113.         head=head->next;
  114.         free(tmp);
  115.     }
  116. }
  117.  
  118. int main()
  119. {
  120.     char slova[255];
  121.     Node*head=NULL;
  122.     printf("Enter your words\nthe end of input is 0\n");
  123.     scanf("%s", slova);
  124.     while(slova[0]!='0')
  125.     {
  126.         Node*q=new_el(slova);
  127.         head=adding(head, q);
  128.         scanf("%s", slova);
  129.     }
  130.     printf("\nYour list:");
  131.     print(head);
  132.     head=modification(head);
  133.     printf("\nYour new list:");
  134.     print(head);
  135.     printf("\n");
  136.     lisr_delete(head);
  137.    
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement