Stack 3-Data Structures and Representation-Assigment, Exercises of Data Structures and Algorithms

This task was assigned by Prof. Chandrashekar Baag at Amrita Vishwa Vidyapeetham. This assignment is related to Data Structures and Representation course. It includes: Choice, Switch, Stack, Push, Case, Break, Pop, Wrong, Enter, Number

Typology: Exercises

2011/2012

Uploaded on 07/30/2012

dhanvantari
dhanvantari 🇮🇳

2

(2)

45 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
#include<conio.h>
#include<iostream.h>
void push (int*,int*,int);
int pop (int*,int*);
void main ()
{
int queue[10],len,top=-1,stack[10],stop=-1,input,popout;
char choice='y';
clrscr();
while(choice=='y')
{
cout<<"\nEnter the number of pages you want to print:\t"; /*The person
with least number of pages will be given the highest priority */
cin>>input;
if(top==-1) /*For the first entry*/
{
top=0;
queue[0]=input;
}
if (queue[top]>input) /*For entry having less number of
pages as compared to the highest number of pages in the queue*/
{
while(queue[top]>input && top>-1)
{
popout=pop(&queue[top],&top); /*Moves higher number
of pages than the current entry to stack such that elements in stack
settels in ascending order, from top to bottom*/
push(&stack[stop],&stop,popout);
top--;
}
top=top+1;
queue[top]=input; /*Enters curent entry at
its appropriate position*/
while(stop>-1)
{
popout=pop(&stack[stop],&top);stop--; /*Pushes back
the values from stack into the queue such that they automaticaly settels
in ascending order in the queue*/
push(&queue[top],&top,popout);
}
}
if(queue[top]<input)
{
push(&queue[top],&top,input); /*If current value
is higher than any entry of the queue*/
}
cout<<"\nCurrent Queue-->\n\n\t";
for(int j=0;j<=top;j++)
{
cout<<queue[j]<<" "; /*To Display*/
docsity.com
pf2

Partial preview of the text

Download Stack 3-Data Structures and Representation-Assigment and more Exercises Data Structures and Algorithms in PDF only on Docsity!

#include<conio.h> #include<iostream.h> void push (int,int,int); int pop (int,int); void main () { int queue[10],len,top=-1,stack[10],stop=-1,input,popout; char choice='y'; clrscr();

while(choice=='y') { cout<<"\nEnter the number of pages you want to print:\t"; /The person with least number of pages will be given the highest priority / cin>>input; if(top==-1) /For the first entry/ { top=0; queue[0]=input; } if (queue[top]>input) /For entry having less number of pages as compared to the highest number of pages in the queue/ { while(queue[top]>input && top>-1) { popout=pop(&queue[top],&top); /Moves higher number of pages than the current entry to stack such that elements in stack settels in ascending order, from top to bottom/ push(&stack[stop],&stop,popout); top--; } top=top+1; queue[top]=input; /Enters curent entry at its appropriate position/

while(stop>-1) { popout=pop(&stack[stop],&top);stop--; /Pushes back the values from stack into the queue such that they automaticaly settels in ascending order in the queue/ push(&queue[top],&top,popout); } }

if(queue[top]<input) { push(&queue[top],&top,input); /If current value is higher than any entry of the queue/ }

cout<<"\nCurrent Queue-->\n\n\t"; for(int j=0;j<=top;j++) { cout<<queue[j]<<" "; /To Display/

docsity.com

cout<<"\n\n\n\nPress 'y' for new entry, any other to exit:\t"; cin>>choice; } }

void push (int st,int t,int in) { if (t>8 && in>st) { cout<<" "; } else { t=t+1; st++; *st=in;

} }

int pop (int *s,int to) { if(to<0) { cout<<"The stack is empty\n"; } else { return *s; s--; } }

docsity.com