In: Computer Science
Below is a definition of the class of the circular link list.
class CircChain;
class ChainNode {
friend class CircChain;
private:
int data;
ChainNode *link ;
};
class CircChain{
public:
...
private:
ChainNode *last; // The last node points.
}
=>
Create a member function to delete the last node. Returns false if empty list; otherwise, delete last node and return true.
'Bool CircChain::DeleteLastNode()'
Hello!
Here the last node is always the new node created.
Before we assign last to node we have to make sure the previous node get the link for newly created node
And if you understand that then it will easy , so please take time and understand the concepts as i have used comments also.
In delete:
I will travel from first node and when i encounter the first node i will stop and changes the links of the node
So everything i have explained in comments please take some time and get clear idea and let me know if you have any doubts regarting the concept and the doubt regarding to any line of the code.
CODE:
#include <iostream>
using namespace std;
class CircChain;
class ChainNode {
friend class CircChain;
private:
int data;
ChainNode *link ;
};
class CircChain{
public:
CircChain(){
last = NULL;
}
// method for inserting data
void insert(int data){
// creating a pointer object of ChainNode
ChainNode *node = new ChainNode;
// data is given to node created
node->data = data;
node->link = NULL;
// if no node is present
if(last == NULL){
// last points the created node
last = node;
// link of node point the last node
node->link = last;
}
// if atleast one node exists
else{
ChainNode *temp = new ChainNode;
// temp stores the node linked for last node
temp = last->link;
// last node link gets changed to new node
last->link = node;
// node link points to the first node
node->link = temp;
// the last node becomes the node created
last = node;
}
}
void show(){
ChainNode *node = new ChainNode;
// node will have the first node
node = last->link;
// checks the node having link to last node
while(node != last){
// prints the data in node
cout << node->data << endl;
// node gets changed to linked node
node = node->link;
}
cout << node->data << endl;
}
bool DeleteLastNode(){
// returns false if last points to NULL
if(last == NULL){
return false;
}
// else deletes the last node
else{
ChainNode *node = new ChainNode;
// node points to the first node of list
node = last->link;
// checks the node having link to last node
while(node->link != last){
// node gets changed to linked node
node = node->link;
}
//node link gets updated with last linked node
node->link = last->link;
// finally the last points to last node
last = node;
// and returns true
return true;
}
}
private:
ChainNode *last; // The last node points.
};
int main() {
// creating a circular linked list
CircChain c;
// before inserting data
cout << "Before inserting data \n***Deleting node**** \n";
cout << "Last node deleted: " << c.DeleteLastNode() << endl;
cout << "Inserting data\n";
// inserting data into list
c.insert(15);
c.insert(24);
c.insert(6);
cout << "Elements before deletion:\n";
// displaying data after insertion
c.show();
// deleting the last node
cout << "***Deleting node**** \n";
cout << "Last node deleted: " << c.DeleteLastNode() << endl;
cout << "Elements after deletion:\n";
c.show();
return 0;
}
OUTPUT:
Hope this helps and clear.
I strive to provide the best of my knowledge so please upvote if you like the content.
Thank you!