allcollection q28
Post on 12-Aug-2015
19 Views
Preview:
TRANSCRIPT
/* Wap in c++ using pointers to find the smallest and the largest element in a dynamically created array.*/
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int *array, smallest, largest, n;
cout<<"\n\nEnter the number of elements : ";
cin>>n;
array=new[n];
cout<<"\n\nEnter the elements : \n\n";
for(int i=0; i<n; i++)
cin>>array[i];
i=0;
cout<<"\n\nThe array formed is : \n\n";
while(i!=n)
{
cout<<array[i]<<" ";
i++;
}
smallest=array[0];
for(i=0; i<n; i++)
{
if(array[i]<=smallest)
smallest=array[i];
}
largest=array[0];
for(i=0; i<n; i++)
{
if(array[i]>=largest)
largest=array[i];
}
cout<<"\n\nThe smallest element is : "<<smallest<<"\n\nThe largest element is : "<<largest;
getch();
}
Output :
Enter the number of elements : 5
Enter the elements :
1
9
2
8
6
The array formed is :
1 9 2 8 6
The smallest element is : 1
The largest element is : 9
/* Wap in c++ to implement Queue as a linked list */#include<iostream.h>#include<conio.h>
struct node { int roll;
node* next; }*front,*rear,*ptr,*newptr,*np;
node *create(int a){ ptr=new node; ptr->roll=a; ptr->next=NULL; return ptr; }
void insert(node *np){ if(front==NULL) front=rear=np; else
{ rear->next=np;
rear=np; }
}
void delet(){ if(front==NULL) cout<<"\n Underflow!!!!"; else {
ptr=front; front=front->next; delete ptr;
}}
void display(node *np){ while(np!=NULL) {
cout<<np->roll<<"-> "; np=np->next;
}}
void main(){ clrscr(); front=rear=NULL; int n,m; char ans,ch; do { cout<<"\nChoose from the menu : "
<<"\n 1) Insert."<<"\n 2) Delete<<"\n 3) Display."<<"\n\n Enter your choice : ";
cin>>n; switch(n) { case 1: ans='y';
while(ans=='y'||ans=='Y') {
cout<<"\n Enter element to be inserted .";cin>>m;
newptr=create(m);
if(newptr==NULL)cout<<"\n Cannot create !!!!";
insert(newptr);
cout<<"\n The Queue formed is : ";
display(front);
cout<<"\n Want to enter more nodes ?: ";cin>>ans;
} break;
case 2: ans='y'; while(ans=='y'||ans=='Y') {
delet();cout<<"\n Queue : ";display(front);cout<<"\n Want to delete more ?: ";cin>>ans;
} break;
case 3: cout<<"\n Queue : "; display(front); break;
default: cout<<"\n You entered wrong choice..."; }
cout<<"\n Want to return to main menu ? : "; cin>>ch;
}while(ch=='y'||ch=='Y'); getch();}
OUTPUT
Choose from the menu : 1. Insert 2. Delete
3. Display
Enter your choice : 1
Enter element to be inserted : 1
The Queue formed is :
1-->
Want to enter more nodes ?: y
Enter element to be inserted .2
The Queue formed is :
1-> 2->
Want to enter more nodes ?: y
Enter element to be inserted .3
The Queue formed is :
1-> 2-> 3->
Want to enter more nodes ?: n
Want to return to main menu ? : y
Choose from the menu : 1. Insert 2. Delete 3. Display
Enter your choice : 2
Queue :
2-> 3->
Want to delete more ?: n
Want to return to main menu ? : y
Choose from the menu : n 1. Insert 2. Delete 3. Display
Enter your choice : 3
Queue :
2-> 3->
Want to return to main menu ? : n
/* Wap to implement stack as a linked list */
#include<iostream.h>
#include<conio.h>
#include<process.h>
struct node {
int roll;
node* next;
}*top,*save,*ptr,*newptr,*np;
node *create(int a)
{
ptr=new node;
ptr->roll=a;
ptr->next=NULL;
return ptr;
}
void push(node *np)
{
if(top==NULL)
top=np;
else
{
save=top;
top=np;
np->next=save;
}
}
void pop()
{
if(top==NULL)
cout<<"\n Underflow!!!!";
else
{
ptr=top;
top=top->next;
delete ptr;
}
}
void display(node *np)
{
while(np!=NULL)
{
cout<<np->roll<<" -> ";
np=np->next;
}
}
void main()
{
clrscr();
top=NULL;
int n,m;
char k,ch;
do {
cout<<"\nChoose from the menu :\n"
<<"\n 1. Push."
<<"\n 2. Pop."
<<"\n 3. Display."
<<"\n 4. Quit."
<<"\n\nEnter your choice : ";
cin>>n;
switch(n)
{
case 1: k='y';
while(k=='y'||k=='Y')
{
cout<<"\n Enter element to be inserted .";
cin>>m;
newptr=create(m);
if(newptr==NULL)
cout<<"\n Cannot create !!!!";
push(newptr);
cout<<"\n The Stack formed is : ";
display(top);
cout<<"\n\n Want to enter again ?: ";
cin>>k;
}
break;
case 2: k='y';
while(k=='y'||k=='Y')
{
pop();
cout<<"\n The Stack formed is : \n\n";
display(top);
cout<<"\n\n Want to delete again ?: ";
cin>>k;
}
break;
case 3: cout<<"\n The Stack formed is : ";
display(top);
break;
case 4: exit(0);
break;
default: cout<<"\n Please enter desired keyword : ";
}
cout<<"\n Do you want to continue..? : ";
cin>>ch;
}while(ch=='y'||ch=='Y');
getch();
}
Output:
Choose from the menu :
1. Push.
2. Pop.
3. Display.
4. Quit.
Enter your choice : 1
Enter element to be inserted : 1
The Stack formed is :
1 ->
Want to enter again ?: y
Enter element to be inserted : 2
The Stack formed is :
2 -> 1 ->
Want to enter again ?: y
Enter element to be inserted : 3
The Stack formed is :
3 -> 2 -> 1 ->
Want to enter again ?: n
Do you want to continue..? : y
Choose from the menu :
1. Push.
2. Pop.
3. Display.
4. Quit.
Enter your choice : 2
The Stack formed is :
2 -> 1 ->
Want to delete again ?: y
The Stack formed is :
1 ->
Want to delete again ?: N
Do you want to continue..? : y
Choose from the menu :
1. Push.
2. Pop.
3. Display.
4. Quit.
Enter your choice : 3
The Stack formed is :
1 ->
Do you want to continue..? : y
Choose from the menu :
1. Push.
2. Pop.
3. Display.
4. Quit.
Enter your choice : 4
/* Wap in C++ to implement queue as an array */
#include<iostream.h>
#include<conio.h>
#include<process.h>
const int size=50;
int q[size],rear=-1,front=-1;
int insert(int q[],int ele)
{
if(rear==size-1)
return(-1);
else if(rear==-1)
{
front=rear=0;
q[rear]=ele;
}
else
{
rear++;
q[rear]=ele;
}
return(0);
}
int delet(int q[])
{
int r ;
if(front==-1)
return(-1);
else
{
r=q[front];
if(rear==front)
front=rear=-1;
else
front++;
}
return r;
}
void display(int q[],int front,int rear)
{
if(front==-1)
return;
else
{
for(int i=front;i<=rear;i++)
cout<<q[i]<<" ";
}
}
void main()
{
clrscr();
int n,u,k,m;
char ch,ans;
do
{
cout<<"\nChoose from the menu :\n"
<<"\n 1. Insert"
<<"\n 2. Delete"
<<"\n 3. Display"
<<"\n\n Enter your choice : "; cin>>n;
switch(n)
{
case 1: ans='y';
while(ans=='y'||ans=='Y')
{
cout<<"\n Enter element to be inserted :"; cin>>m;
k=insert(q,m);
if(k==-1)
cout<<"\n Overflow !!!!";
cout<<"\n The resultant Queue is : ";
display(q,front,rear);
cout<<"\n\n Want to enter again ?: ";
cin>>ans;
}
break;
case 2: ans='y';
while(ans=='y'||ans=='Y')
{
u=delet(q);
if(u==-1)
{
cout<<"\n Underflow !!!!";
break;
}
else
{
cout<<"\n The deleted element is "<<u<<"\n";
cout<<"\n The resultant Queue is : \n\n";
display(q,front,rear);
}
cout<<"\n\n Want to delete again ?: ";
cin>>ans;
}
break;
case 3: cout<<"\n The Queue is : \n\n";
display(q,front,rear);
break;
default: cout<<"\n Please enter desired keyword : ";
}
cout<<"\n Choose from the menu again ? : ";cin>>ch;
}while(ch=='y'||ch=='Y');
getch();
}
Output:
Choose from the menu :
1. Insert
2. Delete
3. Display
Enter your choice : 1
Enter element to be inserted :1
The resultant Queue is :
1-->
Want to enter again ?: y
Enter element to be inserted :2
The resultant Queue is :
1-->2-->
Want to enter again ?: y
Enter element to be inserted :3
The resultant Queue is :
1-->2-->3-->
Want to enter again ?: n
Choose from the menu again ? : y
Choose from the menu :
1. Insert
2. Delete
3. Display
Enter your choice : 2
The deleted element is 1
The resultant Queue is :
2-->3-->
Want to delete again ?: y
The deleted element is 2
The resultant Queue is :
3-->
Want to delete again ?: n
Choose from the menu again ? : y
Choose from the menu :
1. Insert
2. Delete
3. Display
Enter your choice : 3
The Queue is :
3-->
Choose from the menu again ? :N
/* Wap in c++ to implement stack as an array */
#include<iostream.h>
#include<conio.h>
#include<process.h>
int pop(int[],int&);
int push(int[],int&,int);
void display(int[],int);
const int size=50;
void main()
{
clrscr();
char m,ch;
int k,stack[size],item,top=-1,res;
do
{ cout<<"\nChoose from the following : \n\n"
<<"\n 1. Push"
<<"\n 2. Pop"
<<"\n 3. Display"
<<"\n 4. Exit"
<<"\n\nEnter your choice : "; cin>>k;
switch(k)
{
case 1: ch='y';
while(ch=='y'||ch=='Y')
{ cout<<"\nEnter the element : ";
cin>>item;
res=push(stack,top,item);
if(res==-1)
{cout<<"\nOverflow !!!!";
exit(1); }
cout<<"\nThe stack formed is : \n\n";
display(stack,top);
cout<<"\n\n\nWant to enter again ?: ";
cin>>ch;
}
break;
case 2: ch='y';
while(ch=='y'||ch=='Y')
{ res=pop(stack,top);
if(res==-1)
{
cout<<"\nUnderflow !!!!";
exit(1);
}
else
{
cout<<"\nThe deleted Element is : "<<res<<endl;
cout<<"\nThe resultant stack is : \n\n";
display(stack,top); }
cout<<"\nWant to delete again ? : ";
cin>>ch;
}
break;
case 3: cout<<"\nThe resultant stack is : ";
display(stack,top);
break;
case 4: exit(0);
break;
default: cout<<"\nPlease enter desired keyword : ";
} // end of switch
cout<<"\n\nChoose from the menu again ? : ";
cin>>m;
}while(m=='y'||m=='Y'); // end of do-while loop
getch();
} // end of main()
int push(int stack[],int &top,int el)
{
if(top==size-1)
return -1;
else
{
top++;
stack[top]=el;
return 0;
}
}
int pop(int stack[],int &top)
{
int ret;
if(top==-1)
return -1;
else
{
ret=stack[top];
top--;
return ret;
}
}
void display(int stack[],int top)
{
cout<<stack[top]<<"<--";
for(int i=top-1;i>=0;i--)
cout<<stack[i]<<"<--";
}
Enter your choice : 1
Enter the element : 1
The stack formed is :
1
Want to enter again ?: y
Enter the element : 2
The stack formed is :
2 1
Want to enter again ?: y
Enter the element : 3
The stack formed is :
3 2 1
Want to enter again ?: y
Enter the element : 4
The stack formed is :
4 3 2 1
Want to enter again ?: N
Choose from the menu again ? : y
Choose from the following :
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 2
The deleted Element is : 4
The resultant stack is :
3 2 1
Want to delete again ? : y
The deleted Element is : 3
The resultant stack is :
2 1
Want to delete again ? : n
Choose from the menu again ? : y
Choose from the following :
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 3
The resultant stack is :
2 1
Choose from the menu again ? : n
/* Wap in c++ to implement stack as an array */
#include<iostream.h>
#include<conio.h>
#include<process.h>
int pop(int[],int&);
int push(int[],int&,int);
void display(int[],int);
const int size=50;
void main()
{
clrscr();
char m,ch;
int k,stack[size],item,top=-1,res;
do
{ cout<<"\nChoose from the following : \n\n"
<<"\n 1. Push"
<<"\n 2. Pop"
<<"\n 3. Display"
<<"\n 4. Exit"
<<"\n\nEnter your choice : "; cin>>k;
switch(k)
{
case 1: ch='y';
while(ch=='y'||ch=='Y')
{ cout<<"\nEnter the element : ";
cin>>item;
res=push(stack,top,item);
if(res==-1)
{cout<<"\nOverflow !!!!";
exit(1); }
cout<<"\nThe stack formed is : \n\n";
display(stack,top);
cout<<"\n\n\nWant to enter again ?: ";
cin>>ch;
}
break;
case 2: ch='y';
while(ch=='y'||ch=='Y')
{ res=pop(stack,top);
if(res==-1)
{
cout<<"\nUnderflow !!!!";
exit(1);
}
else
{
cout<<"\nThe deleted Element is : "<<res<<endl;
cout<<"\nThe resultant stack is : \n\n";
display(stack,top); }
cout<<"\nWant to delete again ? : ";
cin>>ch;
}
break;
case 3: cout<<"\nThe resultant stack is : ";
display(stack,top);
break;
case 4: exit(0);
break;
default: cout<<"\nPlease enter desired keyword : ";
} // end of switch
cout<<"\n\nChoose from the menu again ? : ";
cin>>m;
}while(m=='y'||m=='Y'); // end of do-while loop
getch();
} // end of main()
int push(int stack[],int &top,int el)
{
if(top==size-1)
return -1;
else
{
top++;
stack[top]=el;
return 0;
}
}
int pop(int stack[],int &top)
{
int ret;
if(top==-1)
return -1;
else
{
ret=stack[top];
top--;
return ret;
}
}
void display(int stack[],int top)
{
cout<<stack[top]<<"<--";
for(int i=top-1;i>=0;i--)
cout<<stack[i]<<"<--";
}
Output:
Choose from the following :
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 1
Enter the element : 1
The stack formed is :
1
Want to enter again ?: y
Enter the element : 2
The stack formed is :
2 1
Want to enter again ?: y
Enter the element : 3
The stack formed is :
3 2 1
Want to enter again ?: y
Enter the element : 4
The stack formed is :
4 3 2 1
Want to enter again ?: N
Choose from the menu again ? : y
Choose from the following :
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 2
The deleted Element is : 4
The resultant stack is :
3 2 1
Want to delete again ? : y
The deleted Element is : 3
The resultant stack is :
2 1
Want to delete again ? : n
Choose from the menu again ? : y
Choose from the following :
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 3
The resultant stack is :
2 1
Choose from the menu again ? : n
/* Wap in c++ to implement stack as an array */
#include<iostream.h>
#include<conio.h>
#include<process.h>
int pop(int[],int&);
int push(int[],int&,int);
void display(int[],int);
const int size=50;
void main()
{
clrscr();
char m,ch;
int k,stack[size],item,top=-1,res;
do
{ cout<<"\nChoose from the following : \n\n"
<<"\n 1. Push"
<<"\n 2. Pop"
<<"\n 3. Display"
<<"\n 4. Exit"
<<"\n\nEnter your choice : "; cin>>k;
switch(k)
{
case 1: ch='y';
while(ch=='y'||ch=='Y')
{ cout<<"\nEnter the element : ";
cin>>item;
res=push(stack,top,item);
if(res==-1)
{cout<<"\nOverflow !!!!";
exit(1); }
cout<<"\nThe stack formed is : \n\n";
display(stack,top);
cout<<"\n\n\nWant to enter again ?: ";
cin>>ch;
}
break;
case 2: ch='y';
while(ch=='y'||ch=='Y')
{ res=pop(stack,top);
if(res==-1)
{
cout<<"\nUnderflow !!!!";
exit(1);
}
else
{
cout<<"\nThe deleted Element is : "<<res<<endl;
cout<<"\nThe resultant stack is : \n\n";
display(stack,top); }
cout<<"\nWant to delete again ? : ";
cin>>ch;
}
break;
case 3: cout<<"\nThe resultant stack is : ";
display(stack,top);
break;
case 4: exit(0);
break;
default: cout<<"\nPlease enter desired keyword : ";
} // end of switch
cout<<"\n\nChoose from the menu again ? : ";
cin>>m;
}while(m=='y'||m=='Y'); // end of do-while loop
getch();
} // end of main()
int push(int stack[],int &top,int el)
{
if(top==size-1)
return -1;
else
{
top++;
stack[top]=el;
return 0;
}
}
int pop(int stack[],int &top)
{
int ret;
if(top==-1)
return -1;
else
{
ret=stack[top];
top--;
return ret;
}
}
void display(int stack[],int top)
{
Choose from the following :
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 1
Enter the element : 1
The stack formed is :
1
Want to enter again ?: y
Enter the element : 2
The stack formed is :
2 1
Want to enter again ?: y
Enter the element : 3
The stack formed is :
3 2 1
Want to enter again ?: y
Enter the element : 4
The stack formed is :
4 3 2 1
Want to enter again ?: N
Choose from the menu again ? : y
Choose from the following :
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 2
The deleted Element is : 4
The resultant stack is :
3 2 1
Want to delete again ? : y
The deleted Element is : 3
The resultant stack is :
2 1
Want to delete again ? : n
Choose from the menu again ? : y
Choose from the following :
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 3
The resultant stack is :
2 1
Choose from the menu again ? : n
/* Wap in c++ to implement stack as an array */
#include<iostream.h>
#include<conio.h>
#include<process.h>
int pop(int[],int&);
int push(int[],int&,int);
void display(int[],int);
const int size=50;
void main()
{
clrscr();
char m,ch;
int k,stack[size],item,top=-1,res;
do
{ cout<<"\nChoose from the following : \n\n"
<<"\n 1. Push"
<<"\n 2. Pop"
<<"\n 3. Display"
<<"\n 4. Exit"
<<"\n\nEnter your choice : "; cin>>k;
switch(k)
{
case 1: ch='y';
while(ch=='y'||ch=='Y')
{ cout<<"\nEnter the element : ";
cin>>item;
res=push(stack,top,item);
if(res==-1)
{cout<<"\nOverflow !!!!";
exit(1); }
cout<<"\nThe stack formed is : \n\n";
display(stack,top);
cout<<"\n\n\nWant to enter again ?: ";
cin>>ch;
}
break;
case 2: ch='y';
while(ch=='y'||ch=='Y')
{ res=pop(stack,top);
if(res==-1)
{
cout<<"\nUnderflow !!!!";
exit(1);
}
else
{
cout<<"\nThe deleted Element is : "<<res<<endl;
cout<<"\nThe resultant stack is : \n\n";
display(stack,top); }
cout<<"\nWant to delete again ? : ";
cin>>ch;
}
break;
case 3: cout<<"\nThe resultant stack is : ";
display(stack,top);
break;
case 4: exit(0);
break;
default: cout<<"\nPlease enter desired keyword : ";
} // end of switch
cout<<"\n\nChoose from the menu again ? : ";
cin>>m;
}while(m=='y'||m=='Y'); // end of do-while loop
getch();
} // end of main()
int push(int stack[],int &top,int el)
{
if(top==size-1)
return -1;
else
{
top++;
stack[top]=el;
return 0;
}
}
int pop(int stack[],int &top)
{
int ret;
if(top==-1)
return -1;
else
{
ret=stack[top];
top--;
return ret;
}
}
void display(int stack[],int top)
{
cout<<stack[top]<<"<--";
for(int i=top-1;i>=0;i--)
cout<<stack[i]<<"<--";
}
Enter the element : 1
The stack formed is :
1
Want to enter again ?: y
Enter the element : 2
The stack formed is :
2 1
Want to enter again ?: y
Enter the element : 3
The stack formed is :
3 2 1
Want to enter again ?: y
Enter the element : 4
The stack formed is :
4 3 2 1
Want to enter again ?: N
Choose from the menu again ? : y
Choose from the following :
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 2
The deleted Element is : 4
The resultant stack is :
3 2 1
Want to delete again ? : y
The deleted Element is : 3
The resultant stack is :
2 1
Want to delete again ? : n
Choose from the menu again ? : y
Choose from the following :
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 3
The resultant stack is :
2 1
Choose from the menu again ? : n
/* Wap in c++ to implement stack as an array */
#include<iostream.h>
#include<conio.h>
#include<process.h>
int pop(int[],int&);
int push(int[],int&,int);
void display(int[],int);
const int size=50;
void main()
{
clrscr();
char m,ch;
int k,stack[size],item,top=-1,res;
do
{ cout<<"\nChoose from the following : \n\n"
<<"\n 1. Push"
<<"\n 2. Pop"
<<"\n 3. Display"
<<"\n 4. Exit"
<<"\n\nEnter your choice : "; cin>>k;
switch(k)
{
case 1: ch='y';
while(ch=='y'||ch=='Y')
{ cout<<"\nEnter the element : ";
cin>>item;
res=push(stack,top,item);
if(res==-1)
{cout<<"\nOverflow !!!!";
exit(1); }
cout<<"\nThe stack formed is : \n\n";
display(stack,top);
cout<<"\n\n\nWant to enter again ?: ";
cin>>ch;
}
break;
case 2: ch='y';
while(ch=='y'||ch=='Y')
{ res=pop(stack,top);
if(res==-1)
{
cout<<"\nUnderflow !!!!";
exit(1);
}
else
{
cout<<"\nThe deleted Element is : "<<res<<endl;
cout<<"\nThe resultant stack is : \n\n";
display(stack,top); }
cout<<"\nWant to delete again ? : ";
cin>>ch;
}
break;
case 3: cout<<"\nThe resultant stack is : ";
display(stack,top);
break;
case 4: exit(0);
break;
default: cout<<"\nPlease enter desired keyword : ";
} // end of switch
cout<<"\n\nChoose from the menu again ? : ";
cin>>m;
}while(m=='y'||m=='Y'); // end of do-while loop
getch();
} // end of main()
int push(int stack[],int &top,int el)
{
if(top==size-1)
return -1;
else
{
top++;
stack[top]=el;
return 0;
}
}
int pop(int stack[],int &top)
{
int ret;
if(top==-1)
return -1;
else
{
ret=stack[top];
top--;
return ret;
}
}
void display(int stack[],int top)
{
cout<<stack[top]<<"<--";
for(int i=top-1;i>=0;i--)
cout<<stack[i]<<"<--";
}
Output:
Choose from the following :
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 1
Enter the element : 1
The stack formed is :
1
Want to enter again ?: y
Enter the element : 2
The stack formed is :
2 1
Want to enter again ?: y
Enter the element : 3
The stack formed is :
3 2 1
Want to enter again ?: y
Enter the element : 4
The stack formed is :
4 3 2 1
Want to enter again ?: N
Choose from the menu again ? : y
Choose from the following :
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 2
The deleted Element is : 4
The resultant stack is :
3 2 1
Want to delete again ? : y
The deleted Element is : 3
The resultant stack is :
2 1
Want to delete again ? : n
Choose from the menu again ? : y
Choose from the following :
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 3
The resultant stack is :
2 1
Choose from the menu again ? : n
v/* Wap in c++ to implement stack as an array */
#include<iostream.h>
#include<conio.h>
#include<process.h>
int pop(int[],int&);
int push(int[],int&,int);
void display(int[],int);
const int size=50;
void main()
{
clrscr();
char m,ch;
int k,stack[size],item,top=-1,res;
do
{ cout<<"\nChoose from the following : \n\n"
<<"\n 1. Push"
<<"\n 2. Pop"
<<"\n 3. Display"
<<"\n 4. Exit"
<<"\n\nEnter your choice : "; cin>>k;
switch(k)
{
case 1: ch='y';
while(ch=='y'||ch=='Y')
{ cout<<"\nEnter the element : ";
cin>>item;
res=push(stack,top,item);
if(res==-1)
{cout<<"\nOverflow !!!!";
exit(1); }
cout<<"\nThe stack formed is : \n\n";
display(stack,top);
cout<<"\n\n\nWant to enter again ?: ";
cin>>ch;
}
break;
case 2: ch='y';
while(ch=='y'||ch=='Y')
{ res=pop(stack,top);
if(res==-1)
{
cout<<"\nUnderflow !!!!";
exit(1);
}
else
{
cout<<"\nThe deleted Element is : "<<res<<endl;
cout<<"\nThe resultant stack is : \n\n";
display(stack,top); }
cout<<"\nWant to delete again ? : ";
cin>>ch;
}
break;
case 3: cout<<"\nThe resultant stack is : ";
display(stack,top);
break;
case 4: exit(0);
break;
default: cout<<"\nPlease enter desired keyword : ";
} // end of switch
cout<<"\n\nChoose from the menu again ? : ";
cin>>m;
}while(m=='y'||m=='Y'); // end of do-while loop
getch();
} // end of main()
int push(int stack[],int &top,int el)
{
if(top==size-1)
return -1;
else
{
top++;
stack[top]=el;
return 0;
}
}
int pop(int stack[],int &top)
{
int ret;
if(top==-1)
return -1;
else
{
ret=stack[top];
top--;
return ret;
}
}
void display(int stack[],int top)
{
cout<<stack[top]<<"<--";
Choose from the following :
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 1
Enter the element : 1
The stack formed is :
1
Want to enter again ?: y
Enter the element : 2
The stack formed is :
2 1
Want to enter again ?: y
Enter the element : 3
The stack formed is :
3 2 1
Want to enter again ?: y
Enter the element : 4
The stack formed is :
4 3 2 1
Want to enter again ?: N
Choose from the menu again ? : y
Choose from the following :
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 2
The deleted Element is : 4
The resultant stack is :
3 2 1
Want to delete again ? : y
The deleted Element is : 3
The resultant stack is :
2 1
Want to delete again ? : n
Choose from the menu again ? : y
Choose from the following :
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 3
The resultant stack is :
2 1
Choose from the menu again ? : n
/* Wap in c++ to implement stack as an array */
#include<iostream.h>
#include<conio.h>
#include<process.h>
int pop(int[],int&);
int push(int[],int&,int);
void display(int[],int);
const int size=50;
void main()
{
clrscr();
char m,ch;
int k,stack[size],item,top=-1,res;
do
{ cout<<"\nChoose from the following : \n\n"
<<"\n 1. Push"
<<"\n 2. Pop"
<<"\n 3. Display"
<<"\n 4. Exit"
<<"\n\nEnter your choice : "; cin>>k;
switch(k)
{
case 1: ch='y';
while(ch=='y'||ch=='Y')
{ cout<<"\nEnter the element : ";
cin>>item;
res=push(stack,top,item);
if(res==-1)
{cout<<"\nOverflow !!!!";
exit(1); }
cout<<"\nThe stack formed is : \n\n";
display(stack,top);
cout<<"\n\n\nWant to enter again ?: ";
cin>>ch;
}
break;
case 2: ch='y';
while(ch=='y'||ch=='Y')
{ res=pop(stack,top);
if(res==-1)
{
cout<<"\nUnderflow !!!!";
exit(1);
}
else
{
cout<<"\nThe deleted Element is : "<<res<<endl;
cout<<"\nThe resultant stack is : \n\n";
display(stack,top); }
cout<<"\nWant to delete again ? : ";
cin>>ch;
}
break;
case 3: cout<<"\nThe resultant stack is : ";
display(stack,top);
break;
case 4: exit(0);
break;
default: cout<<"\nPlease enter desired keyword : ";
} // end of switch
cout<<"\n\nChoose from the menu again ? : ";
cin>>m;
}while(m=='y'||m=='Y'); // end of do-while loop
getch();
} // end of main()
int push(int stack[],int &top,int el)
{
if(top==size-1)
return -1;
else
{
top++;
stack[top]=el;
return 0;
}
}
int pop(int stack[],int &top)
{
int ret;
if(top==-1)
return -1;
else
{
ret=stack[top];
top--;
return ret;
}
}
void display(int stack[],int top)
{
cout<<stack[top]<<"<--";
for(int i=top-1;i>=0;i--)
cout<<stack[i]<<"<--";
}
Enter the element : 1
The stack formed is :
1
Want to enter again ?: y
Enter the element : 2
The stack formed is :
2 1
Want to enter again ?: y
Enter the element : 3
The stack formed is :
3 2 1
Want to enter again ?: y
Enter the element : 4
The stack formed is :
4 3 2 1
Want to enter again ?: N
Choose from the menu again ? : y
Choose from the following :
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 2
The deleted Element is : 4
The resultant stack is :
3 2 1
Want to delete again ? : y
The deleted Element is : 3
The resultant stack is :
2 1
Want to delete again ? : n
Choose from the menu again ? : y
Choose from the following :
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 3
The resultant stack is :
2 1
Choose from the menu again ? : n
/* Wap in c++ to implement stack as an array */
#include<iostream.h>
#include<conio.h>
#include<process.h>
int pop(int[],int&);
int push(int[],int&,int);
void display(int[],int);
const int size=50;
void main()
{
clrscr();
char m,ch;
int k,stack[size],item,top=-1,res;
do
{ cout<<"\nChoose from the following : \n\n"
<<"\n 1. Push"
<<"\n 2. Pop"
<<"\n 3. Display"
<<"\n 4. Exit"
<<"\n\nEnter your choice : "; cin>>k;
switch(k)
{
case 1: ch='y';
while(ch=='y'||ch=='Y')
{ cout<<"\nEnter the element : ";
cin>>item;
res=push(stack,top,item);
if(res==-1)
{cout<<"\nOverflow !!!!";
exit(1); }
cout<<"\nThe stack formed is : \n\n";
display(stack,top);
cout<<"\n\n\nWant to enter again ?: ";
cin>>ch;
}
break;
case 2: ch='y';
while(ch=='y'||ch=='Y')
{ res=pop(stack,top);
if(res==-1)
{
cout<<"\nUnderflow !!!!";
exit(1);
}
else
{
cout<<"\nThe deleted Element is : "<<res<<endl;
cout<<"\nThe resultant stack is : \n\n";
display(stack,top); }
cout<<"\nWant to delete again ? : ";
cin>>ch;
}
break;
case 3: cout<<"\nThe resultant stack is : ";
display(stack,top);
break;
case 4: exit(0);
break;
default: cout<<"\nPlease enter desired keyword : ";
} // end of switch
cout<<"\n\nChoose from the menu again ? : ";
cin>>m;
}while(m=='y'||m=='Y'); // end of do-while loop
getch();
} // end of main()
int push(int stack[],int &top,int el)
{
if(top==size-1)
return -1;
else
{
top++;
stack[top]=el;
return 0;
}
}
int pop(int stack[],int &top)
{
int ret;
if(top==-1)
return -1;
else
{
ret=stack[top];
top--;
return ret;
}
}
void display(int stack[],int top)
{
cout<<stack[top]<<"<--";
for(int i=top-1;i>=0;i--)
cout<<stack[i]<<"<--";
}
Output:
Choose from the following :
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 1
Enter the element : 1
The stack formed is :
1
Want to enter again ?: y
Enter the element : 2
The stack formed is :
2 1
Want to enter again ?: y
Enter the element : 3
The stack formed is :
3 2 1
Want to enter again ?: y
Enter the element : 4
The stack formed is :
4 3 2 1
Want to enter again ?: N
Choose from the menu again ? : y
Choose from the following :
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 2
The deleted Element is : 4
The resultant stack is :
3 2 1
Want to delete again ? : y
The deleted Element is : 3
The resultant stack is :
2 1
Want to delete again ? : n
Choose from the menu again ? : y
Choose from the following :
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 3
The resultant stack is :
2 1
Choose from the menu again ? : n
/* Wap in c++ to implement stack as an array */
#include<iostream.h>
#include<conio.h>
#include<process.h>
int pop(int[],int&);
int push(int[],int&,int);
void display(int[],int);
const int size=50;
void main()
{
clrscr();
char m,ch;
int k,stack[size],item,top=-1,res;
do
{ cout<<"\nChoose from the following : \n\n"
<<"\n 1. Push"
<<"\n 2. Pop"
<<"\n 3. Display"
<<"\n 4. Exit"
<<"\n\nEnter your choice : "; cin>>k;
switch(k)
{
case 1: ch='y';
while(ch=='y'||ch=='Y')
{ cout<<"\nEnter the element : ";
cin>>item;
res=push(stack,top,item);
if(res==-1)
{cout<<"\nOverflow !!!!";
exit(1); }
cout<<"\nThe stack formed is : \n\n";
display(stack,top);
cout<<"\n\n\nWant to enter again ?: ";
cin>>ch;
}
break;
case 2: ch='y';
while(ch=='y'||ch=='Y')
{ res=pop(stack,top);
if(res==-1)
{
cout<<"\nUnderflow !!!!";
exit(1);
}
else
{
cout<<"\nThe deleted Element is : "<<res<<endl;
cout<<"\nThe resultant stack is : \n\n";
display(stack,top); }
cout<<"\nWant to delete again ? : ";
cin>>ch;
}
break;
case 3: cout<<"\nThe resultant stack is : ";
display(stack,top);
break;
case 4: exit(0);
break;
default: cout<<"\nPlease enter desired keyword : ";
} // end of switch
cout<<"\n\nChoose from the menu again ? : ";
cin>>m;
}while(m=='y'||m=='Y'); // end of do-while loop
getch();
} // end of main()
int push(int stack[],int &top,int el)
{
if(top==size-1)
return -1;
else
{
top++;
stack[top]=el;
return 0;
}
}
int pop(int stack[],int &top)
{
int ret;
if(top==-1)
return -1;
else
{
ret=stack[top];
top--;
return ret;
}
}
void display(int stack[],int top)
{
cout<<stack[top]<<"<--";
for(int i=top-1;i>=0;i--)
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 1
Enter the element : 1
The stack formed is :
1
Want to enter again ?: y
Enter the element : 2
The stack formed is :
2 1
Want to enter again ?: y
Enter the element : 3
The stack formed is :
3 2 1
Want to enter again ?: y
Enter the element : 4
The stack formed is :
4 3 2 1
Want to enter again ?: N
Choose from the menu again ? : y
Choose from the following :
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 2
The deleted Element is : 4
The resultant stack is :
3 2 1
Want to delete again ? : y
The deleted Element is : 3
The resultant stack is :
2 1
Want to delete again ? : n
Choose from the menu again ? : y
Choose from the following :
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 3
The resultant stack is :
2 1
Choose from the menu again ? : n
/* Wap in c++ to implement stack as an array */
#include<iostream.h>
#include<conio.h>
#include<process.h>
int pop(int[],int&);
int push(int[],int&,int);
void display(int[],int);
const int size=50;
void main()
{
clrscr();
char m,ch;
int k,stack[size],item,top=-1,res;
do
{ cout<<"\nChoose from the following : \n\n"
<<"\n 1. Push"
<<"\n 2. Pop"
<<"\n 3. Display"
<<"\n 4. Exit"
<<"\n\nEnter your choice : "; cin>>k;
switch(k)
{
case 1: ch='y';
while(ch=='y'||ch=='Y')
{ cout<<"\nEnter the element : ";
cin>>item;
res=push(stack,top,item);
if(res==-1)
{cout<<"\nOverflow !!!!";
exit(1); }
cout<<"\nThe stack formed is : \n\n";
display(stack,top);
cout<<"\n\n\nWant to enter again ?: ";
cin>>ch;
}
break;
case 2: ch='y';
while(ch=='y'||ch=='Y')
{ res=pop(stack,top);
if(res==-1)
{
cout<<"\nUnderflow !!!!";
exit(1);
}
else
{
cout<<"\nThe deleted Element is : "<<res<<endl;
cout<<"\nThe resultant stack is : \n\n";
display(stack,top); }
cout<<"\nWant to delete again ? : ";
cin>>ch;
}
break;
case 3: cout<<"\nThe resultant stack is : ";
display(stack,top);
break;
case 4: exit(0);
break;
default: cout<<"\nPlease enter desired keyword : ";
} // end of switch
cout<<"\n\nChoose from the menu again ? : ";
cin>>m;
}while(m=='y'||m=='Y'); // end of do-while loop
getch();
} // end of main()
int push(int stack[],int &top,int el)
{
if(top==size-1)
return -1;
else
{
top++;
stack[top]=el;
return 0;
}
}
int pop(int stack[],int &top)
{
int ret;
if(top==-1)
return -1;
else
{
ret=stack[top];
top--;
return ret;
}
}
void display(int stack[],int top)
{
cout<<stack[top]<<"<--";
for(int i=top-1;i>=0;i--)
cout<<stack[i]<<"<--";
}
Output:
Choose from the following :
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 1
Enter the element : 1
The stack formed is :
1
Want to enter again ?: y
Enter the element : 2
The stack formed is :
2 1
Want to enter again ?: y
Enter the element : 3
The stack formed is :
3 2 1
Want to enter again ?: y
Enter the element : 4
The stack formed is :
4 3 2 1
Want to enter again ?: N
Choose from the menu again ? : y
Choose from the following :
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 2
The deleted Element is : 4
The resultant stack is :
3 2 1
Want to delete again ? : y
The deleted Element is : 3
The resultant stack is :
2 1
Want to delete again ? : n
Choose from the menu again ? : y
Choose from the following :
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 3
The resultant stack is :
2 1
Choose from the menu again ? : n
/* Wap in c++ to implement stack as an array */
#include<iostream.h>
#include<conio.h>
#include<process.h>
int pop(int[],int&);
int push(int[],int&,int);
void display(int[],int);
const int size=50;
void main()
{
clrscr();
char m,ch;
int k,stack[size],item,top=-1,res;
do
{ cout<<"\nChoose from the following : \n\n"
<<"\n 1. Push"
<<"\n 2. Pop"
<<"\n 3. Display"
<<"\n 4. Exit"
<<"\n\nEnter your choice : "; cin>>k;
switch(k)
{
case 1: ch='y';
while(ch=='y'||ch=='Y')
{ cout<<"\nEnter the element : ";
cin>>item;
res=push(stack,top,item);
if(res==-1)
{cout<<"\nOverflow !!!!";
exit(1); }
cout<<"\nThe stack formed is : \n\n";
display(stack,top);
cout<<"\n\n\nWant to enter again ?: ";
cin>>ch;
}
break;
case 2: ch='y';
while(ch=='y'||ch=='Y')
{ res=pop(stack,top);
if(res==-1)
{
cout<<"\nUnderflow !!!!";
exit(1);
}
else
{
cout<<"\nThe deleted Element is : "<<res<<endl;
cout<<"\nThe resultant stack is : \n\n";
display(stack,top); }
cout<<"\nWant to delete again ? : ";
cin>>ch;
}
break;
case 3: cout<<"\nThe resultant stack is : ";
display(stack,top);
break;
case 4: exit(0);
break;
default: cout<<"\nPlease enter desired keyword : ";
} // end of switch
cout<<"\n\nChoose from the menu again ? : ";
cin>>m;
}while(m=='y'||m=='Y'); // end of do-while loop
getch();
} // end of main()
int push(int stack[],int &top,int el)
{
if(top==size-1)
return -1;
else
{
top++;
stack[top]=el;
return 0;
}
}
int pop(int stack[],int &top)
{
int ret;
if(top==-1)
return -1;
else
{
ret=stack[top];
top--;
return ret;
}
}
void display(int stack[],int top)
{
cout<<stack[top]<<"<--";
for(int i=top-1;i>=0;i--)
cout<<stack[i]<<"<--";
}
Output:
Choose from the following :
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 1
Enter the element : 1
The stack formed is :
1
Want to enter again ?: y
Enter the element : 2
The stack formed is :
2 1
Want to enter again ?: y
Enter the element : 3
The stack formed is :
3 2 1
Want to enter again ?: y
Enter the element : 4
The stack formed is :
4 3 2 1
Want to enter again ?: N
Choose from the menu again ? : y
Choose from the following :
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 2
The deleted Element is : 4
The resultant stack is :
3 2 1
Want to delete again ? : y
The deleted Element is : 3
The resultant stack is :
2 1
Want to delete again ? : n
Choose from the menu again ? : y
Choose from the following :
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 3
The resultant stack is :
2 1
Choose from the menu again ? : n
/* Wap in c++ to implement stack as an array */
#include<iostream.h>
#include<conio.h>
#include<process.h>
int pop(int[],int&);
int push(int[],int&,int);
void display(int[],int);
const int size=50;
void main()
{
clrscr();
char m,ch;
int k,stack[size],item,top=-1,res;
do
{ cout<<"\nChoose from the following : \n\n"
<<"\n 1. Push"
<<"\n 2. Pop"
<<"\n 3. Display"
<<"\n 4. Exit"
<<"\n\nEnter your choice : "; cin>>k;
switch(k)
{
case 1: ch='y';
while(ch=='y'||ch=='Y')
{ cout<<"\nEnter the element : ";
cin>>item;
res=push(stack,top,item);
if(res==-1)
{cout<<"\nOverflow !!!!";
exit(1); }
cout<<"\nThe stack formed is : \n\n";
display(stack,top);
cout<<"\n\n\nWant to enter again ?: ";
cin>>ch;
}
break;
case 2: ch='y';
while(ch=='y'||ch=='Y')
{ res=pop(stack,top);
if(res==-1)
{
cout<<"\nUnderflow !!!!";
exit(1);
}
else
{
cout<<"\nThe deleted Element is : "<<res<<endl;
cout<<"\nThe resultant stack is : \n\n";
display(stack,top); }
cout<<"\nWant to delete again ? : ";
cin>>ch;
}
break;
case 3: cout<<"\nThe resultant stack is : ";
display(stack,top);
break;
case 4: exit(0);
break;
default: cout<<"\nPlease enter desired keyword : ";
} // end of switch
cout<<"\n\nChoose from the menu again ? : ";
cin>>m;
}while(m=='y'||m=='Y'); // end of do-while loop
getch();
} // end of main()
int push(int stack[],int &top,int el)
{
if(top==size-1)
return -1;
else
{
top++;
stack[top]=el;
return 0;
}
}
int pop(int stack[],int &top)
{
int ret;
if(top==-1)
return -1;
else
{
ret=stack[top];
top--;
return ret;
}
}
void display(int stack[],int top)
{
cout<<stack[top]<<"<--";
for(int i=top-1;i>=0;i--)
cout<<stack[i]<<"<--";
2. Pop
3. Display
4. Exit
Enter your choice : 1
Enter the element : 1
The stack formed is :
1
Want to enter again ?: y
Enter the element : 2
The stack formed is :
2 1
Want to enter again ?: y
Enter the element : 3
The stack formed is :
3 2 1
Want to enter again ?: y
Enter the element : 4
The stack formed is :
4 3 2 1
Want to enter again ?: N
Choose from the menu again ? : y
Choose from the following :
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 2
The deleted Element is : 4
The resultant stack is :
3 2 1
Want to delete again ? : y
The deleted Element is : 3
The resultant stack is :
2 1
Want to delete again ? : n
Choose from the menu again ? : y
Choose from the following :
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 3
The resultant stack is :
2 1
Choose from the menu again ? : n
/* Wap in c++ to implement stack as an array */
#include<iostream.h>
#include<conio.h>
#include<process.h>
int pop(int[],int&);
int push(int[],int&,int);
void display(int[],int);
const int size=50;
void main()
{
clrscr();
char m,ch;
int k,stack[size],item,top=-1,res;
do
{ cout<<"\nChoose from the following : \n\n"
<<"\n 1. Push"
<<"\n 2. Pop"
<<"\n 3. Display"
<<"\n 4. Exit"
<<"\n\nEnter your choice : "; cin>>k;
switch(k)
{
case 1: ch='y';
while(ch=='y'||ch=='Y')
{ cout<<"\nEnter the element : ";
cin>>item;
res=push(stack,top,item);
if(res==-1)
{cout<<"\nOverflow !!!!";
exit(1); }
cout<<"\nThe stack formed is : \n\n";
display(stack,top);
cout<<"\n\n\nWant to enter again ?: ";
cin>>ch;
}
break;
case 2: ch='y';
while(ch=='y'||ch=='Y')
{ res=pop(stack,top);
if(res==-1)
{
cout<<"\nUnderflow !!!!";
exit(1);
}
else
{
cout<<"\nThe deleted Element is : "<<res<<endl;
cout<<"\nThe resultant stack is : \n\n";
display(stack,top); }
cout<<"\nWant to delete again ? : ";
cin>>ch;
}
break;
case 3: cout<<"\nThe resultant stack is : ";
display(stack,top);
break;
case 4: exit(0);
break;
default: cout<<"\nPlease enter desired keyword : ";
} // end of switch
cout<<"\n\nChoose from the menu again ? : ";
cin>>m;
}while(m=='y'||m=='Y'); // end of do-while loop
getch();
} // end of main()
int push(int stack[],int &top,int el)
{
if(top==size-1)
return -1;
else
{
top++;
stack[top]=el;
return 0;
}
}
int pop(int stack[],int &top)
{
int ret;
if(top==-1)
return -1;
else
{
ret=stack[top];
top--;
return ret;
}
}
void display(int stack[],int top)
{
cout<<stack[top]<<"<--";
for(int i=top-1;i>=0;i--)
cout<<stack[i]<<"<--";
}
Output:
Choose from the following :
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 1
Enter the element : 1
The stack formed is :
1
Want to enter again ?: y
Enter the element : 2
The stack formed is :
2 1
Want to enter again ?: y
Enter the element : 3
The stack formed is :
3 2 1
Want to enter again ?: y
Enter the element : 4
The stack formed is :
4 3 2 1
Want to enter again ?: N
Choose from the menu again ? : y
Choose from the following :
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 2
The deleted Element is : 4
The resultant stack is :
3 2 1
Want to delete again ? : y
The deleted Element is : 3
The resultant stack is :
2 1
Want to delete again ? : n
Choose from the menu again ? : y
Choose from the following :
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 3
The resultant stack is :
2 1
Choose from the menu again ? : n
/* Wap in c++ to implement stack as an array */
#include<iostream.h>
#include<conio.h>
#include<process.h>
int pop(int[],int&);
int push(int[],int&,int);
void display(int[],int);
const int size=50;
void main()
{
clrscr();
char m,ch;
int k,stack[size],item,top=-1,res;
do
{ cout<<"\nChoose from the following : \n\n"
<<"\n 1. Push"
<<"\n 2. Pop"
<<"\n 3. Display"
<<"\n 4. Exit"
<<"\n\nEnter your choice : "; cin>>k;
switch(k)
{
case 1: ch='y';
while(ch=='y'||ch=='Y')
{ cout<<"\nEnter the element : ";
cin>>item;
res=push(stack,top,item);
if(res==-1)
{cout<<"\nOverflow !!!!";
exit(1); }
cout<<"\nThe stack formed is : \n\n";
display(stack,top);
cout<<"\n\n\nWant to enter again ?: ";
cin>>ch;
}
break;
case 2: ch='y';
while(ch=='y'||ch=='Y')
{ res=pop(stack,top);
if(res==-1)
{
cout<<"\nUnderflow !!!!";
exit(1);
}
else
{
cout<<"\nThe deleted Element is : "<<res<<endl;
cout<<"\nThe resultant stack is : \n\n";
display(stack,top); }
cout<<"\nWant to delete again ? : ";
cin>>ch;
}
break;
case 3: cout<<"\nThe resultant stack is : ";
display(stack,top);
break;
case 4: exit(0);
break;
default: cout<<"\nPlease enter desired keyword : ";
} // end of switch
cout<<"\n\nChoose from the menu again ? : ";
cin>>m;
}while(m=='y'||m=='Y'); // end of do-while loop
getch();
} // end of main()
int push(int stack[],int &top,int el)
{
if(top==size-1)
return -1;
else
{
top++;
stack[top]=el;
return 0;
}
}
int pop(int stack[],int &top)
{
int ret;
if(top==-1)
return -1;
else
{
ret=stack[top];
top--;
return ret;
}
}
void display(int stack[],int top)
{
cout<<stack[top]<<"<--";
for(int i=top-1;i>=0;i--)
cout<<stack[i]<<"<--";
}
Output:
Choose from the following :
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 1
Enter the element : 1
The stack formed is :
1
Want to enter again ?: y
Enter the element : 2
The stack formed is :
2 1
Want to enter again ?: y
Enter the element : 3
The stack formed is :
3 2 1
Want to enter again ?: y
Enter the element : 4
The stack formed is :
4 3 2 1
Want to enter again ?: N
Choose from the menu again ? : y
Choose from the following :
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 2
The deleted Element is : 4
The resultant stack is :
3 2 1
Want to delete again ? : y
The deleted Element is : 3
The resultant stack is :
2 1
Want to delete again ? : n
Choose from the menu again ? : y
Choose from the following :
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 3
The resultant stack is :
2 1
Choose from the menu again ? : n
/* Wap in c++ to implement stack as an array */
#include<iostream.h>
#include<conio.h>
#include<process.h>
int pop(int[],int&);
int push(int[],int&,int);
void display(int[],int);
const int size=50;
void main()
{
clrscr();
char m,ch;
int k,stack[size],item,top=-1,res;
do
{ cout<<"\nChoose from the following : \n\n"
<<"\n 1. Push"
<<"\n 2. Pop"
<<"\n 3. Display"
<<"\n 4. Exit"
<<"\n\nEnter your choice : "; cin>>k;
switch(k)
{
case 1: ch='y';
while(ch=='y'||ch=='Y')
{ cout<<"\nEnter the element : ";
cin>>item;
res=push(stack,top,item);
if(res==-1)
{cout<<"\nOverflow !!!!";
exit(1); }
cout<<"\nThe stack formed is : \n\n";
display(stack,top);
cout<<"\n\n\nWant to enter again ?: ";
cin>>ch;
}
break;
case 2: ch='y';
while(ch=='y'||ch=='Y')
{ res=pop(stack,top);
if(res==-1)
{
cout<<"\nUnderflow !!!!";
exit(1);
}
else
{
cout<<"\nThe deleted Element is : "<<res<<endl;
cout<<"\nThe resultant stack is : \n\n";
display(stack,top); }
cout<<"\nWant to delete again ? : ";
cin>>ch;
}
break;
case 3: cout<<"\nThe resultant stack is : ";
display(stack,top);
break;
case 4: exit(0);
break;
default: cout<<"\nPlease enter desired keyword : ";
} // end of switch
cout<<"\n\nChoose from the menu again ? : ";
cin>>m;
}while(m=='y'||m=='Y'); // end of do-while loop
getch();
} // end of main()
int push(int stack[],int &top,int el)
{
if(top==size-1)
return -1;
else
{
top++;
stack[top]=el;
return 0;
}
}
int pop(int stack[],int &top)
{
int ret;
if(top==-1)
return -1;
else
{
ret=stack[top];
top--;
return ret;
}
}
void display(int stack[],int top)
{
cout<<stack[top]<<"<--";
for(int i=top-1;i>=0;i--)
cout<<stack[i]<<"<--";
}
3. Display
4. Exit
Enter your choice : 1
Enter the element : 1
The stack formed is :
1
Want to enter again ?: y
Enter the element : 2
The stack formed is :
2 1
Want to enter again ?: y
Enter the element : 3
The stack formed is :
3 2 1
Want to enter again ?: y
Enter the element : 4
The stack formed is :
4 3 2 1
Want to enter again ?: N
Choose from the menu again ? : y
Choose from the following :
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 2
The deleted Element is : 4
The resultant stack is :
3 2 1
Want to delete again ? : y
The deleted Element is : 3
The resultant stack is :
2 1
Want to delete again ? : n
Choose from the menu again ? : y
Choose from the following :
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 3
The resultant stack is :
2 1
Choose from the menu again ? : n
/* Wap in c++ to implement stack as an array */
#include<iostream.h>
#include<conio.h>
#include<process.h>
int pop(int[],int&);
int push(int[],int&,int);
void display(int[],int);
const int size=50;
void main()
{
clrscr();
char m,ch;
int k,stack[size],item,top=-1,res;
do
{ cout<<"\nChoose from the following : \n\n"
<<"\n 1. Push"
<<"\n 2. Pop"
<<"\n 3. Display"
<<"\n 4. Exit"
<<"\n\nEnter your choice : "; cin>>k;
switch(k)
{
case 1: ch='y';
while(ch=='y'||ch=='Y')
{ cout<<"\nEnter the element : ";
cin>>item;
res=push(stack,top,item);
if(res==-1)
{cout<<"\nOverflow !!!!";
exit(1); }
cout<<"\nThe stack formed is : \n\n";
display(stack,top);
cout<<"\n\n\nWant to enter again ?: ";
cin>>ch;
}
break;
case 2: ch='y';
while(ch=='y'||ch=='Y')
{ res=pop(stack,top);
if(res==-1)
{
cout<<"\nUnderflow !!!!";
exit(1);
}
else
{
cout<<"\nThe deleted Element is : "<<res<<endl;
cout<<"\nThe resultant stack is : \n\n";
display(stack,top); }
cout<<"\nWant to delete again ? : ";
cin>>ch;
}
break;
case 3: cout<<"\nThe resultant stack is : ";
display(stack,top);
break;
case 4: exit(0);
break;
default: cout<<"\nPlease enter desired keyword : ";
} // end of switch
cout<<"\n\nChoose from the menu again ? : ";
cin>>m;
}while(m=='y'||m=='Y'); // end of do-while loop
getch();
} // end of main()
int push(int stack[],int &top,int el)
{
if(top==size-1)
return -1;
else
{
top++;
stack[top]=el;
return 0;
}
}
int pop(int stack[],int &top)
{
int ret;
if(top==-1)
return -1;
else
{
ret=stack[top];
top--;
return ret;
}
}
void display(int stack[],int top)
{
cout<<stack[top]<<"<--";
for(int i=top-1;i>=0;i--)
cout<<stack[i]<<"<--";
}
Output:
Choose from the following :
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 1
Enter the element : 1
The stack formed is :
1
Want to enter again ?: y
Enter the element : 2
The stack formed is :
2 1
Want to enter again ?: y
Enter the element : 3
The stack formed is :
3 2 1
Want to enter again ?: y
Enter the element : 4
The stack formed is :
4 3 2 1
Want to enter again ?: N
Choose from the menu again ? : y
Choose from the following :
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 2
The deleted Element is : 4
The resultant stack is :
3 2 1
Want to delete again ? : y
The deleted Element is : 3
The resultant stack is :
2 1
Want to delete again ? : n
Choose from the menu again ? : y
Choose from the following :
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 3
The resultant stack is :
2 1
Choose from the menu again ? : n
/* Wap in c++ to implement stack as an array */
#include<iostream.h>
#include<conio.h>
#include<process.h>
int pop(int[],int&);
int push(int[],int&,int);
void display(int[],int);
const int size=50;
void main()
{
clrscr();
char m,ch;
int k,stack[size],item,top=-1,res;
do
{ cout<<"\nChoose from the following : \n\n"
<<"\n 1. Push"
<<"\n 2. Pop"
<<"\n 3. Display"
<<"\n 4. Exit"
<<"\n\nEnter your choice : "; cin>>k;
switch(k)
{
case 1: ch='y';
while(ch=='y'||ch=='Y')
{ cout<<"\nEnter the element : ";
cin>>item;
res=push(stack,top,item);
if(res==-1)
{cout<<"\nOverflow !!!!";
exit(1); }
cout<<"\nThe stack formed is : \n\n";
display(stack,top);
cout<<"\n\n\nWant to enter again ?: ";
cin>>ch;
}
break;
case 2: ch='y';
while(ch=='y'||ch=='Y')
{ res=pop(stack,top);
if(res==-1)
{
cout<<"\nUnderflow !!!!";
exit(1);
}
else
{
cout<<"\nThe deleted Element is : "<<res<<endl;
cout<<"\nThe resultant stack is : \n\n";
display(stack,top); }
cout<<"\nWant to delete again ? : ";
cin>>ch;
}
break;
case 3: cout<<"\nThe resultant stack is : ";
display(stack,top);
break;
case 4: exit(0);
break;
default: cout<<"\nPlease enter desired keyword : ";
} // end of switch
cout<<"\n\nChoose from the menu again ? : ";
cin>>m;
}while(m=='y'||m=='Y'); // end of do-while loop
getch();
} // end of main()
int push(int stack[],int &top,int el)
{
if(top==size-1)
return -1;
else
{
top++;
stack[top]=el;
return 0;
}
}
int pop(int stack[],int &top)
{
int ret;
if(top==-1)
return -1;
else
{
ret=stack[top];
top--;
return ret;
}
}
void display(int stack[],int top)
{
cout<<stack[top]<<"<--";
for(int i=top-1;i>=0;i--)
cout<<stack[i]<<"<--";
}
Output:
Choose from the following :
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 1
Enter the element : 1
The stack formed is :
1
Want to enter again ?: y
Enter the element : 2
The stack formed is :
2 1
Want to enter again ?: y
Enter the element : 3
The stack formed is :
3 2 1
Want to enter again ?: y
Enter the element : 4
The stack formed is :
4 3 2 1
Want to enter again ?: N
Choose from the menu again ? : y
Choose from the following :
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 2
The deleted Element is : 4
The resultant stack is :
3 2 1
Want to delete again ? : y
The deleted Element is : 3
The resultant stack is :
2 1
Want to delete again ? : n
Choose from the menu again ? : y
Choose from the following :
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 3
The resultant stack is :
2 1
Choose from the menu again ? : n
/* Wap in c++ to implement stack as an array */
#include<iostream.h>
#include<conio.h>
#include<process.h>
int pop(int[],int&);
int push(int[],int&,int);
void display(int[],int);
const int size=50;
void main()
{
clrscr();
char m,ch;
int k,stack[size],item,top=-1,res;
do
{ cout<<"\nChoose from the following : \n\n"
<<"\n 1. Push"
<<"\n 2. Pop"
<<"\n 3. Display"
<<"\n 4. Exit"
<<"\n\nEnter your choice : "; cin>>k;
switch(k)
{
case 1: ch='y';
while(ch=='y'||ch=='Y')
{ cout<<"\nEnter the element : ";
cin>>item;
res=push(stack,top,item);
if(res==-1)
{cout<<"\nOverflow !!!!";
exit(1); }
cout<<"\nThe stack formed is : \n\n";
display(stack,top);
cout<<"\n\n\nWant to enter again ?: ";
cin>>ch;
}
break;
case 2: ch='y';
while(ch=='y'||ch=='Y')
{ res=pop(stack,top);
if(res==-1)
{
cout<<"\nUnderflow !!!!";
exit(1);
}
else
{
cout<<"\nThe deleted Element is : "<<res<<endl;
cout<<"\nThe resultant stack is : \n\n";
display(stack,top); }
cout<<"\nWant to delete again ? : ";
cin>>ch;
}
break;
case 3: cout<<"\nThe resultant stack is : ";
display(stack,top);
break;
case 4: exit(0);
break;
default: cout<<"\nPlease enter desired keyword : ";
} // end of switch
cout<<"\n\nChoose from the menu again ? : ";
cin>>m;
}while(m=='y'||m=='Y'); // end of do-while loop
getch();
} // end of main()
int push(int stack[],int &top,int el)
{
if(top==size-1)
return -1;
else
{
top++;
stack[top]=el;
return 0;
}
}
int pop(int stack[],int &top)
{
int ret;
if(top==-1)
return -1;
else
{
ret=stack[top];
top--;
return ret;
}
}
void display(int stack[],int top)
{
cout<<stack[top]<<"<--";
for(int i=top-1;i>=0;i--)
cout<<stack[i]<<"<--";
}
4. Exit
Enter your choice : 1
Enter the element : 1
The stack formed is :
1
Want to enter again ?: y
Enter the element : 2
The stack formed is :
2 1
Want to enter again ?: y
Enter the element : 3
The stack formed is :
3 2 1
Want to enter again ?: y
Enter the element : 4
The stack formed is :
4 3 2 1
Want to enter again ?: N
Choose from the menu again ? : y
Choose from the following :
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 2
The deleted Element is : 4
The resultant stack is :
3 2 1
Want to delete again ? : y
The deleted Element is : 3
The resultant stack is :
2 1
Want to delete again ? : n
Choose from the menu again ? : y
Choose from the following :
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 3
The resultant stack is :
2 1
Choose from the menu again ? : n
/* Program to search an element of an array using Linear search method
#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int A[50]; int n; int p; int subscript; /*Note: subscript and index are same.*/
cout<<"Enter the array size : ";
cin>>n; // n=size upto which user wants to insert values in array
cout<<"\n\nEnter elements of array : \n";
for(int i=0; i<n; i++)
cin>>A[i];
cout<<"\n\nThe array formed = ";
for(i=0; i<n; i++)
cout<<A[i]<<" ";
cout<<"\n\nEnter the element to be searched : ";
cin>>p; // p=element to be searched
void linear_search ( int A[], int n, int p); //function declaration
linear_search(A,n,p);
getch();
}
void linear_search(int A[], int n, int p)
{ int count=0; int B[50]; int flag=0;
for(int i=0; i<n; i++)
{ flag=count;
if( A[i]==p)
{
B[count]=i;
count++;
flag=count;
}
}
if(flag==0)
cout<<"\n\nRequested element not found.";
else
for(i=0; i<=n; i++)
{ if(A[i]==p)
{
cout<<"\n\nElement "<<p<<" is found";
cout<<"\n\nSubscript = "<<i<<"\n\nPosition = "<<i+1<<"\n\n\n";
}
}
}
Output:
Enter the array size : 5
Enter elements of array :
7
9
23
24
12
The array formed = 7 9 23 24 12
Enter the element to be searched : 24
Element 24 is found
Subscript = 3
Position = 4
/* Program to sort the given array in ascending order using bubble sort method */
#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
int A[80],n;
cout<<"Enter desired size of array (<80): "; cin>>n;
cout<<"\n\nEnter the array : \n";
for(int i=0; i<n; i++)
cin>>A[i];
cout<<"\n\nArray of elements is as shown below : \n\n";
for(i=0; i<n; i++)
cout<<A[i]<<" ";
cout<<"\n\nNow Elements will be arranged in ascending order using bubble sort :\n\n";
void bubble_sort(int A[],int n);
bubble_sort(A,n);
getch();
}
void bubble_sort (int A[], int n)
{ int temp; int count=0;
for(int i=0; i<n; i++)
{
for(int j=0; j<n-1; j++)
{ if(A[j+1]<A[j])
{ count++;
temp=A[j+1];
A[j+1]=A[j];
A[j]=temp;
cout<<"\n\nArray for iteration "<<count<<" is : \n\n";
for(int k=0; k<n; k++)
cout<<A[k]<<" ";
}
}
}
}
Output:
Enter desired size of array (<80): 5
Enter the array :
9
7
1
3
4
Array for iteration 1 is :
7 9 1 3 4
Array for iteration 2 is :
7 1 9 3 4
Array for iteration 3 is :
7 1 3 9 4
Array for iteration 4 is :
7 1 3 4 9
Array for iteration 5 is :
1 7 3 4 9
Array for iteration 6 is :
1 3 7 4 9
Array for iteration 7 is :
1 3 4 7 9
Sorted array is :
1 3 4 7 9
/* Program to search an element of an array using Binary search */
#include<iostream.h>
#include<process.h>
#include<conio.h>
void main()
{ clrscr();
int A[50], n,p;
cout<<"Enter the Size of array : ";
cin>>n;
cout<<"\n\nEnter the elements : \n\n";
for(int i=0; i<n; i++)
cin>>A[i];
cout<<"\n\nArray formed is : ";
for(i=0; i<n; i++)
cout<<A[i]<<" ";
cout<<"\n\nEnter the element to be searched : ";
cin>>p;
void binary_search(int A[], int n, int p); //declaration
binary_search(A,n,p);
getch();
}
void binary_search(int A[], int n, int p)
{ int L,U,mid; char ch;
lb: L=0; U=n-1;
while(L<=U) //i.e loop will continue if L<=u. if L>U loop will end
{ mid=(L+U)/2;
if(A[mid]==p)
{ cout<<"\n\nElement "<<p<<" found. Search Successful.";
cout<<"\n\nSubscript = "<<mid<<" \n\nPosition = "<<mid+1;
break;
}
else if(p<=A[mid])
U=mid-1;
else
L=mid+1;
}//end of while loop
if(L>U)
{cout<<"\n\nUnsuccessful search.";
cout<<"\n\n\n\nWant to search again. : "; cin>>ch;
if(ch=='y'||ch=='Y')
{cout<<"\n\n\n\nEnter the element to be searched : ";
cin>>p;
goto lb;}
else
exit(1);
}
}
Output:
Enter the Size of array : 5
Enter the elements :
2
5
8
9
11
Array formed is : 2 5 8 9 11
Enter the element to be searched : 8
Element 8 found. Search Successful.
Subscript = 2
Position = 3
top related