UNIT-I DS Notes PDF
UNIT-I DS Notes PDF
COURSE FILE
ON
DATA STRUCTURES
II B. Tech I Semester
Prepared by,
Mrs. A.Shalini,
Assistant Professor, IT-Dept
TIME TABLE
MON DS
TUE DS
LUNCH
WED DS
THU
FRI DS
SAT
PERIOD I II III IV
DAY/ TIME 10.00-11.00 11.30-12.30 01.30-02.30 03.00-04.00
MON
TUE DS
WED DS
THU
FRI DS
SAT
CS302PC: DATA STRUCTURES
UNIT - I
Introduction to Data Structures, abstract data types, Linear list – singly linked list implementation, insertion,
deletion and searching operations on linear list, Stacks-Operations, array and linked representations of stacks,
stack applications, Queues-operations, array and linked representations.
UNIT - II
Dictionaries: linear list representation, skip list representation, operations - insertion, deletion and searching.
Hash Table Representation: hash functions, collision resolution-separate chaining, open addressinglinear
probing, quadratic probing, double hashing, rehashing, extendible hashing.
UNIT - III
Search Trees: Binary Search Trees, Definition, Implementation, Operations- Searching, Insertion and Deletion,
AVL Trees, Definition, Height of an AVL Tree, Operations – Insertion, Deletion and Searching, Red –Black,
Splay Trees.
UNIT - IV
Graphs: Graph Implementation Methods. Graph Traversal Methods. Sorting: Heap Sort, External Sorting-
Model for external sorting, Merge Sort.
UNIT - V
Pattern Matching and Tries: Pattern matching algorithms-Brute force, the Boyer –Moore algorithm, the Knuth-
Morris-Pratt algorithm, Standard Tries, Compressed Tries, Suffix tries.
TEXTBOOKS:
1. Fundamentals of Data Structures in C, 2nd Edition, E. Horowitz, S. Sahni and Susan Anderson Freed,
Universities Press. 2. Data Structures using C – A. S. Tanenbaum, Y. Langsam, and M.J. Augenstein,
PHI/Pearson Education.
REFERENCE BOOKS:
1. Data Structures: A Pseudocode Approach with C, 2nd Edition, R. F. Gilberg and B.A. Forouzan, Cengage
Learning.
SESSION PLANNER
Text/
Date Date
S.No UNIT Class Topic Ref
Planned Conducted
Book
1 LH1 Introduction T1 1/9 1/9
2 LH2 Abstract Data Types T2 4/9 4/9
3 LH3 Linked lists Vs Arrays T2 8/9 8/9
4 LH4 Single linked list- Definition, Representation T2 9/9 9/9
5 LH5 Singly Linked Lists-Operations-Insertion T2 11/9 11/9
6 LH6 Singly Linked Lists -Deletion, Display T2 15/9 15/9
I
7 LH7 Double Linked Lists- Operations- Insertion T2 16/9 16/9
8 LH8 Double Linked Lists –Deletion, Display T2 18/9 18/9
9 LH9 Circularly linked lists-Operations-Insertion T2 21/9 21/9
10 LH10 Circular linked lists- Delete, Display T1 22/9 22/9
11 LH11 Stacks- Array Implementation- Operations T1 23/9 23/9
12 LH12 Stacks- Linked Implementation- Operations T1 25/9 25/9
13 LH13 Applications of Stacks T1 28/9 28/9
14 LH14 Queues- Array Implementation- Operations T1 29/9 29/9
15 LH15 Queues- Linked Implementation- Operations T1 30/9 30/9
16 LH16 Double ended queues, Circular queues T1 5/10
17 Unit Test-1 6/10
18 LH17 Dictionaries-Definition- Linear list representation T1 7/10
19 LH18 Skip list Representation-Operations-Insertion T1 9/10
Skip list Representation-Operations-Deletion-
T1 12/10
20 LH19 Searching
21 LH20 Hash table-Definition, Representation T1 13/10
Hash functions, Collision resolution- Separate
T2 14/10
22 II LH21 chaining
23 LH22 Collision resolution- Linear probing T2 16/10
24 LH23 Collision resolution-Quadratic probing T1
25 LH24 Collision resolution-Double hashing R1
26 LH25 Collision resolution-rehashing R1
27 LH26 Collision resolution-extendible hashing T1
28 Unit test-2 T1
29 LH27 Trees – Terminology T1
30 LH28 Representation of Trees, T1
31 LH29 Binary search trees-Definition, Implementation T1
32 LH30 Operations-Searching T1
33 LH31 Operations-Insertion, Deletion T2
34 LH32 AVL trees- Definition,Height of AVL tree T1
III
35 LH33 AVL-Operations-Insertion T1
36 LH34 AVL-Operations-Deletion, Searching T1
37 LH35 Red black trees- Definition, Properties T1
41 Unit Test-3
42 LH39 Graphs- Definition,Properties T2
43 LH40 Representation, Terminology T2
44 LH41 Graph Traversal methods- BFS T1
45 LH42 Graph Traversal methods- BFS T1
46 LH43 Graph Traversal methods- DFS T2
IV
47 LH44 Graph Traversal methods- DFS T1
48 LH45 Sorting- Heap sort T2
49 LH46 External sorting-Model T1
50 LH47 Merge sort T1
51 Unit Test-4 T1
52 LH48 Pattern matching algorithm- Brute force T1
53 LH49 Pattern matching algorithm- Brute force T1
54 LH50 Pattern matching algorithm- Boyer Moore T1
55 LH51 Pattern matching algorithm- Boyer Moore T1
Pattern matching algorithm- The Knuth-Morris-
T2
56 LH52 Pratt algorithm
V
Pattern matching algorithm- The Knuth-Morris-
T2
57 LH53 Pratt algorithm
58 LH54 Tries- Standard tries T1
59 LH55 Compressed tries T1
60 LH56 Suffix tries T1
61 Unit Test-5 T1
Course Objectives
Course Outcomes
Ability to select the data structures that efficiently model the information in a problem.
Ability to assess efficiency trade-offs among different data structure implementations or
combinations.
Implement and know the application of algorithms for sorting and searching.
Implement and know the application of algorithms for pattern matching
Design programs using a variety of data structures, including hash tables, binary and
general tree structures
Perform operations on search trees, tries, heaps, graphs, and AVL-trees
Program Outcomes
The Program Outcomes (POs) of the department are defined in a way that the Graduate
Attributes are included, which can be seen in the Program Outcomes (POs) defined. The
Program Outcomes (POs) of the department are as stated below:
PO Graduate
PO Statements
Name Attributes
Engineering Apply the knowledge of mathematics, science, engineering fundamentals, and an
PO1
knowledge engineering specialization to the solution of complex engineering problems
Identify, formulate, review research literature, and analyze complex engineering
PO 2 Problem analysis problems reaching substantiated conclusions using first principles of mathematics,
natural sciences, and engineering sciences.
Design solutions for complex engineering problems and design system
Design/
components or processes that meet the specified needs with appropriate
PO 3 development of
consideration for the public health and safety, and the cultural, societal, and
solutions
environmental considerations.
Conduct Use research-based knowledge and research methods including design of
PO 4 investigations of experiments, analysis and interpretation of data, and synthesis of the information
complex problems to provide valid conclusions
Create, select, and apply appropriate techniques, resources, and modern
Modern tool
PO 5 engineering and IT tools including prediction and modeling to complex
usage
engineering activities with an understanding of the limitations.
Apply reasoning informed by the contextual knowledge to assess societal, health,
The engineer and
PO 6 safety, legal and cultural issues and the consequent responsibilities relevant to the
society
professional engineering practice.
Understand the impact of the professional engineering solutions in societal and
Environment and
PO 7 environmental contexts, and demonstrate the knowledge of, and need for
sustainability
sustainable development.
Apply ethical principles and commit to professional ethics and responsibilities and
PO 8 Ethics
norms of the engineering practice.
Individual and Function effectively as an individual, and as a member or leader in diverse teams,
PO 9
team work and in multidisciplinary settings.
PEO3: Promote design, analyze, and exhibit of products, through strong communication,
leadership and ethical skills, to succeed an entrepreneurial.
CO-PO MAPPING:
CO 1 √ √
CO 2 √ √
CO3 √ √
CO 4 √ √
CO 5 √ √
CO 6 √ √ √
INDEX
SCOPE:
This unit introduces the concept of data structures and also the way of organizing data into
appropriate data structures such as stacks and queues. This unit makes student to understand the
difference between arrays and linked lists and also the operations performed on various types of
linked lists.
OBJECTIVES:
To understand the difference between arrays and linked lists.
INTRODUCTION:
Data Structure is a way of collecting and organizing 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. Data Structures are structures
programmed to store ordered data, so that various operations can be performed on it easily. It
represents the knowledge of data to be organized in memory. It should be designed and
implemented in such a way that it reduces the complexity and increases the efficiency.
1
1.1 Linear List
When we want to work with unknown number of data values, we use a linked list data structure
to organize that data. Linked list is a linear data structure that contains sequence of elements such
that each element links to its next element in the sequence. Each element in a linked list is called
as "Node".
Simply a list is a sequence of data, and linked list is a sequence of data linked with each other.
2
Single linked list is a sequence of elements in which every element has link to its next
element in the sequence.
In any single linked list, the individual element is called as "Node". Every "Node" contains two
fields, data and next. The data field is used to store actual value of that node and next field is
used to store the address of the next node in the sequence.
☀ In a single linked list, the address of the first node is always stored in
a reference node known as "front" (Some times it is also known as
"head").
☀ Always next part (reference part) of the last node must be NULL.
Example
1. Insertion
2. Deletion
3. Display
Before we implement actual operations, first we need to setup empty list. First perform the
following steps before implementing actual operations.
3
Step 1: Include all the header files which are used in the program.
Step 2: Declare all the user defined functions.
Step 3: Define a Node structure with two members data and next
Step 4: Define a Node pointer 'head' and set it to NULL.
Step 4: Implement the main method by displaying operations menu and make suitable
function calls in the main method to perform user selected operation.
Insertion
In a single linked list, the insertion operation can be performed in three ways. They are as
follows...
We can use the following steps to insert a new node at beginning of the single linked list...
We can use the following steps to insert a new node at end of the single linked list...
Step 1: Create a newNode with given value and newNode → next as NULL.
Step 2: Check whether list is Empty (head == NULL).
Step 3: If it is Empty then, set head = newNode.
Step 4: If it is Not Empty then, define a node pointer temp and initialize with head.
Step 5: Keep moving the temp to its next node until it reaches to the last node in the list
(until temp → next is equal to NULL).
Step 6: Set temp → next = newNode.
4
Inserting At Specific location in the list (After a Node)
We can use the following steps to insert a new node after a node in the single linked list...
Deletion
In a single linked list, the deletion operation can be performed in three ways. They are as
follows...
We can use the following steps to delete a node from beginning of the single linked list...
5
Deleting from End of the list
We can use the following steps to delete a node from end of the single linked list...
We can use the following steps to delete a specific node from the single linked list...
We can use the following steps to display the elements of a single linked list...
In single linked list, every node points to its next node in the sequence and the last node points
NULL. But in circular linked list, every node points to its next node in the sequence but the last
node points to the first node in the list.
Circular linked list is a sequence of elements in which every element has link to its next
element in the sequence and the last element has a link to the first element in the
sequence.
That means circular linked list is similar to the single linked list except that the last node points
to the first node in the list.
7
Example
1. Insertion
2. Deletion
3. Display
Before we implement actual operations, first we need to setup 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.
Step 3: Define a Node structure with two members data and next
Step 4: Define a Node pointer 'head' and set it to NULL.
Step 4: Implement the main method by displaying operations menu and make suitable
function calls in the main method to perform user selected operation.
Insertion
In a circular linked list, the insertion operation can be performed in three ways. They are as
follows...
We can use the following steps to insert a new node at beginning of the circular linked list...
8
Step 4: If it is Not Empty then, define a Node pointer 'temp' and initialize with 'head'.
Step 5: Keep moving the 'temp' to its next node until it reaches to the last node (until
'temp → next == head').
Step 6: Set 'newNode → next =head', 'head = newNode' and 'temp → next = head'.
We can use the following steps to insert a new node at end of the circular linked list...
We can use the following steps to insert a new node after a node in the circular linked list...
9
Step 8: If temp is last node then set temp → next = newNode and newNode →
next = head.
Step 9: If temp is not last node then set newNode → next = temp → next and temp →
next = newNode.
Deletion
In a circular linked list, the deletion operation can be performed in three ways those are as
follows...
We can use the following steps to delete a node from beginning of the circular linked list...
Step 1: Check whether list is Empty (head == NULL)
Step 2: If it is Empty then, display 'List is Empty!!! Deletion is not possible' and
terminate the function.
Step 3: If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and
initialize both 'temp1' and 'temp2' with head.
Step 4: Check whether list is having only one node (temp1 → next == head)
Step 5: If it is TRUE then set head = NULL and delete temp1 (Setting Empty list
conditions)
Step 6: If it is FALSE move the temp1 until it reaches to the last node. (until temp1 →
next == head )
Step 7: Then set head = temp2 → next, temp1 → next = head and delete temp2.
We can use the following steps to delete a node from end of the circular linked list...
10
Step 4: Check whether list has only one Node (temp1 → next == head)
Step 5: If it is TRUE. Then, set head = NULL and delete temp1. And terminate from
the function. (Setting Empty list condition)
Step 6: If it is FALSE. Then, set 'temp2 = temp1 ' and move temp1 to its next node.
Repeat the same until temp1 reaches to the last node in the list. (until temp1 →
next == head)
Step 7: Set temp2 → next = head and delete temp1.
We can use the following steps to delete a specific node from the circular linked list...
11
Step 11: If temp1 is last node then set temp2 → next = head and
delete temp1 (free(temp1)).
Step 12: If temp1 is not first node and not last node then set temp2 → next = temp1 →
next and delete temp1 (free(temp1)).
We can use the following steps to display the elements of a circular linked list...
The real life application where the circular linked list is used is our Personal Computers,
where multiple applications are running. All the running applications are kept in a circular
linked list and the OS gives a fixed time slot to all for running. The Operating System keeps
on iterating over the linked list until all the applications are completed.
Another example can be Multiplayer games. All the Players are kept in a Circular Linked List
and the pointer keeps on moving forward as a player's chance ends.
Circular Linked List can also be used to create Circular Queue. In a Queue we have to keep
two pointers, FRONT and REAR in memory all the time, where as in Circular Linked List,
only one pointer is required.
In a single linked list, every node has link to its next node in the sequence. So, we can traverse
from one node to other node only in one direction and we can not traverse back. We can solve
this kind of problem by using double linked list. Double linked list can be defined as follows...
Double linked list is a sequence of elements in which every element has links to its
previous element and next element in the sequence.
12
In double linked list, every node has link to its previous node and next node. So, we can traverse
forward by using next field and can traverse backward by using previous field. Every node in a
double linked list contains three fields and they are shown in the following figure...
Here, 'link1' field is used to store the address of the previous node in the sequence, 'link2' field
is used to store the address of the next node in the sequence and 'data' field is used to store the
actual value of that node.
Example
☀ In double linked list, the first node must be always pointed by head.
☀ Always the previous field of the first node must be NULL.
☀ Always the next field of the last node must be NULL
Insertion
In a double linked list, the insertion operation can be performed in three ways as follows...
13
Inserting At Beginning of the list
We can use the following steps to insert a new node at beginning of the double linked list...
Step 1: Create a newNode with given value and newNode → previous as NULL.
Step 2: Check whether list is Empty (head == NULL)
Step 3: If it is Empty then, assign NULL to newNode → next and newNode to head.
Step 4: If it is not Empty then, assign head to newNode → next and newNode to head.
We can use the following steps to insert a new node at end of the double linked list...
Step 1: Create a newNode with given value and newNode → next as NULL.
Step 2: Check whether list is Empty (head == NULL)
Step 3: If it is Empty, then assign NULL to newNode →
previous and newNode to head.
Step 4: If it is not Empty, then, define a node pointer temp and initialize with head.
Step 5: Keep moving the temp to its next node until it reaches to the last node in the list
(until temp → next is equal to NULL).
Step 6: Assign newNode to temp → next and temp to newNode → previous.
We can use the following steps to insert a new node after a node in the double linked list...
14
Step 6: Every time check whether temp1 is reached to the last node. If it is reached to the
last node then display 'Given node is not found in the list!!! Insertion not
possible!!!' and terminate the function. Otherwise move the temp1 to next node.
Step 7: Assign temp1 → next to temp2, newNode to temp1 →
next, temp1 to newNode → previous, temp2 to newNode →
nextand newNode to temp2 → previous.
Deletion
In a double linked list, the deletion operation can be performed in three ways as follows...
We can use the following steps to delete a node from beginning of the double linked list...
We can use the following steps to delete a node from end of the double linked list...
Step 1: Check whether list is Empty (head == NULL)
Step 2: If it is Empty, then display 'List is Empty!!! Deletion is not possible' and
terminate the function.
15
Step 3: If it is not Empty then, define a Node pointer 'temp' and initialize with head.
Step 4: Check whether list has only one Node (temp → previous and temp → next both
are NULL)
Step 5: If it is TRUE, then assign NULL to head and delete temp. And terminate from
the function. (Setting Empty list condition)
Step 6: If it is FALSE, then keep moving temp until it reaches to the last node in the list.
(until temp → next is equal to NULL)
Step 7: Assign NULL to temp → previous → next and delete temp.
We can use the following steps to delete a specific node from the double linked list...
16
Step 12: If temp is not the first node and not the last node, then
set temp of previous of next to temp of next (temp → previous → next = temp →
next), temp of next of previous to temp of previous (temp → next → previous = temp
→ previous) and delete temp (free(temp)).
We can use the following steps to display the elements of a double linked list...
Stack is a linear data structure in which the insertion and deletion operations are performed at
only one end. In a stack, adding and removing of elements are performed at single position
which is known as "top". That means, new element is added at top of the stack and an element is
removed from the top of the stack. In stack, the insertion and deletion operations are performed
based on LIFO (Last In First Out) principle.
In a stack, the insertion operation is performed using a function called "push" and deletion
operation is performed using a function called "pop".
17
In the figure, PUSH and POP operations are performed at top position in the stack. That means,both the
insertion and deletion operations are performed at one end (i.e., at Top) A stack data structure can be defined
as follows...
Stack is a linear data structure in which the operations are performed based on LIFO
principle.
"A Collection of similar data items in which both insertion and deletion operations are
performed based on LIFO principle".
Example
If we want to create a stack by inserting 10,45,12,16,35 and 50. Then 10 becomes the bottom
most element and 50 is the top most element. Top is at 50 as shown in the image below...
Stack data structure can be implement in two ways. They are as follows...
1. Using Array
2. Using Linked List
When stack is implemented using array, that stack can organize only limited number of elements.
When stack is implemented using linked list, that stack can organize unlimited number of
elements.
18
Stack Using Array
A stack data structure can be implemented using one dimensional array. But stack implemented
using array, can store only fixed number of data values. This implementation is very simple, just
define a one dimensional array of specific size and insert or delete the values into that array by
using LIFO principle with the help of a variable 'top'. Initially top is set to -1. Whenever we
want to insert a value into the stack, increment the top value by one and then insert. Whenever
we want to delete a value from the stack, then delete the top value and decrement the top value
by one.
A stack can be implemented using array as follows...
Before implementing actual operations, first follow the below steps to create an empty stack.
Step 1: Include all the header files which are used in the program and define a
constant 'SIZE' with specific value.
Step 2: Declare all the functions used in stack implementation.
Step 3: Create a one dimensional array with fixed size (int stack[SIZE])
Step 4: Define a integer variable 'top' and initialize with '-1'. (int top = -1)
Step 5: In main method display menu with list of operations and make suitable function
calls to perform operation selected by the user on the stack.
In a stack, push() is a function used to insert an element into the stack. In a stack, the new
element is always inserted at topposition. Push function takes one integer value as parameter and
inserts that value into the stack. We can use the following steps to push an element on to the
stack...
In a stack, pop() is a function used to delete an element from the stack. In a stack, the element is
always deleted from topposition. Pop function does not take any value as parameter. We can use
the following steps to pop an element from the stack...
19
display() - Displays the elements of a Stack
The major problem with the stack implemented using array is, it works only for fixed number of
data values. That means the amount of data must be specified at the beginning of the
implementation itself. Stack implemented using array is not suitable, when we don't know the
size of data which we are going to use. A stack data structure can be implemented by using
linked list data structure. The stack implemented using linked list can work for unlimited number
of values. That means, stack implemented using linked list works for variable size of data. So,
there is no need to fix the size at the beginning of the implementation. The Stack implemented
using linked list can organize as many data values as we want.
In linked list implementation of a stack, every new element is inserted as 'top' element. That
means every newly inserted element is pointed by 'top'. Whenever we want to remove an
element from the stack, simply remove the node which is pointed by 'top' by moving 'top' to its
next node in the list. The next field of the first element must be always NULL.
Example
In above example, the last inserted node is 99 and the first inserted node is 25. The order of
elements inserted is 25, 32,50 and 99.
To implement stack using linked list, we need to set the following things before implementing
actual operations.
20
Step 1: Include all the header files which are used in the program. And declare all
the user defined functions.
Step 2: Define a 'Node' structure with two members data and next.
Step 3: Define a Node pointer 'top' and set it to NULL.
Step 4: Implement the main method by displaying Menu with list of operations and
make suitable function calls in the main method.
push(value) - Inserting an element into the Stack
We can use the following steps to insert a new node into the stack...
We can use the following steps to delete a node from the stack...
We can use the following steps to display the elements (nodes) of a stack...
1) Expression conversion
2)Postfix expression evaluation
21
Expression Conversion
Any expression can be represented using three types of expressions (Infix, Postfix and Prefix).
We can also convert one type of expression to another type of expression like Infix to Postfix,
Infix to Prefix, Postfix to Prefix and vice versa.
To convert any Infix expression into Postfix or Prefix expression we can use the following
procedure...
1. Find all the operators in the given Infix Expression.
2. Find the order of operators evaluated according to their Operator precedence.
3. Convert each operator into required type of expression (Postfix or Prefix) in the same
order.
Example
D=A+B*C
DABC*+=
To convert Infix Expression into Postfix Expression using a stack data structure, We can use the
following steps...
1. Read all the symbols one by one from left to right in the given Infix Expression.
2. If the reading symbol is operand, then directly print it to the result (Output).
3. If the reading symbol is left parenthesis '(', then Push it on to the Stack.
4. If the reading symbol is right parenthesis ')', then Pop all the contents of stack until
respective left parenthesis is poped and print each poped symbol to the result.
5. If the reading symbol is operator (+ , - , * , / etc.,), then Push it on to the Stack. However,
first pop the operators which are already on the stack that have higher or equal
precedence than current operator and print them to the result.
22
Example
(A+B)*(C-D)
The given infix expression can be converted into postfix expression using Stack data Structure as
follows...
23
The final Postfix Expression is as follows...
AB+CD-*
A postfix expression is a collection of operators and operands in which the operator is placed
after the operands. That means, in a postfix expression the operator follows the operands.
Example
A postfix expression can be evaluated using the Stack data structure. To evaluate a postfix
expression using Stack data structure we can use the following steps...
1. Read all the symbols one by one from left to right in the given Postfix Expression
2. If the reading symbol is operand, then push it on to the Stack.
3. If the reading symbol is operator (+ , - , * , / etc.,), then perform TWO pop
operations and store the two popped oparands in two different variables (operand1
and operand2). Then perform reading symbol operation using operand1 and
operand2 and push result back on to the Stack.
4. Finally! perform a pop operation and display the popped value as final result.
Example
...
24
25
1.7 Recursion implementation using stacks
Stack is not only used to perform recursion but any bunch of nested function calls. Recursion is a
special case wherein all the nested function calls are to the same function, or the same function is
called in a cyclic chain of function calls.
Coming to the question, why stack for recursion/ function calling? This is because the nature of
nested function calling and returning is the same as that of a stack. The one that is called at top
level(or the earliest) is returned the last. The later a function is called in the nested hierarchy, the
earliest it returns. So, the start/ return of a function call turns out analogous to push/ pop
operations of a stack and thus a stack can be used for this functionality.
Queue is a linear data structure in which the insertion and deletion operations are performed at
two different ends. In a queue data structure, adding and removing of elements are performed at
two different positions. The insertion is performed at one end and deletion is performed at other
end. In a queue data structure, the insertion operation is performed at a position which is known
as 'rear' and the deletion operation is performed at a position which is known as 'front'. In queue
data structure, the insertion and deletion operations are performed based on FIFO (First In First
Out) principle.
In a queue data structure, the insertion operation is performed using a function called
"enQueue()" and deletion operation is performed using a function called "deQueue()".
Queue data structure is a linear data structure in which the operations are performed
based on FIFO principle.
"Queue data structure is a collection of similar data items in which insertion and deletion
operations are performed based on FIFO principle".
26
Example
Queue data structure can be implemented in two ways. They are as follows...
1. Using Array
2. Using Linked List
When a queue is implemented using array, that queue can organize only limited number of
elements. When a queue is implemented using linked list, that queue can organize unlimited
number of elements.
A queue data structure can be implemented using one dimensional array. But, queue
implemented using array can store only fixed number of data values. The implementation of
queue data structure using array is very simple, just define a one dimensional array of specific
size and insert or delete the values into that array by using FIFO (First In First Out)
principle with the help of variables 'front' and 'rear'. Initially both 'front' and 'rear' are set to -
1. Whenever, we want to insert a new value into the queue, increment 'rear' value by one and
then insert at that position. Whenever we want to delete a value from the queue, then increment
'front' value by one and then display the value at 'front' position as deleted element.
Before we implement actual operations, first follow the below steps to create an empty queue.
Step 1: Include all the header files which are used in the program and define a
constant 'SIZE' with specific value.
Step 2: Declare all the user defined functions which are used in queue implementation.
27
Step 3: Create a one dimensional array with above defined SIZE (int queue[SIZE])
Step 4: Define two integer variables 'front' and 'rear' and initialize both with '-1'. (int
front = -1, rear = -1)
Step 5: Then implement main method by displaying menu of operations list and make
suitable function calls to perform operation selected by the user on 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 parameter and inserts that value into the queue. We can use the following steps
to insert an element into 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...
28
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 is equal to rear (i <= rear)
The major problem with the queue implemented using array is, It will work for only fixed
number of data. That means, the amount of data must be specified in the beginning itself. Queue
using array is not suitable when we don't know the size of data which we are going to use. A
queue data structure can be implemented using linked list data structure. The queue which is
implemented using linked list can work for unlimited number of values. That means, queue using
linked list can work for variable size of data (No need to fix the size at beginning of the
implementation). The Queue implemented using linked list can organize as many data values as
we want.
In linked list implementation of a queue, the last inserted node is always pointed by 'rear' and
the first node is always pointed by 'front'.
Example
In above example, the last inserted node is 50 and it is pointed by 'rear' and the first inserted
node is 10 and it is pointed by 'front'. The order of elements inserted is 10, 15, 22 and 50.
To implement queue using linked list, we need to set the following things before implementing
actual operations.
Step 1: Include all the header files which are used in the program. And declare all
the user defined functions.
Step 2: Define a 'Node' structure with two members data and next.
Step 3: Define two Node pointers 'front' and 'rear' and set both to NULL.
Step 4: Implement the main method by displaying Menu of list of operations and make
suitable function calls in the main method to perform user selected operation.
29
enQueue(value) - Inserting an element into the Queue
We can use the following steps to insert a new node into the queue...
Step 1: Create a newNode with given value and set 'newNode → next' to NULL.
We can use the following steps to delete a node from the queue...
We can use the following steps to display the elements (nodes) of a queue...
In a normal Queue Data Structure, we can insert elements until queue becomes full. But once if
queue becomes full, we can not insert the next element until all the elements are deleted from the
queue. For example consider the queue below...
30
After inserting all the elements into the queue.
Now consider the following situation after deleting three elements from the queue...
This situation also says that Queue is Full and we can not insert the new element because, 'rear'
is still at last position. In above situation, even though we have empty positions in the queue we
cannot make use of them to insert new element. This is the major problem in normal queue data
structure. To overcome this problem we use circular queue data structure.
Circular Queue is a linear data structure in which the operations are performed based on
FIFO (First In First Out) principle and the last position is connected back to the first
position to make a circle.
To implement a circular queue data structure using array, we first perform the following steps
before we implement actual operations.
31
Step 1: Include all the header files which are used in the program and define a
constant 'SIZE' with specific value.
Step 2: Declare all user defined functions used in circular queue implementation.
Step 3: Create a one dimensional array with above defined SIZE (int cQueue[SIZE])
Step 4: Define two integer variables 'front' and 'rear' and initialize both with '-1'. (int
front = -1, rear = -1)
Step 5: Implement main method by displaying menu of operations list and make suitable
function calls to perform operation selected by the user on circular queue.
In a circular queue, enQueue() is a function which is used to insert an element into the circular
queue. In a circular queue, the new element is always inserted at rear position. The enQueue()
function takes one integer value as parameter and inserts that value into the circular queue. We
can use the following steps to insert an element into the circular queue...
Step 1: Check whether queue is FULL. ((rear == SIZE-1 && front == 0) || (front ==
rear+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 check rear == SIZE - 1 && front != 0 if it is TRUE,
then set rear = -1.
Step 4: Increment rear value by one (rear++), set queue[rear] = value and check 'front
== -1' if it is TRUE, then set front = 0.
In a circular queue, deQueue() is a function used to delete an element from the circular queue. In
a circular queue, the element is always deleted from front position. The deQueue() function
doesn't take any value as parameter. We can use the following steps to delete an element from
the circular queue...
32
Step 3: If it is NOT EMPTY, then display queue[front] as deleted element and
increment the front value by one (front ++). Then check whether front == SIZE, if it
is TRUE, then set front = 0. Then check whether both front - 1 and rear are equal
(front -1 == rear), if it TRUE, then set both front and rear to '-1' (front = rear = -1).
We can use the following steps to display the elements of a circular queue...
33
TUTORIAL QUESTIONS
11. Perform the following operations on stack: push 5,puh 29, push100, pop, pop, push 225, pop.
34
UNIVERSITY QUESTIONS
1. What is linked list? Write advantages of doubly linked list over singly linked list.
[JNTUH DEC 2016]
Ref topic 1.2 and 1.4
2. What is a stack? List the applications of stack. [JNTUH DEC 2016]
Ref topic 1.6
3. Show the detailed contents of stack to evaluate the given postfix expression.
{1 2 3 + * 3 2 1 - + *}. [JNTUH DEC 2016]
Ref topic 1.6
4. Write a C program to implement multiple stacks using single array.
[JNTUH DEC 2016]
Ref topic 1.6
5. Convert the infix expression a / b – c + d * e – a * c into postfix expression and
trace that postfix expression for given data a = 6, b = 3, c = 1, d = 2, e = 4.
[JNTUH DEC 2016]
Ref topic 1.6
6. What are the disadvantages of array? [JNTUH MARCH 2017]
Ref topic 1.1
7. What are the disadvantages of queue which is implemented using arrays? How to
overcome it? [JNTUH MARCH 2017]
35
OBJECTIVE QUESTIONS
1. A linear collection of data elements where the linear node is given by means of pointer is
called?
a) Linked list
b) Node list
c) Primitive list
d) None of the mentioned
3. In linked list each node contain minimum of two fields. One field is data field to store the
data second field is?
a) Pointer to character
b) Pointer to integer
c) Pointer to node
d) Node
11. Consider the usual algorithm for determining whether a sequence of parentheses is
balanced.
The maximum number of parentheses that appear on the stack AT ANY ONE TIME
when the algorithm analyzes: (()(())(())) are:
a) 1
b) 2
c) 3
d) 4 or more
12. A queue is a ?
a) FIFO (First In First Out) list
b) LIFO (Last In First Out) list
c) Ordered array
d) Linear tree
13. If the elements “A”, “B”, “C” and “D” are placed in a queue and are deleted one at a
time, in what order will they be removed?
a) ABCD
b) DCBA
c) DCAB
d) ABDC
37
14. A normal queue, if implemented using an array of size MAX_SIZE, gets full when
a) Rear = MAX_SIZE – 1
b) Front = (rear + 1)mod MAX_SIZE
c) Front = rear + 1
d) Rear = front
15. A linear list of elements in which deletion can be done from one end (front) and insertion
can take place only at the other end (rear) is known as a ?
a) Queue
b) Stack
c) Tree
d) Linked list
38
DESCRIPTIVE QUESTIONS
39
UNIT TEST QUESTION PAPER
SET-1
1. A linear list of elements in which deletion can be done from one end (front) and insertion can take place only
at the other end (rear) is known as a ?
a) Queue
b) Stack
c) Tree
d) Linked list
4. If the elements “A”, “B”, “C” and “D” are placed in a queue and are deleted one at a time, in what order will
they be removed?
a) ABCD
b) DCBA
c) DCAB
d) ABDC
5. A data structure in which elements can be inserted or deleted at/from both the ends but not in the middle is?
a) Queue
b) Circular queue
c) Dequeue
d) Priority queue
6. A normal queue, if implemented using an array of size MAX_SIZE, gets full when
a) Rear = MAX_SIZE – 1
b) Front = (rear + 1)mod MAX_SIZE
c) Front = rear + 1
d) Rear = front
40
8. Which of the following is not the type of queue?
a) Ordinary queue
b) Single ended queue
c) Circular queue
d) Priority queue
10. Convert the following infix expressions into its equivalent postfix expressions
(A + B ⋀D)/(E – F)+G
a) (A B D ⋀ + E F – / G +)
b) (A B D +⋀ E F – / G +)
c) (A B D ⋀ + E F/- G +)
d) (A B D E F + ⋀ / – G +)
41
SET-2
2. Which of the following statement(s) about stack data structure is/are NOT correct?
a) Linked List are used for implementing Stacks
b) Top of the Stack always contain the new node
c) Stack is the FIFO data structure
d) Null link is present in the last node at the bottom of the stack
6. Assume that the operators +,-, X are left associative and ^ is right associative.
The order of precedence (from highest to lowest) is ^, X, +, -. The postfix expression for the infix expression a +
b X c – d ^ e ^ f is
a) abc X+ def ^^ –
b) abc X+ de^f^ –
c) ab+c Xd – e ^f^
d) -+aXbc^ ^def
42
7. If the elements “A”, “B”, “C” and “D” are placed in a stack and are deleted one at a time, what is the order of
removal?
a) ABCD
b) DCBA
c) DCAB
d) ABDC
8. Consider an implementation of unsorted singly linked list. Suppose it has its representation with a head
pointer only.
Given the representation, which of the following operation can be implemented in O(1) time?
i) Insertion at the front of the linked list
ii) Insertion at the end of the linked list
iii) Deletion of the front node of the linked list
iv) Deletion of the last node of the linked list
a) I and II
b) I and III
c) I, II and III
d) I, II and IV
9. In linked list each node contain minimum of two fields. One field is data field to store the data second field
is?
a) Pointer to character
b) Pointer to integer
c) Pointer to node
d) Node
10. What would be the asymptotic time complexity to add a node at the end of singly linked list, if the pointer is
initially pointing to the head of the list?
a) O(1)
b) O(n)
c) θ(n)
d) θ(1)
43
SET-3
1. Assume that the operators +,-, X are left associative and ^ is right associative.
The order of precedence (from highest to lowest) is ^, X, +, -. The postfix expression for the infix expression a +
b X c – d ^ e ^ f is
a) abc X+ def ^^ –
b) abc X+ de^f^ –
c) ab+c Xd – e ^f^
d) -+aXbc^ ^def
2. If the elements “A”, “B”, “C” and “D” are placed in a stack and are deleted one at a time, what is the order of
removal?
a) ABCD
b) DCBA
c) DCAB
d) ABDC
3. Consider an implementation of unsorted singly linked list. Suppose it has its representation with a head
pointer only.
Given the representation, which of the following operation can be implemented in O(1) time?
i) Insertion at the front of the linked list
ii) Insertion at the end of the linked list
iii) Deletion of the front node of the linked list
iv) Deletion of the last node of the linked list
a) I and II
b) I and III
c) I, II and III
d) I, II and IV
4. In linked list each node contain minimum of two fields. One field is data field to store the data second field
is?
a) Pointer to character
b) Pointer to integer
c) Pointer to node
d) Node
5. What would be the asymptotic time complexity to add a node at the end of singly linked list, if the pointer is initially
pointing to the head of the list?
a) O(1)
b) O(n)
c) θ(n)
d) θ(1)
44
7. Which of the following statement(s) about stack data structure is/are NOT correct?
a) Linked List are used for implementing Stacks
b) Top of the Stack always contain the new node
c) Stack is the FIFO data structure
d) Null link is present in the last node at the bottom of the stack
10. The type of expression in which operator succeeds its operands is?
a) Infix Expression
b) Prefix Expression
c) Postfix Expression
d) Both Prefix and Postfix Expressions
45
SET-4
2. Which of the following statement(s) about stack data structure is/are NOT correct?
a) Linked List are used for implementing Stacks
b) Top of the Stack always contain the new node
c) Stack is the FIFO data structure
d) Null link is present in the last node at the bottom of the stack
6. Assume that the operators +,-, X are left associative and ^ is right associative.
The order of precedence (from highest to lowest) is ^, X, +, -. The postfix expression for the infix expression a +
b X c – d ^ e ^ f is
a) abc X+ def ^^ –
b) abc X+ de^f^ –
c) ab+c Xd – e ^f^
d) -+aXbc^ ^def
46
7. If the elements “A”, “B”, “C” and “D” are placed in a stack and are deleted one at a time, what is the order of
removal?
a) ABCD
b) DCBA
c) DCAB
d) ABDC
8. Consider an implementation of unsorted singly linked list. Suppose it has its representation with a head
pointer only.
Given the representation, which of the following operation can be implemented in O(1) time?
i) Insertion at the front of the linked list
ii) Insertion at the end of the linked list
iii) Deletion of the front node of the linked list
iv) Deletion of the last node of the linked list
a) I and II
b) I and III
c) I, II and III
d) I, II and IV
9. In linked list each node contain minimum of two fields. One field is data field to store the data second field
is?
a) Pointer to character
b) Pointer to integer
c) Pointer to node
d) Node
10. What would be the asymptotic time complexity to add a node at the end of singly linked list, if the pointer is initially
pointing to the head of the list?
a) O(1)
b) O(n)
c) θ(n)
d) θ(1)
47
ASSIGNMENT QUESTIONS
48
SEMINAR TOPICS
49
REAL TIME APPLICATIONS
A linked list in which last node again points to first node is known as circular linked list.
They are used in variety of applications such as,
Resources allocated by operating system, where the resources are allocated to the
user in circular manner.
Multi player games in which the resources are allocated to all the players and
resumed in circular order
Circular queues.
2. Stacks:
To reverse a word. You push a given word to stack - letter by letter - and then pop letters
from the stack.
An "undo" mechanism in text editors; this operation is accomplished by keeping all text
changes in a stack.
Undo/Redo stacks in Excel or Word.
Language processing :
space for parameters and local variables is created internally using a stack.
compiler's syntax check for matching braces is implemented by using stack.
A stack of plates/books in a cupboard.
A garage that is only one car wide. To remove the first car in we have to take out all the
other cars in after it.
Wearing/Removing Bangles.
Back/Forward stacks on browsers.
Support for recursion
Activation records of method calls.
50
3. Queues:
A queue of people at ticket-window: The person who comes first gets the ticket first. The
person who is coming last is getting the tickets in last. Therefore, it follows first-in-first-
out (FIFO) strategy of queue.
Vehicles on toll-tax bridge: The vehicle that comes first to the toll tax booth leaves the
booth first. The vehicle that comes last leaves last. Therefore, it follows first-in-first-out
(FIFO)
51
NPTEL LINKS
1. https://nptel.ac.in/courses/106102064/
2. https://nptel.ac.in/courses/106106127/
52
BLOOMS TAXONOMY
We can use the following steps to insert a new node at beginning of the single linked list...
The real life application where the circular linked list is used is our Personal Computers,
where multiple applications are running. All the running applications are kept in a
circular linked list and the OS gives a fixed time slot to all for running. The Operating
System keeps on iterating over the linked list until all the applications are completed.
53
Another example can be Multiplayer games. All the Players are kept in a Circular Linked List and the pointer keeps on
moving forward as a player's chance ends.
Circular Linked List can also be used to create Circular Queue. In a Queue we have to
keep two pointers, FRONT and REAR in memory all the time, where as in Circular
Linked List, only one pointer is required.
54
Topic 2: Stacks
In a stack, push() is a function used to insert an element into the stack. In a stack, the new
element is always inserted at top position. Push function takes one integer value as parameter
and inserts that value into the stack. We can use the following steps to push an element on to the
stack...
A linked list is a data structure that can store a collection of items. In other words, linked lists can be utilized to
store several objects of the same type. Each unit or element of the list is referred as a node. Each node has its
own data and the address of the next node. It is like a chain. Linked Lists are used to create graph and trees.
4) Describe what is Node in link list? And name the types of Linked Lists?
Together (data + link) is referred as the Node.
Singly Linked list are a type of data structure. In a singly linked list, each node in the list stores the contents of
the node and a reference or pointer to the next node in the list. It does not store any reference or pointer to the
previous node.
56
6) Mention what is the difference between Linear Array and Linked List?
The difference between Linear Array and Linked List are shown below,
Deletion and Insertions are difficult. Deletion and Insertions can be done easily.
For insertion and deletion, it needs For insertion and deletion, it does not require
movements movement of nodes
To avail each element same amount of To avail each element different amount of time
time is required. is required.
We can reach there directly if we have To reach a particular node, you need to go
to go to a particular element through all those nodes that come before that node.
7) Mention what are the applications of Linked Lists?
Applications of Linked Lists are,
10) Mention what is the difference between singly and doubly linked lists?
A doubly linked list nodes contain three fields:
Whereas a singly linked list contains points only to the next node.
11) Mention what are the applications that use Linked lists?
Both queues and stacks are often implemented using linked lists. Other applications are list, binary tree, skip,
unrolled linked list, hash table, etc.
57
12) Explain how to add an item to the beginning of the list?
To add an item to the beginning of the list, you have to do the following:
If you are using a function to do this operation, you need to alter the head variable. To do this, you must pass a
pointer to the pointer variable (a double pointer). so you will be able to modify the pointer itself.
14) Mention how to delete first node from singly linked list?
To delete first node from singly linked list
58
Course Outcomes
Ability to select the data structures that efficiently model the information in a problem.
Thinking Abilities
From this unit student will be able to store any data based on some principle and perform real time
operations on them.
Recursion can be very well understood and implemented using stack for storing recent values.
Any college data can be taken and student details can be inserted at any position, deleted at any
position, searching, sorting can be performed as a case study.
59