Pages

E-mail us at prgms4starters@gmail.com for any queries or to share your programs. Please mention the program definition in the subject line.

In case you need help in finishing a program, do mention "incomplete pgm" in the subject line. Our team of experts will do their best to help you.

Click the link to follow this page for updates and reminders: FOLLOW THIS SITE

You can also visit our Facebook page by clicking HERE

**don't forget to participate in the polls at the bottom of the page

Friday, November 5, 2010

stack implementation program in c++

#include<iostream.h>
#include<conio.h>
#include<process.h>

int stack[50],top=-1,item,n,i,size;

class stack1
{
public:
void push()
{

if(top==size-1)
{
cout<<"\n no more items can be pushed\n";
}
else
{
cout<<"enter the item to be pushed\n";
top++;
cin>>item;
stack[top]=item;
}
}


void pop()
{
if(top==-1)

cout<<"\nstack underflow\n";
else
{
cout<<"the item deleted is \t"<<stack[top];
top--;
}}

void stacktop()
{
if(top==-1)
cout<<"\nstack underflow\n";
else
cout<<"the top of the stack is "<<stack[top];
}

void clearstack()
{

if(top==-1)
cout<<"\n0 items in stack\n";
else
{
for(i=top;i>=0;i--)
stack[i]='\0';
top=-1;
}
}



void display()
{
if(top==-1)
cout<<"\n 0 items in stack\n";
else
{
for(i=top;i>=0;i--)
cout<<stack[i]<<"\n";
}
}
};

void main()
{
clrscr();
int a,b=1;
stack1 s;
cout<<"enter the size of the stack\t";
cin>>size;
while(b==1)
{
clrscr();
cout<<"\nMAIN MENU\n";
cout<<"\n1.push\n"<<"2.pop\n"<<"3.stack top\n"<<"4.clearstack\n"<<"5.display\n"<<"6.exit\n";
cout<<"\nenter your choice\t";
cin>>a;
switch(a)
{
case 1:s.push();
break;

case 2:s.pop();
break;

case 3:s.stacktop();
break;

case 4: s.clearstack();
cout<<"\nthe stack has been cleared\n";
break;

case 5: s.display();
break;
default:
cout<<"\nwrong choice\n";

case 6: exit(0);
break;


}
cout<<"\npress 1 to go to main menu or 0 to exit\n";
cin>>b;
}
}

No comments:

Post a Comment

We have noticed that 20% of our visitors are still using internet explorer. For a better and faster surfing experience we recommend that you switch to either Google Chrome or Mozilla Firefox. We cannot provide you the downloads but here are the links to sites where you can download them from

Google Chrome - click here to go to site

Mozilla Firefox - click here to go to site

Where are you from

Do you think this site was helpful

Should we put up more C or C++ programs