subject
Computers and Technology, 17.04.2021 18:10 2077087

Can someone help me with this lab assignment? I really do not know what should I do? This assignment
The program you wrote in Stacks 1 is incomplete. Without a destructor it creates a memory leak. A destructor has been defined in this program but it is incorrect. To visualize how the destructor works, a cout statement has been added. Fix the errors, including the cout statement, to display the value in each node of the stack before releasing the memory.
Write a loop in main() to enter an unknown number of positive integers. The loop stops when you enter 0 or a negative number. As you are entering integers, they are to be pushed onto a stack.
Ex.: If the user enters '10 20 30 -1` the output (from the destructor) should be:
30 - deleted!
20 - deleted!
10 - deleted!
Empty stack!
Ex.: If the user enters '-1` the output should be:
Empty stack!
This is the code:
#include
using namespace std;
class Stack_int
{
private:
// Structure for the stack nodes
struct StackNode {
int value; // Value in the node
StackNode *next; // Pointer to next node
};
StackNode *top; // Pointer to the stack top
int length; // Number of nodes
public:
Stack_int(){ top = NULL; length = 0; } //Constructor
~Stack_int(); // Destructor
// Stack operations
// bool isEmpty();
bool push(int);
// int pop();
// int peek();
// int getLength();
};
/**~*~*
Member function push: pushes the argument onto the stack.
*~**/
bool Stack_int::push(int item)
{
StackNode *newNode; // Pointer to a new node
// Allocate a new node and store num there.
newNode = new StackNode;
if (!newNode)
return false;
newNode->value = item;
// Update links and counter
newNode->next = top;
top = newNode;
length++;
return true;
}
/**~*~*
Destructor
*~**/
Stack_int::~Stack_int()
{
StackNode *currNode;
// Position nodePtr at the top of the stack.
currNode = top;
// Traverse the list deleting each node.
while (currNode)
{
cout << currNode->value << " - deleted!" << endl;
delete currNode;
currNode = NULL;
currNode = currNode->next;
}
cout << "Empty stack!" << endl;
}
int main() {
Stack_int s;
int item;
return 0;
}

ansver
Answers: 2

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 09:30
Is a string of code written to hurt others by damaging or destroying
Answers: 1
question
Computers and Technology, 23.06.2019 04:00
Another name for addicting games.com
Answers: 1
question
Computers and Technology, 23.06.2019 18:30
Where can page numbers appear? check all that apply. in the header inside tables in the footer at the bottom of columns at the top of columns
Answers: 1
question
Computers and Technology, 23.06.2019 20:30
Column a of irma’s spreadsheet contains titles for each row, but her document is too big and will be printed three pages across. she wants to be sure that every page will be understood. what can irma do to with this problem?
Answers: 3
You know the right answer?
Can someone help me with this lab assignment? I really do not know what should I do? This assignmen...
Questions
question
History, 21.04.2020 00:56
question
Mathematics, 21.04.2020 00:56
question
English, 21.04.2020 00:56
Questions on the website: 13722361