Advertisement
Guest User

Untitled

a guest
May 29th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. struct item {
  4. int data;
  5. item *next, *prev;
  6. };
  7. item *head, *tail, *cur;
  8.  
  9. void create() {
  10. int n;
  11. cout << "Введите кол-во элементов: ";
  12. cin >> n;
  13. if (!n)return;
  14. //{cur->next = new item; cur = cur->next; cin >> cur->data; cur->prev = cur; cur->next = NULL;
  15. // Create NEW Node
  16. head = new item;
  17. head->prev = NULL;
  18. head->next = NULL;
  19. cout << "Введите элементы: ";
  20. cin >> head->data;
  21. cur = tail = head;
  22. // Fill
  23. for (int i = 1; i < n; i++) {
  24. cur->next = new item;
  25. cur = cur->next;
  26. cur->next = NULL;
  27. tail->next = cur;
  28. cur->prev = tail;
  29. tail = cur;
  30. cin >> cur->data;
  31. }
  32. }
  33.  
  34. void show() {
  35. cur = head;
  36. while (cur) {
  37. cout << cur->data << " ";
  38. cur = cur->next;
  39. } cout << endl;
  40. }
  41.  
  42. void showback() {
  43. cur = tail;
  44. while (cur) {
  45. cout << cur->data << " ";
  46. cur = cur->prev;
  47. }
  48. cout << endl;
  49. }
  50.  
  51. void destroy() {
  52. while (tail) {
  53. cur = tail;
  54. tail = cur->prev;
  55. delete cur;
  56. }
  57. head = tail;
  58. }
  59.  
  60. void quantity()
  61. {
  62. cur = tail;
  63. int quantity = 0;
  64. while (cur)
  65. {
  66. quantity++;
  67. cur = cur->prev;
  68. }
  69. cout << quantity << endl;
  70. }
  71.  
  72. double average() {
  73. double sr = 0.0, i = 0.0;
  74. cur = head;
  75. while (cur) {
  76. sr += cur->data;
  77. cur = cur->next;
  78. i++;
  79. }
  80. cout << "\nCписок: "; show();
  81. sr /= i;
  82. return sr;
  83. }
  84.  
  85. void adding(int n) {
  86. item *x = new item;
  87. x->data = n;
  88. x->next = NULL;
  89. x->prev = tail;
  90. tail->next = x;
  91. x->prev = tail;
  92. tail = x;
  93. cout << "\nИзменённый список: "; show(); cout << endl;
  94. }
  95.  
  96. void adding(int ind, int n) {
  97. item *NewItem = new(item);
  98. NewItem->data = n;
  99. NewItem->prev = NULL;
  100. NewItem->next = NULL;
  101. if (head == NULL) {//список пуст
  102. head = NewItem;
  103. }
  104. else {//список не пуст
  105. item *cur = head;
  106. for (int i = 1; i < ind && cur->next != NULL; i++)
  107. cur = cur->next;
  108. if (ind == 0) {
  109. //вставляем новый элемент на первое место
  110. NewItem->next = head;
  111. head->prev = NewItem;
  112. head = NewItem;
  113. }
  114. else {//вставляем новый элемент на непервое место
  115. if (cur->next != NULL) cur->next->prev = NewItem;
  116. NewItem->next = cur->next;
  117. cur->next = NewItem;
  118. NewItem->prev = cur;
  119. cur = NewItem;
  120. }
  121. }
  122. show();
  123. }
  124. void Delete(){
  125. cur = tail;
  126. tail = cur->prev;
  127. delete cur;
  128. tail->next = NULL;
  129. show();
  130. }
  131. void Delete(int ind) {
  132. item *ptr;//вспомогательный указатель
  133. item *cur = head;
  134. for (int i = 1; i < ind && cur != NULL; i++)
  135. cur = cur->next;
  136. if (cur != NULL) {//проверка на корректность
  137. if (cur->prev == NULL) {//удаляем первый элемент
  138. head = head->next;
  139. delete(cur);
  140. head->prev = NULL;
  141. cur = head;
  142. }
  143. else {//удаляем непервый элемент
  144. if (cur->next == NULL) {
  145. //удаляем последний элемент
  146. cur->prev->next = NULL;
  147. delete(cur);
  148. cur = head;
  149. }
  150. else {//удаляем непервый и непоследний элемент
  151. ptr = cur->next;
  152. cur->prev->next = cur->next;
  153. cur->next->prev = cur->prev;
  154. delete(cur);
  155. cur = ptr;
  156. }
  157. }
  158. }
  159. show();
  160. }
  161. int main() {
  162. int z, m;
  163. setlocale(LC_ALL, "Russian");
  164. create();
  165. cout << "Прямой список: ";
  166. show();
  167. cout << "Обратный список: ";
  168. showback();
  169. cout << "Количество элементов в списке: ";
  170. quantity();
  171. cout << "Среднее арифметическое: " << average() << "\n";
  172. cout << "Введите элемент для добавления в конец списка: ";
  173. cin >> z;
  174. adding(z);
  175. cout << endl;
  176. cout << "Введите индекс и элемент для добавления: ";
  177. cin >> m >> z;
  178. adding(m, z);
  179. cout << endl;
  180. cout << "Список с удаленным последним элементом::";
  181. Delete();
  182. cout << "Введите индекс удаляемого элемента: ";
  183. cin >> m;
  184. Delete(m);
  185. system("pause");
  186. return 0;
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement