SlideShare a Scribd company logo
1) ‘C’ program
/*************************************************************
         Program for implementing the Queue using arrays

*************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define size 5
struct 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
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!‖);
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 basic
operations such as insertion,deletion of the elements by taking
care of queue Full and queue Empty conditions.The appropriate
display is for displaying the queue
*************************************************************/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define size 5
int 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;
}
/*
  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()
{
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 ***********************/
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 function
Purpose:This function initialises all the front and rear positions of
individual queues.All thses queues has to      be in a single dimensional
array
Input:index of the queue
Output:none,it simply initializes the locations of front and rear
Called 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)
{
 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]++;
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
  {
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;
/* 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;
}
/* 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]);
}
/* The main function
  Input:none
  Output:none
  Called By:O.S.

Calls:Qfull,Qempty,insert_rear,Insert_front,delete_front,delete_rear,displ
ay
*/
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 by
frontn4.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);
}
               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 5

int 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()
{    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);
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;
         }
printf(―n Do You Want TO continue?‖);
        ans =getche();
  }while(ans==‘Y‘ ||ans==‘y‘);
 getch();
}
/***************** End Of the program********************/

More Related Content

PPTX
Stack using Linked List
PPTX
Stack using Array
PDF
VTU Data Structures Lab Manual
PDF
Data structures lab manual
PDF
C programs
PPT
Unit2 C
PPTX
C Programming Language Part 7
Stack using Linked List
Stack using Array
VTU Data Structures Lab Manual
Data structures lab manual
C programs
Unit2 C
C Programming Language Part 7

What's hot (20)

PPTX
C Programming Language Part 6
PDF
Data structure lab manual
DOCX
DataStructures notes
DOC
Ds lab manual by s.k.rath
PPTX
Circular linked list
DOCX
Data Structures Using C Practical File
PPTX
Single linked list
PPTX
Double linked list
DOC
Daa practicals
PPSX
C programming function
PDF
05 queues
PPTX
C Programming Language Step by Step Part 2
PDF
Datastructures asignment
PPTX
Double linked list
PPT
Fucntions & Pointers in C
PPT
Queue implementation
PPSX
C programming array & shorting
PPTX
Function (rule in programming)
PPT
03 stacks and_queues_using_arrays
DOCX
C Programming Language Part 6
Data structure lab manual
DataStructures notes
Ds lab manual by s.k.rath
Circular linked list
Data Structures Using C Practical File
Single linked list
Double linked list
Daa practicals
C programming function
05 queues
C Programming Language Step by Step Part 2
Datastructures asignment
Double linked list
Fucntions & Pointers in C
Queue implementation
C programming array & shorting
Function (rule in programming)
03 stacks and_queues_using_arrays
Ad

Viewers also liked (9)

PDF
Telecamere a1
PPT
Presentatie Brabantse beleidsmedewerkers VVE 18-10-2011
PDF
Helgeland Sparebank regnskapspresentasjon 3. kvartal 2011
PDF
09 04-28 renstra-ditjenanggaran05-09
PDF
Diseño interior tienda de ropa Lepreg
PPTX
Leticia e alessandro 2 d
PDF
Osteocardix: Брошура на продукта
PPS
First World
PDF
Newcom research & consultancy Vertrouwensindex 2011
Telecamere a1
Presentatie Brabantse beleidsmedewerkers VVE 18-10-2011
Helgeland Sparebank regnskapspresentasjon 3. kvartal 2011
09 04-28 renstra-ditjenanggaran05-09
Diseño interior tienda de ropa Lepreg
Leticia e alessandro 2 d
Osteocardix: Брошура на продукта
First World
Newcom research & consultancy Vertrouwensindex 2011
Ad

Similar to Qprgs (20)

DOCX
cs3381-object oriented programming-ab-manual
PDF
VTU DSA Lab Manual
PDF
DATA STRUCTURE USING C & C++
PDF
DSU C&C++ Practical File Diploma
PPT
Algo>Queues
DOCX
Stack prgs
PDF
Data Structure using C
PPTX
Queue oop
DOC
Basic c programs updated on 31.8.2020
DOCX
#include fstream#include iostream#include cstdlib#includ.docx
DOCX
Data structure and algorithm lab spiral (1) (4) (1).docx
PPTX
Array menu
PPTX
Queue(lecture8).pptx
PDF
in this assignment you are asked to write a simple driver program an.pdf
PDF
C++ Searching & Sorting5. Sort the following list using the select.pdf
PDF
Program of sorting using shell sort #include stdio.h #de.pdf
DOCX
object oriented programming lab manual .docx
PDF
Can you give an example of a binary heap programCan you give an .pdf
PPTX
The presention is about the queue data structure
cs3381-object oriented programming-ab-manual
VTU DSA Lab Manual
DATA STRUCTURE USING C & C++
DSU C&C++ Practical File Diploma
Algo>Queues
Stack prgs
Data Structure using C
Queue oop
Basic c programs updated on 31.8.2020
#include fstream#include iostream#include cstdlib#includ.docx
Data structure and algorithm lab spiral (1) (4) (1).docx
Array menu
Queue(lecture8).pptx
in this assignment you are asked to write a simple driver program an.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdf
Program of sorting using shell sort #include stdio.h #de.pdf
object oriented programming lab manual .docx
Can you give an example of a binary heap programCan you give an .pdf
The presention is about the queue data structure

More from Ssankett Negi (9)

PDF
Binary tree
PDF
Multi way&btree
PPTX
Heapsort
PDF
Binary trees
PDF
Threaded binarytree&heapsort
PPTX
U3.stack queue
PPTX
Recursion
PPT
Circular queues
PPTX
U2.linked list
Binary tree
Multi way&btree
Heapsort
Binary trees
Threaded binarytree&heapsort
U3.stack queue
Recursion
Circular queues
U2.linked list

Recently uploaded (20)

PDF
Zenith AI: Advanced Artificial Intelligence
PPTX
O2C Customer Invoices to Receipt V15A.pptx
PDF
2021 HotChips TSMC Packaging Technologies for Chiplets and 3D_0819 publish_pu...
PPTX
Tartificialntelligence_presentation.pptx
PDF
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
PDF
Hybrid model detection and classification of lung cancer
PPTX
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
PDF
NewMind AI Weekly Chronicles – August ’25 Week III
PDF
Developing a website for English-speaking practice to English as a foreign la...
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
PDF
Hindi spoken digit analysis for native and non-native speakers
PDF
Getting started with AI Agents and Multi-Agent Systems
PDF
A contest of sentiment analysis: k-nearest neighbor versus neural network
PDF
WOOl fibre morphology and structure.pdf for textiles
PPTX
TLE Review Electricity (Electricity).pptx
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
1. Introduction to Computer Programming.pptx
PDF
A novel scalable deep ensemble learning framework for big data classification...
Zenith AI: Advanced Artificial Intelligence
O2C Customer Invoices to Receipt V15A.pptx
2021 HotChips TSMC Packaging Technologies for Chiplets and 3D_0819 publish_pu...
Tartificialntelligence_presentation.pptx
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
Hybrid model detection and classification of lung cancer
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
NewMind AI Weekly Chronicles – August ’25 Week III
Developing a website for English-speaking practice to English as a foreign la...
NewMind AI Weekly Chronicles - August'25-Week II
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
Hindi spoken digit analysis for native and non-native speakers
Getting started with AI Agents and Multi-Agent Systems
A contest of sentiment analysis: k-nearest neighbor versus neural network
WOOl fibre morphology and structure.pdf for textiles
TLE Review Electricity (Electricity).pptx
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Programs and apps: productivity, graphics, security and other tools
1. Introduction to Computer Programming.pptx
A novel scalable deep ensemble learning framework for big data classification...

Qprgs

  • 1. 1) ‘C’ program /************************************************************* Program for implementing the Queue using arrays *************************************************************/ #include<stdio.h> #include<stdlib.h> #include<conio.h> #define size 5 struct 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. 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. 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 basic operations such as insertion,deletion of the elements by taking care of queue Full and queue Empty conditions.The appropriate display is for displaying the queue *************************************************************/ #include<stdio.h> #include<conio.h> #include<stdlib.h> #define size 5 int 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. /* 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. 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. 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 function Purpose:This function initialises all the front and rear positions of individual queues.All thses queues has to be in a single dimensional array Input:index of the queue Output:none,it simply initializes the locations of front and rear Called 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. { 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. 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. 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. /* 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. } /* 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. } /* The main function Input:none Output:none Called By:O.S. Calls:Qfull,Qempty,insert_rear,Insert_front,delete_front,delete_rear,displ ay */ 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 by frontn4.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. } 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 5 int 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(―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. 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. printf(―n Do You Want TO continue?‖); ans =getche(); }while(ans==‘Y‘ ||ans==‘y‘); getch(); } /***************** End Of the program********************/