0% found this document useful (0 votes)
453 views70 pages

UNIT-I DS Notes PDF

This document outlines a course on data structures taught at the Department of Computer Science and Engineering. The course covers 5 units over 15 weeks, including topics like linear lists, stacks, queues, dictionaries, hash tables, search trees, graphs, sorting, pattern matching, and tries. The objectives are to learn fundamental data structures, explore concepts like stacks and queues, introduce various data structures and algorithms, and design algorithms for real problems. The outcomes include selecting efficient data structures, implementing sorting/searching algorithms, and using structures like trees, heaps, and graphs.

Uploaded by

shalini
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)
453 views70 pages

UNIT-I DS Notes PDF

This document outlines a course on data structures taught at the Department of Computer Science and Engineering. The course covers 5 units over 15 weeks, including topics like linear lists, stacks, queues, dictionaries, hash tables, search trees, graphs, sorting, pattern matching, and tries. The objectives are to learn fundamental data structures, explore concepts like stacks and queues, introduce various data structures and algorithms, and design algorithms for real problems. The outcomes include selecting efficient data structures, implementing sorting/searching algorithms, and using structures like trees, heaps, and graphs.

Uploaded by

shalini
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
You are on page 1/ 70

Department of Computer Science and Engineering

COURSE FILE
ON

DATA STRUCTURES

II B. Tech I Semester

Prepared by,

Mrs. A.Shalini,
Assistant Professor, IT-Dept
TIME TABLE

With effective from 21/09/2020 to till date:

DAY/TIME 09:30- 10:45- 12:00- 01:00- 02:00- 03:15-


10:30 11:45 01:00 02:00 03:00 04:15

MON DS

TUE DS

LUNCH
WED DS

THU

FRI DS

SAT

From 01/09/2020 to 20/09/2020

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

B.TECH II Year I Sem. LTPC


3104

Prerequisites: A course on “Programming for Problem Solving”.

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

38 LH36 Red black trees- Operations T2

39 LH37 Splay trees- Definition T2


40 LH38 Splay trees- Operations T2

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

 To learn the fundamentals of data structures.


 To explore various concepts of data structures such as stacks and queues.
 To introduce variety of data structures such as hash tables, search trees, tries, heaps and
graphs.
 To understand the usage of various searching and sorting algorithms.
 To design algorithms for real time problems.
 To introduce various pattern matching algorithms..

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.

Communicate effectively on complex engineering activities with the engineering


community and with society at large, such as, being able to comprehend and write
PO 10 Communication
effective reports and design documentation, make effective presentations, and
give and receive clear instructions.

Project Demonstrate knowledge and understanding of the engineering and management


PO 11 management and principles and apply these to one’s own work, as a member and leader in a team,
finance to manage projects and in multidisciplinary environments.
Recognize the need for, and have the preparation and ability to engage in
PO 12 Life-long learning independent and life-long learning in the broadest context of technological
change.
Program Educational Objectives

PEO1: Establish a successful professional career in industry, government or academia.

PEO2: Gain multidisciplinary knowledge providing a sustainable competitive edge in higher


studies or Research.

PEO3: Promote design, analyze, and exhibit of products, through strong communication,
leadership and ethical skills, to succeed an entrepreneurial.

CO-PO MAPPING:

Course Program Specific


Program Outcomes (POs)
Outcomes Outcomes (PSOs)
(COs) 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3

CO 1 √ √
CO 2 √ √
CO3 √ √
CO 4 √ √
CO 5 √ √
CO 6 √ √ √
INDEX

S.no Topic From To


1 Scope,Introduction, Objectives 1 1
2 Lecture Notes 2 33
3 Tutorial questions 34 34
4 University questions 35 35
5 Objective questions 36 38
6 Descriptive 39 39
7 Unit Test papers 40 47
8 Assignment Questions 48 48
9 Seminar Topics 49 49
10 Real time applications 50 51
11 NPTEL links 52 52
12 Blooms Taxonomy 53 55
13 GATE/Placement questions 56 58
14 OBE & Thinking abilities 59 59
UNIT-I

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.

 To gain knowledge on different types of linked lists.

 To understand about linear data structures like stacks and queues.

 To able to perform operations on stacks and queues.

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

A list can be implemented using arrays and linked lists.


Both Arrays and Linked List can be used to store linear data of similar types, but they both have
some advantages and disadvantages over each other.
Following are the points in favour of Linked Lists.
(1) The size of the arrays is fixed: So we must know the upper limit on the number of elements in
advance. Also, generally, the allocated memory is equal to the upper limit irrespective of the
usage, and in practical uses, upper limit is rarely reached.
(2) Inserting a new element in an array of elements is expensive, because room has to be created
for the new elements and to create room existing elements have to shifted.
For example, suppose we maintain a sorted list of IDs in an array id[].
id[] = [1000, 1010, 1050, 2000, 2040, …..].
And if we want to insert a new ID 1005, then to maintain the sorted order, we have to move all
the elements after 1000 (excluding 1000).
Deletion is also expensive with arrays until unless some special techniques are used. For
example, to delete 1010 in id[], everything after 1010 has to be moved.
So Linked list provides following two advantages over arrays
1) Dynamic size
2) Ease of insertion/deletion

Linked lists have following drawbacks:


1) Random access is not allowed. We have to access elements sequentially starting from the first
node. So we cannot do binary search with linked lists.
2) Extra memory space for a pointer is required with each element of the list.
3) Arrays have better cache locality that can make a pretty big difference in performance.

1.2 Single linked lists

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.

The formal definition of a single linked list is as follows...

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.

The graphical representation of a node in a single linked list is as follows...

☀ 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

In a single linked list we perform the following operations...

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...

1. Inserting At Beginning of the list


2. Inserting At End of the list
3. Inserting At Specific location in the list

Inserting At Beginning of the list

We can use the following steps to insert a new node at beginning of the single linked list...

 Step 1: Create a newNode with given value.


 Step 2: Check whether list is Empty (head == NULL)
 Step 3: If it is Empty then, set newNode→next = NULL and head = newNode.
 Step 4: If it is Not Empty then, set newNode→next = head and head = newNode.

Inserting At End of the 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...

 Step 1: Create a newNode with given value.


 Step 2: Check whether list is Empty (head == NULL)
 Step 3: If it is Empty then, set newNode → next = NULL and 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 node after which we
want to insert the newNode (until temp1 → data is equal to location, here location is the
node value after which we want to insert the newNode).
 Step 6: Every time check whether temp is reached to last node or not. If it is reached to
last node then display 'Given node is not found in the list!!! Insertion not
possible!!!' and terminate the function. Otherwise move the temp to next node.
 Step 7: Finally, Set 'newNode → next = temp → next' and 'temp → next = newNode'

Deletion

In a single linked list, the deletion operation can be performed in three ways. They are as
follows...

1. Deleting from Beginning of the list


2. Deleting from End of the list
3. Deleting a Specific Node

Deleting from Beginning of the list

We can use the following steps to delete a node from beginning of the single 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 a Node pointer 'temp' and initialize with head.
 Step 4: Check whether list is having only one node (temp → next == NULL)
 Step 5: If it is TRUE then set head = NULL and delete temp (Setting Empty list
conditions)
 Step 6: If it is FALSE then set head = temp → next, and delete temp.

5
Deleting from End of the list

We can use the following steps to delete a node from end of the single 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 'temp1' with head.
 Step 4: Check whether list has only one Node (temp1 → next == NULL)
 Step 5: If it is TRUE. Then, set head = NULL and delete temp1. And terminate 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 it reaches to the last node in the list. (until temp1 →
next == NULL)
 Step 7: Finally, Set temp2 → next = NULL and delete temp1.

Deleting a Specific Node from the list

We can use the following steps to delete a specific node from the single 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 'temp1' with head.
 Step 4: Keep moving the temp1 until it reaches to the exact node to be deleted or to the
last node. And every time set 'temp2 = temp1' before moving the 'temp1' to its next
node.
 Step 5: If it is reached to the last node then display 'Given node not found in the list!
Deletion not possible!!!'. And terminate the function.
 Step 6: If it is reached to the exact node which we want to delete, then check whether list
is having only one node or not
 Step 7: If list has only one node and that is the node to be deleted, then
set head = NULL and delete temp1 (free(temp1)).
 Step 8: If list contains multiple nodes, then check whether temp1 is the first node in the
list (temp1 == head).
6
 Step 9: If temp1 is the first node then move the head to the next node (head = head →
next) and delete temp1.
 Step 10: If temp1 is not first node then check whether it is last node in the list (temp1 →
next == NULL).
 Step 11: If temp1 is last node then set temp2 → next = NULL 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)).

Displaying a Single Linked List

We can use the following steps to display the elements of a single linked list...

 Step 1: Check whether list is Empty (head == NULL)


 Step 2: If it is Empty then, display 'List is Empty!!!' and terminate the function.
 Step 3: If it is Not Empty then, define a Node pointer 'temp' and initialize with head.
 Step 4: Keep displaying temp → data with an arrow (--->) until temp reaches to the last
node
 Step 5: Finally display temp → data with arrow pointing to NULL (temp → data --->
NULL).

1.3 Circular linked lists

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

In a circular linked list, we perform the following operations...

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...

1. Inserting At Beginning of the list


2. Inserting At End of the list
3. Inserting At Specific location in the list

Inserting At Beginning of the list

We can use the following steps to insert a new node at beginning of the circular linked list...

 Step 1: Create a newNode with given value.


 Step 2: Check whether list is Empty (head == NULL)
 Step 3: If it is Empty then, set head = newNode and newNode→next = head .

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'.

Inserting At End of the list

We can use the following steps to insert a new node at end of the circular linked list...

 Step 1: Create a newNode with given value.


 Step 2: Check whether list is Empty (head == NULL).
 Step 3: If it is Empty then, set head = newNode and newNode → next = 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 == head).
 Step 6: Set temp → next = newNode and newNode → next = head.

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 circular linked list...

 Step 1: Create a newNode with given value.


 Step 2: Check whether list is Empty (head == NULL)
 Step 3: If it is Empty then, set head = newNode and newNode → next = 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 node after which we
want to insert the newNode (until temp1 → data is equal to location, here location is the
node value after which we want to insert the newNode).
 Step 6: Every time check whether temp is reached to the last node or not. If it is reached
to last node then display 'Given node is not found in the list!!! Insertion not
possible!!!' and terminate the function. Otherwise move the temp to next node.
 Step 7: If temp is reached to the exact node after which we want to insert the newNode
then check whether it is last node (temp → next == head).

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...

1. Deleting from Beginning of the list


2. Deleting from End of the list
3. Deleting a Specific Node

Deleting from Beginning of the list

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.

Deleting from End of the list

We can use the following steps to delete a node from end 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 'temp1' with head.

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.

Deleting a Specific Node from the list

We can use the following steps to delete a specific node from 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 'temp1' with head.
 Step 4: Keep moving the temp1 until it reaches to the exact node to be deleted or to the
last node. And every time set 'temp2 = temp1' before moving the 'temp1' to its next
node.
 Step 5: If it is reached to the last node then display 'Given node not found in the list!
Deletion not possible!!!'. And terminate the function.
 Step 6: If it is reached to the exact node which we want to delete, then check whether list
is having only one node (temp1 → next == head)
 Step 7: If list has only one node and that is the node to be deleted then
set head = NULL and delete temp1 (free(temp1)).
 Step 8: If list contains multiple nodes then check whether temp1 is the first node in the
list (temp1 == head).
 Step 9: If temp1 is the first node then set temp2 = head and keep moving temp2 to its
next node until temp2 reaches to the last node. Then set head = head → next, temp2 →
next = head and delete temp1.
 Step 10: If temp1 is not first node then check whether it is last node in the list (temp1 →
next == head).

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)).

Displaying a circular Linked List

We can use the following steps to display the elements of a circular linked list...

 Step 1: Check whether list is Empty (head == NULL)


 Step 2: If it is Empty, then display 'List is Empty!!!' and terminate the function.
 Step 3: If it is Not Empty then, define a Node pointer 'temp' and initialize with head.
 Step 4: Keep displaying temp → data with an arrow (--->) until temp reaches to the last
node
 Step 5: Finally display temp → data with arrow pointing to head → data.

Applications of 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.

1.4 Double Linked List

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

In a double linked list, we perform the following operations...


1. Insertion
2. Deletion
3. Display

Insertion

In a double linked list, the insertion operation can be performed in three ways as follows...

1. Inserting At Beginning of the list


2. Inserting At End of the list
3. Inserting At Specific location in the list

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.

Inserting At End of the list

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.

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 double linked list...

 Step 1: Create a newNode with given value.


 Step 2: Check whether list is Empty (head == NULL)
 Step 3: If it is Empty then, assign NULL to newNode → previous & newNode →
next and newNode to head.
 Step 4: If it is not Empty then, define two node pointers temp1 & temp2 and
initialize temp1 with head.
 Step 5: Keep moving the temp1 to its next node until it reaches to the node after which
we want to insert the newNode (until temp1 → data is equal to location, here location is
the node value after which we want to insert the newNode).

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...

1. Deleting from Beginning of the list


2. Deleting from End of the list
3. Deleting a Specific Node

Deleting from Beginning of the list

We can use the following steps to delete a node from beginning 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.
 Step 3: If it is not Empty then, define a Node pointer 'temp' and initialize with head.
 Step 4: Check whether list is having only one node (temp → previous is equal to temp
→ next)
 Step 5: If it is TRUE, then set head to NULL and delete temp (Setting Empty list
conditions)
 Step 6: If it is FALSE, then assign temp → next to head, NULL to head →
previous and delete temp.

Deleting from End of the 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.

Deleting a Specific Node from the list

We can use the following steps to delete a specific node from 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.
 Step 3: If it is not Empty, then define a Node pointer 'temp' and initialize with head.
 Step 4: Keep moving the temp until it reaches to the exact node to be deleted or to the
last node.
 Step 5: If it is reached to the last node, then display 'Given node not found in the list!
Deletion not possible!!!' and terminate the fuction.
 Step 6: If it is reached to the exact node which we want to delete, then check whether list
is having only one node or not
 Step 7: If list has only one node and that is the node which is to be deleted then
set head to NULL and delete temp (free(temp)).
 Step 8: If list contains multiple nodes, then check whether temp is the first node in the
list (temp == head).
 Step 9: If temp is the first node, then move the head to the next node (head = head →
next), set head of previous to NULL (head → previous = NULL) and delete temp.
 Step 10: If temp is not the first node, then check whether it is the last node in the list
(temp → next == NULL).
 Step 11: If temp is the last node then set temp of previous of next to NULL (temp →
previous → next = NULL) and delete temp(free(temp)).

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)).

Displaying a Double Linked List

We can use the following steps to display the elements of a double linked list...

 Step 1: Check whether list is Empty (head == NULL)


 Step 2: If it is Empty, then display 'List is Empty!!!' and terminate the function.
 Step 3: If it is not Empty, then define a Node pointer 'temp' and initialize with head.
 Step 4: Display 'NULL <--- '.
 Step 5: Keep displaying temp → data with an arrow (<===>) until temp reaches to the
last node
 Step 6: Finally, display temp → data with arrow pointing to NULL (temp → data --->
NULL).

1.5 Stack ADT

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.

Stack can also be defined as

"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...

The following operations are performed on the stack...

1. Push (To insert an element on to the stack)


2. Pop (To delete an element from the stack)
3. Display (To display elements of the stack)

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.

push(value) - Inserting value into 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...

 Step 1: Check whether stack is FULL. (top == SIZE-1)


 Step 2: If it is FULL, then display "Stack is FULL!!! Insertion is not possible!!!" and
terminate the function.
 Step 3: If it is NOT FULL, then increment top value by one (top++) and set stack[top]
to value (stack[top] = value).

pop() - Delete a value from 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...

 Step 1: Check whether stack is EMPTY. (top == -1)


 Step 2: If it is EMPTY, then display "Stack is EMPTY!!! Deletion is not
possible!!!" and terminate the function.
 Step 3: If it is NOT EMPTY, then delete stack[top] and decrement top value by one
(top--).

19
display() - Displays the elements of a Stack

We can use the following steps to display the elements of a stack...

 Step 1: Check whether stack is EMPTY. (top == -1)


 Step 2: If it is EMPTY, then display "Stack is EMPTY!!!" and terminate the function.
 Step 3: If it is NOT EMPTY, then define a variable 'i' and initialize with top.
Display stack[i] value and decrement i value by one (i--).
 Step 3: Repeat above step until i value becomes '0'.

Stack using Linked List

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...

 Step 1: Create a newNode with given value.


 Step 2: Check whether stack is Empty (top == NULL)
 Step 3: If it is Empty, then set newNode → next = NULL.
 Step 4: If it is Not Empty, then set newNode → next = top.
 Step 5: Finally, set top = newNode.

pop() - Deleting an Element from a Stack

We can use the following steps to delete a node from the stack...

 Step 1: Check whether stack i.s Empty (top == NULL).


 Step 2: If it is Empty, then display "Stack is Empty!!! Deletion is not possible!!!" and
terminate the function
 Step 3: If it is Not Empty, then define a Node pointer 'temp' and set it to 'top'.
 Step 4: Then set 'top = top → next'.
 Step 7: Finally, delete 'temp' (free(temp)).

display() - Displaying stack of elements

We can use the following steps to display the elements (nodes) of a stack...

 Step 1: Check whether stack is Empty (top == NULL).


 Step 2: If it is Empty, then display 'Stack is Empty!!!' and terminate the function.
 Step 3: If it is Not Empty, then define a Node pointer 'temp' and initialize with top.
 Step 4: Display 'temp → data --->' and move it to the next node. Repeat the same
until temp reaches to the first node in the stack (temp → next != NULL).
 Step 4: Finally! Display 'temp → data ---> NULL'.

1.6 Applications of 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

Consider the following Infix Expression to be converted into Postfix Expression...

D=A+B*C

 Step 1: The Operators in the given Infix Expression : = , + , *


 Step 2: The Order of Operators according to their preference : * , + , =
 Step 3: Now, convert the first operator * ----- D = A + B C *
 Step 4: Convert the next operator + ----- D = A BC* +
 Step 5: Convert the next operator = ----- D ABC*+ =

Finally, given Infix Expression is converted into Postfix Expression as follows...

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

Consider the following Infix Expression...

(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-*

Postfix Expression Evaluation

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.

Postfix Expression has following general structure...

Operand1 Operand2 Operator

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

Consider the following Expression...

...

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.

1.8 Queue ADT

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 can be defined as follows...

Queue data structure is a linear data structure in which the operations are performed
based on FIFO principle.

A queue can also be defined as

"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 after inserting 25, 30, 51, 60 and 85.

The following operations are performed on a queue data structure...

1. enQueue(value) - (To insert an element into the queue)


2. deQueue() - (To delete an element from the queue)
3. display() - (To display the elements of the queue)

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.

Queue Using Array

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.

Queue data structure using array can be implemented as follows...

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.

enQueue(value) - Inserting value into the 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...

 Step 1: Check whether queue is FULL. (rear == SIZE-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 increment rear value by one (rear++) and
set queue[rear] = value.

deQueue() - Deleting a value from 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...

 Step 1: Check whether queue is EMPTY. (front =rear=-1)


 Step 2: If it is EMPTY, then display "Queue is EMPTY!!! Deletion is not
possible!!!" and terminate the function.
 Step 3: If it is NOT EMPTY, then increment the front value by one (front ++). Then
display queue[front] as deleted element. Then check whether both front and rear are
equal (front == rear), if it TRUE, then set both front and rear to '-1' (front = rear = -
1).

display() - Displays the elements of a Queue

We can use the following steps to display the elements of a queue...

 Step 1: Check whether queue is EMPTY. (front = rear=-1)


 Step 2: If it is EMPTY, then display "Queue is EMPTY!!!" and terminate the function.

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)

Queue using Linked List

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.

 Step 2: Check whether queue is Empty (rear == NULL)


 Step 3: If it is Empty then, set front = newNode and rear = newNode.
 Step 4: If it is Not Empty then, set rear → next = newNode and rear = newNode.

deQueue() - Deleting an Element from Queue

We can use the following steps to delete a node from the queue...

 Step 1: Check whether queue is Empty (front == NULL).


 Step 2: If it is Empty, then display "Queue is Empty!!! Deletion is not possible!!!" and
terminate from the function
 Step 3: If it is Not Empty then, define a Node pointer 'temp' and set it to 'front'.
 Step 4: Then set 'front = front → next' and delete 'temp' (free(temp)).

display() - Displaying the elements of Queue

We can use the following steps to display the elements (nodes) of a queue...

 Step 1: Check whether queue is Empty (front == NULL).


 Step 2: If it is Empty then, display 'Queue is Empty!!!' and terminate the function.
 Step 3: If it is Not Empty then, define a Node pointer 'temp' and initialize with front.
 Step 4: Display 'temp → data --->' and move it to the next node. Repeat the same until
'temp' reaches to 'rear' (temp → next != NULL).
 Step 4: Finally! Display 'temp → data ---> NULL'.

1.9 Circular 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.

A Circular Queue can be defined as follows...

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.

Graphical representation of a circular queue is as follows...

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.

enQueue(value) - Inserting value into the 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.

deQueue() - Deleting a value from the Circular Queue

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...

 Step 1: Check whether queue is EMPTY. (front == -1 && rear == -1)


 Step 2: If it is EMPTY, then display "Queue is EMPTY!!! Deletion is not
possible!!!" and terminate the function.

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).

display() - Displays the elements of a Circular Queue

We can use the following steps to display the elements of a circular queue...

 Step 1: Check whether queue is EMPTY. (front == -1)


 Step 2: If it is EMPTY, then display "Queue is EMPTY!!!" and terminate the function.
 Step 3: If it is NOT EMPTY, then define an integer variable 'i' and set 'i = front'.
 Step 4: Check whether 'front <= rear', if it is TRUE, then display 'queue[i]' value and
increment 'i' value by one (i++). Repeat the same until 'i <= rear' becomes FALSE.
 Step 5: If 'front <= rear' is FALSE, then display 'queue[i]' value and increment 'i' value
by one (i++). Repeat the same until'i <= SIZE - 1' becomes FALSE.
 Step 6: Set i to 0.
 Step 7: Again display 'cQueue[i]' value and increment i value by one (i++). Repeat the
same until 'i <= rear' becomes FALSE.

33
TUTORIAL QUESTIONS

1. Differentiate arrays and linked lists.


2. Design an algorithm to insert a node at the end of single linked list.
3. Write a C program to delete a node at the end of double linked list.
4. What are the advantages and applications of circular linked lists?
5. Convert the expression A+B*C-D into postfix expression using stacks.
6. Write an algorithm to count no:of nodes in single linked list.
7. What are the applications of stacks?
8. Define queue. What are the operations on queue?
9. Write an algorithm to perform push operation on stacks using arrays.
10. Write a C program to implement operations on queue using linked lists.

11. Perform the following operations on stack: push 5,puh 29, push100, pop, pop, push 225, pop.

12. Compare single and double linked lists.

13.Write an algorithm to display nodes in reverse order in a single linked list.

14. Give examples for stack overflow and underflow conditions.

15.List some real time applications of linear data structures.

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]

Ref topic 1.8


8. Differentiate between doubly and circular linked lists. [JNTUH MARCH 2017]
Ref topic 1.3 and 1.4
9. Write a C program to concatenate single linked lists. [JNTUH MARCH 2017]
Ref topic 1.2
10. Write a C function for popping an integer item from a stack. Assume stack is
implemented using array. [JNTUH MARCH 2017]
Ref topic 1.6

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

2. 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

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

4. Which of the following is false about a doubly linked list?


a) We can navigate in both the directions
b) It requires more space than a singly linked list
c) The insertion and deletion of a node take a bit longer
d) None of the mentioned

5. What is a memory efficient double linked list?


a) Each node has only one pointer to traverse the list back and forth
b) The list has breakpoints for faster traversal
c) An auxiliary singly linked list acts as a helper list to traverse through the doubly linked
list
d) None of the mentioned

6. What differentiates a circular linked list from a normal linked list?


a) You cannot have the ‘next’ pointer point to null in a circular linked list
b) It is faster to traverse the circular linked list
c) You may or may not have the ‘next’ pointer point to null in a circular linked list
d) All of the mentioned
36
7. Consider a small circular linked list. How to detect the presence of cycles in this list
effectively?
a) Keep one node as head and traverse another temp node till the end to check if its ‘next
points to head
b) Have fast and slow pointers with the fast pointer advancing two nodes at a time
and slow pointer advancing by one node at a time
c) Cannot determine, you have to pre-define if the list contains cycles
d) None of the mentioned

8. Process of inserting an element in stack is called _


a) Create
b) Push
c) Evaluation
d) Pop

9. Entries in a stack are “ordered”. What is the meaning of this statement?


a) A collection of stacks is sortable
b) Stack entries may be compared with the ‘<‘ operation
c) The entries are stored in a linked list
d) There is a Sequential entry that is one by one

10. Which of the following applications may use a stack?


a) A parentheses balancing program
b) Tracking of local variables at run time
c) Compiler Syntax Analyzer
d) All of the mentioned

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

1. Define single linked list. Explain its operations.


2. Explain the process of inserting and deleting elements in double linked list.
3. Describe the process of evaluating a postfix expression.
4. Write down the applications of circular queues.
5. Explain the operations that can be performed on stack.

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

2. A queue follows __________


a) FIFO (First In First Out) principle
b) LIFO (Last In First Out) principle
c) Ordered array
d) Linear tree

3. Circular Queue is also known as ________


a) Ring Buffer
b) Square Buffer
c) Rectangle Buffer
d) Curve Buffer

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

7. Queues serve major role in ______________


a) Simulation of recursion
b) Simulation of arbitrary linked list
c) Simulation of limited resource allocation
d) Simulation of heap sort

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

9. The result of evaluating the postfix expression 5, 4, 6, +, *, 4, 9, 3, /, +, * is?


a) 600
b) 350
c) 650
d) 588

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 +)

Answer the following:


1. Write about the operations performed on a single linked list.
2. Design an algorithm to convert an infix expression to postfix.

41
SET-2

1. Convert the following Infix expression to Postfix form using a stack


x + y * z + (p * q + r) * s, Follow usual precedence rule and assume that the expression is legal.
a) xyz*+pq*r+s*+
b) xyz*+pq*r+s+*
c) xyz+*pq*r+s*+
d) xyzp+**qr+s*+

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

3. Consider the following operation performed on a stack of size 5.


Push(1);
Pop();
Push(2);
Push(3);
Pop();
Push(4);
Pop();
Pop();
Push(5);
After the completion of all operation, the number of elements present in stack are
a) 1
b) 2
c) 3
d) 4

4. Which of the following is not an inherent application of stack?


a) Reversing a string
b) Evaluation of postfix expression
c) Implementation of recursion
d) Job scheduling

5. 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

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)

Answer the following:


1. Explain about double linked list.
2. Describe the operations performed on queue.

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)

6. Convert the following Infix expression to Postfix form using a stack


x + y * z + (p * q + r) * s, Follow usual precedence rule and assume that the expression is legal.
a) xyz*+pq*r+s*+
b) xyz*+pq*r+s+*
c) xyz+*pq*r+s*+
d) xyzp+**qr+s*+

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

8. Consider the following operation performed on a stack of size 5.


Push(1);
Pop();
Push(2);
Push(3);
Pop();
Push(4);
Pop();
Pop();
Push(5);
After the completion of all operation, the number of elements present in stack are
a) 1
b) 2
c) 3
d) 4

9. Which of the following is not an inherent application of stack?


a) Reversing a string
b) Evaluation of postfix expression
c) Implementation of recursion
d) Job scheduling

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

Answer the following:

1. Describe the operations on circular linked list.


2. Evaluate the postfix expression 3478+*-.

45
SET-4

1. Convert the following Infix expression to Postfix form using a stack


x + y * z + (p * q + r) * s, Follow usual precedence rule and assume that the expression is legal.
a) xyz*+pq*r+s*+
b) xyz*+pq*r+s+*
c) xyz+*pq*r+s*+
d) xyzp+**qr+s*+

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

3. Consider the following operation performed on a stack of size 5.


Push(1);
Pop();
Push(2);
Push(3);
Pop();
Push(4);
Pop();
Pop();
Push(5);
After the completion of all operation, the number of elements present in stack are
a) 1
b) 2
c) 3
d) 4

4. Which of the following is not an inherent application of stack?


a) Reversing a string
b) Evaluation of postfix expression
c) Implementation of recursion
d) Job scheduling

5. 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

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)

Answer the following:

1. Differentiate between arrays and linked lists.


2. Write a C program to perform operations on stack using arrays.

47
ASSIGNMENT QUESTIONS

1. Write a C program to perform operations on single linked list.


2. Write a C program to convert infix expression into postfix using stacks.
3. Write a C program to implement queue using arrays.
4. Write a C program to perform operations on circular linked list.
5. Write a C program to perform operations on double linked list.
6. Write a C program to implement stack using arrays.
7. Write a C program to implement queue using arrays.
8. Write a C program to implement stack using linked list.
9. Write a C program to implement stack using linked list.
10. Write a C program to implement evaluate postfix expression using stacks.

48
SEMINAR TOPICS

1. Arrays Vs Linked lists


2. Insertion operations on Single linked list
3. Single linked lists Vs Circular linked lists
4. Applications of stacks
5. Operations on queues

49
REAL TIME APPLICATIONS

1. Circular Linked Lists:

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

Topic 1 : Linked Lists

1. IIlustrate the use of single linked lists.


A list can be implemented using arrays and linked lists.
Both Arrays and Linked List can be used to store linear data of similar types, but they
both have some advantages and disadvantages over each other.
Following are the points in favour of Linked Lists.
(1) The size of the arrays is fixed: So we must know the upper limit on the number of
elements in advance. Also, generally, the allocated memory is equal to the upper limit
irrespective of the usage, and in practical uses, upper limit is rarely reached.
(2) Inserting a new element in an array of elements is expensive, because room has to be
created for the new elements and to create room existing elements have to shifted.
So Linked list provides following two advantages over arrays
1) Dynamic size
2) Ease of insertion/deletion

2. Design an algorithm to insert element at the beginning in single linked list.

We can use the following steps to insert a new node at beginning of the single linked list...

 Step 1: Create a newNode with given value. 


 Step 2: Check whether list is Empty (head == NULL)
 Step 3: If it is Empty then, set newNode→next = NULL and head = newNode.
 Step 4: If it is Not Empty then, set newNode→next = head and head = newNode.

3. Write down the applications of 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.

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

1. Represent stack data structure diagrammatically.

2. Design an algorithm to insert an element into stack using arrays.

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...

 Step 1: Check whether stack is FULL. (top == SIZE-1)


 Step 2: If it is FULL, then display "Stack is FULL!!! Insertion is not possible!!!" and
terminate the function. 
 Step 3: If it is NOT FULL, then increment top value by one (top++) and set stack[top]
to value (stack[top] = value).
3. Write down the applications of 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.
 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.
 Back/Forward stacks on browsers.
 Support for recursion


      
GATE/PLACEMENT QUESTIONS

1) Mention what is Linked lists?

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.

2) What type of memory allocation is referred for Linked lists?


Dynamic memory allocation is referred for Linked lists.

3) Mention what is traversal in linked lists?


Term Traversal is used to refer the operation of processing each element in the list.

4) Describe what is Node in link list? And name the types of Linked Lists?
Together (data + link) is referred as the Node.

Types of Linked Lists are,

 Singly Linked List


 Doubly Linked List
 Multiply Linked List
 Circular Linked List
5) Mention what is Singly Linked list?

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,

Linear Array Linked List

 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

 In it space is wasted  In it space is not wasted

 It is expensive  It is not expensive

 It cannot be reduced or extended  It can be reduced or extended according to


according to requirements requirements

 To avail each element same amount of  To avail each element different amount of time
time is required. is required.

 In consecutive memory locations  Elements may or may not be stored in


elements are stored. consecutive memory locations

 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,

 Linked lists are used to implement queues, stacks, graphs, etc.


 In Linked Lists you don’t need to know the size in advance.
 Linked lists let you insert elements at the beginning and end of the list.
8) What does the dummy header in linked list contain?
In linked list, the dummy header contains the first record of the actual data

10) Mention what is the difference between singly and doubly linked lists?
A doubly linked list nodes contain three fields:

 An integer value and


 Two links to other nodes
 one to point to the previous node and
 other to point to the next node.

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:

 Create a new item and set its value


 Link the new item to point to the head of the list
 Set the head of the list to be our new item

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.

13) Mention what is the biggest advantage of linked lists?


The biggest benefit of linked lists is that you do not specify a fixed size for your list. The more elements you add
to the chain, the bigger the chain gets.

14) Mention how to delete first node from singly linked list?
To delete first node from singly linked list

 Store Current Start in Another Temporary Pointer


 Move Start Pointer One position Ahead
 Delete temp i.e Previous Starting Node as we have Updated Version of Start Pointer

58
Course Outcomes

 Ability to select the data structures that efficiently model the information in a problem.

 Ability to assess efficiency trade-offs among different linear data structure


implementations or combinations.
 Implement and know the applications of stacks and queues.
 Design programs using a variety of data structures.

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

You might also like