Get premium membership and access questions with answers, video lessons as well as revision papers.

Using the stack class, write a function called loadstack() that returns a stack that is already loaded with letters of the alphabet (a-z.). Assign this...

      

Using the stack class, write a function called loadstack() that returns a stack that is already loaded with letters of the alphabet (a-z.). Assign this stack to another object in the calling routine and prove that it contains the alphabet. Be sure to change the stack size so it is large enough to hold the alphabet.

  

Answers


Davis
// Load a stack with the alphabet.
#include
using namespace std;
#define SIZE 27
// Declare a stack class for characters.
class stack {
char stck [SIZE]; //holds the stack
int tos; //index of top of stack
public:
stack () ; //constructor
void push(char ch); // push character stack
char pop () ; // pop character from stack
};
// Initialize the stack.
stack::stack ()
{
cout << "Constructing a stack \n";
tos = 0;
}
/push a character.
void stack ::push(char ch) {
if(tos==SIZE) {
cout<<"stack is full\n";
return; }
stck[tos] = ch;
tos++; }
// pop a characterstack::pop() {
if(tos==0) {
cout << "stack is empty\n";
return 0; //return null on empty stack }
tos--;
return stck[tos];
}
void showstack();
stack loadstack();
int main() {
stack s1;
s1=loadstack();
showstack(s1);
return 0;
}
// Display the contents of a stack.
void showstack(stack o){
char c;
// when this statement ends, the o stack is empty
while(c=o.pop()) cout << c << "\n";
cout << "\n";
}
// Load a stack with the letter the alphabet.
stack loadstack ()
{
stack t;
char c;
for(c= 'a ' ; c <= 'z' ; c++) t.push(c);
return t;
}
Githiari answered the question on May 29, 2018 at 17:35


Next: Outline 4 means of payments as provided by post office
Previous: When an object of a derived class is assigned to another object of the same derived class, is the data associated with the base class...

View More Computer Science Questions and Answers | Return to Questions Index


Learn High School English on YouTube

Related Questions