ALLInterview.com :: Home Page            
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
Google
   
 
Categories >> Software >> Programming Languages >> C++ >> C++ General
 
 


 

 
 STL interview questions  STL Interview Questions (90)
 OOPS interview questions  OOPS Interview Questions (554)
 C++ General interview questions  C++ General Interview Questions (458)
Question
write a program  in c++ to implement stack using  functions 
in header file stack.h
 Question Submitted By :: Guest
I also faced this Question!!     Rank Answer Posted By  
 
  Re: write a program in c++ to implement stack using functions in header file stack.h
Answer
# 1
#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 ?    29 Yes 26 No
Sv
 
  Re: write a program in c++ to implement stack using functions in header file stack.h
Answer
# 2
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 ?    28 Yes 30 No
Sachindra Bagchi
 
 
 
  Re: write a program in c++ to implement stack using functions in header file stack.h
Answer
# 3
# 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 ?    14 Yes 17 No
Jhil
 

 
 
 
Other C++ General Interview Questions
 
  Question Asked @ Answers
 
What and all can a compiler provides by default? HP 3
catch(exception &e) { . . . } Referring to the sample code above, which one of the following lines of code produces a written description of the type of exception that "e" refers to? a) cout << e.type(); b) cout << e.name(); c) cout << typeid(e).name(); d) cout << e.what(); e) cout << e; Quark 2
Implement a 2 dimensional array by one dimentional array CTS 1
In C++ cout is: a) object b) class c) something else Lehman-Brothers 11
Can we have "Virtual Constructors"? TCS 9
Write the program for fibonacci in c++? Wipro 18
Write a function which takes a character array as input and reverses it in place. Lehman-Brothers 2
Given the following function definition: int doit(int &x, int y, int &z) { x = 3*x; y = y + 5; z = x+y; return z - 4; } int a = 5, b = 7, c = 9, d = 11; d = doit(a,b,c);   2
Do we have private destructors? Symphony 12
Explain the ISA and HASA class relationships. How would you implement each in a class design?   2
What is size of a object of following class? class Foo { public: void foo(){} } CA 2
what is data Abstraction? and give example TCS 142
 
For more C++ General Interview Questions Click Here 
 
 
 
 
 


   
Copyright Policy  |  Terms of Service  |  Articles  |  Site Map  |  RSS Site Map  |  Contact Us
   
Copyright 2013  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  KalAajKal.com