Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include<conio.h>
- #include<stdlib.h>
- #include<locale.h>
- #include <list>
- using namespace std;
- list<char*> List;
- typedef struct _Node
- {
- char *value;
- struct _Node *next;
- struct _Node *prev;
- } Node;
- typedef struct _DblLinkedList
- {
- Node *head;
- Node *tail;
- } DblLinkedList;
- void pushBack(DblLinkedList *list, char data);
- void printLinkedList(DblLinkedList *list);
- void insert(DblLinkedList *list,Node *cur,char value);
- Node *find(Node *prevv);
- Node* Find( Node *prevv);
- void InsertToBack(DblLinkedList *list,Node *cur, char value);
- void DeleteAllMemory(DblLinkedList *list,Node *cur);
- void DeleteSymbol(DblLinkedList *list,Node *cur);
- void main()
- {
- setlocale (LC_ALL,".1251");
- char c;
- int count=0;
- DblLinkedList* list = (DblLinkedList*)malloc(sizeof(DblLinkedList));
- if (list==NULL)
- return exit(1);
- list->head = list->tail = NULL;
- while ((c=getchar())!='\n')
- {
- pushBack(list,c);
- }
- Node *cur=list->head;
- while (cur!=NULL)
- {
- if(cur==list->tail)
- {
- DeleteSymbol(list,cur);
- break;
- }
- if (*cur->value=='.')
- {
- DeleteSymbol(list,cur);
- count++;
- insert(list,cur,*cur->value);
- insert(list,cur,*cur->value);
- cur=cur->next;
- cur=cur->next;
- }
- cur=cur->next;
- }
- if (*list->tail->value=='.')
- {
- InsertToBack(list,list->tail,*list->tail->value);
- InsertToBack(list,list->tail,*list->tail->value);
- }
- cur=list->head;
- printLinkedList(list);
- DeleteAllMemory(list,cur);
- getch();
- }
- void pushBack(DblLinkedList* list, char data)
- {
- Node* tmp = (Node*)malloc(sizeof(Node));
- if (tmp==NULL)
- exit(2);
- tmp->value=(char*)malloc(sizeof(char));
- if(tmp->value==NULL)
- exit(3);
- *tmp->value=data;
- tmp->next = tmp->prev = NULL;
- if (list->head == NULL)
- {
- list->head = list->tail = tmp;
- return;
- }
- tmp->prev =list->tail;
- tmp->next=NULL;
- list->tail->next=tmp;
- list->tail =tmp;
- return;
- }
- void printLinkedList(DblLinkedList *list)
- {
- Node* cur = list->head;
- while (cur != NULL)
- {
- putchar(*cur->value);
- cur = cur->next;
- }
- putchar('\n');
- }
- void insert(DblLinkedList *list,Node *cur,char value)
- {
- Node *ins=(Node*)malloc(sizeof(Node));
- ins->value=(char*)malloc(sizeof(char));
- if (ins==NULL)
- return;
- *ins->value=value;
- ins->prev=cur;
- ins->next=cur->next;
- cur->next->prev=ins;
- cur->next=ins;
- }
- void InsertToBack(DblLinkedList *list,Node *cur, char value)
- {
- Node *ins=(Node*)malloc(sizeof(Node));
- ins->value=(char*)malloc(sizeof(char));
- if (ins==NULL)
- return;
- *ins->value=value;
- ins->prev=cur->prev;
- ins->next=cur;
- cur->prev->next=ins;
- cur->prev=ins;
- }
- void DeleteAllMemory(DblLinkedList *list,Node *tmp)
- {
- Node *buf=tmp;
- while (tmp!=NULL)
- {
- buf=tmp;
- tmp=buf->next;
- free(buf);
- }
- free(tmp);
- }
- void DeleteSymbol(DblLinkedList *list,Node *cur)
- {
- Node *Buf=cur->prev;
- if (cur->prev!=NULL)
- if (cur->prev->prev==NULL)
- {
- cur->prev=NULL;
- list->head=cur;
- free(Buf);
- }
- else
- {
- cur->prev=cur->prev->prev;
- cur->prev->next=cur;
- free(Buf);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement