Skip to main content

Featured

functions in Python

Array as a stack and as a queue program in C++

/* Following program implements array as a stack and as a queue with push and pop operations.*/  
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void arrstack()
{
clrscr();
int top=-1;
int size=5;
int arr[5];
int ch; char ans;
do{
cout<<"1.Push\n";
cout<<"2.Pop\n";
cout<<"3.Back to Main Menu\n";
cin>>ch;
 if(ch==1)
 {   int item;
 cout<<"Enter the item to be inserted\n";
 cin>>item;
  if(top==size-1)
  {
  cout<<"overflow";
  return;
  }
  else
  arr[++top]=item;
 cout<<"\n Array Status\n";
 for(int i=top;i>=0;i--)
 cout<<arr[i]<<"\n";
 }
 if(ch==2)
 {
 if(top==-1)
 {
  cout<<"underflow";
  return;
 }
 else
 {
  cout<<"Element deleted is"<<arr[top];
  top--;
 }
 cout<<"\n Array Status\n";
 for(int i=top;i>=0;i--)
 cout<<arr[i]<<"\n";
 }
 if(ch==3)
 {
  return;
 }
cout<<"\nWanna see array stack menu\n";
cin>>ans;
}while(ans=='y'||ans=='Y');

}//end arrstack

void arrqueue()
{
clrscr();
int front=-1;
int rear=-1;
int size=5;
int arr[5];
int ch; char ans;
do{
cout<<"1.Push\n";
cout<<"2.Pop\n";
cout<<"3.Back to Main Menu\n";
cin>>ch;
 if(ch==1)
 {   int item;
 cout<<"Enter the item to be inserted\n";
 cin>>item;

  if(rear==size-1)
  {
  cout<<"overflow";
  return;
  }
  else if(front==-1&&rear==-1)
  {
  front++;
  }

  arr[++rear]=item;

 cout<<"\n Array Status\n";
 for(int i=front;i<=rear;i++)
 cout<<arr[i]<<"\n";
 }
 if(ch==2)
 {
 if(front==-1&&rear==-1)
 {
  cout<<"underflow";
  return;
 }
if(front==rear)
{
  cout<<"Element deleted is"<<arr[front];
  front=rear=-1; 
}
else
{
  cout<<"Element deleted is"<<arr[front];
  front++;
}
 cout<<"\n Array Status\n";
 for(int i=front;i<=rear;i++)
 cout<<arr[i]<<"\n";
 }
 if(ch==3)
 {
  return;
 }
cout<<"\nWanna see array stack menu\n";
cin>>ans;
}while(ans=='y'||ans=='Y');


}//end arrqueue

void main()
{
clrscr();
int ch;
char ans;
do
{
cout<<"\n1. Array as Stack\n";
cout<<"\n2. Array as Queue\n";
cout<<"\n3.Exit Program\n";
cout<<"\nEnter your choice\n";
cin>>ch;
if(ch==1)
{
arrstack();
}
else if(ch==2)
{
arrqueue();
}
else if(ch==3)
{
exit(0);
}
cout<<"Wanna See menu again y/n";
cin>>ans;
}while(ans=='y'||ans=='Y');
     getch();
}

OUTPUT:
Array as stack and queue program in C++




Comments

Popular Posts