Guest User

Untitled

a guest
Jul 21st, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. struct Node{
  4. int data;
  5. struct Node *next;
  6. };
  7.  
  8. // Given a reference (pointer to pointer) to the start pointer, appends a new node at the end of the list
  9. void append(Node **start, const int new_data){
  10. // create a new Node
  11. Node *new_node = new Node;
  12. new_node->data = new_data;
  13. new_node->next = NULL;
  14.  
  15. // if the list is empty then set the newly created note as start
  16. if(*start == NULL){
  17. *start = new_node;
  18. return;
  19. }
  20.  
  21. // else travel the list till the last node
  22. Node *node = *start;
  23. while(node->next != NULL){
  24. node = node->next;
  25. }
  26.  
  27. // set the new node as the next of the last node in the list
  28. node->next = new_node;
  29. return;
  30. }
  31.  
  32. // adds a node to the front of the list
  33. void push(Node **start, int new_data){
  34. Node *new_node = new Node;
  35. new_node->data = new_data;
  36. new_node->next = *start;
  37. *start = new_node;
  38. }
  39.  
  40. void display(struct Node *node){
  41. while (node != NULL){
  42. std::cout << node->data << " ";
  43. node = node->next;
  44. }
  45. }
  46.  
  47. // adds a node after a specified node in the list
  48. void insert_after(Node *prev_node, const int new_data){
  49. if(!prev_node){
  50. std::cout << "the given previous node can not be NULL";
  51. }
  52.  
  53. Node *new_node = new Node;
  54. new_node->data = new_data;
  55. new_node->next = prev_node->next;
  56. prev_node->next = new_node;
  57. }
  58.  
  59. // remove the node whose data matches the given key
  60. void remove(Node **start, int key){
  61. Node *node = *start;
  62. Node *prev;
  63.  
  64. // if start does not exist then return
  65. if(!start){
  66. return;
  67. }
  68.  
  69. // if start matches the key then remove start
  70. if(node->data == key){
  71. *start = node->next;
  72. delete node;
  73. return;
  74. }
  75.  
  76. // else find the node which matches the key
  77. while(node->next){
  78. if(node->data == key){
  79. break;
  80. }
  81. prev = node;
  82. node = node->next;
  83. }
  84.  
  85. // if the no node matches the key then do nothing
  86. if(!node->next && node->data != key){
  87. std::cout << "the given key was not found.\n";
  88. return;
  89. }
  90.  
  91. // else delete the matching node
  92. prev->next = node->next;
  93. delete node;
  94. }
  95.  
  96. int main(int argc, char const *argv[]) {
  97. Node *start = NULL;
  98.  
  99. append(&start, 10);
  100. append(&start, 20);
  101. append(&start, 30);
  102. append(&start, 40);
  103. append(&start, 50);
  104. push(&start, 20);
  105. remove(&start, 50);
  106. remove(&start, 100);
  107. display(start);
  108.  
  109. return 0;
  110. }
Add Comment
Please, Sign In to add comment