Modify the C++ class for the abstract stack type shown below to use a linked list representation and test it with the same code that appears in this chapter. #include class Stack { private: //** These members are visible only to other //** members and friends (see Section 11.6.4) int *stackPtr; int maxLen; int topSub; public: //** These members are visible to clients Stack() { //** A constructor stackPtr = new int [100]; maxLen = 99; topSub = -1; } ~Stack() {delete [] stackPtr;}; //** A destructor void push(int number) { if (topSub == maxLen) cerr << "Error in push--stack is full\n"; else stackPtr[++topSub] = number; } void pop() { if (empty()) cerr << "Error in pop--stack is empty\n"; else topSub--; } int top() { if (empty()) cerr << "Error in top--stack is empty\n"; else return (stackPtr[topSub]); } int empty() {return (topSub == -1);} }

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 3PE
icon
Related questions
Question

Modify the C++ class for the abstract stack type shown below
to use a linked list representation and test it with the same code that
appears in this chapter.

#include <iostream.h>
class Stack {
private: //** These members are visible only to other
//** members and friends (see Section 11.6.4)
int *stackPtr;
int maxLen;
int topSub;
public: //** These members are visible to clients
Stack() { //** A constructor
stackPtr = new int [100];
maxLen = 99;
topSub = -1;
}
~Stack() {delete [] stackPtr;}; //** A destructor
void push(int number) {
if (topSub == maxLen)
cerr << "Error in push--stack is full\n";
else stackPtr[++topSub] = number;
}
void pop() {
if (empty())
cerr << "Error in pop--stack is empty\n";
else topSub--;
}
int top() {
if (empty())
cerr << "Error in top--stack is empty\n";
else
return (stackPtr[topSub]);
}
int empty() {return (topSub == -1);}

}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Stack
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning