Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You are given the following header file for a class IntList: The linked list contains integers struct ListNode{ int value ; struct ListNode* next;}; //

You are given the following header file for a class IntList:

The linked list contains integers

struct ListNode{  int value;  struct ListNode* next;};// Specification file for the IntList classclass IntList{private:  ListNode* head;  // List head pointer  // Destroy function  void destroy();public:  // Constructor  IntList()  {    head = nullptr;  }  // Destructor  ~IntList();  // List operations  void insertNode(int);  void deleteNode(int);  void print();  void reverse();  void removeByPos(int);  void destroy();};

Implement the functions as follows:

  1. Constructor- already implemented in the h file so you do not need to do anything
  2. Destructor- as studied in previous modules. Remember, the destructor of a class that contain dynamically allocated memory must free that memory. Note that this class has a destroy function which frees the lists' memory. So, maybe you would need to call that function from the destructor to avoid code redundancy.
  3. insertNode- insert a node anywhere in the list in sorted order. Receives the integer to be inserted. No duplicates. You should have a loop that traverse your list and if a number to be inserted is already in the list, your program should not insert it.
  4. deleteNode- receives the integer that must be deleted and removes the node containing that integer. If your program receives an integer that is not in the list, your program should do nothing
  5. print- should print the current link list
  6. reverse- should reverse the linked list using iteration ( see expalnations below)

Implement your reverse function using iteration. Note: 5 points will be deducted if you implement the reverse function using recursion. I know recursion is more efficient and easier but the purpose of this exercise is to practice manipulating pointers and linked nodes, so you need to implement that function using iteration(looping).

  1. removeByPos- this function should remove a node by position: 0 means removing the very first node. If the position is greater than the list you have, your program should do nothing. For example, if you have only 5 nodes but the position is 10 ( there is no position 10)

PART B

Convert your class to template and test it with:

--- floats

---characters

You should turn in:

  1. Your header file and your cpp implementation filescopied and pasted on a word document
  2. For part B, you need to implement the functions in the header file because template function implementations "like" to be on the same file as the header file
  3. In any way, please do not implement your functions inside the class definition. 5 points without the right of resubmission will be deducted from your gradeif you implement your functions inside the class definition. At this point, you guys should know that this is not a good practice

Your code part 1 will be tested with the following driver program. For part 2, I will be using the same program but will be creating lists of floats and characters.

#include #include #include #include "IntList.h"using namespace std;int main(){ /// Create an instance of IntList. IntList myList; cout "********************************************************************************** "; cout "Testing the insertNode function, nodes inserted in this order: 12, 4, 3, 5, 77, 13 "; cout "********************************************************************************** "; /// Build a list.Will test the program with : sorted list 3,4,5,12,13,77 myList.insertNode(12); myList.insertNode(4); myList.insertNode(3);  // beginning myList.insertNode(5);  //middle myList.insertNode(77); //end myList.insertNode(13); /// Display the nodes. cout "Here are the values in myList: "; myList.print(); cout "******************************************** "; cout "Testing the removeByPos function "; cout "******************************************** "; /// Remove node at position 0( beginning position ). cout "Removing node at position 0 which is the first node, 3 should be removed... "; myList.removeByPos(0); myList.print(); /// Remove node at position 4 ( end position).List now 4,5,12,13,77 cout "Removing node at position 4 which is the last node, 77 should be removed... "; myList.removeByPos(4); myList.print(); /// Remove node at position 2 (middle position). List now 4,5,12,13 cout "Removing node at position 2 which is a middle node, 12 should be removed... "; myList.removeByPos(2); myList.print(); /// Try a position that is too big.List should remain the same cout "Removing node 99, a position that is too big.List should remain the same... "; myList.removeByPos(99); myList.print(); cout " Adding back the deleted nodes so we can rest the deleteNode function... "; myList.insertNode(12); myList.insertNode(3); myList.insertNode(77); myList.print(); /// delete node. cout " Testing the deleteNode function "; cout "Removing number 5, the first node, 5 should be removed... "; myList.deleteNode(5); myList.print(); cout "Removing number 77 which is at the end, 77 should be removed... "; myList.deleteNode(77); myList.print(); cout "Removing number 12 which is in the middle of the list, 12 should be removed... "; myList.deleteNode(12); myList.print(); cout "Removing number 100 which is not in the list, list should remain the same... "; myList.deleteNode(100); myList.print(); cout " Adding back the deleted nodes "; myList.insertNode(12); myList.insertNode(5); myList.insertNode(77); myList.print(); cout "******************************************** "; cout "Testing the reverse function "; cout "******************************************** "; myList.reverse(); myList.print(); return 0;}

This is the output for part A. The output for part be must be identical except it should handle characters and floats.

Output

image text in transcribed
Testing the insertNode function, nodes inserted in this order: 12, 4, 3, 5, 77, 13 Here are the values in myList: 3 4 5 12 13 77 Testing the removeByPos function Removing node at position 0 which is the first node, 3 should be removed. .. 4 5 12 13 77 Removing node at position 4 which is the last node, 77 should be removed. . . 4 5 12 13 Removing node at position 2 which is a middle node, 12 should be removed. .. 4 5 13 Removing node 99, a position that is too big. List should remain the same.. . 4 5 13 Adding back the deleted nodes so we can rest the deleteNode function... 4 5 12 13 77 Testing the deleteNode function Removing number 5, the first node, 5 should be removed. . . 3 4 12 13 77 Removing number 77 which is at the end, 77 should be removed. .. 4 12 13 Removing number 12 which is in the middle of the list, 12 should be removed. .. 4 13 Removing number 100 which is not in the list, list should remain the same. .. 3 4 13 Adding back the deleted nodes 4 5 12 13 77 Testing the reverse function ******* 77 13 12 5 4 3 Process returned 0 (0x0) execution time : 0.476 s

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

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions

Question

Describe ways that Eaton is addressing global challenges.

Answered: 1 week ago

Question

Answered: 1 week ago

Answered: 1 week ago