Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Program Specifications The goal of this problem is to extend the IntList class to add an additional function, deleteSub(startPos, n). This function does not return

Program Specifications

The goal of this problem is to extend the IntList class to add an additional function, deleteSub(startPos, n). This function does not return anything and deletes n consecutive Nodes starting with the Node at the start position passed in. The head Node has a start position of 0.

  • If there are less than n Nodes from the start position to the end of the list, then delete all Nodes from start position to the end of the list.
  • If the start position passed in is too large, do not delete any Nodes, but also don't let the program crash. Just do nothing.

For example, given deleteSub(2, 3) is called on the following IntList

11 -> 32 -> 43 -> 99 -> 17 -> 42 

the IntList after the call should be

11 -> 32 -> 42 

Be sure to NOT leave any DANGLING POINTERS or MEMORY LEAKS!

Note that the IntNode class we are using in this program is slightly different from the one you have used for the previous labs and programs. The value field of the IntNode is declared const, which means that you cannot assign or modify the value of a node after it is created.

main.cpp

#include #include "IntList.h"

using namespace std;

int main() { cout << "Enter a test case #: "; int testCase; cin >> testCase; cout << endl;

if (testCase == 0) { IntList il; il.push_front(9); il.push_front(8); il.push_front(7); il.push_front(6); il.push_front(5); cout << "Before: " << il << endl; il.deleteSub(2, 1); cout << "After: " << il << endl; } else if (testCase == 1) { IntList il; cout << "Before: " << il << endl; il.deleteSub(0, 2); il.deleteSub(2, 1); cout << "After: " << il << endl; } else if (testCase == 2) { IntList il; il.push_front(-1); cout << "Before: " << il << endl; il.deleteSub(0, 1); cout << "After: " << il << endl; } else if (testCase == 3) { IntList il; il.push_front(-1); il.push_front(-2); il.push_front(-3); il.push_front(-4); il.push_front(-5); il.push_front(7); il.push_front(2); il.push_front(1); il.push_front(9); cout << "Before: " << il << endl; il.deleteSub(0, 3); cout << "After: " << il << endl; } else if (testCase == 4) { IntList il; il.push_front(-1); il.push_front(-2); il.push_front(-3); il.push_front(-4); il.push_front(-5); il.push_front(7); il.push_front(2); il.push_front(1); il.push_front(9); cout << "Before: " << il << endl; il.deleteSub(8,1); cout << "After: " << il << endl; } else if (testCase == 5) { IntList il; il.push_front(-1); il.push_front(-2); il.push_front(-3); il.push_front(-4); il.push_front(-5); il.push_front(7); il.push_front(2); il.push_front(1); il.push_front(9); cout << "Before: " << il << endl; il.deleteSub(0,9); cout << "After: " << il << endl; } else if (testCase == 6) { IntList il; il.push_front(-1); il.push_front(-2); il.push_front(-3); il.push_front(-4); il.push_front(-5); il.push_front(7); il.push_front(2); il.push_front(1); il.push_front(9); cout << "Before: " << il << endl; il.deleteSub(5,9); cout << "After: " << il << endl; } else if (testCase == 7) { IntList il; il.push_front(-1); il.push_front(-2); il.push_front(-3); il.push_front(-4); il.push_front(-5); il.push_front(7); il.push_front(2); il.push_front(1); il.push_front(9); cout << "Before: " << il << endl; il.deleteSub(5,0); cout << "After: " << il << endl; } }

IntList.h

#ifndef __INTLIST_H #define __INTLIST_H

#include

using namespace std;

// IntNode struct // Note: value cannot be changed after initialization struct IntNode { const int value; IntNode* next; IntNode(int value) : value(value), next(nullptr) {} };

class IntList { IntNode* head; public: IntList();

// Inserts a data value to the front of the list. void push_front(int);

void deleteSub(unsigned, unsigned); friend ostream& operator<<(ostream &out, const IntList &rhs) { IntNode *t = rhs.head; if (t != nullptr) { out << t->value; for (t = t->next; t != nullptr; t = t->next) { out << " " << t->value; } } return out; } };

#endif /* __INTLIST_H */

IntList.cpp

#include "IntList.h"

/* Initializes an empty list. */ IntList::IntList() : head(nullptr) {}

/* Inserts a data value to the front of the list. */ void IntList::push_front(int val) { if (!head) { head = new IntNode(val); } else { IntNode *temp = new IntNode(val); temp->next = head; head = temp; } }

void IntList::deleteSub(unsigned pos, unsigned n) { // TODO: Implement me }

ICE:zybook C++

Please answer this question within 1 hour.

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

Database Administrator Limited Edition

Authors: Martif Way

1st Edition

B0CGG89N8Z

More Books

Students also viewed these Databases questions

Question

Why do HCMSs exist? Do they change over time?

Answered: 1 week ago