Upcoming SlideShare
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Standard text messaging rates apply

# Qprgs

261

Published on

Qprgs

Qprgs

1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total Views
261
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
4
0
Likes
1
Embeds 0
No embeds

No notes for slide

### Transcript

• 1. 1) &#x2018;C&#x2019; program/************************************************************* Program for implementing the Queue using arrays*************************************************************/#include&lt;stdio.h&gt;#include&lt;stdlib.h&gt;#include&lt;conio.h&gt;#define size 5struct queue{ int que[size]; int front,rear;}Q;/* The Qfull Function Input:none Output:1 or 0 for q full or not Called By:main Calls:none*/ Qfull() { if(Q.rear &gt;=size&#x2013;1) return 1; else return 0;}/* The insert Function Input:item -which is to be inserted in the Q Output:rear value Called By:main Calls:none*/int insert(int item){ if(Q.front == &#x2013;1) Q.front++; Q.que[++Q.rear] = item; return Q.rear;}int Qempty(){ if((Q.front == &#x2013; 1) || (Q.front &gt; Q.rear)) return 1; else return 0;}/* The delete Function Input:none Output:front value Called By:main
• 2. Calls:none*/int delete(){ int item; item = Q.que[Q.front]; Q.front++; printf(&#x2015;n The deleted item is %d&#x2016;,item); return Q.front;}/* The display Function Input:none Output:none Called By:main Calls:none*/void display(){ int i; for(i=Q.front;i&lt;=Q.rear;i++) printf(&#x2015; %d&#x2016;,Q.que[i]);}void main(void){ int choice,item; char ans; clrscr(); Q.front = &#x2013;1; Q.rear = &#x2013;1; do { printf(&#x2015;n Main Menu&#x2016;); printf(&#x2015;n1.Insertn2.Deleten3.Display&#x2016;); printf(&#x2015;n Enter Your Choice&#x2016;); scanf(&#x2015;%d&#x2016;,&amp;choice); switch(choice) { case 1:if(Qfull()) //checking for Queue overflow printf(&#x2015;n Can not insert the element&#x2016;); else { printf(&#x2015;n Enter The number to be inserted&#x2016;); scanf(&#x2015;%d&#x2016;,&amp;item); insert(item); } break; case 2:if(Qempty()) printf(&#x2015;n Queue Underflow!!&#x2016;); else delete(); break; case 3:if(Qempty()) printf(&#x2015;nQueue Is Empty!&#x2016;);
• 3. else display(); break; default:printf(&#x2015;n Wrong choice!&#x2016;); break; } printf(&#x2015;n Do You Want to continue?&#x2016;); ans =getche(); }while(ans ==&#x2018;Y&#x2018;||ans ==&#x2018;y&#x2018;);}/********************** End Of Program ***********************/2) &#x2018;C&#x2019; program/*************************************************************Program For circular queue using arrays.It performs the basicoperations such as insertion,deletion of the elements by takingcare of queue Full and queue Empty conditions.The appropriatedisplay is for displaying the queue*************************************************************/#include&lt;stdio.h&gt;#include&lt;conio.h&gt;#include&lt;stdlib.h&gt;#define size 5int queue[size];int front=&#x2013;1;int rear =0;/* The qFull Function Input:none Output:returns 1 or 0 for q.full or not Called By:add Calls:none*/int qFull(){ if(front==(rear+1)%size) return 1; else return 0;}/* The qEmpty Function Input:none Output:returns 1 or 0 for q.empty or not Called By:delete and display Calls:none*/int qEmpty(){ if(front ==&#x2013;1) return 1; else return 0;}
• 4. /* The add Function Input:the element to be inserted Output:none Called By:main Calls:qFull*/void add(int Item){ if(qFull()) printf(&#x2015;n The circular Queue is full!&#x2016;); else { if(front ==&#x2013;1) front=rear=0; else rear =(rear+1)%size; queue[rear]=Item; }}/* The delete Function Input:none Output:returns the deleted element Called By:main Calls:qEmpty*/void delete(){ int Item; if(qEmpty()) printf(&#x2015;n Queue Is Empty!&#x2016;); else { Item=queue[front]; if(front ==rear) { front=rear=&#x2013;1; } else front =(front+1)%size; printf(&#x2015;n The deleted item is %d&#x2016;,Item); } }/* The display Function Input: none Output: prints the contents of the queue Called By:main Calls:qEmpty*/void display(){
• 5. int i; if(qEmpty()) { printf(&#x2015;nThe Queue Is Empty&#x2016;); return; } i=front; while(i!=rear) { printf(&#x2015; %d&#x2016;,queue[i]); i=(i+1)%size; } printf(&#x2015; %d&#x2016;,queue[i]); }/* The main Function Input:none Output:none Called By:O.S. Calls:add,delete,display*/void main(void){ int choice,Item; char ans; clrscr(); do { printf(&#x2015;n Main Menu&#x2016;); printf(&#x2015;n 1.Insertn2.Deleten3.Display&#x2016;); printf(&#x2015;n Enter Your Choice&#x2016;); scanf(&#x2015;%d&#x2016;,&amp;choice); switch(choice) { case 1:printf(&#x2015;nEnter The element&#x2016;); scanf(&#x2015;%d&#x2016;,&amp;Item); add(Item); break; case 2:delete(); break; case 3:display(); break; default:exit(0); } printf(&#x2015;n Do u want to continue?&#x2016;); ans =getch(); }while(anvb ==&#x2018;Y&#x2018;||ans == &#x2017;y&#x2018;); getch();}/************************ End Of Main ***********************/
• 6. 3)C Program/*************************************************************Program for implementing the Queue using one dimensional array*************************************************************/#include&lt;stdio.h&gt;#include&lt;stdlib.h&gt;#include&lt;conio.h&gt;#define size 20/*data structure for multiple queue*/struct mult_que { int que[size]; int rear[size],front[size]; }Q;int sz[size];/*stores sizes of individual queue*//* The set_que functionPurpose:This function initialises all the front and rear positions ofindividual queues.All thses queues has to be in a single dimensionalarrayInput:index of the queueOutput:none,it simply initializes the locations of front and rearCalled By:main,before performing any operation on individual queue Calls:none*/void set_que(int index){ int sum,i; if(index==1) { Q.front[index]=0; Q.rear[index]=Q.front[index]-1;} else { sum=0; for(i=0;i&lt;index-1;i++) sum=sum+sz[i];Q.front[index]=sum;Q.rear[index]=Q.front[index]-1;/*computed the rear position based on previous queues */ }}/* The Qfull Function Purpose:to check whethe queue is full or not Input:index of the queue Output:1 or 0 for q full or not Called By:main Calls:none*/int Qfull(int index)
• 7. { int sum,i; sum=0; for(i=0;i&lt;index;i++) sum=sum+sz[i]; if(Q.rear[index]==sum-1) return 1; else return 0;}/* The insert Function Purpose:This function if for inserting the items in the requested queue Input:item which is to be inserted in the queue Output:rear value Called By:main Calls:none*/void insert(int item,int index){ int j; j=Q.rear[index]; Q.que[++j] = item; Q.rear[index]=Q.rear[index]+1;}/* The Qempty function Purpose:It Checks whether the queue is empty or not Input:index of the queue Output: returns 1 if queue is empty otherwise 0 Called By:main Calls:none*/int Qempty(int index){ if(Q.front[index]&gt;Q.rear[index]) return 1; else return 0;}/* The delet Function Purpose:for deletion of the element from the requeseted queue Input:index of the que Output:item which is deleted Called By:main Calls:none*/int delet(int index){ int item; item = Q.que[Q.front[index]]; Q.que[Q.front[index]]=-1; Q.front[index]++;
• 8. return item;}/* The display Function Purpose:displays all the queues Input:num which indicates total number of queues Output:none Called By:main Calls:none*/void display(int num){ int index,i; index=1; do { if(Qempty(index)) printf(&#x2015;n The Queue %d is Empty&#x2016;,index); else { printf(&#x2015;n Queue number %d is:&#x2016;,index); for(i=Q.front[index];i&lt;=Q.rear[index];i++) { if(Q.que[i]!=-1) printf(&#x2015; %d&#x2016;,Q.que[i]); } } index++; }while(index&lt;=num);}/* The main function Input:none Output:none Called By:O.S. Calls:set_que,Qfull,Qempty,insert,delete,display*/void main(void){ int choice,item,num,i,index; char ans; clrscr(); printf(&#x2015;ntt Program For Multiple Queues&#x2016;); printf(&#x2015;n How many Queues do you want?&#x2016;); scanf(&#x2015;%d&#x2016;,&amp;num); printf(&#x2015;n Enter The size of queue(Combined size of all Queues is 20)&#x2016;); for(i=0;i&lt;num;i++) scanf(&#x2015;%d&#x2016;,&amp;sz[i]); for(index=1;index&lt;=num;index++) set_que(index);/*initialized front and rear of each queue*/ do {
• 9. printf(&#x2015;n Main Menu&#x2016;); printf(&#x2015;n1.Insertn2.Deleten3.Display&#x2016;); printf(&#x2015;n Enter Your Choice&#x2016;); scanf(&#x2015;%d&#x2016;,&amp;choice); switch(choice) { case 1: printf(&#x2015;n Enter in which queue you wish to insert the item?&#x2016;); scanf(&#x2015;%d&#x2016;,&amp;index); if(Qfull(index)) /*checking for Queue overflow*/ printf(&#x2015;n Can not insert the elemnt&#x2016;); else { printf(&#x2015;n Enter The number to be inseted&#x2016;); scanf(&#x2015;%d&#x2016;,&amp;item); insert(item,index); } break; case 2: printf(&#x2015;n Enter From which queue you wish to delete the item?&#x2016;); scanf(&#x2015;%d&#x2016;,&amp;index); if(Qempty(index)) printf(&#x2015;n Queue Underflow!!&#x2016;); else { item= delet(index); printf(&#x2015;n The deleted item is %d&#x2016;,item); } break; case 3:display(num); break; default:printf(&#x2015;n Wrong choice!&#x2016;); break; } printf(&#x2015;n Do You Want to continue?&#x2016;); ans =getche(); }while(ans ==&#x2018;Y&#x2018;||ans ==&#x2018;y&#x2018;);}4)C Program/************************************************************* Program To implement Doubly ended queue using arrays*************************************************************/#include&lt;stdio.h&gt;#include&lt;conio.h&gt;#include&lt;stdlib.h&gt;#define size 5/*Data structure for deque*/struct queue { int que[size]; int front,rear; }Q;
• 10. /* The Qfull function Input:none Output:returns 1 if Queue is full otherwise 0 Called By:main Calls:none*/int Qfull(){ if(Q.rear==size-1) return 1; else return 0;}/* The function Input:Qempty Output:none Called By:main Calls: none*/int Qempty(){ if((Q.front&gt;Q.rear)||(Q.front==-1&amp;&amp;Q.rear==-1)) return 1; else return 0;}/* The Qfull function Input: Output: Called By: Calls:*/int insert_rear(int item){ if(Q.front==-1&amp;&amp;Q.rear==-1) Q.front++; Q.que[++Q.rear]=item; return Q.rear;}/* The delete_front function Input:none Output:returns the deleted item Called By:main Calls:none*/int delete_front(){ int item; if(Q.front==-1) Q.front++; item=Q.que[Q.front]; Q.que[Q.front]=-1; Q.front++; return item;
• 11. }/* The insert_front function Input:the item to be inserted by front Output:returns the front Called By:main Calls:none*/int insert_front(int item){ int i,j; if(Q.front==-1) Q.front++; i=Q.front-1; while(i&gt;=0) { Q.que[i+1]=Q.que[i]; i&#x2014;; } j=Q.rear; while(j&gt;=Q.front) { Q.que[j+1]=Q.que[j]; j&#x2014;; } Q.rear++; Q.que[Q.front]=item; return Q.front;}/* The delete_rear function Input: none Output: the item to deleted Called By:main Calls:none*/int delete_rear(){ int item; item=Q.que[Q.rear]; Q.que[Q.rear]=-1;/*logical deletion*/ Q.rear&#x2014;; return item;}/* The display function Input:none Output:none,it displays the contents in the queue Called By:main Calls:none*/void display(){ int i; printf(&#x2015;n Straight Queue is:&#x2016;); for(i=Q.front;i&lt;=Q.rear;i++) printf(&#x2015; %d&#x2016;,Q.que[i]);
• 12. }/* The main function Input:none Output:none Called By:O.S.Calls:Qfull,Qempty,insert_rear,Insert_front,delete_front,delete_rear,display*/void main(){ int choice,i,item; char ans; ans=&#x2018;y&#x2018;; Q.front=-1; Q.rear=-1; for(i=0;i&lt;size;i++) Q.que[i]=-1; clrscr(); printf(&#x2015;ntt Program For doubly ended queue using arrays&#x2016;); do { printf(&#x2015;n1.insert by rearn2.delete by frontn3.insert byfrontn4.delete by rear&#x2016;); printf(&#x2015;n5.displayn6.exit&#x2016;); printf(&#x2015;n Enter Your choice&#x2016;); scanf(&#x2015;%d&#x2016;,&amp;choice); switch(choice) { case 1:if(Qfull()) printf(&#x2015;n Doubly ended Queue is full&#x2016;); else { printf(&#x2015;n Enter The item to be inserted&#x2016;); scanf(&#x2015;%d&#x2016;,&amp;item); Q.rear=insert_rear(item); } break; case 2:if(Qempty()) printf(&#x2015;n Doubly ended Queue is Empty&#x2016;); else { item=delete_front(); printf(&#x2015;n The item deleted from queue is%d&#x2016;,item); } break; case 3:if(Qfull()) printf(&#x2015;n Doubly ended Queue is full&#x2016;); else { printf(&#x2015;n Enter The item to be inserted&#x2016;); scanf(&#x2015;%d&#x2016;,&amp;item); Q.front=insert_front(item);
• 13. } break; case 4:if(Qempty()) printf(&#x2015;n Doubly ended Queue is Empty&#x2016;); else { item=delete_rear(); printf(&#x2015;n The item deleted from queue is%d&#x2016;,item); } break; case 5:display(); break; case 6:exit(0); } printf(&#x2015;n Do You Want To Continue?&#x2016;); ans=getch(); }while(ans==&#x2018;y&#x2018;||ans==&#x2018;Y&#x2018;); getch();}5)&#x2018;C&#x2019; program/************************************************************Program for implementing the ascending priority Queue ************************************************************//*Header Files*/#include&lt;stdio.h&gt;#include&lt;conio.h&gt;#include&lt;process.h&gt;#define size 5int rear,front;int que[size];/* The Qfull function Input:none Output:1 or 0 for Qfull or not resp. Called By:main Calls:none*/Qfull(){if(rear ==size-1) return 1; else return 0;}/* The insert function Input:none Output:none Called By:main Calls:priority*/void insert()
• 14. { int item; void priority(); printf(&#x2015;nEnter the elementn&#x2016;); scanf(&#x2015;%d&#x2016;,&amp;item); if(front ==-1) front++; que[++rear]= item; priority();}/* The priority function Input:none Output:none Called By:insert Calls:none*/void priority(){ int i,j,temp; for(i=front;i&lt;=rear-1;i++) for(j=front;j&lt;rear;j++) if(que[j]&gt;que[j+1]) { temp = que[j]; que[j] = que[j+1]; que[j+1] =temp; } }/* The Qempty function Input:none Output:1 or 0 Called By:main Calls:none*/Qempty(){ if((front==-1)||(front&gt;rear)) return 1; else return 0;}/* The delet function Input:none Output:none Called By:main Calls:none*/void delet(){ int item; item=que[front]; printf(&#x2015;n The item deleted is %d&#x2016;,item);
• 15. front++;}/* The display function Input:none Output:none Called By:main Calls:none*/void display(){ int i; printf(&#x2015;n The queue is:&#x2016;); for(i=front;i&lt;=rear;i++) printf(&#x2015; %d&#x2016;,que[i]);}/* Name:main() Input:none Output:none Calls:Qfull,Qempty,insert,delet,display*/void main(void) { int choice; char ans; clrscr(); front=rear=-1; printf(&#x2015;ntt Priority Queuen&#x2016;); do { printf(&#x2015;n Main Menu&#x2016;); printf(&#x2015;n1.Insertn2.Deleten3.Display&#x2016;); printf(&#x2015;n Enter Your Choice&#x2016;); scanf(&#x2015;%d&#x2016;,&amp;choice); switch(choice) { case 1:if(Qfull()) printf(&#x2015;n Queue IS full&#x2016;); else insert(); break; case 2:if(Qempty()) printf(&#x2015;n Cannot delete element&#x2016;); else delet(); break; case 3:if(Qempty()) printf(&#x2015;n Queue is empty&#x2016;); else display(); break; default:printf(&#x2015;n Wrong choice&#x2016;); break; }
• 16. printf(&#x2015;n Do You Want TO continue?&#x2016;); ans =getche(); }while(ans==&#x2018;Y&#x2018; ||ans==&#x2018;y&#x2018;); getch();}/***************** End Of the program********************/