write a program in c++ to implement stack using functions
in header file stack.h
Answer Posted / jhil
# include<iostream.h>
# include<conio.h>
# define SIZE 20
class stack
{
int a[SIZE];
int top; // Top of Stack
public:
stack();
void push(int);
int pop();
int isempty();
int isfull();
};
stack::stack()
{
top=0; //Initialize Top of Stack
}
int stack::isempty()
{
return (top==0?1:0);
}
int stack::isfull()
{
return (top==SIZE?1:0);
}
void stack::push(int i)
{
if(!isfull())
{
cout<<"Pushing a data "<<i<<endl;
a[top]=i;
top++;
}
else
{
cout<<"Stack overflow error !Possible Data Loss !";
}
}
int stack::pop()
{
if(!isempty())
{
cout<<"Popping "<<a[top-1]<<endl;
return(a[--top]);
}
else
{
cout<<"Stack is empty! What to pop...!";
}
return 0;
}
void main()
{
clrscr();
stack s;
s.push(1);
s.push(2);
s.push(3);
s.pop();
s.pop();
getch();
}
| Is This Answer Correct ? | 15 Yes | 23 No |
Post New Answer View All Answers
Explain friend class?
What is the use of dot in c++?
How do you clear a set in c++?
What is a c++ object?
What are the various situations where a copy constructor is invoked?
What are the uses of pointers?
What does the linker do?
What are iterators in c++?
What is an adaptor class or wrapper class in c++?
How does list r; differs from list r();?
What is a local reference?
Explain the difference between struct and class in terms of access modifier.
How a modifier is similar to mutator?
What is the object serialization?
What is function declaration in c++ with example?