A
aki
Hi All ,
I am learning some basic concepts .
i have written a program for stack implementation .
// Progarm start
#include<iostream>
using namespace std;
#include<string.h>
#include <curses.h>
#define SIZE 5
class stack
{
int a[SIZE];
int tos;
public:
stack();
~stack();
void push(int);
int pop();
int isfull();
int isempty();
};
stack::stack()
{
for(int i=0;i<SIZE;i++)
{
a=NULL;
}
tos=0;
}
stack::~stack()
{
}
int stack::isempty()
{
return (tos==0?:1,0);
}
int stack::isfull()
{
return(tos==SIZE?:1,0);
}
void stack:ush(int n)
{
if(!isfull())
{
a[tos]=n;
tos++;
}
else
cout<<"stack overflow ..sorry"<<endl;
}
int stack:op()
{
if(!isempty())
{
return( a[--tos]);
}
else
cout <<"stack underflow...sorry"<<endl;
return 0;
}
int main()
{
stack s;
int ch=1,num;
while(ch!=0)
{
cout<<"Stack Operations Mani Menu"<<endl<<
"1.Push"<<endl<<
"2.Pop"<<endl<<
"3.IsEmpty"<<endl<<
"4.IsFull"<<endl<<
"0.Exit"<<endl;
cin>>ch;
switch(ch)
{
case 0:
exit(1); //Normal Termination of Program
case 1:
cout<<"Enter the number to push";
cin>>num;
s.push(num);
break;
case 2:
cout<<"Number popped from the stack is: "<<s.pop()<<endl;
break;
case 3:
(s.isempty())?(cout<<"Stack is empty")cout<<"Stack is not
empty.");
break;
case 4:
(s.isfull())?(cout<<"Stack is full.")cout<<"Stack is not full.");
break;
default:
cout<<"Illegal Option.Please try again"<<endl;
}
}//end of while
//getch();
cin>>ch;
return 0;
}
//Program end
Compilation is ok .
When i am running the program with option 2 (pop ) operation .
As initially no element is in stack it should throw error according
to program
but i am getting return value 2 ..
this i do not understand .
I guess memory handling , i am not doing properly ..
Can somebody look into this
Thanks and regards
Aki
// output start
Stack Operations Mani Menu
1.Push
2.Pop
3.IsEmpty
4.IsFull
0.Exit
2
Number popped from the stack is: 2
Stack Operations Mani Menu
1.Push
2.Pop
3.IsEmpty
4.IsFull
0.Exit
// output end
I am learning some basic concepts .
i have written a program for stack implementation .
// Progarm start
#include<iostream>
using namespace std;
#include<string.h>
#include <curses.h>
#define SIZE 5
class stack
{
int a[SIZE];
int tos;
public:
stack();
~stack();
void push(int);
int pop();
int isfull();
int isempty();
};
stack::stack()
{
for(int i=0;i<SIZE;i++)
{
a=NULL;
}
tos=0;
}
stack::~stack()
{
}
int stack::isempty()
{
return (tos==0?:1,0);
}
int stack::isfull()
{
return(tos==SIZE?:1,0);
}
void stack:ush(int n)
{
if(!isfull())
{
a[tos]=n;
tos++;
}
else
cout<<"stack overflow ..sorry"<<endl;
}
int stack:op()
{
if(!isempty())
{
return( a[--tos]);
}
else
cout <<"stack underflow...sorry"<<endl;
return 0;
}
int main()
{
stack s;
int ch=1,num;
while(ch!=0)
{
cout<<"Stack Operations Mani Menu"<<endl<<
"1.Push"<<endl<<
"2.Pop"<<endl<<
"3.IsEmpty"<<endl<<
"4.IsFull"<<endl<<
"0.Exit"<<endl;
cin>>ch;
switch(ch)
{
case 0:
exit(1); //Normal Termination of Program
case 1:
cout<<"Enter the number to push";
cin>>num;
s.push(num);
break;
case 2:
cout<<"Number popped from the stack is: "<<s.pop()<<endl;
break;
case 3:
(s.isempty())?(cout<<"Stack is empty")cout<<"Stack is not
empty.");
break;
case 4:
(s.isfull())?(cout<<"Stack is full.")cout<<"Stack is not full.");
break;
default:
cout<<"Illegal Option.Please try again"<<endl;
}
}//end of while
//getch();
cin>>ch;
return 0;
}
//Program end
Compilation is ok .
When i am running the program with option 2 (pop ) operation .
As initially no element is in stack it should throw error according
to program
but i am getting return value 2 ..
this i do not understand .
I guess memory handling , i am not doing properly ..
Can somebody look into this
Thanks and regards
Aki
// output start
Stack Operations Mani Menu
1.Push
2.Pop
3.IsEmpty
4.IsFull
0.Exit
2
Number popped from the stack is: 2
Stack Operations Mani Menu
1.Push
2.Pop
3.IsEmpty
4.IsFull
0.Exit
// output end