Write a stack member function called elements that returns the number of element
ID: 3758950 • Letter: W
Question
Write a stack member function called elements that returns the number of elements in a stack (the stack should not be changed):
int elements ();
Write a stack member function called print_ends that prints the top element in the stack and then the bottom element in the stack with a space between them. It should not print a label, just the values from the stack.
void print_ends ();
Write a stack member function called change_bottom that sets the element at the bottom of the stack to its parameter:
void change_bottom (char new_item);
Make the following changes to the application file (there are comments to help instruct you where to place each item):
Declare a stack called stacky.
Push the characters F, L, O, W, E, and R on the stack.
Call your print_ends function to print the stack.
Change the bottom of the stack to ‘P’ using change_bottom
Pop the stack.
Call your print_ends function to print the stack
Print the number of elements in the stack (using the elements function to find the number of elements). Plug the call into the already present cout.
Stack.h
#include <iostream>
using namespace std;
// Implementation file for the stack
const int stack_size = 1000;
class stack
{
private:
// array of elements in the stack
char data [stack_size];
// index to the top of the stack
int top;
public:
// stack is a constructor, creates an empty stack
stack ();
// removes an element from the stack and returns it
char pop ();
// adds an element to the top of the stack
void push (char item);
// returns true if the stack is empty, else false
bool empty ();
// returns true if the stack is full, else false
bool full ();
};
// stack is a constructor, creates an empty stack
stack::stack ()
{
top = -1;
}
// removes an element from the stack and returns it
char stack::pop ()
{
// if the stack is empty, print an error
if (empty ())
{
cout << " Stack error: pop";
cout << " Popping an empty stack";
cout << " Returning a space";
return ' ';
}
else // OK to pop the stack
{
top--;
return data [top + 1];
}
}
// adds an element to the top of the stack
void stack::push (char item)
{
// if the stack is full, print an error message
if (full ())
{
cout << " Stack error: push";
cout << " Pushing onto a full stack";
}
else // OK to push
{
top++;
data [top] = item;
}
}
// returns true if the stack is empty, else false
bool stack::empty ()
{
return top == -1;
}
// returns true if the stack is full, else false
bool stack::full ()
{
return top == stack_size - 1;
}
main
#include <cstdlib>
#include <iostream>
#include "stack.h" // Note: stack implementation file is included
using namespace std;
int main(int argc, char *argv[])
{
// declare a stack called stacky
// Push characters F, L, O, W, E, and R on the stack
cout << "The initial stack top and bottom are: ";
// Call print_ends
// Change the bottom of the stack to 'P' using change_bottom
// Pop the stack
cout << " The stack top and bottom after changes are: ";
// Call print_ends
// Print the number of elements in the stack using elements function
cout << " There are now " << " " << " elements in the stack";
cout << " ";
system("PAUSE");
return EXIT_SUCCESS;
}
Explanation / Answer
/* A class that supports all the stack operations and one additional operation getMin() that returns the minimum element from stack at any time. This class inherits from the stack class and uses an auxiliarry stack that holds minimum elements */
class SpecialStack: public Stack
{
Stack min;
public:
int pop();
void push(int x);
int getMin();
};
/* SpecialStack's member method to insert an element to it. This method makes sure that the min stack is also updated with appropriate minimum values */
void SpecialStack::push(int x)
{
if(isEmpty()==true)
{
Stack::push(x);
min.push(x);
}
else
{
Stack::push(x);
int y = min.pop();
min.push(y);
if( x < y )
min.push(x);
else
min.push(y);
}
}
/* SpecialStack's member method to remove an element from it. This method removes top element from min stack also. */
int SpecialStack::pop()
{
int x = Stack::pop();
min.pop();
return x;
}
/* SpecialStack's member method to get minimum element from it. */
int SpecialStack::getMin()
{
int x = min.pop();
min.push(x);
return x;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.