Algorithm Queue
Algorithm Queue
ALGORITHM &
COMPLEXITY-I
QUEUE
Array #define maxsize 5
Representation void insert();
of a Queue void delete();
void display();
int front = -1, rear = -1;
int queue[maxsize];
void main ()
{
int choice;
while(choice != 4)
{
printf("\n1.insert an element
\n2.Delete an element
\n3.Display the queue
\n4.Exit\n");
printf("\nEnter your choice ?");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nEnter valid choice??\n");
}
} }
void insert()
{
int item;
printf("Enter the element\n");
scanf("\n%d",&item);
if(rear == maxsize-1)
{
printf("\nOVERFLOW\n");
return;
}
if(front == -1 && rear == -1)
{
front = 0;
rear = 0;
}
else
{
rear = rear+1;
}
queue[rear] = item;
printf("\nValue inserted ");
}
void delete() if(front == rear)
{
{
front = -1;
int item;
rear = -1 ;
if (front == -1 || front > rear) }
{ else
printf("\nUNDERFLOW\n"); {
front = front + 1;
return;
}
}
printf("\nvalue deleted ");
else
}
{
item = queue[front]; }
void display()
{
int i;
if(rear == -1)
{
printf("\nEmpty queue\n");
}
Display else
{ printf("\nprinting values .....\n");
for(i=front;i<=rear;i++)
{
printf("\n%d\n",queue[i]);
}
}
}
Linked List
Implementation
of Queue
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front;
struct node *rear;
void insert();
void delete();
void display();
void main () switch(choice)
{ {
int choice; case 1:
while(choice != 4) insert();
{ break;
printf("\n1.insert an element case 2:
\n2.Delete an element delete();
\n3.Display the queue break;
\n4.Exit\n"); case 3:
printf("\nEnter your choice ?"); display();
scanf("%d",& choice); break;
case 4:
exit(0);
break;
default:
printf("\nEnter valid choice??\n");
}
} }
void insert() ptr -> data = item;
{ if(front == NULL)
struct node *ptr; {
int item; front = ptr;
rear = ptr;
ptr = (struct node *) malloc front -> next = NULL;
(sizeof(struct node)); rear -> next = NULL;
if(ptr == NULL) }
{ else
printf("\nOVERFLOW\n"); {
return; rear -> next = ptr;
} rear = ptr;
else rear->next = NULL;
{ }
printf("\nEnter value?\n"); }
scanf("%d",&item); }
void delete ()
{
struct node *ptr;
if(front == NULL)
{
printf("\nUNDERFLOW\n");
return;
Delete }
else
{
ptr = front;
front = front -> next;
free(ptr);
}
}
void display()
{
struct node *ptr;
ptr = front;
if(front == NULL)
{
printf("\nEmpty queue\n");
Display }
else
{ printf("\nprinting values .....\n");
while(ptr != NULL)
{
printf("\n%d\n",ptr -> data);
ptr = ptr -> next;
}
}
}
ACTIVITY 1
Due to COVID-19, most of the restaurants came to the solution
of food package take away. It is very difficult for the restaurant
people to manage both the third party delivery