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

unit 2 data structure

The document provides an overview of queues and linked lists, explaining their structures, operations, and applications. It details the differences between queues and stacks, types of queues (linear, circular, and double-ended), and the implementation of linked lists (singly and doubly linked). Additionally, it outlines the advantages and disadvantages of queues, as well as algorithms for insertion and deletion in both queues and linked lists.

Uploaded by

divyashreer58
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

unit 2 data structure

The document provides an overview of queues and linked lists, explaining their structures, operations, and applications. It details the differences between queues and stacks, types of queues (linear, circular, and double-ended), and the implementation of linked lists (singly and doubly linked). Additionally, it outlines the advantages and disadvantages of queues, as well as algorithms for insertion and deletion in both queues and linked lists.

Uploaded by

divyashreer58
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

UNIT-2

QUEUE:
Queue is an ordered (linear) collection of items where an item is inserted at one end called the
rear and an existing item is removed at the other end called the front. Queue follows FIFO (First
in First out).
A queue is an ordered collection of items where the addition of new items and the removal of
existing items always take place at different ends.

Enque: insertion of item.


Dequeue: deletion of item.
The difference between stack and queues is in removing. In a stack we remove the item the most
recently added; in a queue, we remove the item the very recently added.
Applications of queue:
1. Ticket reservation counters
2. Bank token system
3. Hospital token system
4. Simulation
5. Various features of operating system
6. Multi programming platform systems
7. Different types of scheduling algorithms
8. Round robin algorithm
9. Printer pooling
10. Customer care

Operations on queue:
Queue(): create a new queue that is empty. It needs no parameters and returns an empty
queue
Enqueuer(item): adds a new item to the rear of the queue. It needs the item and returns
nothing. This operation is generally called as push
Dequeu(): removes the front item from the queue. It needs no parameters and returns the
item. The queue is modified. This operation is generally called as pop
Isempty(): test to see whether the queue is empty. It needs no parameters and returns a
Boolean balue.
Size(): returns the number of items in the queue. It needs no parameters and returns an
integer.

Memory representation of a queue using


1. Array: queue is represented in memory array. There are two pointer variables called front
and rear are maintained. The pointer variable front contains the location of the element to
be removed and the pointer variable rear contains the location of the element inserted.
The condition front=-1 indicates the queue is empty ad the condition rear=n-1 indicates
that the queue is full.
Advantages:
 Easy to implement.
 A large amount of data can be managed efficiently with ease.
 Operations such as insertion and deletion can be performed with ease as it follows
the first in first out rule.

Disadvantages
 static Data Structure, fixed size.
 If the queue has a large number of enqueue and dequeue operations, at some point
(in case of linear increment of front and rear indexes) we may not be able to insert
elements in the queue even if the queue is empty (this problem is avoided by
using circular queue).
 Maximum size of a queue must be defined prior

2. Linked list: queues are also represented using linked lists. In queues an item should be
inserted from rear end and an item should be removed from the front end. The pointer
front contains location of the first node of the linked list and another pointer rear contains
location of the last node.

Algorithm for insertion: let q is the array name consisting n elements. Front is the pointer that
contains the location of the element to be deleted and rear contains the location of the inserted
element. Item is the element to be inserted
Step1: if rear=n-1
Step2: display “ queue is full or overflow”
Step3: exit
Step4: end if
Step5: if front=-1 then
Step6: front=0
Step7: rear=0
Step8: else
Step9: rear=rear+1
Step10: q[rear]=item
Step11: return

Algorithm for deletion: let q is the array name consisting n elements. Front is the pointer that
contains the location of the element to be deleted and rear contains the location of the inserted
element. Item is the element to be inserted. This algorithm deletes the element at front position
Step1: if front=-1
Step2: display “ queue is empty or underflow”
Step3: exit
Step4: end if
Step5: item=q[front]
Step6: if front=rear then
Step7: front=0
Step8: rear=0
Step9: else
Step10: front=front+1
Step11: return

Types of queue:
Linear or simple queue: in simple queue insertion occurs at the rear end of the list and deletion
occurs at the front end of the list.

Circular queue: a circular queue is a queue in which all nodes are treated as circular such that the
last node follows the first node.
Front=0
Rear=6
Dequeue( double ended queue): it is a queue in which insertion and deletion takes place at both
the ends

LINKED LIST
A linked list is a collection of nodes. Each node is divided into two parts. The first part
contains Data part store the data item Link part store the address of next node
Linked list start with a special pointer called start pointer and terminated with NULL
pointer. An arrow is used to indicate the location of the next node. The NULL tells about
end of the list

Singly linked list: it is linear collection of data item called node, where each node has divided
into two parts i.e. data part and link part, data part store the data item and link part store the
address of next node. Limitation of the linked list is we can traverse only in one direction i.e
forward direction.
A singly linked list is a list that consists of a collection of nodes, and each node has two parts;
one part is the data part, and another part is the address. The singly linked can also be called a
chain as each node refers to another node through its address part. We can perform various
operations on a singly linked list like insertion, deletion, and traversing.
Doubly linked list:
A doubly linked list is another type of linked list. It is called a doubly linked list because it
contains two addresses while a singly linked list contains a single address. It is a list that has total
three parts, one is a data part, and others two are the pointers, i.e., previous and next. The
previous pointer holds the address of the previous node, and the next pointer holds the address of
the next node. Therefore, we can say that list has two references, i.e., forward and backward
reference to traverse in either direction.

We can also perform various operations on a doubly-linked list like insertion, deletion, and
traversing.

Data part store the data item , backward direction store address of previous node and forward
direction store address of next node.
Advantage of doubly linked list is we can traverse in any direction forward or backward.
Drawback of doubly linked list is it requires more memory compared to singly linked list
because we need an extra pointer to point previous node.
Inserting node at the beginning
Node *newnode
newnode->data=value
newnode->link=start
start=newnode

deleting node at the ending


step1: set ptr=start
step2: repeat step 4 and 5 until
step3: ptr->next!=null
step4: set loc=ptr
step5: set ptr=ptr->next
step6: set loc->next=null
step7: free(ptr)

application of linked list:


1. Implementation of stacks and queues
2. Dynamic memory allocation
3. Image viewer-previous and next images are linked and can be accessed by the next and
previous buttons
4. Previous and next page in web browser
5. Music player
6. Used in graph algorithm
7. Used in networking
8. Used in polynomial representation
Ex: 5x+3x2+2x3+1
Ans:2x3+3x2+5x+1

Implementation of singly linked list:

A singly linked list is a list that consists of a collection of nodes, and each node has two parts;
one part is the data part, and another part is the address. The singly linked can also be called a
chain as each node refers to another node through its address part. We can perform various
operations on a singly linked list like insertion, deletion, and traversing.
Memory representation of singly linked list

A singly linked list is a list that consists of a collection of nodes, and each node has two parts;
one part is the data part, and another part is the address. The singly linked can also be called a
chain as each node refers to another node through its address part. We can perform various
operations on a singly linked list like insertion, deletion, and traversing.
Memory representation of Doubly linked list: A doubly linked list is another type of linked list.
It is called a doubly linked list because it contains two addresses while a singly linked list
contains a single address. It is a list that has total three parts, one is a data part, and others two
are the pointers, i.e., previous and next. The previous pointer holds the address of the previous
node, and the next pointer holds the address of the next node. Therefore, we can say that list has
two references, i.e., forward and backward reference to traverse in either direction.

We can also perform various operations on a doubly-linked list like insertion, deletion, and
traversing.

You might also like