m2skills

delete node pointer c++

Jan 28th, 2018
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <stack>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. class node{
  8.  
  9. protected:
  10.     int element;
  11.     node* link;
  12.  
  13. public:
  14.     //constructor that accepts only element
  15.     node(int element) {
  16.         this->element = element;
  17.         this->link = NULL;
  18.     }
  19.  
  20.     //constructor that accepts both link and element
  21.     node(int element, node* link){
  22.         this->element = element;
  23.         this->link = link;
  24.     }
  25.  
  26.     //method to update the element
  27.     void updateData(int element){
  28.         this->element = element;
  29.     }
  30.  
  31.     //method to update or setup link
  32.     void updateLink(node* link){
  33.         this->link = link;
  34.     }
  35.  
  36.     //method to get the element from the node
  37.     int getElement(){
  38.         return this->element;
  39.     }
  40.  
  41.     //method to get the next node
  42.     node* getNextNode(){
  43.         return this->link;
  44.     }
  45. };
  46.  
  47. class Linkedlist {
  48. public:
  49.     node *head;
  50.  
  51.     //constructor for the Linked List class
  52.     Linkedlist() {
  53.         head = NULL;
  54.     }
  55.  
  56.  
  57.     //returns head node
  58.     node* getHead() {
  59.         return this->head;
  60.     }
  61.  
  62.     // method to add a node at the end
  63.     void insert(int element) {
  64.         node *tempNode = new node(element);
  65.         node *p = head;
  66.         if (head == NULL) {
  67.             head = tempNode;
  68.             return;
  69.         }
  70.         else {
  71.             while (p->getNextNode() != NULL) {
  72.                 p = p->getNextNode();
  73.             }
  74.             p->updateLink(tempNode);
  75.             return;
  76.         }
  77.     }
  78.  
  79.     //method to display all the elements of the Linked List
  80.     void display() {
  81.         cout << "\n";
  82.         node *tempNode = head;
  83.         while (tempNode != NULL) {
  84.             if (tempNode->getNextNode() != NULL)
  85.                 cout << tempNode->getElement() << " --> ";
  86.             else
  87.                 cout << tempNode->getElement();
  88.  
  89.             tempNode = tempNode->getNextNode();
  90.         }
  91.         return;
  92.     }
  93.  
  94.  
  95.     // deleting node with only that node pointer
  96.     void deleteNode(node* nodePtr){
  97.         if(nodePtr == NULL){
  98.             return;
  99.         }else{
  100.             // make a pointer to next node
  101.             node* nextNode = nodePtr->getNextNode();
  102.  
  103.             // copy deteils of next node to current node
  104.             nodePtr->updateData(nextNode->getElement());
  105.             nodePtr->updateLink(nextNode->getNextNode());
  106.  
  107.             // delete next node
  108.             free(nextNode);
  109.         }
  110.     }
  111.  
  112. };
  113.  
  114. int main() {
  115.     cout<<"Program to Delete a node from Linked List with only pointer to the node"<<endl;
  116.     Linkedlist l1;
  117.     l1.insert(1);
  118.     l1.insert(3);
  119.     l1.insert(5);
  120.     l1.insert(7);
  121.     l1.insert(9);
  122.     l1.insert(11);
  123.     l1.insert(13);
  124.     l1.insert(15);
  125.  
  126.     node* third = l1.getHead();
  127.     for(int i=0; i<2; i++){
  128.         third = third->getNextNode();
  129.     }
  130.  
  131.     cout<<"\nBefore deleting the third node the linked list is : ";
  132.     l1.display();
  133.  
  134.     l1.deleteNode(third);
  135.  
  136.     cout<<"\nAfter deleting the third node the linked list is : ";
  137.     l1.display();
  138.  
  139.     return 0;
  140. }
Add Comment
Please, Sign In to add comment