Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ HELP, PLEASE! Just add those 5 function(implement them) to LinkedListType.cpp. all is given except that. Thanks! Add the following member functions to the LinkedListType

C++ HELP, PLEASE! Just add those 5 function(implement them) to LinkedListType.cpp. all is given except that. Thanks!

  1. Add the following member functions to the LinkedListType class. LinkedListType.h, LinkedListType.cpp, and LLmain.cpp files are uploaded. Your LLmain.cpp should be running without any change. Do not make any changes to LLmain.cpp and LinkedListType.h unless there are syntax errors. But, you need to add definitions for the new member functions requested.

void LinkedListType::duplicateRange (int low, int up);

//While traversing the given linked list,

//If you encounter a node whose int value, x, where low <= x <= up,

// then insert another duplicate node with the same value x before

// that node. For example, for low=25 and up=35, and the given list

// 23 -> 26 -> 77 -> 12 -> 33,

// then the updated linked list should contain

// 23 -> 26 -> 26 -> 77 -> 12 -> 33 -> 33.

// If the linked list is empty then no action is taken.

void LinkedListType::removeEven ();

//While traversing the given linked list,

// If you encounter a node whose int value, x, is an even number,

// then remove that node.

// For example, for the given list

// 23 -> 26 -> 77 -> 12 -> 33,

// then the updated linked list should contain

// 23 -> 77 -> 33.

// If the linked list is empty then no action is taken.

void LinkedListType::removeFirstN (int N);

// Remove the first N nodes from the linked list

// N >= 1 should be checked.

// If N > number-of-nodes then the resulting list will be empty

// Your code should Not traverse the linked list more than 1 time.

void LinkedListType::removeLastN (int N);

// Remove the last N nodes from the linked list

// N >= 1 should be checked.

// If N > number-of-nodes then the resulting list will be empty

// Your code should Not traverse the linked list more than 2 times.

void LinkedListType::subtractNodes (LinkedListType & anotherList);

// Remove nodes if its value appears in one of the nodes in

// anotherList. anotherList remains intact.

// For example, if the linked list is as follows:

// 23 -> 26 -> 77 -> 12 -> 33

// and if anotherList is as follows:

// 15 -> 26 -> 77 -> 33 -> 66

// Then after this function call, the linked list will be changed to

// 23 -> 12

//

Input:

Enter numbers ending with -999 for list:

12 45 23 24 76 23 9 124 234 87 43 55 12 67 123 126 29 -999

Enter low and up for duplicating nodes in list (duplicateRange() function):

20 50

Enter the number of nodes to be removed from the front of list:

8

Enter the number of nodes to be removed from the back of list:

5

Enter numbers ending with -999 for another list:

41 36 12 78 114 126 29 7 45 20 24 76 23 9 137 101 14 88 97 -999

LinkedListType(1).h

#ifndef LinkedList_H #define LinkedList_H #include  #include  using namespace std; //Definition of the node struct nodeType { int info; nodeType *link; }; class linkedListType { public: linkedListType(); void removeFromFront(); //Removing the node from the head of // the linked list void removeAfterMe(nodeType * afterMePtr); //Remove the Node after a Given Node from a Linked List void insertAtFront(int dataVal); //Insert a Node at the Front of a Linked List void insertAfterMe(nodeType *afterMePtr, int dataVal); //Insert a Node after a Given Node in a Linked List void printList(ostream &outs); //Output the Data Parts of the Nodes of a Linked List(List Traversal) void destroyList(); //Destroy a Linked List void appendNode(int dataVal); //Append a Node to a Linked List nodeType * searchValue(int target); //Searching a Linked List for a node with target value // returns a pointer to the first found node with the target //default constructor //Initializes the list to an empty state. //Postcondition: first = NULL, last = NULL, count = 0; linkedListType(const linkedListType& otherList); //copy constructor ~linkedListType(); //destructor //Deletes all the nodes from the list. //Postcondition: The list object is destroyed. void LinkedListType::duplicateRange (int low, int up); //While traversing the given linked list, //If you encounter a node whose int value, x, where low <= x <= up, // then insert another duplicate node with the same value x before // that node. For example, for low=25 and up=35, and the given list // 23 -> 26 -> 77 -> 12 -> 33, // then the updated linked list should contain // 23 -> 26 -> 26 -> 77 -> 12 -> 33 -> 33. // If the linked list is empty then no action is taken. void LinkedListType::removeEven (); //While traversing the given linked list, // If you encounter a node whose int value, x, is an even number, // then remove that node. // For example, for the given list // 23 -> 26 -> 77 -> 12 -> 33, // then the updated linked list should contain // 23 -> 77 -> 33. // If the linked list is empty then no action is taken. void LinkedListType::removeFirstN (int N); // Remove the first N nodes from the linked list // N >= 1 should be checked. // If N > number-of-nodes then the resulting list will be empty // Your code should Not traverse the linked list more than 1 time. void LinkedListType::removeLastN (int N); // Remove the last N nodes from the linked list // N >= 1 should be checked. // If N > number-of-nodes then the resulting list will be empty // Your code should Not traverse the linked list more than 2 times. void LinkedListType::subtractNodes (LinkedListType & anotherList); // Remove nodes if its value appears in one of the nodes in // anotherList. anotherList remains intact. // For example, if the linked list is as follows: // 23 -> 26 -> 77 -> 12 -> 33 // and if anotherList is as follows: // 15 -> 26 -> 77 -> 33 -> 66 // Then after this function call, the linked list will be changed to // 23 -> 12 // protected: int count; //variable to store the number of list elements // nodeType *head; //pointer to the first node of the list private: }; #endif

LinkedListType(2).cpp

#include  #include  #include "LinkedListType.h" using namespace std; linkedListType::linkedListType() //default constructor { head = NULL; count = 0; } void linkedListType::removeFromFront() { nodeType *discardptr; if (head == NULL) // cannot remove node from empty list: stop { cerr << endl << "cannot remove node from an empty linked list" << endl; exit(1); } discardptr = head; head = discardptr->link; delete discardptr; count--; } void linkedListType::removeAfterMe(nodeType * afterMePtr) { nodeType *discardptr; if (afterMePtr == NULL || afterMePtr->link == NULL) // There is no node to be removed { cerr << endl << "there is no node to be removed" << endl; exit(1); } discardptr = afterMePtr->link; afterMePtr->link = discardptr->link; delete discardptr; count--; } void linkedListType::insertAtFront(int dataVal) { nodeType *newptr = new nodeType; newptr->info = dataVal; newptr->link = head; head = newptr; count++; } void linkedListType::insertAfterMe(nodeType *afterMePtr, int dataVal) { if (afterMePtr == NULL) // Invalid address: stop { cerr << endl << "Invalid address " << endl; exit(1); } nodeType *newptr = new nodeType; newptr->info = dataVal; newptr->link = afterMePtr->link; afterMePtr->link = newptr; count++; } void linkedListType::printList(ostream &outs) { nodeType * currentptr; currentptr = head; while (currentptr != NULL) { outs << currentptr->info << " "; currentptr = currentptr->link; } outs << endl; } void linkedListType::destroyList() { nodeType * currentptr, // to hold the address of the current node *releaseptr; // to hold the address of the node to be removed currentptr = head; while (currentptr != NULL) { releaseptr = currentptr; currentptr = currentptr->link; delete releaseptr; } count = 0; } void linkedListType::appendNode(int dataVal) { /* create the new node to be inserted */ nodeType *newptr = new nodeType; newptr->info = dataVal; newptr->link = NULL; if (head == NULL) // Make this node the head of the list if the list is empty head = newptr; else { nodeType * currentptr; currentptr = head; while (currentptr->link != NULL) // find the last node in the list currentptr = currentptr -> link; currentptr->link = newptr; // insert the new node count++; } } nodeType * linkedListType::searchValue(int target) { nodeType *currentPtr = head; while (currentPtr != NULL && currentPtr->info != target) currentPtr = currentPtr->link; return(currentPtr); } linkedListType::~linkedListType() //destructor { destroyList(); }//end destructor LLmain(1).cpp 
#include  #include "linkedListType.h" using namespace std; int main() { linkedListType list0, list1, list2, list3, list4, list5, secondList; int num; bool found = false; cout << "Enter numbers ending with -999 for list: " << endl; cin >> num; while (num != -999) { list0.appendNode(num); list1.appendNode(num); list2.appendNode(num); list3.appendNode(num); list4.appendNode(num); list5.appendNode(num); cin >> num; } cout << endl; cout << "List: "; list0.printList(cout); int low=-1; int up=-1; int n = 0; cout << "Enter low and up for duplicating nodes in list: " << endl; cin >> low >> up; list1.duplicateRange(low,up); cout << "List: "; list1.printList(cout); list2.removeEven(); cout << "List: "; list2.printList(cout); cout << "Enter the number of nodes to be removed from the front of list: " << endl; cin >> n; list3.removeFirstN(5); cout << "List: "; list3.printList(cout); cout << "Enter the number of nodes to be removed from the back of list: " << endl; cin >> n; list4.removeLastN(n); cout << "List: "; list4.printList(cout); cout << "Enter numbers ending with -999 for another list: " << endl; cin >> num; while (num != -999) { secondList.appendNode(num); cin >> num; } list5.subtractNodes(secondList); ; cout << "List after appending second list: "; list5.printList(cout); // system("PAUSE"); // for Visual Studio! return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

The nature and importance of the global marketplace.

Answered: 1 week ago