Unit I
Unit I
Objectives:
1. To apply the concepts of Linked List in the applications of various linear data
structures.
2. To demonstrate the understanding of stacks, queues and their applications.
3. To apply the concepts of Linked List in the applications of various nonlinear data
structures.
4. To understand the implementation of graphs and their applications.
5. To be able to incorporate various sorting and hashing techniques in real time scenarios
UNIT-I LINEAR DATA STRUCTURE – LIST
Self-Referential Structures, Dynamic Memory Allocation, Linked list implementation -
Singly Linked List, Doubly Linked List, Circular Linked List, Applications of List.
UNIT-II LINEAR DATA STRUCTURE –STACK AND QUEUE
Stack – Operations, Array and Linked list implementation, Applications – Evaluation of
Arithmetic Expressions, Queues- Operations, Array and Linked list Implementation.
UNIT-III NONLINEAR DATA STRUCTURE –TREES
Tree Terminologies, Binary Tree Representation, Tree Traversals, Binary Search Trees,
Binary Heap, Height Balance Trees – AVL Trees.
UNIT-IV NONLINEAR DATA STRUCTURE –GRAPH
Representation of Graphs, Topological Sort, Depth First Search and Breadth-First Search,
Minimum Spanning Tree – Prim's Algorithm, Shortest path algorithm – Dijikstra’s
Algorithm.
UNIT-V SORTING AND HASHING
Sorting Techniques –Insertion Sort, Quick Sort, Merge Sort, Hashing- Hashing functions –
Mid square, Division, Folding, Collision Resolution Techniques – Separate Chaining – Open
Addressing – Rehashing.
Course Outcomes:
On completion of the course, the students will be able to
1. Understand and apply the various concepts of Linear data Structures
2. Understand and apply the various concepts of Non Linear data Structures.
3. Understand and apply the various sorting and hashing concepts.
4. Analyse and apply the suitable data structure for their research.
5. Choose efficient data structures and apply them to solve real world problems.
SUGGESTED ACTIVITIES
● Role play- Linked List (Unit 1).
● Mind Map, Poster Design - Stack and Queue (Unit 2).
● Flipped Classroom - Binary Heap (Unit 3).
● Poster Design - Graph (Unit4).
● Implementation of small module- Hashing (Unit5).
SUGGESTED EVALUATION METHODS
● Assignment problems - Linked List (Unit 1).
● Tutorial problems - Applications – Evaluation of Arithmetic Expressions (Unit 2).
● Quizzes - BST and Binary Heap (Unit 3).
● Tutorial problems- Graph traversal (Unit 4).
● Quizzes - Hashing and Sorting(Unit5) .
Text Books(s):
1 “Data Structures and Algorithm Analysis in C”, Mark Allen Weiss, 2nd Edition, Pearson
Education, 2005
2 “Data Structures and Algorithm Analysis in C++ - Anna University, Mark Allen Weiss,
Pearson Education, 2017.
Reference Books:
1 “Data Structures Using C and C++”, Langsam, Augenstein and Tanenbaum, 2nd Edition,
Pearson Education, 2015.
2 Thomas H. Cormen, Charles E. Leiserson, Ronald L.Rivest, Clifford Stein, Introduction to
Algorithms”, Fourth Edition, Mcgraw Hill/ MIT Press, 2022.
2. Linked Lists:
Like arrays, Linked List is a linear data structure. Unlike arrays, linked list elements
are not stored at a contiguous location; the elements are linked using pointers.
3. Stack:
Stack is a linear data structure which follows a particular order in which the
operations are performed. The order may be LIFO (Last In First Out) or FILO (First In Last
Out). In stack, all insertion and deletion are permitted at only one end of the list.
Stack Operations:
1. push(): When this operation is performed, an element is inserted into the stack.
2. pop(): When this operation is performed, an element is removed from the top of the
stack and is returned.
3. top(): This operation will return the last inserted element that is at the top without
removing it.
4. size(): This operation will return the size of the stack i.e. the total number of elements
present in the stack.
5. isEmpty(): This operation indicates whether the stack is empty or not.
4. Queue:
Like Stack, Queue is a linear structure which follows a particular order in which the
operations are performed. The order is First in First out (FIFO). In the queue, items are
inserted at one end and deleted from the other end. A good example of the queue is any queue
of consumers for a resource where the consumer that came first is served first. The difference
between stacks and queues is in removing. In a stack we remove the item the most recently
added; in a queue, we remove the item the least recently added.
Queue Operations:
1. Enqueue(): Adds (or stores) an element to the end of the queue..
2. Dequeue(): Removal of elements from the queue.
3. Peek() or front(): Acquires the data element available at the front node of the
queue without deleting it.
4. rear(): This operation returns the element at the rear end without removing it.
5. isFull(): Validates if the queue is full.
6. isNull(): Checks if the queue is empty.
5. Binary Tree:
Unlike Arrays, Linked Lists, Stack and queues, which are linear data structures, trees
are hierarchical data structures. A binary tree is a tree data structure in which each node has at
most two children, which are referred to as the left child and the right child. It is implemented
mainly using Links. A Binary Tree is represented by a pointer to the topmost node in the tree.
If the tree is empty, then the value of root is NULL. A Binary Tree node contains the
following parts.
1. Data
2. Pointer to left child
3. Pointer to the right child
6. Binary Search Tree:
A Binary Search Tree is a Binary Tree following the additional properties:
● The left part of the root node contains keys less than the root node key.
● The right part of the root node contains keys greater than the root node key.
● There is no duplicate key present in the binary tree.
● A Binary tree having the following properties is known as Binary search tree
(BST).
7.Heap:
A Heap is a special Tree-based data structure in which the tree is a complete binary
tree. Generally, Heaps can be of two types:
Max-Heap: In a Max-Heap the key present at the root node must be greatest among the keys
present at all of its children. The same property must be recursively true for all sub-trees in
that Binary Tree.
Min-Heap: In a Min-Heap the key present at the root node must be minimum among the
keys present at all of its children. The same property must be recursively true for all sub-trees
in that Binary Tree.
8. Hash Table Data Structure
Hashing is an important Data Structure which is designed to use a special function
called the Hash function which is used to map a given value with a particular key for faster
access of elements. The efficiency of mapping depends on the efficiency of the hash function
used. Let a hash function H(x) maps the value x at the index x%10 in an Array. For example,
if the list of values is [11, 12, 13, 14, 15] it will be stored at positions {1, 2, 3, 4, 5} in the
array or Hash table respectively.
9. Matrix:
A matrix represents a collection of numbers arranged in an order of rows and
columns. It is necessary to enclose the elements of a matrix in parentheses or brackets.
A matrix with 9 elements is shown below.
10. Trie
Trie is an efficient information retrieval data structure. Using Trie, search
complexities can be brought to an optimal limit (key length). If we store keys in the binary
search tree, a well-balanced BST will need time proportional to M * log N, where M is
maximum string length and N is the number of keys in the tree. Using Trie, we can search the
key in O(M) time. However, the penalty is on Trie storage requirements.
11. Graph:
Graph is a data structure that consists of a collection of nodes (vertices) connected by
edges. Graphs are used to represent relationships between objects and are widely used in
computer science, mathematics, and other fields. Graphs can be used to model a wide variety
of real-world systems, such as social networks, transportation networks, and computer
networks.
We mainly find the node after which we need to insert the new node. If we encounter
a NULL before reaching that node, it means that the given position is invalid. Below is the
function for insertion at a specific position of the singly linked list:
Deletion at the Beginning of Singly Linked List:
To delete the first node, update the head to point to the second node in the list.
Steps-by-step approach:
1. Check if the head is NULL.
2. If it is, return NULL (the list is empty).
3. Store the current head node in a temporary variable temp.
4. Move the head pointer to the next node.
5. Delete the temporary node.
6. Return the new head of the linked list.
Below is the function for deletion at the beginning of singly linked list:
Deletion at the End of Singly Linked List:
To delete the last node, traverse the list until the second-to-last node and update its
next field to none.
Step-by-step approach:
1. Check if the head is NULL.
2. If it is, return NULL (the list is empty).
3. Check if the head's next is NULL (only one node in the list).
4. If true, delete the head and return NULL.
5. Traverse the list to find the second last node (second_last).
6. Delete the last node (the node after second_last).
7. Set the next pointer of the second last node to NULL.
8. Return the head of the linked list.
Below is the function for deletion at the end of singly linked list:
Deletion at a Specific Position of Singly Linked List
To delete a node at a specific position, traverse the list to the desired position, update
the links to bypass the node to be deleted.
Step-by-step approach:
1. Check if the list is empty or the position is invalid, return if so.
2. If the head needs to be deleted, update the head and delete the node.
3. Traverse to the node before the position to be deleted.
4. If the position is out of range, return.
5. Store the node to be deleted.
6. Update the links to bypass the node.
7. Delete the stored node.
Below is the function for deletion at a specific position of singly linked list:
Doubly Linked List
A doubly linked list is a more complex data structure than a singly linked list, but it
offers several advantages. The main advantage of a doubly linked list is that it allows for
efficient traversal of the list in both directions. This is because each node in the list contains a
pointer to the previous node and a pointer to the next node. This allows for quick and easy
insertion and deletion of nodes from the list, as well as efficient traversal of the list in both
directions.
What is a Doubly Linked List?
A doubly linked list is a data structure that consists of a set of nodes, each of which
contains a value and two pointers, one pointing to the previous node in the list and one
pointing to the next node in the list. This allows for efficient traversal of the list in both
directions, making it suitable for applications where frequent insertions and deletions are
required.
Representation of Doubly Linked List in Data Structure
In a data structure, a doubly linked list is represented using nodes that have three fields:
1. Data
2. A pointer to the next node (next)
3. A pointer to the previous node (prev)
Operations on doubly linked list
1. Traversal in Doubly Linked List
2. Searching in Doubly Linked List
3. Finding Length of Doubly Linked List
4. Insertion in Doubly Linked List:
● Insertion at the beginning of Doubly Linked List
● Insertion at the end of the Doubly Linked List
● Insertion at a specific position in Doubly Linked List
5. Deletion in Doubly Linked List:
● Deletion of a node at the beginning of Doubly Linked List
● Deletion of a node at the end of Doubly Linked List
● Deletion of a node at a specific position in Doubly Linked List
To insert a new node at the beginning of the doubly list, we can use the following steps:
● Create a new node, say new_node with the given data and set its previous pointer to
null, new_node->prev = NULL.
● Set the next pointer of new_node to current head, new_node->next = head.
● If the linked list is not empty, update the previous pointer of the current head to
new_node, head->prev = new_node.
● Return new_node as the head of the updated linked list.
Insertion at the End of Doubly Linked List
To insert a new node at the end of the doubly linked list, we can use the following steps:
1. Allocate memory for a new node and assign the provided value to its data field.
2. Initialize the next pointer of the new node to nullptr.
3. If the list is empty:
● Set the previous pointer of the new node to nullptr.
● Update the head pointer to point to the new node.
4. If the list is not empty:
● Traverse the list starting from the head to reach the last node.
● Set the next pointer of the last node to point to the new node.
● Set the previous pointer of the new node to point to the last node.
To delete a node at the beginning in doubly linked list, we can use the following steps:
1. Check if the list is empty, there is nothing to delete. Return.
2. Store the head pointer in a variable, say temp.
3. Update the head of linked list to the node next to the current head, head =
head->next.
4. If the new head is not NULL, update the previous pointer of new head to
NULL, head->prev = NULL.
Deletion at the End of Doubly Linked List
To delete a node at the end in doubly linked list, we can use the following steps:
1. Check if the doubly linked list is empty. If it is empty, then there is nothing to delete.
2. If the list is not empty, then move to the last node of the doubly linked list, say curr.
3. Update the second-to-last node's next pointer to NULL, curr->prev->next = NULL.
4. Free the memory allocated for the node that was deleted.
Deletion at a Specific Position in Doubly Linked List
To delete a node at a specific position in doubly linked list, we can use the following steps:
1. Traverse to the node at the specified position, say curr.
2. If the position is valid, adjust the pointers to skip the node to be deleted.
● If curr is not the head of the linked list, update the next pointer of the node
before curr to point to the node after curr, curr->prev->next = curr-next.
● If curr is not the last node of the linked list, update the previous pointer of the
node after curr to the node before curr, curr->next->prev = curr->prev.
3. Free the memory allocated for the deleted node.
Advantages of Doubly Linked List
1. Efficient traversal in both directions: Doubly linked lists allow for efficient
traversal of the list in both directions, making it suitable for applications where
frequent insertions and deletions are required.
2. Easy insertion and deletion of nodes: The presence of pointers to both the previous
and next nodes makes it easy to insert or delete nodes from the list, without having to
traverse the entire list.
3. Can be used to implement a stack or queue: Doubly linked lists can be used to
implement both stacks and queues, which are common data structures used in
programming.
4. Disadvantages of Doubly Linked List
5. More complex than singly linked lists: Doubly linked lists are more complex than
singly linked lists, as they require additional pointers for each node.
6. More memory overhead: Doubly linked lists require more memory overhead than
singly linked lists, as each node stores two pointers instead of one.
Applications of Doubly Linked List
1. Implementation of undo and redo functionality in text editors.
2. Cache implementation where quick insertion and deletion of elements are required.
3. Browser history management to navigate back and forth between visited pages.
4. Music player applications to manage playlists and navigate through songs efficiently.
5. Implementing data structures like Deque (double-ended queue) for efficient insertion
and deletion at both ends.
Circular linked list
A circular linked list is a data structure where the last node connects back to the first,
forming a loop. This structure allows for continuous traversal without any interruptions.
Circular linked lists are especially helpful for tasks like scheduling and managing
playlists, this allowing for smooth navigation. In this tutorial, we’ll cover the basics of
circular linked lists, how to work with them, their advantages and disadvantages, and their
applications.
What is a Circular Linked List?
A circular linked list is a special type of linked list where all the nodes are connected
to form a circle. Unlike a regular linked list, which ends with a node pointing to NULL, the
last node in a circular linked list points back to the first node. This means that you can keep
traversing the list without ever reaching a NULL value.
Types of Circular Linked Lists
We can create a circular linked list from both singly linked lists and doubly linked
lists. So, circular linked list are basically of two types:
1. Circular Singly Linked List
In Circular Singly Linked List, each node has just one pointer called the “next”
pointer. The next pointer of last node points back to the first node and this results in forming
a circle. In this type of Linked list we can only move through the list in one direction.