write a program in c++ to implement stack using functions
in header file stack.h

Answer Posted / sachindra bagchi

Implementing Stack using Class (with constructor etc).

# include<iostream.h>
# include<conio.h>
# define SIZE 20

class stack
{
int a[SIZE];
int tos; // Top of Stack
public:
stack();
void push(int);
int pop();
int isempty();
int isfull();
};
stack::stack()
{
tos=0; //Initialize Top of Stack
}

int stack::isempty()
{
return (tos==0?1:0);
}
int stack::isfull()
{
return (tos==SIZE?1:0);
}

void stack::push(int i)
{

if(!isfull())
{
cout<<"Pushing "<<i<<endl;
a[tos]=i;
tos++;
}
else
{
cerr<<"Stack overflow error !
Possible Data Loss !";
}
}
int stack::pop()
{
if(!isempty())
{
cout<<"Popping "<<a[tos-1]<<endl;
return(a[--tos]);
}
else
{
cerr<<"Stack is empty! What to pop...!";
}
return 0;
}

void reverse(stack s)
{
stack s2;
while(!s.isempty())
{
s2.push(s.pop());
}
cout<<"Reversed contents of the stack..."<<endl;
while(!s2.isempty())
{
cout<<s2.pop()<<endl;
}
}//end of fn.
void main()
{
clrscr();
stack s;

s.push(1);
s.push(2);
s.push(3);

reverse(s);
getch();
}

Is This Answer Correct ?    30 Yes 32 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain operator overloading.

604


What is oop in c++?

601


List the types of polymorphism in c++?

627


What is the average salary of a c++ programmer?

544


. If employee B is the boss of A and C is the boss of B and D is the boss of C and E is the boss of D. Then write a program using the Database such that if an employee name is Asked to Display it also display his bosses with his name. For eg. If C is displayed it should also display D and E with C?

2758






What is virtual base class?

573


how to access grid view row?

1814


When should you use global variables?

630


Describe linkages and types of linkages?

569


Do you need a main function in c++?

561


What are the various compound assignment operators in c++?

553


what is Member Functions in Classes?

620


What is the use of vtable?

673


What are iterators in c++?

598


What is a constructor and how is it called?

600