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

Write a program to interchange 2 variables without using the third one.

797


Differentiate between C and C++.

890


What is a character in c++?

766


total amount of milk produced each morning and then calculates and outputs the number of cartons needed for this milk , the cost of producing the milk and the profit from producing this milk.

2287


Explain what data encapsulation is in c++?

771


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

862


When does a 'this' pointer get created?

819


What is the best c c++ compiler for windows?

768


Can we declare a base-class destructor as virtual?

753


What is scope operator in c++?

804


Can we make any program in c++ without using any header file and what is the shortest program in c++.

823


What are register variables?

831


What is the role of C++ shorthand's?

970


What is the difference between c++ and turbo c++?

783


Are strings mutable in c++?

888