0% found this document useful (0 votes)
45 views8 pages

Array Implementation of Queue Adt

The document describes how to implement a queue data structure using both arrays and linked lists in C. It provides algorithms and full code examples to perform queue operations like insert, delete, and display on each implementation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views8 pages

Array Implementation of Queue Adt

The document describes how to implement a queue data structure using both arrays and linked lists in C. It provides algorithms and full code examples to perform queue operations like insert, delete, and display on each implementation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

ARRAY IMPLEMENTATION OF QUEUE ADT

AIM:

To write a C program to implement Queue ADT by using arrays

ALGORITHM:
1. Create a Queue[ ] with MAX size as your wish.
2. Write function for all the basic operations of stack - Enqueue(), Dequeue() and
Display().
3. By using Switch case, select Enqueue() operation to insert element in to the
rear/back end of the queue.

4. Check whether queue is FULL. (rear == SIZE-1)


5. If it is FULL, then display "Queue is FULL!!! Insertion is not possible!!!" and
terminate the function.
6. If it is NOT FULL, then increment rear value by one (rear++) and set
queue[rear] = value
7. Similarly, by using Switch case, select Dequeue() function is used to remove
the element from the front end of the queue.
Check whether queue is EMPTY. (front == rear)

If it is EMPTY, then display "Queue is EMPTY!!! Deletion is not


possible!!!" and terminate the function.

Step 3: If it is NOT EMPTY, then increment the front value by one


(front ++). Then display queue[front] as deleted element. Then check
whether both front and rear are equal (front == rear), if it TRUE, then
set both front and rear to '-1' (front = rear = -1).

8. Similarly, by using Switch case, select display() operation to display all


element of the queue.
Step 1: Check whether queue is EMPTY. (front == rear)
Step 2: If it is EMPTY, then display "Queue is EMPTY!!!" and terminate the
function.
Step 3: If it is NOT EMPTY, then define an integer variable 'i' and set 'i =
front+1'.

Step 3: Display 'queue[i]' value and increment 'i' value by one (i++).
Repeat the same until 'i' value is equal to rear (i <= rear)
9. Close the program
PROGRAM:

#include<stdio.h>
#include<stdlib.h>
#define maxsize 5
void insert();
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\n
4.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("\nEnter 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()
{
int item;
if (front == -1 || front > rear)
{
printf("\nUNDERFLOW\n");
return;

}
else
{
item = queue[front];
if(front == rear)
{
front = -1;
rear = -1 ;
}
else
{
front = front + 1;
}
printf("\nvalue deleted ");
}

void display()
{
int i;
if(rear == -1)
{
printf("\nEmpty queue\n");
}
else
{ printf("\nprinting values .....\n");
for(i=front;i<=rear;i++)
{
printf("\n%d\n",queue[i]);
}
}
}

Result:
Thus the C program for implementation of queue using array is executed successfully
Outcome:
Thus the student can develop a program to implement Linear Data Structure–Queue
by using array
Applications:
1. Queue is used by Operating systems for job scheduling.
2. Queue is used in networking to handle congestion.
LINKED LIST IMPLEMENTATION OF QUEUE ADT

AIM

To write a C program to implement Queue ADT by using linked list

ALGORITHM

1. Include all the header files which are used in the program. And declare all the
user defined functions.
2. Define a 'Node' structure with two members data and next.
3. Define two Node pointers 'front' and 'rear' and set both to NULL.
4. Implement the main method by displaying Menu of list of operations and
make suitable function calls in the main method to perform user selected
operation.
5. enqueue(value) - Inserting an element into the queue

 Create a newNode with given value and set 'newNode → next' to


NULL.
 Check whether queue is Empty (rear == NULL)
 If it is Empty then, set front = newNode and rear = newNode.
 If it is Not Empty then, set rear → next = newNode and rear =
newNode.
6. dequeue() - Deleting an Element from queue

 Check whether queue is Empty (front == NULL).


 If it is Empty, then display "Queue is Empty!!! Deletion is not
possible!!!" and terminate from the function
 If it is Not Empty then, define a Node pointer 'temp' and set it to 'front'.
 Then set 'front = front → next' and delete 'temp' (free(temp)).
7. display() - Displaying queue of elements

 Check whether queue is Empty (front == NULL).


 If it is Empty then, display 'Queue is Empty!!!' and terminate the
function.
 If it is Not Empty then, define a Node pointer 'temp' and initialize with
front.
 Display 'temp → data --->' and move it to the next node. Repeat
the same until 'temp' reaches to 'rear' (temp → next != NULL).
 Finally! Display 'temp → data ---> NULL'.
PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct Node
{
int Data;
struct Node* next;
}*rear, *front;

void delQueue()
{
struct Node *temp, *var=rear;
if(var==rear)
{
rear = rear->next;
free(var);
}
else
printf("\nQueue Empty");
}

void push(int value)


{
struct Node *temp;
temp=(struct Node *)malloc(sizeof(struct Node));
temp->Data=value;
if (front == NULL)
{
front=temp;
front->next=NULL;
rear=front;
}
else
{
front->next=temp;
front=temp;
front->next=NULL;
}
}
void display()
{
struct Node *var=rear;
if(var!=NULL)
{
printf("\nElements are as: ");
while(var!=NULL)
{
printf("\t%d",var->Data);
var=var->next;
}
printf("\n");
}
else
printf("\nQueue is Empty");
}

int main()
{
int i=0;
front=NULL;
printf(" \n1. Push to Queue");
printf(" \n2. Pop from Queue");
printf(" \n3. Display Data of Queue");
printf(" \n4. Exit\n");
while(1)
{
printf(" \nChoose Option: ");
scanf("%d",&i);
switch(i)
{
case 1:
{
int value;
printf("\nEnter a valueber to push into Queue: ");
scanf("%d",&value);
push(value);
display();
break;
}
case 2:
{
delQueue();
display();
break;
}
case 3:
{
display();
break;
}
case 4:
{
exit(0);
}
default:
{
printf("\n wrong choice for operation");
}
}
}
}

Result:
Thus the C program to implement Queue ADT by using linked list is executed
successfully and the output is verified.

Outcome:
Thus the student can develop a program to implement Linear Data Structure–Queue by using
linked list

Applications:
CPU scheduling algorithms are implemented by using queue.

You might also like