Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. struct node
  8. {
  9. int val;
  10. node* next;
  11. node* prev;
  12. };
  13.  
  14. node* SetE(node* &head, int k)
  15. {
  16. node* e=head;
  17. for(int i =0; i<k-1; i++)
  18. {
  19. e=e->next;
  20. }
  21. return e;
  22. }
  23.  
  24.  
  25. void AddFirst(node* &head, int value)
  26. {
  27. node* tmp = new node;
  28. tmp->val = value;
  29. tmp->next = head;
  30.  
  31. if (head != NULL)
  32. {
  33. head = tmp;
  34. tmp->prev = NULL;
  35. tmp->next->prev = tmp;
  36. }
  37. else head = tmp;
  38. }
  39.  
  40. void AddWhereYouWant(node *&head, node* e, int value)
  41. {
  42. node *tmp = new node;
  43. tmp->val = value;
  44.  
  45. if (head == e)
  46. {
  47. AddFirst(head, value);
  48. }
  49. else
  50. {
  51. e= e->prev;
  52. tmp->next = e->next;
  53. e->next = tmp;
  54. }
  55. }
  56.  
  57. void DeleteFirst(node* &head)
  58. {
  59. node* tmp;
  60. tmp = head;
  61. if(tmp)
  62. {
  63. head = tmp->next;
  64. delete tmp;
  65. }
  66. }
  67.  
  68.  
  69. void DeleteWhereYouWant(node* &head, node* e)
  70. {
  71. node* tmp;
  72.  
  73. if(head == e)
  74. {
  75. DeleteFirst(head);
  76. }
  77. else
  78. {
  79. tmp = head;
  80. while(tmp->next != e)
  81. {
  82. tmp = tmp->next;
  83. }
  84. tmp->next = e->next;
  85. delete e;
  86. }
  87. }
  88.  
  89. void DisconnectAllSameValues(node* &head, int value)
  90. {
  91. node* tmp;
  92. tmp = head;
  93. node* e;
  94.  
  95.  
  96. if(head!=NULL)
  97. {
  98.  
  99. if(head->val == value)
  100. {
  101. DeleteFirst(head);
  102.  
  103. }
  104.  
  105.  
  106. while(tmp->next->next != NULL)
  107. {
  108.  
  109.  
  110.  
  111.  
  112. if(tmp->next->val==value)
  113. {
  114.  
  115. e=tmp->next->next;
  116. tmp->next=e;
  117. if(tmp->next->next==NULL && tmp->next->val==value)
  118. {
  119. tmp->next=NULL;
  120. break;
  121. }
  122. }
  123. tmp = tmp->next;
  124.  
  125.  
  126.  
  127. }
  128.  
  129. }
  130. else cout << "Lista jest pusta lub nie ma danej wartości";
  131. }
  132.  
  133.  
  134. void ShowList(node* head)
  135. {
  136. node* tmp = head;
  137. while (tmp != NULL)
  138. {
  139. cout << tmp->val << " ";
  140. tmp = tmp->next;
  141. }
  142. }
  143.  
  144. void ReadFromFile(node* &head, char* Filename)
  145. {
  146. ifstream plik;
  147. plik.open(Filename);
  148. int a;
  149. while (!plik.eof())
  150. {
  151. plik >> a;
  152. AddFirst(head, a);
  153. }
  154.  
  155. }
  156.  
  157.  
  158. int main()
  159. {
  160. node* head = NULL;
  161. ReadFromFile(head, "plik1.txt");
  162. ShowList(head);
  163. AddWhereYouWant(head, SetE(head, 4), 55);
  164. cout << endl;
  165. ShowList(head);
  166. DisconnectAllSameValues(head, 3);
  167. cout << endl;
  168. ShowList(head);
  169.  
  170.  
  171. system("pause");
  172. return 0;
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement