0% found this document useful (0 votes)
45 views

Algorithm Queue

The document discusses queues as a data structure. It defines a queue as a first-in, first-out list that allows insertions at the rear and deletions at the front. Queues can be used to implement waiting lists and buffers. The document then describes array-based and linked list-based implementations of queues, including functions for insertion, deletion, and display. Code examples are provided for both implementations.

Uploaded by

AARON ALEX X
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Algorithm Queue

The document discusses queues as a data structure. It defines a queue as a first-in, first-out list that allows insertions at the rear and deletions at the front. Queues can be used to implement waiting lists and buffers. The document then describes array-based and linked list-based implementations of queues, including functions for insertion, deletion, and display. Code examples are provided for both implementations.

Uploaded by

AARON ALEX X
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

12/04/2022 1

ALGORITHM &
COMPLEXITY-I
QUEUE

Data Structures and Application


12/04/2022 2

ADT LINKED LISTS, STACKS, AND QUEUES


• Manipulation – Insertion, Deletion, Searching a Key,
Reversal of a List, use of recursion to Reverse/Search
MODULE 2 • Doubly Linked Lists and Circular Linked Lists
• Stacks and Queues as dynamic Data Structures
implemented using Linked Lists.

Data Structures and Application


12/04/2022 3

ADT LINKED LISTS,


STACKS, AND QUEUES
MODULE 2
(QUEUES)

Data Structures and Application


12/04/2022 4

Data Structures and Application


Queue
 A queue can be defined as an ordered list which enables
insert operations to be performed at one end called REAR
and delete operations to be performed at another end called
FRONT.
Introduction
 Queue is referred to be as First In First Out list.
Queue
Representatio
n
 Queues are widely used as waiting lists for a single
shared resource like printer, disk, CPU.
 Queues are used as buffers in most of the applications
like MP3 media player, CD player, etc.
Applications
 Queue are used to maintain the play list in media
players in order to add and remove the songs from the
play-list.
Array
Representation
of Queues
Queue-  https://visualgo.net/en/list
Visualization
#include<stdio.h>   
#include<stdlib.h>  

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

ACTIVITY 2 guys(Swiggy,Zomato etc) and the direct customers who need


take away as well. Hence, Suggest a suitable data structure to
provide the optimum solution to the restaurant so that the
waiting time for picking up the food will be minimal
THANK YOU

You might also like