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

What is the difference between a baller and a reference in C++?

578


How the keyword struct is different from the keyword class in c++?

585


Snake Game: This is normal snake game which you can find in most of the mobiles. You can develop it in Java, C/C++, C# or what ever language you know.

2440


Show the declaration for a static function pointer.

580


In what situations do you have to use initialization list rather than assignment in constructors?

634






Explain what are the sizes and ranges of the basic c++ data types?

640


What is c++ manipulator?

555


What is a class template in c++?

525


What is a stack c++?

575


Explain Memory Allocation in C/C++ ?

639


What is exception handling? Does c++ support exception handling?

589


Does c++ have a hash table?

545


How is data hiding achieved in c++?

572


Explain what is polymorphism in c++?

628


What is pure virtual function? Or what is abstract class?

592