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, 23.06.2019 01:00
Complete the sentence about a presentation delivery method
Answers: 2
question
Computers and Technology, 23.06.2019 22:50
What is an rss reader used for? for creating a user account on a social new site
Answers: 2
question
Computers and Technology, 24.06.2019 11:00
Which of the statements below describe the guidelines for the use of text in presentation programs? a. do not use numbered lists. b. fonts should be appropriate for your audience. c. limit the number of fonts you use to three or four. d. only use bulleted lists for sales promotions. e. select font sizes that are appropriate for your delivery method. f. use font colors that work well with your background. select all that apply
Answers: 1
question
Computers and Technology, 24.06.2019 13:00
In a heat transfer course, we can derive the equation for the temperature distribution in a flat rectangular plate. in this example, we will look at a plate at steadystate with three sides being held at t1, and one side held at t2. the temperature for any location on the plate, t(x,y), can be calculated by where create a function (prob3_5) that will take inputs of vectors x and y in feet, scalar n, scalars l and w in feet and scalars t1 and t2 in degrees fahrenheit. it will output a matrix t which is the temperature of each x and y locations. t will have the number of columns equal to the number of elements in x and rows equal to the number of elements in y. though this can be done without loops (perhaps more efficiently), your program must use a nested loop.
Answers: 2
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
Mathematics, 12.06.2021 01:00
question
Mathematics, 12.06.2021 01:00
question
Mathematics, 12.06.2021 01:00
Questions on the website: 13722362