Guest User

Untitled

a guest
Nov 18th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. #define _CRTDBG_MAP_ALLOC
  2. #include <stdlib.h>
  3. #include <crtdbg.h>
  4. #include <iostream>
  5.  
  6. using std::cout;
  7. using std::cin;
  8. using std::endl;
  9.  
  10. struct node {
  11. int key;
  12. struct node *next;
  13. };
  14.  
  15. class linked_list {
  16. private:
  17. struct node *head;
  18. struct node *tail;
  19. public:
  20. linked_list() {
  21. head = nullptr;
  22. tail = nullptr;
  23. }
  24.  
  25. void create(int key) {
  26. struct node *temp;
  27. temp = new struct node;
  28. temp->key = key;
  29. temp->next = nullptr;
  30. head = temp;
  31. tail = head;
  32. }
  33.  
  34.  
  35. void insert(int key) {
  36. if (key < head->key) {
  37. insert_beginning(key);
  38. }
  39. else if ((head->next == nullptr) || (key > tail->key)) {
  40. insert_end(key);
  41. }
  42. else {
  43. insert_middle(key);
  44. }
  45. }
  46.  
  47. void insert_beginning(int key) {
  48. if (head->next == nullptr) {
  49. tail = head;
  50. }
  51. struct node *temp;
  52. temp = new struct node;
  53. temp->key = key;
  54. temp->next = head;
  55. head = temp;
  56. }
  57.  
  58. void insert_end(int key) {
  59. struct node *temp;
  60. temp = new struct node;
  61. temp->key = key;
  62. temp->next = nullptr;
  63. if (head->next == nullptr) {
  64. head->next = temp;
  65. tail = temp;
  66. }
  67. else {
  68. tail->next = temp;
  69. }
  70. tail = temp;
  71. }
  72.  
  73.  
  74. void insert_middle(int key) {
  75. struct node *temp;
  76. temp = new struct node;
  77. temp->key = key;
  78.  
  79. struct node *current = head;
  80. struct node *prev = current;
  81.  
  82. while (current->key < temp->key) {
  83. prev = current;
  84. current = current->next;
  85. }
  86. prev->next = temp;
  87. temp->next = current;
  88. }
  89.  
  90. void delete_node(int key) {
  91. if (head == nullptr) {
  92. cout << "List is emptyn";
  93. return;
  94. }
  95.  
  96. if (head->key == key) {
  97. if (head->next == nullptr) {
  98. delete(head);
  99. head = tail = nullptr;
  100. }
  101. struct node *temp = head;
  102. head = head->next;
  103. delete(temp);
  104. }
  105. else {
  106. struct node *current = head;
  107. struct node *prev = current;
  108.  
  109. while ((current->key != key) && (current->next != nullptr)) {
  110. prev = current;
  111. current = current->next;
  112. }
  113.  
  114. if ((current->key != key) && (current->next == nullptr)) {
  115. cout << "Key not foundn";
  116. }
  117. else if ((current->key == key) && (current->next == nullptr)) {
  118. tail = prev;
  119. prev->next = nullptr;
  120. delete(current);
  121. }
  122. else {
  123. prev->next = current->next;
  124. delete(current);
  125. }
  126.  
  127. }
  128. }
  129.  
  130. void search_node(int key) {
  131. if (head->key == key || tail->key == key) {
  132. cout << "Node foundn";
  133. return;
  134. }
  135. struct node *current = head;
  136. while ((current->key != key) && (current->next != nullptr)) {
  137. current = current->next;
  138. }
  139.  
  140. if (current->key == key) {
  141. cout << "Node foundn";
  142. }
  143. else {
  144. cout << "Node not foundn";
  145. }
  146. }
  147.  
  148. void print_nodes(void) {
  149. struct node *current = head;
  150. while (current != nullptr) {
  151. cout << current->key << 'n';
  152. current = current->next;
  153. }
  154. }
  155.  
  156. ~linked_list() {
  157. struct node *current = head;
  158. struct node *prev = current;
  159.  
  160. while (current->next != nullptr) {
  161. current = current->next;
  162. delete(prev);
  163. prev = current;
  164. }
  165. delete(prev);
  166. }
  167. };
  168.  
  169.  
  170. int main(void) {
  171. linked_list list;
  172.  
  173. list.create(0);
  174.  
  175. for (int i = 1; i < 20; ++i) {
  176. list.insert(i);
  177. }
  178.  
  179. list.search_node(5);
  180. list.search_node(0);
  181. list.search_node(-1);
  182.  
  183. list.delete_node(19);
  184. list.delete_node(0);
  185.  
  186. list.print_nodes();
  187.  
  188. _CrtDumpMemoryLeaks();
  189. }
Add Comment
Please, Sign In to add comment