0% found this document useful (0 votes)
11 views28 pages

Queus&Linked Lists

great
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)
11 views28 pages

Queus&Linked Lists

great
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

DATA STRUCTURES & COMPUTER ALGORITHMS

QUEUES & LINKED LISTS

1
QUEUES
Definition of Queue
• Queue Operations
o Insertion (Enqueue)
o Removing (Dequeue)
• Applications of the Queues

DEFINITION OF QUEUE
A queue is an ordered collection of
items from which items may be deleted
at one end (front of the queue) and into
which items may be inserted at the
other end (rear of the queue).
The first element inserted into the queue is
the first element to be removed. For this
reason a queue is sometimes called a fifo
(first-in first-out) list as opposed to the
stack, which is a lifo (last-in first-out).
2
QUEUE OPERATIONS

Initialize the queue


Insert to the rear of the queue (also called as Enqueue)
Remove (Delete) from the front of the queue (also called as Dequeue)
Is the Queue Empty
Is the Queue Full
What is the size of the Queue

INITIALIZE THE QUEUE (USING LINEAR ARRAY)


The queue is initialized by having the rear set to -1, and front set to 0. Let us assume that
maximum number of the element we have in a queue is 5 elements as shown below.

INSERT / REMOVE ITEMS


Insert A, B, C to the rear of the queue.

Remove two items from the front of the queue.


Insert D, E, to the rear of the queue.

9
What happens if we want to insert a new item F into the queue?
Although there is some empty space, the queue is full. One of the methods to
overcome this problem is to consider a circular array, where the next item
position after position 4 in the above queue is position 0.

1
4
Declaration and Initialization of a Queue.

#define MAXQUEUE 10 /* size of the queue items*/ struct queue{


int front;
int rear;
int items[MAXQUEUE];
};
struct queue q; q.front = MAXQUEUE-1; q.rear=
MAXQUEUE-1;

Remove Operation
int remove(struct queue *qptr)
{
if(qptr->front == qptr->rear){ printf("Queue underflow"); exit(1);
}
if(qptr->front == MAXQUEUE-1) qptr->front=0;
else
qptr->front++;
return qptr->items[qptr->front];
}

Insert Operation

void insert(struct queue *qptr, int x)


{
if(qptr->rear == MAXQUEUE-1) qptr->rear=0;
else
qptr->rear++;

if(qptr->rear == qptr->front){ printf("Queue overflow"); exit(1);


qptr->items[qptr->rear]=x;
1 }
5
Enqueue (ItemType newItem)

Function: Adds newItem to the rear of the queue.


Preconditions: Queue has been initialized and is not full.
Postconditions: newItem is at rear of queue.

Dequeue (ItemType& item)

Function: Removes front item from queue and returns it in item.


Preconditions: Queue has been initialized and is not empty.
Postconditions: Front element has been removed from queue and
item is a copy of removed element.
Queue underflow
The condition resulting from trying to remove an element from an
empty queue.
if(!q.IsEmpty())
q.Dequeue(item);
Queue overflow
The condition resulting from trying to add an element onto a
full queue.

if(!q.IsFull())
q.Enqueue(item);
Application of Queue

1. Queue is useful in CPU scheduling, Disk Scheduling.


2. When data is transferred asynchronously between two
processes. Queue is used for synchronization. Examples : IO
Buffers, pipes, file IO, etc.
3. In real life, Call Center phone systems will use Queues, to hold
people calling them in an order, until a service representative
is free.
Review of Linear Data Structure cont’d

Linked Lists
Outlines

Definition of Linked Lists


Inserting and Removing Nodes from a List
Linked lists using Dynamic Variables
Allocating and Freeing Dynamic Variables
Primitive Functions for Linked Lists using Dynamic Variables
Fundamental functions and applications of Linked Lists
o Search and Display List Operations

DEFINITION OF LINKED LISTS

A linked list is a linear collection of data elements, called nodes, where the linear order is
given by means of pointers.
Each node is divided into two parts:
1. The first part contains the information of the element and
2. The second part contains the address of the next node (link /next
pointer field) in the list.
1
9
2 Advanced Computer Algorithm
0
Applications of Linked list

I. Implement stack and queues


II. Represents trees and graphs
III. Keep track of blocks of memory allocated to a file
IV. Web browser navigation.
V. playing next song in the music player

You might also like