Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Here's a link to my assingment. https://www.dropbox.com/s/g0vmvwkkkg63lkk/CSC%20260%20-%20IC4-Trace_03-13-18%281%29%202.pdf?dl=0 Here's what's given: MainDriver_ReverseList.cpp // List Reverse #include /* cin, cout, */ #include /* srand, rand */ #include

Here's a link to my assingment.

https://www.dropbox.com/s/g0vmvwkkkg63lkk/CSC%20260%20-%20IC4-Trace_03-13-18%281%29%202.pdf?dl=0

Here's what's given:

MainDriver_ReverseList.cpp

// List Reverse #include  /* cin, cout, */ #include  /* srand, rand */ #include  /* time */ #include "IntList.h" /* user defined list operations */ using namespace std; int main() { // # of nodes the users wants in the list int node_count; // Create an instance of IntList. IntList myList; // Build a static list. myList.appendNode(2); // Append 2 to the list myList.appendNode(4); // Append 4 to the list myList.appendNode(6); // Append 6 to the list // Display the nodes. cout << "Here are the values in myList: "; myList.print(); cout << endl; // Build a new randomly generated list. cout << "Please enter the number of values you would like to add to your list" << endl; cin >> node_count; /* initialize random seed: */ srand (time(NULL)); for (int i=0; i 

IntList.h

// Specification file for the IntList class #ifndef INTLIST_H #define INTLIST_H class IntList { private: // Declare a structure for the list struct ListNode { int value; struct ListNode *next; }; ListNode *head; // List head pointer // Destroy function void destroy(); public: // Constructor IntList() { head = NULL; } // Copy constructor IntList(const IntList &); // Destructor ~IntList(); // List operations void appendNode(int); void insertNode(int); void deleteNode(int); void print(); void reverse(); }; #endif 

IntList.cpp

// Implementation file for the IntList class #include  // For cout and NULL #include "IntList.h" using namespace std; //************************************************** // Copy constructor * //************************************************** IntList::IntList(const IntList &listObj) { ListNode *nodePtr; // Initialize the head pointer. head = NULL; // Point to the other object's head. nodePtr = listObj.head; // Traverse the other object. while (nodePtr) { // Append a copy of the other object's // node to this list. appendNode(nodePtr->value); // Go to the next node in the other object. nodePtr = nodePtr->next; } } //************************************************** // appendNode appends a node containing the * // value pased into num, to the end of the list. * //************************************************** void IntList::appendNode(int num) { ListNode *newNode, *nodePtr; // Allocate a new node & store num newNode = new ListNode; newNode->value = num; newNode->next = NULL; // If there are no nodes in the list // make newNode the first node if (!head) head = newNode; else // Otherwise, insert newNode at end { // Initialize nodePtr to head of list nodePtr = head; // Find the last node in the list while (nodePtr->next) nodePtr = nodePtr->next; // Insert newNode as the last node nodePtr->next = newNode; } } //************************************************** // The print function displays the value * // stored in each node of the linked list * // pointed to by head. * //************************************************** void IntList::print() { ListNode *nodePtr; nodePtr = head; while (nodePtr) { cout << nodePtr->value << endl; nodePtr = nodePtr->next; } } //************************************************** // The insertNode function inserts a node with * // num copied to its value member. * //************************************************** void IntList::insertNode(int num) { ListNode *newNode, *nodePtr, *previousNode = NULL; // Allocate a new node & store num newNode = new ListNode; newNode->value = num; // If there are no nodes in the list // make newNode the first node if (!head) { head = newNode; newNode->next = NULL; } else // Otherwise, insert newNode { // Initialize nodePtr to head of list and // previousNode to NULL. nodePtr = head; previousNode = NULL; // Skip all nodes whose value member is less // than num. while (nodePtr != NULL && nodePtr->value < num) { previousNode = nodePtr; nodePtr = nodePtr->next; } // If the new node is to be the 1st in the list, // insert it before all other nodes. if (previousNode == NULL) { head = newNode; newNode->next = nodePtr; } else // Otherwise, insert it after the prev node { previousNode->next = newNode; newNode->next = nodePtr; } } } //************************************************** // The deleteNode function searches for a node * // with num as its value. The node, if found, is * // deleted from the list and from memory. * //************************************************** void IntList::deleteNode(int num) { ListNode *nodePtr, *previousNode; // If the list is empty, do nothing. if (!head) return; // Determine if the first node is the one. if (head->value == num) { nodePtr = head->next; delete head; head = nodePtr; } else { // Initialize nodePtr to head of list nodePtr = head; // Skip all nodes whose value member is // not equal to num. while (nodePtr != NULL && nodePtr->value != num) { previousNode = nodePtr; nodePtr = nodePtr->next; } // If nodePtr is not at the end of the list, // link the previous node to the node after // nodePtr, then delete nodePtr. if (nodePtr) { previousNode->next = nodePtr->next; delete nodePtr; } } } void IntList::reverse() { ListNode *newHead = NULL, *newNode, *nodePtr, *tempPtr; // Traverse the list, building a // copy of it in reverse order. nodePtr = head; while (nodePtr) { // Allocate a new node & store the // value of the current list node in it. newNode = new ListNode; newNode->value = nodePtr->value; newNode->next = NULL; // Shift the existing nodes in the new // list down one, inserting the new // node at the top. if (newHead != NULL) { tempPtr = newHead; newHead = newNode; newNode->next = tempPtr; } else { newHead = newNode; } // Go to the next node in the list. nodePtr = nodePtr->next; } // Destroy the existing list. destroy(); // Make head point to the new list. head = newHead; } //************************************************** // The destroy function destroys all the nodes * // in the list. * //************************************************** void IntList::destroy() { ListNode *nodePtr, *nextNode; nodePtr = head; while (nodePtr != NULL) { nextNode = nodePtr->next; delete nodePtr; nodePtr = nextNode; } head = NULL; } //************************************************** // Destructor * // This function deletes every node in the list. * //************************************************** IntList::~IntList() { destroy(); } 

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

Recommended Textbook for

Advances In Spatial And Temporal Databases 10th International Symposium Sstd 2007 Boston Ma Usa July 2007 Proceedings Lncs 4605

Authors: Dimitris Papadias ,Donghui Zhang ,George Kollios

2007th Edition

3540735399, 978-3540735397

More Books

Students also viewed these Databases questions

Question

Write a Python program to check an input number is prime or not.

Answered: 1 week ago

Question

a. Describe the encounter. What made it intercultural?

Answered: 1 week ago