Question

In: Computer Science

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 an x value at the end of the list.

void Chain:BeforeInsert(int x, int val) {

Solutions

Expert Solution

class Chain;

class ChainNode {

friend class Chain;

private:

int data;

ChainNode *link ;

};

class Chain{

public:
   void BeforeInsert(int x, int val);

private:

ChainNode *first; // The first node points.

};

void Chain::BeforeInsert(int x, int val) {
   ChainNode* current = first, *prev = first;
  
   ChainNode* newNode = new ChainNode;
   newNode->data = x;
   newNode->link = NULL;
  
   if(first->data == val){
       newNode->link = first;
       first = newNode;
       return;
   }
   while(current != NULL && current->data != val){
       prev = current;
       current = current->link;
   }
   newNode->link = current;
   prev->link = newNode;
}


Related Solutions

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()'
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....
Q1. Explain the definition of Chain infection and list down the 6 links.
  Q1. Explain the definition of Chain infection and list down the 6 links. Q2. In your point of view, how can we stop the communicable diseases?
what is the dimension Chain? what is the closing link,increasing link and decreasing link?
what is the dimension Chain? what is the closing link,increasing link and decreasing link?
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...
1. You are to write a simple program with two classes. One controller class and a class to hold your object definition.
1. You are to write a simple program with two classes. One controller class and a class to hold your object definition. (Similar to what we used in class) 2. Use a package in your project, the package name should be xxxprojectname. xxx is your initials taken from the first three characters of your Cal Poly email address. 3. Read in the following from the JOptionPane input window: a. Customer First Name b. Customer Last Name c. Customer Phone Number...
File Account.java (see 4.1. A Flexible Account Class exercise) contains a definition for a simple bank...
File Account.java (see 4.1. A Flexible Account Class exercise) contains a definition for a simple bank account class withmethods to withdraw, deposit, get the balance and account number, and return a String representation. Note that theconstructor for this class creates a random account number. Save this class to your directory and study it to see how it works.Now modify it to keep track of the total number of deposits and withdrawals (separately) for each day, and the total amountdeposited and...
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...
Which part of the supply chain link is responsible for managing it?
Which part of the supply chain link is responsible for managing it?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT