Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. class lists
  6. {
  7.  
  8. private:
  9. struct node{ //structure to hold the node
  10. int data; //value
  11. node *next; // pionter to the next list
  12. };
  13. node *head; // pointer to the first list
  14.  
  15. public:
  16. lists() //constructor
  17. {
  18. head= nullptr;
  19. }
  20. void add_node(int num) // adding a node at the end
  21. {
  22. node *newnode= new node; //allocate a new node to temporarily store number to append
  23. node *nodeptr; // for transversal
  24. newnode->data=num;
  25. newnode->next= nullptr;
  26.  
  27. if(!head) // if the list is empty
  28. {
  29. head=newnode;
  30. }
  31. else // if the list is not empty append the number
  32. {
  33. nodeptr=head;
  34. while(nodeptr->next) // transverse to get to the last node
  35. {
  36. nodeptr=nodeptr->next;
  37. }
  38. nodeptr->next=newnode;
  39. }
  40.  
  41. }
  42.  
  43. void insert_node(int num) //to add a node in an organized ascending order
  44. {
  45. node *newnode= new node; // to store new data
  46. node *prvnode=nullptr;
  47. node *nodeptr;
  48.  
  49. newnode->data=num;
  50.  
  51.  
  52. if(!head) // if theres no existing list
  53. {
  54. head=newnode;
  55. newnode->next=nullptr;
  56. }else{
  57. prvnode=nullptr;
  58. nodeptr=head;
  59.  
  60. while(nodeptr!=nullptr && nodeptr->data<num) //transverse through the list until u get a number greater
  61. {
  62. prvnode=nodeptr;
  63. nodeptr=nodeptr->next;
  64. }
  65. if(prvnode==nullptr) // if you don't get a data less than the actual number you want to insert, that means there will be no previous, the new number you want to insert will be the head
  66. {
  67. head=newnode;
  68. newnode->next=nodeptr; // newnode becomes the head nd pionts to the next node
  69. }else{
  70. prvnode->next=newnode;
  71. newnode->next=nodeptr;
  72. }
  73. }
  74.  
  75.  
  76. }
  77.  
  78. void show_list() // function to display all nodes
  79. {
  80. node *nodeptr; // for transversal
  81. nodeptr=head; // starting from the first list
  82. while(nodeptr)
  83. {
  84. cout<<nodeptr->data<<endl;
  85. nodeptr=nodeptr->next;
  86. }
  87.  
  88. }
  89.  
  90.  
  91. void delete_node(int num) // function to delete a number/ a node
  92. {
  93. node *prvnode;
  94. node *nodeptr;
  95.  
  96. if(!head) // if theres no list return
  97. {
  98. return;
  99. }
  100. if(head->data==num) //if the first list is the number we are looking for
  101. {
  102. nodeptr=head->next; //let nodeptr become what head was pointing to (i.e next list)
  103. delete head; // delete head
  104. head=nodeptr; // let head become nodeptr
  105. }
  106. else
  107. {
  108. nodeptr=head;
  109. prvnode = nullptr;
  110. while(nodeptr!=nullptr && nodeptr->data!=num) // transverse until we find the number
  111. {
  112. prvnode=nodeptr;
  113. nodeptr=nodeptr->next;
  114. }
  115. if(nodeptr) // if we find the number,
  116. {
  117. prvnode->next= nodeptr->next; // previous node should point to nodeptr node
  118. delete nodeptr; // delete nodeptr
  119. }
  120. }
  121. }
  122. ~lists() //de-constructor
  123. {
  124. node *nodeptr;
  125. node *nextnode;
  126. nodeptr=head; //assign nodeptr to the first list
  127.  
  128. while(nodeptr!= nullptr) // transverse
  129. {
  130. nextnode=nodeptr->next;
  131. delete nodeptr;
  132. nodeptr=nextnode; //position nodeptr at the nextnode again
  133. }
  134. }
  135. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement