Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that implements a stack using an array. Your user will decide ho

ID: 3550471 • Letter: W

Question

Write a program that implements a stack using an array. Your user will decide how large an array to create for your stack. The stack will hold only positive integer numbers.

Your program should keep track of the stack's attributes, size and top.
Size is an integer that holds the number of elements currently stored in the stack and top is a pointer that holds the address where the next element in the stack will be added. These attributes can be implemented as global variables eliminating the need to pass their values to your functions. You can also globally declare a pointer variable that will be used to point to your stack and an int variable to contain its capacity. Initialize the stack pointer value to NULL.
Suggested global variable declarations:
int * stack - always points to the location of the first element in the stack
int * top - points to the location where the next element will be stored (push and pop will increment/decrement this address)
int capacity - the number of elements that can be stored in the stack (user will provide this value, it never changes because you're implementing a static stack)
int size - the number of elements actually stored in the stack (push and pop will increment/decrement this value)

Present the user with a menu containing the following options: Create, Push, Pop, Count, Display and Exit. Your program should loop continuously until the user chooses Exit.



Two helper functions should also be created that will be used by your push, pop and display functions. They are:



Welcome to the CSC 130 Stack Implementation Program! Make a choice from one of the 6 options below. Enter either a 1, 2, 3, 4, 5, or 6: Create Push Pop Count Display Exit

Explanation / Answer

#include<iostream>

#include<cstdlib>

using namespace std;


int stack[15],capacity,top=-1;


int isFull()

{

if(top==capacity)

{cout<<"Stack is full.Oveflow!!";

return 0;

}

else

return 1;

}

void push(int num)

{

int flag=isFull();

if(flag)

stack[++top]=num;

}

int isEmpty()

{

if(top==-1)

{

cout<<"Stack is Empty.Underflow!!";

return 0;

}

else

return 1;

}

void pop()

{

int flag=isEmpty();

if(flag)

cout<<"deleted element: "<<stack[top--];

}

void count()

{

int x=top,c;

while(x>=0)

{

x--;

c++;

}

cout<<" number of elements stored in stack are: "<<c;

}

void display()

{

int i;

cout<<"elements of stack are: ";

for(i=top;i>-1;i--)

cout<<stack[i]<<" ";

}


int main()

{

int ch,x;

do

{

cout<<" 1.create 2.Push 3.pop 4.count 5.display 6.Exit choose your choice:";

cin>>ch;

switch(ch)

{

case 1 : cout<<"enter the size of stack: ";

cin>>capacity;

break;

case 2 : cout<<" Enter number";

cin>>x;

push(x);

break;

case 3 : pop();

break;

case 4 : count();

break;

case 5 : display();

break;

case 6 : exit(0);

}

}while(ch!=6);

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote