Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Class notes

Class notes AIDS Stacks and Queue

Rating
-
Sold
-
Pages
27
Uploaded on
27-10-2023
Written in
2022/2023

This notes is all bout the queue data structure which is most important topic in Data Structures. The notes covers all the related topics to the Queue.

Institution
Course

Content preview

Data Structures



UNIT-II
Queues
Queue:
Queue is an ordered collection of items from which items may be deleted at one end
(called the front of the Queue) and into which items may be inserted at the other end (called
rear of the Queue).
The first inserted element into the Queue is the first deleted element. For this reason, a Queue is
sometimes called First-In-First-Out (FIFO).

Array representation of Queue:
Use an array to hold the elements of the Queue and to use two variables, front and rear to hold
the positions within the array of the first and last elements of the Queue.
#define max 10
struct queue
{
int items[max];
int front, rear;
};
struct queue q;
Initially q.front = 0; and q.rear = -1;
The queue is empty whenever q.front > q.rear.
No of elements in Queue = q.rear – q.front + 1;

Operations on Queue:

Empty operation;
int empty(struct queue *q)
{
if(q→rear < q→front)
return 1;
else
return 0;
}

Insert operation:
Using an array to hold a queue, check overflow condition.

void insert(struct queue *q, int a)
{
if(q→rear = = max-1)
printf(“\nQueue is overflow”);
else

K SRINIVAS DEPT OF IT SRKREC 1

,Data Structures


{
q→rear++;
q→items[q→rear] = a;
}
}

Remove operation:
Check empty condition.
int remove(struct queue *q)
{
int a;
if(q→rear < q→front)
return -1;
else
{
a = q→items[q→front];
q→front++;
return a;
}
}

Peek operation:
int peek(struct queue *qu)
{
if(qu->front>qu->rear)
return -1;
else
return(qu->items[qu->front]);
}

/*queue operations using Array program*/
/*queue operations using Array*/
#define max 10
#include<stdio.h>
struct queue
{
int front,rear;
int items[max];
};
void insert(struct queue *,int);
int del(struct queue *);
int empty(struct queue *);
int peek(struct queue *);

K SRINIVAS DEPT OF IT SRKREC 2

, Data Structures


void display(struct queue *);
main()
{
int choice,x;
struct queue q;
q.front=0;
q.rear=-1;
while(1)
{
printf("\n\n***************************\n\n");
printf("\t\tMENU\n");
printf("\n***************************\n");
printf("1.insert\n2.delete\n3.empty\n4.peek\n5.display\n6.exit\n");
printf("Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("\nEnter the element:");
scanf("%d",&x);
insert(&q,x);
printf("\nElements are:");
display(&q);
break;
case 2: x=del(&q);
if(x==-1)
printf("\nqueue is empty");
else
printf("\ndeleted element is %d",x);
printf("\nelements are:");
display(&q);
break;
case 3: x=empty(&q);
if(x==1)
printf("\nqueue is empty");
else
printf("\nqueue is not empty");
break;
case 4: x=peek(&q);
if(x==-1)
printf("\nQueue is empty");
else
printf("\nFront element is:%d",x);
break;

K SRINIVAS DEPT OF IT SRKREC 3

Written for

Institution
Course
Unknown

Document information

Uploaded on
October 27, 2023
Number of pages
27
Written in
2022/2023
Type
Class notes
Professor(s)
K srinivas
Contains
All classes

Subjects

$7.49
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
manikantasai811

Get to know the seller

Seller avatar
manikantasai811 Srkr Engineering College
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
2 year
Number of followers
0
Documents
2
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions