Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct Node {
  5. int data;
  6. Node* next;
  7. };
  8. class LinkedList
  9. {
  10. public:
  11. Node* head = NULL;
  12. int size = 0;
  13. Node* tail = NULL;
  14.  
  15. void printLinkedList() {
  16. Node *search = head;
  17. if (head == NULL) {
  18. cout << "linkedlist is empty" << endl;
  19. }
  20. else {
  21. while (search != NULL){
  22. cout << search->data << endl;
  23. search = search->next;
  24. }
  25. }
  26. }
  27. int sizeLinkedList() {
  28. size = 0;
  29. if (head->next != NULL){
  30. size = 1;
  31. Node* current = head;
  32. while (current->next != NULL) {
  33. current = current->next;
  34. size = size + 1;
  35. }
  36. }
  37. cout << size << endl;
  38. return size;
  39. }
  40.  
  41. Node *getNode(int position){
  42. Node *current = head;
  43. for (int i = 0; i<position; i++)
  44. {
  45. current = current->next;
  46. }
  47.  
  48. return current;
  49. }
  50. void appendNode(int n) {
  51. Node *newNode = new Node; //creating new node
  52. newNode->data = n;
  53. newNode->next = NULL;
  54. size++;
  55. if (head == NULL)
  56. {
  57. head = newNode;
  58. return;
  59. }
  60. else {
  61. Node *current = head;
  62. while (current->next != NULL) {
  63. current = current->next;
  64. }
  65. current->next = newNode;
  66. }
  67. }
  68.  
  69. void insertNode(int n, int position) {
  70. Node *newNode = new Node;
  71. newNode->data = n;
  72. newNode->next = NULL;
  73. if (position == 0){
  74. newNode->next = head;
  75. head = newNode;
  76. }
  77.  
  78. else if (position == sizeLinkedList()) {
  79. appendNode(n);
  80. }
  81.  
  82. else {
  83. Node *prevNode = getNode(position-1);
  84. Node *nextNode = getNode(position);
  85. prevNode->next = newNode;
  86. newNode->next = nextNode;
  87. }
  88. }
  89.  
  90.  
  91. void deleteNode(int position) {
  92. Node *currentNode;
  93.  
  94. int my_size = sizeLinkedList();
  95. if ((my_size == 0) || (position > my_size)) {
  96. return;
  97. }
  98. if (position == 0) {
  99. currentNode = head->next;
  100. head = currentNode;
  101. }
  102. else if (position == size-1) {
  103. delete getNode(position);
  104. getNode(position - 1)->next = NULL;
  105. }
  106. else {
  107. currentNode = getNode(position);
  108. getNode(position - 1)->next = getNode(position+1);
  109. delete currentNode;
  110. }
  111. }
  112. List()::~List()
  113. {
  114. for( Link* ptr= Head; Head; Head= Head->next)
  115. {
  116. delete ptr;
  117. }
  118. }
  119.  
  120.  
  121. int main()
  122. {
  123. //
  124. IntList list;
  125.  
  126. // Build the list
  127. list.appendNode(2); //
  128. list.appendNode(4); //
  129. list.appendNode(6); // A
  130.  
  131. //
  132. cout << "Here are the initial values:\n";
  133. list.displayList();
  134. cout << endl;
  135.  
  136. //
  137. cout << "Now inserting the value 5.\n";
  138. list.insertNode(5);
  139.  
  140. //
  141. cout << "Here are the nodes now.\n";
  142. list.displayList();
  143. cout << endl;
  144.  
  145. //
  146. cout << "Now deleting the last node.\n";
  147. list.deleteNode(6);
  148.  
  149. //
  150. cout << "Here are the nodes left.\n";
  151. list.displayList();
  152.  
  153. return 0;
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement