Unit 2 - DS - W2 - Class
Unit 2 - DS - W2 - Class
Unit 2 - DS - W2 - Class
A B C
5
A linked list can also be defined as the collection of
the nodes in which one node is connected to
another node, and node consists of two parts, i.e.,
one is the data part and the second one is the
address part, as shown in the below figure:
10
primitive operations on linked list
11
Type of Linked list
• Simple Linked List − Item navigation is forward only.
• Doubly Linked List − Items can be navigated forward
and backward.
• Circular Linked List − Last item contains link of the first
element as next and the first element has a link to the
last element as previous.
12
Inserting a Node in a
List
13
•The insertion into a singly linked list can
performed at different positions. Based be
position of the new node being inserted, the insertion
on the
is categorized into the following categories.
14
Create a node in Linked list
15
Algorithm for creating node
• Through create function, we are creating only one node, i.e the first
node in the list. The remaining nodes will be created through
Insertion function.
Inserting a node in the Linked List
• Insert a node in the linked list is possible in three ways:
• In beginning
• In end
• Insert in specified position
Inserting a node at beginning
Inserting a node at End
Inserting a node at given position
#include <stdio.h> // Function to traverse and print the list
#include <stdlib.h>
void traverse() {
// Singly Linked List structure
struct Node* current = head;
struct Node {
while (current != NULL) {
int data;
} else {
}
return 0;
// Link the new node to the last node
}
current->next = newNode;
} }
Deleting a node in the Linked List
• Delete a node in the linked list is possible in three ways:
• First node
• Last node
• Delete node at given position
Delete a node at beginning
Delete a node at End
Delete a node at given position
Display Operation
Searching a Linked List
• If we want to search any element in the single linked list the below
method can be used:
• For Example
• Let us say we want to search 20 in the above give details, Assume Key
is 20 (You are going to search)
• So Procedure for searching
Procedure for Searching a Linked List
Disadvantage of Single Linked List
•In a singly linked list, we could traverse only in one
direction, because each node contains address of
the next node and it doesn't have any record of its
previous nodes.
•However, doubly linked list overcome this limitation
of singly linked list.
•Due to the fact that, each node of the list contains
the address of its previous node, we can find all the
details about the previous node as well by using the
previous address stored inside the previous part of
each node.
Doubly linked list
•Doubly linked list is a complex type of linked list in
which a node contains a pointer to the previous as
well as the next node in the sequence.
•Therefore, in a doubly linked list, a node consists of
three parts: node data, pointer to the next node in
sequence (next pointer) , pointer to the previous
node (previous pointer).
• A sample node in a doubly linked list is shown in the
figure.
Memory Representation of a doubly linked list
• In the following image, the first
element of the list that is i.e. 13
stored at address 1. The head
pointer points to the starting
address 1. Since this is the first
element being added to the list
therefore the prev of the
list contains null. The next node
of the list resides at address 4
therefore the first node contains
4 in its next pointer.
• We can traverse the list in this
way until we find any node
containing null or 1 in its next
part.
Doubly linked list
•Using Doubly linked list (DLL), We can traverse (or) visit the
list of elements in both the directions.
•Primitive Operation:
•Create
•Insert
•Delete
•Display
structure of a node in doubly linked list can
be given as :
•Declaration:
How to create a node in DLL, So first the create a structure,
struct node
{
struct node *prev;
int data;
struct node *next;
}
The prev part of the first node and the next part of the last node will
always contain null indicating end in each direction.
Sample code
Insert in DLL
•Insert in Beginning
•Insert in end
•Insert in Required position
Insertion at beginning in DLL
Insertion at End in DLL
Sample code for Insertion at End in DLL
Insertion at Required position in DLL
Sample code for Insertion at Required
position in DLL
Delete in DLL
•Delete in Beginning
•Delete in end
•Delete in Required position
Delete at beginning in DLL
Delete at End in DLL
Sample code for Delete at End in DLL
Delete at Required position in DLL
Sample code for Delete at Required position
in DLL
Display Operation in DLL
Circular Linked List
•Circular Linked List is a variation of Linked list in which
the first element points to the last element and the last
element points to the first element.
•Type
•Singly Linked List as Circular
•Doubly Linked List as Circular
Singly Linked List as Circular
• In singly linked list, the next pointer of the last node points to the
first node.
Doubly Linked List as Circular
• In doubly linked list, the next pointer of the last node points to
the first node and the previous pointer of the first node points to
the last node making the circular in both directions.
Basic Operations
• Following are the important operations supported by a circular
Single linked list.
• insert − Inserts an element at the start of the list.
• delete − Deletes an element from the start of the list.
• display − Displays the list.
Insert in Single Linked List as Circular
• Insertion in single circular linked list:
• It is possible at 3 positions
•At beginning
•At ending
•At any position
At beginning - single circular linked list
Inserting At Beginning of the list
Steps to insert a new node at beginning of the circular linked list.
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'.
At beginning - single circular linked list
Inserting At the End of the list
• Steps to insert a new node at end of the circular linked list.
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 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).
Inserting At Specific location in the list (After a Node)
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.
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).
Step 8 - If temp is last node then set temp → next = newNode and
Step 9 - If temp is not last node then set newNode → next = temp → next
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 first node from Singly Circular Linked List
• Given a Circular Linked List. The task is to write programs to delete nodes from this list
present at:
• First position.
• Last Position.
• At any given position i.
DELETING FROM END OF THE LIST
• The following steps to delete a node from end of the circular linked list.
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 == 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)
•Josephus problem,
•Addition of two polynomials,
•Sparse Matrix
Josephus problem
Example
Start at 1
Start at 1
Program logic with diagram
Deleting 3
Addition Of Two Polynomials
• What is polynomial:
• A polynomial p(x) is the expression in variable x which is in
the form (axn + bxn-1 + …. + jx+ k), where a, b, c …., k fall in
the category of real numbers and 'n' is non negative
integer, which is called the degree of polynomial.
• An essential characteristic of the polynomial is that each
term in the polynomial expression consists of two parts:
• one is the coefficient
• other is the exponent
• Example
• 10x2 + 26x, here 10 and 26 are coefficients and 2, 1 is its
exponential value.
Polynomial Representation
Linked list representation for Polynomial
Syntax for polynomial
• Declaration
Algorithm
Sparse Matrix
•A matrix can be defined as a two-dimensional array
having 'm' columns and 'n' rows representing m*n
matrix. Sparse matrices are those matrices that have
the majority of their elements equal to zero.
•In other words, the sparse matrix can be defined as
the matrix that has a greater number of zero
elements than the non-zero elements.
Why do we need to use a sparse matrix instead of a simple
matrix?
•The following are the advantages of using a sparse
matrix:
• Storage: As we know, a sparse matrix that contains lesser
non-zero elements than zero so less memory can be used
to store elements. It evaluates only the non-zero
elements.
• Computing time: In the case of searching n sparse
matrix, we need to traverse only the non-zero elements
rather than traversing all the sparse matrix elements. It
saves computing time by logically designing a data
structure traversing non-zero elements.
Sparse Matrix Representation - Array representation
• The non-zero elements can be stored with triples, i.e., rows, columns, and value.
The sparse matrix can be represented by using Array representation
• Array Representation
• The 2d array can be used to represent a sparse matrix in which there are three rows
named as:
1. Row: It is an index of a row where a non-zero element is located.
2. Column: It is an index of the column where a non-zero element is located.
3. Value: The value of the non-zero element is located at the index (row, column).
• Let's understand the sparse matrix using array representation through an
example.
Answer - Sparse Matrix Representation - Array representation
• As we can observe above, that sparse matrix is represented using triplets, i.e., row,
column, and value. In the above sparse matrix, there are 13 zero elements and 7
non-zero elements.
• This sparse matrix occupies 54 20 memory space. If the size of the sparse matrix
is increased, then the wastage of memory space will also be increased.
• The above sparse matrix can be represented in the tabular form shown as below:
Row 🡪
Column 🡪
Value 🡪
Linked List representation of the sparse
matrix
• In a linked list representation, the linked list data structure is used to represent the
sparse matrix. The advantage of using a linked list to represent the sparse matrix is
that the complexity of inserting or deleting a node in a linked list is lesser than the
array.
• Unlike the array representation, a node in the linked list representation consists of four
fields. The four fields of the linked list are given as follows -
• Row - It represents the index of the row where the non-zero element is located.
• Column - It represents the index of the column where the non-zero element is located.
• Value - It is the value of the non-zero element that is located at the index (row,
column).
• Next node - It stores the address of the next node.
• The node structure of the linked list representation of the sparse matrix is shown in the
below image -
• Example -
• Let's understand the linked list representation of sparse matrix with the help of the example given below -
• Consider the sparse matrix -
• In the above figure, we can observe a 44 sparse matrix containing 5 non-zero elements and 11 zero elements. Above matrix
occupies 44 16 memory space. Increasing the size of matrix will increase the wastage space.
• The linked list representation of the above matrix is given below –
• In the above figure, the sparse matrix is represented in the linked list form. In the node, the first field represents the index of the
row, the second field represents the index of the column, the third field represents the value, and the fourth field contains the
address of the next node.
• In the above figure, the first field of the first node of the linked list contains 0, which means 0th row, the second field contains 2,
which means 2nd column, and the third field contains 1 that is the non-zero element. So, the first node represents that element 1 is
stored at the 0th row-2nd column in the given sparse matrix. In a similar manner, all of the nodes represent the non-zero elements of
the sparse matrix.
#include<stdio.h> // Making of new matrix
int main() int k = 0;
{ for (int i = 0; i < 4; i++)
// Assume 4x5 sparse matrix
for (int j = 0; j < 5; j++)
int sparseMatrix[4][5] =
if (sparseMatrix[i][j] != 0)
{
{
{0 , 0 , 3 , 0 , 4 },
compactMatrix[0][k] = i;
{0 , 0 , 5 , 7 , 0 },
{0 , 0 , 0 , 0 , 0 },
compactMatrix[1][k] = j;
{0 , 2 , 6 , 0 , 0 } compactMatrix[2][k] = sparseMatrix[i][j];
}; k++;
}
int size = 0;
for (int i = 0; i < 4; i++) for (int i=0; i<3; i++)
for (int j = 0; j < 5; j++) {
if (sparseMatrix[i][j] != 0) for (int j=0; j<size; j++)
size++;
printf("%d ", compactMatrix[i][j]);