Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include <string.h>
- typedef struct Node
- {
- char word[255];
- struct Node*next;
- struct Node*prev;
- } Node;
- Node*new_el(char*q) //создаю новый элемент
- {
- Node* cur=(Node*)malloc(sizeof(Node));
- strcpy(cur->word, q);
- cur->next=NULL;
- cur->prev=NULL;
- return cur;
- }
- Node*adding(Node*head, Node*cur) //добавляю новый элемент в двусвязный список
- {
- if(head==NULL)
- {
- head=cur;
- }
- else
- {
- Node*p=head;
- while(p->next)
- {
- p=p->next;
- }
- p->next=cur;
- cur->prev=p;
- }
- return head;
- }
- Node*three_Nodes() //сразу создаю три подряд идущих узла
- {
- char one[2]={"1"};
- char two[2]={"2"};
- char three[2]={"3"};
- Node*first=new_el(one);
- Node*second=new_el(two);
- Node*third=new_el(three);
- first->next=second;
- second->prev=first;
- second->next=third;
- third->prev=second;
- return first;
- }
- Node*modification(Node*head) //удаляю элемент со звездочкой и вместо него вставляю три узла
- {
- Node*p=head->next;
- while(p->next)
- {
- if (strchr(p->word, '*')!=NULL)
- {
- Node*cur=three_Nodes();
- Node*tmp=p;
- cur->next->next->next=p->next;
- cur->prev=p->prev;
- p->prev->next=cur;
- p->next->prev=cur->next->next;
- free(tmp);
- tmp->next=NULL;
- tmp->prev=NULL;
- p=cur->next->next;
- }
- p=p->next;
- }
- if(strchr(p->word,'*')!=NULL) //отдельно проверяю хвост списка
- {
- Node*cur=three_Nodes();
- cur->prev=p->prev;
- Node*q=p;
- p->prev->next=cur;
- free(q);
- q->prev=NULL;
- q->next=NULL;
- }
- if(strchr(head->word, '*')!=NULL) //отдельно проверяю голову
- {
- Node*tmp=head;
- Node*cur=three_Nodes();
- cur->next->next->next=head->next;
- head->next->prev=cur->next->next;
- free(tmp);
- tmp->next=NULL;
- tmp->prev=NULL;
- head=cur;
- }
- return head;
- }
- void print(Node*head) //печатаю список
- {
- Node*q=head;
- while(q)
- {
- printf("\n%s", q->word);
- q=q->next;
- }
- }
- void lisr_delete(Node*head) //удаляю список
- {
- while(head)
- {
- Node*tmp=head;
- head=head->next;
- free(tmp);
- }
- }
- int main()
- {
- char slova[255];
- Node*head=NULL;
- printf("Enter your words\nthe end of input is 0\n");
- scanf("%s", slova);
- while(slova[0]!='0')
- {
- Node*q=new_el(slova);
- head=adding(head, q);
- scanf("%s", slova);
- }
- printf("\nYour list:");
- print(head);
- head=modification(head);
- printf("\nYour new list:");
- print(head);
- printf("\n");
- lisr_delete(head);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement