0% found this document useful (0 votes)
9 views4 pages

Exp 4

The document outlines a laboratory experiment for second-year engineering students on implementing a linear queue ADT using an array in C. It details the theory behind queue operations such as enqueue, dequeue, and display, along with the corresponding algorithms and program code. The practical session includes a menu-driven program to insert and delete elements from the queue, demonstrating the queue's functionality and conditions for overflow and underflow.
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)
9 views4 pages

Exp 4

The document outlines a laboratory experiment for second-year engineering students on implementing a linear queue ADT using an array in C. It details the theory behind queue operations such as enqueue, dequeue, and display, along with the corresponding algorithms and program code. The practical session includes a menu-driven program to insert and delete elements from the queue, demonstrating the queue's functionality and conditions for overflow and underflow.
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/ 4

Programmed: Undergraduate

Department / Course: Second Year Engineering

Laboratory: Data Structures Lab

Subject: Data Structures

Year: 2022-23 Semester: III

Practical per week: 2 Hrs. Credit assigned: 02


Resources / Hardware:
Software: Online
Apparatus Required Computer System
GDB Complier

Student Roll No.: Student Name:

Experiment No 4: Implement Linear Queue ADT using array.


AIM: Write a C Program to implement linear queue adt using array.

THEORY:

In queue, insertion and deletion happen at the opposite ends, so implementation is not as
simple as stack.
To implement a queue using array, create an array arr of size n and take two
variables front and rear both of which will be initialized to 0 which means the queue is
currently empty. Element rear is the index up to which the elements are stored in the array
and front is the index of the first element of the array. Now, some of the implementation of
queue operations are as follows:

1. Enqueue: Addition of an element to the queue. Adding an element will be performed


after checking whether the queue is full or not. If rear < n which indicates that the
array is not full then store the element at arr[rear] and increment rear by 1 but if rear
== n then it is said to be an Overflow condition as the array is full.

2. Dequeue: Removal of an element from the queue. An element can only be deleted
when there is at least an element to delete i.e., rear > 0. Now, element
at arr[front] can be deleted but all the remaining elements must shift to the left by one
position for the dequeue operation to delete the second element from the left on
another dequeue operation.

3. Front: Get the front element from the queue i.e., arr[front] if queue is not empty.

4. Display: Print all element of the queue. If the queue is non-empty, traverse and print
all the elements from index front to rear.

Algorithm:
enQueue(value) - Inserting value into the queue
In a queue data structure, enQueue () is a function used to insert a new element into the
queue. In a queue, the new element is always inserted at rear position. The enQueue ()
function takes one integer value as a parameter and inserts that value into the queue. We can
use the following steps to insert an element into the queue-

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


● Step 2 - If it is FULL, then display "Queue is FULL!!! Insertion is not
possible!!!" and terminate the function.
● Step 3 - If it is NOT FULL, then increment rear value by one (rear++) and
set queue[rear] = value.

deQueue () - Deleting a value from the Queue


In a queue data structure, deQueue () is a function used to delete an element from the queue.
In a queue, the element is always deleted from front position. The deQueue () function does
not take any value as parameter. We can use the following steps to delete an element from the
queue...

● Step 1 - Check whether queue is EMPTY. (front == rear)


● Step 2 - 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).

display() - Displays the elements of a Queue


We can use the following steps to display the elements of a 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 4 - Display 'queue[i]' value and increment 'i' value by one (i++). Repeat the
same until 'i' value reaches to rear (i <= rear)

Program Code:
#include<stdio.h>
#include<conio.h>

struct queue
{
int a[5];
int front;
int rear;

};

void insert(struct queue*,int);


int empty(struct queue*);
int full(struct queue*);
int del(struct queue*);

int main()
{
struct queue s;
int c,v;
s.front=s.rear=-1;

do
{
printf("\n\n-------Menu-------\n1.Insert\n2.Delete\n3.Exit\nPlease select an operation :");
scanf("%d",&c);
switch(c)
{
case 1:
if(full(&s))
printf("Queue is Full.");
else
{
printf("Enter an element to be inserted :");
scanf("%d",&v);
insert(&s,v);

}
break;

case 2:
if(empty(&s))
printf("Queue is Empty.");
else
{
v=del(&s);
printf("Element Deleted : %d",v);

}
break;

default :
printf("Please select a proper Operation:");
break;

}while(c!=3);

getch();
return 0;
}

void insert(struct queue*z,int v)


{
(z->rear)++;
z->a[z->rear]=v;
if(z->front==-1)
z->front=0;

int del(struct queue*z)


{
int d;
d=z->a[z->front];
if(z->front==z->rear)
z->front=z->rear=-1;
else
(z->front)++;
return(d);

int full(struct queue*z)


{
return(z->rear==4);
}

int empty(struct queue*z)


{
return(z->front==-1);
}

Output :

You might also like