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

QUEUE (ADT - Abstract Data Type)

Queue is a First-In-First-Out (FIFO) data structure. Elements can be added to the rear of the queue and removed from the front of the queue. There are two common implementations - using a linked list by adding nodes to the rear and removing from the front, and using a static-size array where a front and rear pointer track the first and last elements. The document provides code examples for enqueue and dequeue operations for both linked list and array implementations of a queue data structure.

Uploaded by

타나카 조
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

QUEUE (ADT - Abstract Data Type)

Queue is a First-In-First-Out (FIFO) data structure. Elements can be added to the rear of the queue and removed from the front of the queue. There are two common implementations - using a linked list by adding nodes to the rear and removing from the front, and using a static-size array where a front and rear pointer track the first and last elements. The document provides code examples for enqueue and dequeue operations for both linked list and array implementations of a queue data structure.

Uploaded by

타나카 조
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

QUEUE (ADT – Abstract Data Type)

FIFO METHOD
First-In-First-Out Structure

Front/Head -> any removal must happen from front/head


Tail/Rear -> an insertion must happen from one end that we call rear or tail of queue.

Interface Operation

Enqueue Operation – insert an element at tail/rear of queue.


Dequeue Operation – remove an element from front or head of queue.

LINKED LIST IMPLEMENTATION

struct Node{
Int data;
struct Node* next;
};

struct Node* front= NULL;


struct Node* rear= NULL;

void enqueue(int x){


struct Node* temp=
(struct Node*)malloc(sizeof(struct Node*));
temp -> data = x;
temp -> next = NULL;
if (front == NULL && rear == NULL){
front = rear = temp;
return;
}
rear -> next = temp;
rear = temp;
}

void Dequeue ()
{
struct Node* temp = front;
If (front == NULL ) return;
If (front == rear ) {
Front = rear = NULL;
}
else {
front = front ->next;
}
free temp;
}
ARRAY IMPLEMENTATION

#include <stdio.h>

#define MAX 50

void insert();
void delete();
void display();
int queue_array[MAX];
int rear = - 1;
int front = - 1;
main()
{
int choice;
while (1)
{
printf("1.Insert element to queue \n");
printf("2.Delete element from queue \n");
printf("3.Display all elements of queue \n");
printf("4.Quit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice \n");
} /* End of switch */
} /* End of while */
} /* End of main() */

void insert()
{
int add_item;
if (rear == MAX - 1)
printf("Queue Overflow \n");
else
{
if (front == - 1)
/*If queue is initially empty */
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
} /* End of insert() */

void delete()
{
if (front == - 1 || front > rear)
{
printf("Queue Underflow \n");
return ;
}
else
{
printf("Element deleted from queue is : %d\n", queue_array[front]);
front = front + 1;
}
} /* End of delete() */

void display()
{
int i;
if (front == - 1)
printf("Queue is empty \n");
else
{
printf("Queue is : \n");
for (i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("\n");
}
}

You might also like