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


Please Help Members By Posting Answers For Below Questions

Can manipulators fall in love?

559


What are the new features that iso/ansi c++ has added to original c++ specifications?

583


What is difference between rand () and srand ()?

588


Is java easier than c++?

586


How to declare a function pointer?

575






What is a type library?

684


Eplain extern keyword?

558


Explain the static member function.

701


What is purpose of abstract class?

582


What does override mean in c++?

578


Can you Mention some Application of C/C++?

621


What does new do in c++?

631


Is it possible to use a new for the reallocation of pointers ?

595


Differentiate between a constructor and a destructor in c++.

558


Difference between a copy constructor and an assignment operator.

565