Advertisement
Guest User

Untitled

a guest
Dec 1st, 2015
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. #include <stdio.h>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4. #include<locale.h>
  5. #include <list>
  6. using namespace std;
  7. list<char*> List;
  8. typedef struct _Node
  9. {
  10. char *value;
  11. struct _Node *next;
  12. struct _Node *prev;
  13. } Node;
  14. typedef struct _DblLinkedList
  15. {
  16. Node *head;
  17. Node *tail;
  18. } DblLinkedList;
  19. void pushBack(DblLinkedList *list, char data);
  20. void printLinkedList(DblLinkedList *list);
  21. void insert(DblLinkedList *list,Node *cur,char value);
  22. Node *find(Node *prevv);
  23. Node* Find( Node *prevv);
  24. void InsertToBack(DblLinkedList *list,Node *cur, char value);
  25. void DeleteAllMemory(DblLinkedList *list,Node *cur);
  26. void DeleteSymbol(DblLinkedList *list,Node *cur);
  27. void main()
  28. {
  29. setlocale (LC_ALL,".1251");
  30. char c;
  31. int count=0;
  32. DblLinkedList* list = (DblLinkedList*)malloc(sizeof(DblLinkedList));
  33. if (list==NULL)
  34. return exit(1);
  35. list->head = list->tail = NULL;
  36. while ((c=getchar())!='\n')
  37. {
  38. pushBack(list,c);
  39. }
  40. Node *cur=list->head;
  41. while (cur!=NULL)
  42. {
  43. if(cur==list->tail)
  44. {
  45. DeleteSymbol(list,cur);
  46. break;
  47. }
  48. if (*cur->value=='.')
  49. {
  50. DeleteSymbol(list,cur);
  51. count++;
  52. insert(list,cur,*cur->value);
  53. insert(list,cur,*cur->value);
  54. cur=cur->next;
  55. cur=cur->next;
  56. }
  57. cur=cur->next;
  58. }
  59. if (*list->tail->value=='.')
  60. {
  61. InsertToBack(list,list->tail,*list->tail->value);
  62. InsertToBack(list,list->tail,*list->tail->value);
  63. }
  64. cur=list->head;
  65. printLinkedList(list);
  66. DeleteAllMemory(list,cur);
  67. getch();
  68. }
  69. void pushBack(DblLinkedList* list, char data)
  70. {
  71. Node* tmp = (Node*)malloc(sizeof(Node));
  72. if (tmp==NULL)
  73. exit(2);
  74. tmp->value=(char*)malloc(sizeof(char));
  75. if(tmp->value==NULL)
  76. exit(3);
  77. *tmp->value=data;
  78. tmp->next = tmp->prev = NULL;
  79. if (list->head == NULL)
  80. {
  81. list->head = list->tail = tmp;
  82. return;
  83. }
  84. tmp->prev =list->tail;
  85. tmp->next=NULL;
  86. list->tail->next=tmp;
  87. list->tail =tmp;
  88. return;
  89. }
  90. void printLinkedList(DblLinkedList *list)
  91. {
  92. Node* cur = list->head;
  93. while (cur != NULL)
  94. {
  95. putchar(*cur->value);
  96. cur = cur->next;
  97. }
  98. putchar('\n');
  99. }
  100.  
  101. void insert(DblLinkedList *list,Node *cur,char value)
  102. {
  103. Node *ins=(Node*)malloc(sizeof(Node));
  104. ins->value=(char*)malloc(sizeof(char));
  105. if (ins==NULL)
  106. return;
  107. *ins->value=value;
  108. ins->prev=cur;
  109. ins->next=cur->next;
  110. cur->next->prev=ins;
  111. cur->next=ins;
  112. }
  113. void InsertToBack(DblLinkedList *list,Node *cur, char value)
  114. {
  115. Node *ins=(Node*)malloc(sizeof(Node));
  116. ins->value=(char*)malloc(sizeof(char));
  117. if (ins==NULL)
  118. return;
  119. *ins->value=value;
  120. ins->prev=cur->prev;
  121. ins->next=cur;
  122. cur->prev->next=ins;
  123. cur->prev=ins;
  124. }
  125. void DeleteAllMemory(DblLinkedList *list,Node *tmp)
  126. {
  127. Node *buf=tmp;
  128. while (tmp!=NULL)
  129. {
  130. buf=tmp;
  131. tmp=buf->next;
  132. free(buf);
  133. }
  134. free(tmp);
  135. }
  136. void DeleteSymbol(DblLinkedList *list,Node *cur)
  137. {
  138. Node *Buf=cur->prev;
  139. if (cur->prev!=NULL)
  140. if (cur->prev->prev==NULL)
  141. {
  142. cur->prev=NULL;
  143. list->head=cur;
  144. free(Buf);
  145. }
  146. else
  147. {
  148. cur->prev=cur->prev->prev;
  149. cur->prev->next=cur;
  150. free(Buf);
  151. }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement