Upcoming SlideShare
×

# Qprgs

446 views

Published on

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
446
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
5
0
Likes
1
Embeds 0
No embeds

No notes for slide

### Qprgs

1. 1. 1) ‘C’ program/************************************************************* Program for implementing the Queue using arrays*************************************************************/#include<stdio.h>#include<stdlib.h>#include<conio.h>#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 >=size–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 == –1) Q.front++; Q.que[++Q.rear] = item; return Q.rear;}int Qempty(){ if((Q.front == – 1) || (Q.front > Q.rear)) return 1; else return 0;}/* The delete Function Input:none Output:front value Called By:main
2. 2. Calls:none*/int delete(){ int item; item = Q.que[Q.front]; Q.front++; printf(―n The deleted item is %d‖,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<=Q.rear;i++) printf(― %d‖,Q.que[i]);}void main(void){ int choice,item; char ans; clrscr(); Q.front = –1; Q.rear = –1; do { printf(―n Main Menu‖); printf(―n1.Insertn2.Deleten3.Display‖); printf(―n Enter Your Choice‖); scanf(―%d‖,&choice); switch(choice) { case 1:if(Qfull()) //checking for Queue overflow printf(―n Can not insert the element‖); else { printf(―n Enter The number to be inserted‖); scanf(―%d‖,&item); insert(item); } break; case 2:if(Qempty()) printf(―n Queue Underflow!!‖); else delete(); break; case 3:if(Qempty()) printf(―nQueue Is Empty!‖);
3. 3. else display(); break; default:printf(―n Wrong choice!‖); break; } printf(―n Do You Want to continue?‖); ans =getche(); }while(ans ==‘Y‘||ans ==‘y‘);}/********************** End Of Program ***********************/2) ‘C’ 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<stdio.h>#include<conio.h>#include<stdlib.h>#define size 5int queue[size];int front=–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 ==–1) return 1; else return 0;}
4. 4. /* The add Function Input:the element to be inserted Output:none Called By:main Calls:qFull*/void add(int Item){ if(qFull()) printf(―n The circular Queue is full!‖); else { if(front ==–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(―n Queue Is Empty!‖); else { Item=queue[front]; if(front ==rear) { front=rear=–1; } else front =(front+1)%size; printf(―n The deleted item is %d‖,Item); } }/* The display Function Input: none Output: prints the contents of the queue Called By:main Calls:qEmpty*/void display(){
5. 5. int i; if(qEmpty()) { printf(―nThe Queue Is Empty‖); return; } i=front; while(i!=rear) { printf(― %d‖,queue[i]); i=(i+1)%size; } printf(― %d‖,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(―n Main Menu‖); printf(―n 1.Insertn2.Deleten3.Display‖); printf(―n Enter Your Choice‖); scanf(―%d‖,&choice); switch(choice) { case 1:printf(―nEnter The element‖); scanf(―%d‖,&Item); add(Item); break; case 2:delete(); break; case 3:display(); break; default:exit(0); } printf(―n Do u want to continue?‖); ans =getch(); }while(anvb ==‘Y‘||ans == ‗y‘); getch();}/************************ End Of Main ***********************/
6. 6. 3)C Program/*************************************************************Program for implementing the Queue using one dimensional array*************************************************************/#include<stdio.h>#include<stdlib.h>#include<conio.h>#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<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. 7. { int sum,i; sum=0; for(i=0;i<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]>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. 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(―n The Queue %d is Empty‖,index); else { printf(―n Queue number %d is:‖,index); for(i=Q.front[index];i<=Q.rear[index];i++) { if(Q.que[i]!=-1) printf(― %d‖,Q.que[i]); } } index++; }while(index<=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(―ntt Program For Multiple Queues‖); printf(―n How many Queues do you want?‖); scanf(―%d‖,&num); printf(―n Enter The size of queue(Combined size of all Queues is 20)‖); for(i=0;i<num;i++) scanf(―%d‖,&sz[i]); for(index=1;index<=num;index++) set_que(index);/*initialized front and rear of each queue*/ do {
9. 9. printf(―n Main Menu‖); printf(―n1.Insertn2.Deleten3.Display‖); printf(―n Enter Your Choice‖); scanf(―%d‖,&choice); switch(choice) { case 1: printf(―n Enter in which queue you wish to insert the item?‖); scanf(―%d‖,&index); if(Qfull(index)) /*checking for Queue overflow*/ printf(―n Can not insert the elemnt‖); else { printf(―n Enter The number to be inseted‖); scanf(―%d‖,&item); insert(item,index); } break; case 2: printf(―n Enter From which queue you wish to delete the item?‖); scanf(―%d‖,&index); if(Qempty(index)) printf(―n Queue Underflow!!‖); else { item= delet(index); printf(―n The deleted item is %d‖,item); } break; case 3:display(num); break; default:printf(―n Wrong choice!‖); break; } printf(―n Do You Want to continue?‖); ans =getche(); }while(ans ==‘Y‘||ans ==‘y‘);}4)C Program/************************************************************* Program To implement Doubly ended queue using arrays*************************************************************/#include<stdio.h>#include<conio.h>#include<stdlib.h>#define size 5/*Data structure for deque*/struct queue { int que[size]; int front,rear; }Q;
10. 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>Q.rear)||(Q.front==-1&&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&&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. 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>=0) { Q.que[i+1]=Q.que[i]; i—; } j=Q.rear; while(j>=Q.front) { Q.que[j+1]=Q.que[j]; j—; } 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—; 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(―n Straight Queue is:‖); for(i=Q.front;i<=Q.rear;i++) printf(― %d‖,Q.que[i]);
12. 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=‘y‘; Q.front=-1; Q.rear=-1; for(i=0;i<size;i++) Q.que[i]=-1; clrscr(); printf(―ntt Program For doubly ended queue using arrays‖); do { printf(―n1.insert by rearn2.delete by frontn3.insert byfrontn4.delete by rear‖); printf(―n5.displayn6.exit‖); printf(―n Enter Your choice‖); scanf(―%d‖,&choice); switch(choice) { case 1:if(Qfull()) printf(―n Doubly ended Queue is full‖); else { printf(―n Enter The item to be inserted‖); scanf(―%d‖,&item); Q.rear=insert_rear(item); } break; case 2:if(Qempty()) printf(―n Doubly ended Queue is Empty‖); else { item=delete_front(); printf(―n The item deleted from queue is%d‖,item); } break; case 3:if(Qfull()) printf(―n Doubly ended Queue is full‖); else { printf(―n Enter The item to be inserted‖); scanf(―%d‖,&item); Q.front=insert_front(item);
13. 13. } break; case 4:if(Qempty()) printf(―n Doubly ended Queue is Empty‖); else { item=delete_rear(); printf(―n The item deleted from queue is%d‖,item); } break; case 5:display(); break; case 6:exit(0); } printf(―n Do You Want To Continue?‖); ans=getch(); }while(ans==‘y‘||ans==‘Y‘); getch();}5)‘C’ program/************************************************************Program for implementing the ascending priority Queue ************************************************************//*Header Files*/#include<stdio.h>#include<conio.h>#include<process.h>#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. 14. { int item; void priority(); printf(―nEnter the elementn‖); scanf(―%d‖,&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<=rear-1;i++) for(j=front;j<rear;j++) if(que[j]>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>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(―n The item deleted is %d‖,item);
15. 15. front++;}/* The display function Input:none Output:none Called By:main Calls:none*/void display(){ int i; printf(―n The queue is:‖); for(i=front;i<=rear;i++) printf(― %d‖,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(―ntt Priority Queuen‖); do { printf(―n Main Menu‖); printf(―n1.Insertn2.Deleten3.Display‖); printf(―n Enter Your Choice‖); scanf(―%d‖,&choice); switch(choice) { case 1:if(Qfull()) printf(―n Queue IS full‖); else insert(); break; case 2:if(Qempty()) printf(―n Cannot delete element‖); else delet(); break; case 3:if(Qempty()) printf(―n Queue is empty‖); else display(); break; default:printf(―n Wrong choice‖); break; }
16. 16. printf(―n Do You Want TO continue?‖); ans =getche(); }while(ans==‘Y‘ ||ans==‘y‘); getch();}/***************** End Of the program********************/