DSA - Unit 2
DSA - Unit 2
ALGORITHMS
Unit2- Linear Data Structures
CONTENTS
WHAT IS DATA STRUCTURE?
Data Structure is a way of collecting and
organising data in such a way that we can
perform operations on these data in an effective
way.
Data Structures is about rendering data
elements in terms of some relationship, for better
organization and storage.
The way in which the data is organized affects
the performance of a program for different tasks.
WHY TO LEARN DATA STRUCTURES?
As applications are getting complex and data
rich, there are three common problems that
applications face now-a-days-
1. Data Search
2. Processor Speed
3. Multiple Requests
To solve the above-mentioned problems, data
structures come to rescue.
CHARACTERISTICS OF A DATA STRUCTURE
Correctness − Data structure implementation
should implement its interface correctly.
Array
Stack
Queue
Expression Conversion
Infix to Postfix
Infix to Prefix
Postfix to Infix
Prefix to Infix
Backtracking
Memory Management
INFIX NOTATION
We write expression in infix notation, e.g. a - b +
c, where operators are used in-between operands.
It is easy for us humans to read, write, and speak
in infix notation but the same does not go well
with computing devices.
An algorithm to process infix notation could be
difficult and costly in terms of time and space
consumption.
PREFIX NOTATION
4. Return.
09/10/08 55
Example: Consider the following queue (linear queue).
Rear = 4 and Front = 1 and N = 7
10 50 30 40
1 2 3 4 5 6 7
(1) Insert 20. Now Rear = 5 and Front = 1
10 50 30 40 20
1 2 3 4 5 6 7
(2) Delete Front Element. Now Rear = 5 and Front = 2
50 30 40 20
1 2 3 4 5 6 7
(3) Delete Front Element. Now Rear = 5 and Front = 3
30 40 20
1 2 3 4 5 6 7
(4) Insert 60. Now Rear = 6 and Front = 3
30 40 20 60
1 2 3 4 5 6 7
09/10/08 56
Drawback of Linear Queue
• Once the queue is full, even though few elements from the front are deleted and
some occupied space is relieved, it is not possible to add anymore new elements,
as the rear has already reached the Queue’s rear most position.
Circular Queue
• This queue is not linear but circular.
"Rear" most element, if and only if the "Front" Figure: Circular Queue having
Rear = 5 and Front = 0
has moved forward. otherwise it will again be
09/10/08 57
Algorithms for Insert and Delete Operations in Circular Queue
For Insert Operation
Insert-Circular-Q(CQueue, Rear, Front, N, Item)
Here, CQueue is a circular queue where to store data. Rear represents the
location in which the data element is to be inserted and Front represents the
location from which the data element is to be removed. Here N is the maximum
size of CQueue and finally, Item is the new item to be added. Initailly Rear = 0 and
Front = 0.
1. If Front = 0 and Rear = 0 then Set Front := 1 and go to step 4.
2. If Front =1 and Rear = N or Front = Rear + 1
then Print: “Circular Queue Overflow” and Return.
3. If Rear = N then Set Rear := 1 and go to step 5.
4. Set Rear := Rear + 1
5. Set CQueue [Rear] := Item.
6. Return
09/10/08 58
For Delete Operation
Delete-Circular-Q(CQueue, Front, Rear, Item)
Here, CQueue is the place where data are stored. Rear represents the location in
which the data element is to be inserted and Front represents the location from
which the data element is to be removed. Front element is assigned to Item.
Initially, Front = 1.
1. If Front = 0 then
Print: “Circular Queue Underflow” and Return. /*..Delete without Insertion
09/10/08 59
Example: Consider the following circular queue with N = 5.
1. Initially, Rear = 0, Front = 0. 4. Insert 20, Rear = 3, Front = 0.
Front
Rear
Rear
3. Insert 50, Rear = 2, Front = 1. 6. Delete front, Rear = 4, Front = 2.
Front Rear Front
Rear
09/10/08 60
7. Insert 100, Rear = 5, Front = 2. 10. Delete front, Rear = 1, Front = 3.
Rear
Front
Front
Rear
Front
Rear Front
Front
09/10/08 61
PRIORITY QUEUE
PRIORITY QUEUE
Priority Queue is more specialized data structure
than Queue.
In Priority queue items are ordered by key value
so that item with the lowest value of key is at
front and item with the highest value of key is at
rear or vice versa.
So we're assigned priority to item based on its
key value.
A priority queue is a collection in which items can
be added at any time, but the only item that can
be removed is the one with the highest priority.
OPERATIONS IN PRIORITY QUEUE
It supports the following operations:
add(x): add item x
Deletion
Display
OPERATIONS ON SINGLE LINKED LIST
Before we implement actual operations, first we need to
set up an empty list. First, perform the following steps
before implementing actual operations.
Step 1 - Include all the header files which are used
in the program.
Step 2 - Declare all the user defined functions.
Deletion
Display
INSERTION – DOUBLY LINKED
LIST
INSERTION – DOUBLY LINKED LIST
Deletion
Display
INSERTION – CIRCULAR
LINKED LIST
INSERTION – CIRCULAR LINKED LIST