Question

In: Computer Science

Below is a definition of the class of the circular link list. class CircChain; class ChainNode...

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()'

Solutions

Expert Solution

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!


Related Solutions

Below is a definition of the class of a simple link list. class Chain; class ChainNode...
Below is a definition of the class of a simple link list. class Chain; class ChainNode { friend class Chain; private: int data; ChainNode *link ; }; class Chain{ public: ... private: ChainNode *first; // The first node points. } Write a member function that inserts a node with an x value just in front of a node with a val value by taking two parameters, x and val. If no node has a val value, insert the node with...
A incomplete definition of a class Temperature is given below: public class Temperature { private double...
A incomplete definition of a class Temperature is given below: public class Temperature { private double value[] = {36.5, 40, 37, 38.3}; } [6] (i) Copy and put it in a new class. Write a method toString() of the class, which does not have any parameters and returns a string containing all the values separated by newlines. When the string is printed, each value should appear on a line in the ascending order of their indexes. Copy the content of...
A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end;...
A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end; the nodes form a full circle. Instead of keeping track of the node at the front, we keep track of a current node instead. Write a class for a circular doubly-linked list using the attached Job class as your node objects. It should have: • A private instance variable for the current node • A getCurrent() method that returns a reference to the current...
Use the class definition below to answer the following questions. [Total 8 Marks] public class TrafficLight...
Use the class definition below to answer the following questions. [Total 8 Marks] public class TrafficLight { String stopLight = "red"; String waitLight; String goLight; public void setStopLight(String colour) { stopLight = colour; } public String getGreenLight() { return goLight; } } Question 21 Not yet answered Marked out of 1.00 Flag question Question text D3a - [1 Mark] How many field attributes are there in the TrafficLight class? Answer: Question 22 Not yet answered Marked out of 1.00 Flag...
Use the class definition below to answer the following questions. [Total 8 Marks] public class TrafficLight...
Use the class definition below to answer the following questions. [Total 8 Marks] public class TrafficLight { String stopLight = "red"; String waitLight; String goLight; public void setStopLight(String colour) { stopLight = colour; } public String getGreenLight() { return goLight; } } 1 :How many field attributes are there in the TrafficLight class? 2 :Name a field attribute for this class. 3 :What is the name of the method that is an accessor? 4 :What is the name of the...
In this discussion, we will link together and examine the circular flow diagram, and how that...
In this discussion, we will link together and examine the circular flow diagram, and how that connects to alternative market systems. Think of a business that you recently frequented (Target, Macy’s, McDonald’s, anything). What prompted the producers of the individual products in the store to produce them and offer them for sale? How did they decide on how and what resources to use to produce them? Who made those resources available, and why? How does the market determine who will...
C++ Using an appropriate definition of ListNode, design a simple linked list class with only two...
C++ Using an appropriate definition of ListNode, design a simple linked list class with only two member functions and a default constructor: void add(double x); boolean isMember(double x); LinkedList( ); The add function adds a new node containing x to the front (head) of the list, while the isMember function tests to see if the list contains a node with the value x. Test your linked list class by adding various numbers to the list and then testing for membership....
Python class DLLNode: """ Class representing a node in the doubly linked list implemented below. """...
Python class DLLNode: """ Class representing a node in the doubly linked list implemented below. """ def __init__(self, value, next=None, prev=None): """ Constructor @attribute value: the value to give this node @attribute next: the next node for this node @attribute prev: the previous node for this node """ self.__next = next self.__prev = prev self.__value = value def __repr__(self): return str(self.__value) def __str__(self): return str(self.__value) def get_value(self): """ Getter for value :return: the value of the node """ return self.__value...
List the withdrawals and injections to the circular flow.
List the withdrawals and injections to the circular flow.
Java program to implement circular linked list. public class CircularLinkedList { private Node tail; private int...
Java program to implement circular linked list. public class CircularLinkedList { private Node tail; private int size; public CircularLinkedList() { tail= null; size = 0; } public int size(){ return size; } public boolean isEmpty() { return size==0; } //if list is not empty return the first element public E first() { if (isEmpty()) return null; //code here return 0; } //if list not empty return last element public E last() { if (isEmpty()) return null; return tail.getElement(); } /*...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT