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

Answers were Sorted based on User's Feedback



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

Answer / sv

#include <iostream>
#include <stack>

using namespace std;

int main()
{
string title;
int howmany;


stack<string> discs;
//Asking the user how many discs he wants to enter in the
stack.
//The loop will rotate that many number of times and then
//prompt the user for input.
cout<<"How many discs :";
cin>>howmany;

for(int i=0;i>title;
//pushing the discs one upon the other
discs.push(title);
}

cout<<"Now at the top of the CD Stack we have :"<<discs.top
()<<endl;
cout<<"The first one entered is "<<endl;
while(!discs.empty())
{
title = discs.top();
discs.pop();
}
cout<<title<<endl;
return 0;
}

Is This Answer Correct ?    31 Yes 29 No

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

Answer / 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

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

Answer / 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

More C++ General Interview Questions

Define anonymous class.

0 Answers  


Can a Structure contain a Pointer to itself?

0 Answers  


What are the basic data types used in c++?

0 Answers  


What is heap sort in c++?

0 Answers  


What does override mean in c++?

0 Answers  






What are the differences between new and malloc?

0 Answers  


Explain the differences between list x; & list x();.

0 Answers  


What is nested class in c++?

0 Answers  


Write a program using display() function which takes two arguments.

0 Answers  


What is a multimap c++?

0 Answers  


Is there something that we can do in C and not in C++?

14 Answers   Patni,


Define basic type of variable used for a different condition in C++?

0 Answers  


Categories