DS Unit-2 Material
DS Unit-2 Material
1. Define single linked list. Write algorithms to insert a node at the beginning ending and at
a given position.
Single linked list: Singly linked list can be defined as the collection of ordered set of elements.
The number of elements may vary according to need of the program. A node in the singly
linked list consist of two parts: data part and link part. Data part of the node stores actual
information that is to be represented by the node while the link part of the node stores the
address of its immediate successor.
Consider an example where the marks obtained by the student in three subjects are stored in a
linked list as shown in the figure.
In the above figure, the arrow represents the links. The data part of every node contains the
marks obtained by the student in the different subject. The last node in the list is identified by
the null pointer which is present in the address part of the last node. We can have as many
elements we require, in the data part of the list.
Algorithm to insert a node at the beginning
Step 1: If Ptr = Null
Write Overflow
Go To Step 7
[End Of If]
Step 2: Set New_Node = Ptr
Step 3: Set Ptr = Ptr → Next
Step 4: Set New_Node → Data = Val
Step 5: Set New_Node → Next = Head
Step 6: Set Head = New_Node
Step 7: Exit
3. With neat diagram explain the following operations in single linked list.
a. Insert element at the beginning
b. Insert element at the end
c. Delete the specified element
d. Search the given element
Insert element at the beginning:
We can use the following steps to insert a new node at beginning of the single linked list...
Step 1 - Create a new Node 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.
At any moment, the data of the node matches with the val, stop, and return 1.
If, during traversal, the temp reaches the end, it means that it has reached the last
point, which is NULL, hence, returns false at the end.
4. Define double linked list. Write algorithms to delete a node at the beginning, ending and at
a given position.
Double linked list:
A double linked list or a two-way linked list is a more complex type of linked list which
contains a pointer to the next as well as the previous node in the sequence. Therefore, it consists
of three parts—data, a pointer to the next node, and a pointer to the previous node
Algorithm to delete the node at beginning:
Step 1: If Head = Null
Write Underflow
Goto Step 6
Step 2: Set Ptr = Head
Step 3: Set Head = Head → Next
Step 4: Set Head → Prev = Null
Step 5: Free Ptr
Step 6: Exit
Algorithm to delete the node at a specific location:
Step 1: If Head = Null
Write Underflow
Go To Step 9
[End Of If]
Step 2: Set Temp = Head
Step 3: Repeat Step 4 While Temp -> Data != Item
Step 4: Set Temp = Temp -> Next
[End Of Loop]
Step 5: Set Ptr = Temp -> Next
Step 6: Set Temp -> Next = Ptr -> Next
Step 7: Set Ptr -> Next -> Prev = Temp
Step 8: Free Ptr
Step 9: Exit.
Algorithm to delete the node at end:
Step 1: If Head = Null
Write Underflow
Go To Step 7
[End Of If]
Step 2: Set Temp = Head
Step 3: Repeat Step 4 While Temp->Next != Null
Step 4: Set Temp = Temp->Next
[End Of Loop]
Step 5: Set Temp ->Prev-> Next = Null
Step 6: Free Temp
Step 7: Exit.
5. Write algorithms to insert a node at the beginning, ending and at a given position in the
double linked list.
Algorithm to insert a node at the beginning
Step 1: If Ptr = Null
Write Overflow
Go To Step 9
[End Of If]
Step 2: Set New_Node = Ptr
Step 3: Set Ptr = Ptr -> Next
Step 4: Set New_Node -> Data = Val
Step 5: Set New_Node -> Prev = Null
Step 6: Set New_Node -> Next = Start
Step 7: Set Head -> Prev = New_Node
Step 8: Set Head = New_Node
Step 9: Exit
Alogorithm to insert a node at a specific position
Step 1: If Ptr = Null
Write Overflow
Go To Step 15
[End Of If]
Step 2: Set New_Node = Ptr
Step 3: Set Ptr = Ptr -> Next
Step 4: Set New_Node -> Data = Val
Step 5: Set Temp = Start
Step 6: Set I = 0
Step 7: Repeat 8 To 10 Until I<="" Li="">
Step 8: Set Temp = Temp -> Next
Step 9: If Temp = Null
Step 10: Write "Less Than Desired No. Of Elements"
Goto Step 15
[End Of If]
[End Of Loop]
Step 11: Set New_Node -> Next = Temp -> Next
Step 12: Set New_Node -> Prev = Temp
Step 13 : Set Temp -> Next = New_Node
Step 14: Set Temp -> Next -> Prev = New_Node
Step 15: Exit.
Alogorithm to insert a node at end
Step 1: If Ptr = Null
Write Overflow
Go To Step 11
[End Of If]
Step 2: Set New_Node = Ptr
Step 3: Set Ptr = Ptr -> Next
Step 4: Set New_Node -> Data = Val
Step 5: Set New_Node -> Next = Null
Step 6: Set Temp = Start
Step 7: Repeat Step 8 While Temp -> Next != Null
Step 8: Set Temp = Temp -> Next
[End Of Loop]
Step 9: Set Temp -> Next = New_Node
Step 10: Set New_Node -> Prev = Temp
Step 11: Exit.
6. Define circular linked list. Write algorithms to insert a node at the beginning ending and at
a given position.
(or)
What are the advantages and applications of Circular Linked List? Write the Pseudo
code to perform all standard operations on Circular Linked List and explain the same
with neat diagrams wherever necessary.
The circular linked list is a linked list where all nodes are connected to form a circle. In a
circular linked list, the first node and the last node are connected to each other which forms a
circle. There is no NULL at the end.
Figure 1.48 shows the algorithm to insert a new node at the beginning of a linked list. In Step 1,
we first check whether memory is available for the new node. If the free memory has
exhausted, then an OVERFLOW message is printed. Otherwise, if free memory cell is
available, then we allocate space for the new node. Set its DATA part with the given VAL and
the NEXT part is initialized with the address of the first node of the list, which is stored in
START. Now, since the new node is added as the first node of the list, it will now be known as
the START node, that is, the START pointer variable will now hold the address of the
NEW_NODE.
While inserting a node in a circular linked list, we have to use a while loop to traverse to the
last node of the list. Because the last node contains a pointer to START, its NEXT field is
updated so that after insertion it points to the new node which will be now known as START.
Inserting a Node at the End of a Circular Linked List:
Consider the below linked list . Suppose we want to add a new node with data 9 as the last
node of the list. Then the following changes will be done in the linked list.
Below is the algorithm to insert a new node at the end of a circular linked list. In Step 6,
we take a pointer variable PTR and initialize it with START. That is, PTR now points to the
first node of the linked list. In the while loop, we traverse through the linked list to reach the
last node. Once we reach the last node, in Step 9, we change the NEXT pointer of the last node
to store the address of the new node. Remember that the NEXT field of the new node contains
the address of the first node which is denoted by START.
7. Show the memory representation of Polynomials using Singly Linked list? Explain the
algorithm to perform Polynomial Multiplication.
Representing a Polynomial Term
In the linked list representation of a polynomial, each node corresponds to a term of the
polynomial. The node structure usually contains three components:
Coefficient: The numerical multiplier of the term.
Exponent: The power to which the variable is raised in the term.
Next: A pointer to the next term (node) in the polynomial.
This structure allows the polynomial to be stored as a linked list, where each node directly
represents a term, and the entire list represents the polynomial in its entirety.
To illustrate how a polynomial is represented using a linked list, let’s consider a specific
example. Suppose we have a polynomial:
P(x)=7x^3+5x^2−2x+4
This polynomial will be represented as a linked list where each node represents a term of the
polynomial.
Node Structure
Each node in the linked list will typically have the following structure:
Coefficient: The numerical coefficient of the term.
Exponent: The exponent of the variable in the term.
Next: A pointer to the next term (node) in the polynomial.
Representation of the Polynomial as a Linked List
1. First Node (7x^3):
Coefficient: 7
Exponent: 3
Next: Pointer to the second node
2. Second Node (5x^2):
Coefficient: 5
Exponent: 2
Next: Pointer to the third node
3. Third Node (-2x):
Coefficient: -2
Exponent: 1
Next: Pointer to the fourth node
4. Fourth Node (4):
Coefficient: 4
Exponent: 0 (since it’s a constant term)
Next: NULL (as it is the last term)
Visual Representation
The linked list for polynomial P(x) would visually look like this:
The algorithm for polynomial multiplication using linked lists in C:
First define the node structure to hold the coefficient and exponent values, as well as a
pointer to the next node.
Create a function to create a new node with the specified coefficient and exponent values.
Create a function to insert a new node into a linked list based on its exponent value.
Create a function to print the linked list.
Create a function to multiply two polynomials represented as linked lists.
Iterate over each term in the first polynomial, multiply it with each term in the second
polynomial, and add the resulting term to the result polynomial using the insert_node()
function.
Return the resulting polynomial as a linked list.
Step 2: Traverse the entire linked list by moving through each node using the ‘temp’ pointer
until it reaches the end (marked as ‘NULL’).
At each iteration within the traversal,
Save the reference to the next node that ‘temp’ is pointing to in a variable called ‘front’. This
helps retain the link to the subsequent node before altering the ‘next’ pointer.
Reverse the direction of the ‘next’ pointer of the current node (pointed to by ‘temp’) to point to
the ‘prev’ node. This effectively reversed the direction of the linked list, making the current
node point to the previous node.
Move the ‘prev’ pointer to the current node. This sets up the ‘prev’ pointer for the next iteration
of the loop.
Move the ‘temp’ pointer to the ‘front’ node. This advances the traversal to the next node in the
original order.
Step 3: Keep traversing through the linked list using the ‘temp’ pointer until it reaches the end,
thereby reversing the entire list. Once the ‘temp’ pointer reaches the end, return
the new head of the reversed linked list, which is now indicated by the ‘prev’ pointer.
This ‘prev’ pointer becomes the first node in the newly reversed list.
9. Outline the applications of Single Linked List and write algorithms to merge two single
linked lists into one list.
1.Create a new head pointer to an empty linked list.
2.Check the first value of both linked lists.
3.Whichever node from L1 or L2 is smaller, append it to the new list and move the pointer to
the next node.
4.Continue this process until you reach the end of a linked list.
EXAMPLE
L1 = 1 -> 3 -> 10
L2 = 5 -> 6 -> 9
L3 = null
Compare the first two nodes in both linked lists: (1, 5), 1 is smaller so add it to the new linked
list and move the pointer in L1.
L1 = 3 -> 10
L2 = 5 -> 6 -> 9
L3 = 1
Compare the first two nodes in both linked lists: (3, 5), 3 is smaller so add it to the new linked
list and move the pointer in L1.
L1 = 10
L2 = 5 -> 6 -> 9
L3 = 1 -> 3
Compare the first two nodes in both linked lists: (10, 5), 5 is smaller so add it to the new linked
list and move the pointer in L2.
L1 = 10
L2 = 6 -> 9
L3 = 1 -> 3 -> 5
Compare the first two nodes in both linked lists: (10, 6), 6 is smaller so add it to the new linked
list and move the pointer in L2.
L1 = 10 L2 = 9
L3 = 1 -> 3 -> 5 -> 6
Compare the first two nodes in both linked lists: (10, 9), 9 is smaller so add it to the new linked
list and move the pointer in L2.
L1 = 10
L2 = null
L3 = 1 -> 3 -> 5 -> 6 -> 9
Because L2 points to null, simply append the rest of the nodes from L1 and we have our merged
linked list.
L3 = 1 -> 3 -> 5 -> 6 -> 9 -> 10
10. Analyze the differences between linked lists and linear arrays and write a program to
differentiate insert and delete operations.
Array elements store in a contiguous Linked list elements can be stored anywhere
memory location. in the memory or randomly stored.
Array works with a static memory. Here The Linked list works with dynamic memory.
static memory means that the memory size Here, dynamic memory means that the
is fixed and cannot be changed at the run memory size can be changed at the run time
time. according to our requirements.
Array elements are independent of each Linked list elements are dependent on each
other. other. As each node contains the address of
the next node so to access the next node, we
need to access its previous node.
Array takes more time while performing any Linked list takes less time while performing
operation like insertion, deletion, etc. any operation like insertion, deletion, etc.
In the case of an array, memory is allocated In the case of a linked list, memory is
at compile-time. allocated at run time.
11. How to represent Sparse Matrix using Single Linked List? Discuss.
Sparse Matrix using Linked List in C Language:
Now, we will discuss the representation of a sparse matrix, and also, we will see how to display
a sparse matrix from that representation. We have already studied this topic in arrays. Now we
are learning it in the linked list. We have an example of a sparse matrix of size 5×6 i.e. 5 rows
and 6 columns.
Here a lot of elements are zeroes and some are non-zeroes elements i.e. 5, 3, 4, 2, 8, and 7. How
to represent these elements?
Representation of Sparse Matrix:
To represent a sparse matrix, we want to avoid storing zeroes. We want to store only non-zero
elements. For example, if we take 5, this is a non-zero element. So, for representing it, we
should know the non-zero element and also the position of that element in the matrix. For that,
we should know the row and column numbers for non-zero elements. 5 is present at (0, 2)
location. Similarly following are the locations of non-zero elements.
5 – (0, 2)
3 – (1, 4)
4 – (2, 2)
2 – (3, 1)
8 – (3, 3)
7 – (4, 0)
So, we have to represent this data in such that we should be able to regenerate the same sparse
matrix. So let us see how we can represent this one by just taking non-zero elements.
For representation, we have taken an array. This array will represent rows. As our matrix has 5
rows so we have taken an array of size 5. The indices of this array represent the row number of
the matrix.
Now for representing non-zero elements, as the array represents the row number so we just need
to know the column number and the element itself. Let us say the element is 5 and the column
number is 2. So, this we will represent as a node.
Here 0th row has a node that contains column number (2), non-zero element (5), and a
pointer to the next node (here that is null ‘/’). Now let us add all the non-zero elements
according to their row number in the above array.
The above diagram represents all the non-zero elements of the given matrix. Let us say it is
array A. So we can say that A is an array of linked lists. Now, how we can form this
structure? This structure is having a collection of linked lists and a linked list is a collection
of nodes, so each node contains a column number and a non-zero element. So let us define
the node structure,
The node structure contains 3 things that are column number, a non-zero element, and a
pointer to the next node (if any is otherwise null). We can define the structure of this node
in C language as,
struct Node{
int col;
int val;
struct Node *next;
}
Now how to create the structure for storing only non-zero elements? For creating a
structure, we need an array of the number of rows size. If the matrix is of order m x n, we
will create an array of size m.