Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Int.List.cpp // Implementation file for the IntList class #include #include IntList.h using namespace std; //************************************************** // appendNode appends a node containing the * // value

image text in transcribed

Int.List.cpp

// Implementation file for the IntList class #include #include "IntList.h" using namespace std;

//************************************************** // appendNode appends a node containing the * // value pased into num, to the end of the list. * //**************************************************

void IntList::appendNode(int num) { ListNode *newNode, *nodePtr = nullptr;

// Allocate a new node & store num newNode = new ListNode; newNode->value = num; newNode->next = nullptr;

// 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 insertNode function inserts a node with * // num copied to its value member. * //**************************************************

void IntList::insertNode(int num) { ListNode *newNode, *nodePtr, *previousNode = nullptr;

// 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 = nullptr; } else // Otherwise, insert newNode { // Initialize nodePtr to head of list and // previousNode to a null pointer. nodePtr = head; previousNode = nullptr;

// Skip all nodes whose value member is less // than num. while (nodePtr != nullptr && nodePtr->value next; }

// If the new node is to be the 1st in the list, // insert it before all other nodes. if (previousNode == nullptr) { 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 = nullptr;

// 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 != nullptr && 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; } } }

//************************************************** // Destructor * // This function deletes every node in the list. * //**************************************************

IntList::~IntList() { ListNode *nodePtr, *nextNode = nullptr;

nodePtr = head; while (nodePtr != nullptr) { nextNode = nodePtr->next; delete nodePtr; nodePtr = nextNode; } }

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

public: IntList() // Constructor { head = nullptr; } ~IntList(); // Destructor void appendNode(int); void insertNode(int); void deleteNode(int); };

#endif

Source.cpp

#include #include "IntList.h" using namespace std;

int main() { // Create an instance of IntList IntList list;

// Build the list list.appendNode(2); // Append 2 to the list list.appendNode(4); // Append 4 to the list list.appendNode(6); // Append 6 to the list

// Print the nodes.

// Insert the value 5 into the list. cout

// Delete the node holding 6. cout

// Print the nodes. system("pause"); return 0; }

About Data Type Please provide a program that can demonstrate that the data type int has a storage range. Make a screen capture of the code and a screen capture of the console output. About Dynamic Memory Allocation Run Pr-9-14 and create a code having the performance of Pr-9-14 with static arrays. In the new code, allocate a large amount of memory and to use only a small amount. The new code is with the following lines of code: double sales [1000]; Provide the new code below-you can use screen capture to provide the new code. Provide the screen captures of the two-console output below-they should be the same. Please illustrate what is the difference in memory allocation between the two codes. Please provide a form of comparison of static data structures and dynamic data structures. About Linked Lists In the program List in the project folder, in the file source.cpp, there are three times, where the main file wants to print out the values of the list. Modify the program to add a function called print me. The program should print out all the values in the linked list. Please provide a screen capture of the function print me, and a screen capture of console output of the whole program

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

Graph Databases In Action

Authors: Dave Bechberger, Josh Perryman

1st Edition

1617296376, 978-1617296376

More Books

Students also viewed these Databases questions