0% found this document useful (0 votes)
23 views21 pages

DS Unit-2 Material

Uploaded by

P.Padmini Rani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views21 pages

DS Unit-2 Material

Uploaded by

P.Padmini Rani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Unit-2

1. What is linked list?


 Linked List can be defined as collection of objects called nodes that are randomly stored in
the memory.
 A node contains two fields i.e. data stored at that particular address and the pointer which
contains the address of the next node in the memory.
 The last node of the list contains pointer to the null.
2. List different types of linked list.
 Singly Linked List
 Doubly Linked List
 Circular Linked List
 Circular Doubly Linked List.
3. What are the different operations on linked list?
 Traversal - To access each element of the linked list.
 Insertion - To add/insert a new node to the list.
 Deletion - To remove an existing node from the list.
 Search - To find a node in the list.
 Sort - To sort the nodes.
4. What is circular linked list?
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 form a circle. There
is no NULL at the end.
5. Write differences between array and linked list.

6. List out applications of linked list.


 Polynomial Manipulation representation
 Addition of long positive integers
 Representation of sparse matrices
 Addition of long positive integers
 Symbol table creation
 Mailing list
 Memory management
 Linked allocation of files
 Multiple precision arithmetic etc.
7. What are the advantages of linked list?
 Dynamic data structure: No memory wastage: In the Linked list, efficient memory
utilization can be achieved since the size of the linked list increase or decrease at run time so
there is no memory wastage and there is no need to pre-allocate the memory.
 Implementation: Linear data structures like stacks and queues are often easily implemented
using a linked list.
 Insertion and Deletion Operations: Insertion and deletion operations are quite easier in the
linked list. There is no need to shift elements after the insertion or deletion of an element only
the address present in the next pointer needs to be updated.
 Flexible: This is because the elements in Linked List are not stored in contiguous memory
locations unlike the array.
 Efficient for large data: When working with large datasets linked lists play a crucial role as
it can grow and shrink dynamically.
 Scalability: Contains the ability to add or remove elements at any position.

8. What are the disadvantages of linked list?


 Memory usage: More memory is required in the linked list as compared to an array. Because
in a linked list, a pointer is also required to store the address of the next element and it
requires extra memory for itself.
 Traversal: In a Linked list traversal is more time-consuming as compared to an array. Direct
access to an element is not possible in a linked list as in an array by index. For example, for
accessing a node at position n, one has to traverse all the nodes before it.
 Reverse Traversing: In a singly linked list reverse traversing is not possible, but in the case
of a doubly-linked list, it can be possible as it contains a pointer to the previously connected
nodes with each node. For performing this extra memory is required for the back pointer
hence, there is a wastage of memory.
 Random Access: Random access is not possible in a linked list due to its dynamic memory
allocation.
 Lower efficiency at times: For certain operations, such as searching for an element or
iterating through the list, can be slower in a linked list.
 Complex implementation: The linked list implementation is more complex when compared
to array. It requires a complex programming understanding.
 Difficult to share data: This is because it is not possible to directly access the memory
address of an element in a linked list.
 Not suited for small dataset: Cannot provide any significant benefits on small dataset
compare to that of an array.
9. What is double 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).
10. Write difference between linear and circular linked list.
 Circularly linked lists are useful to traverse an entire list starting at any point. In a linear
linked list, it is required to know the head pointer to traverse the entire list. The linear linked
list cannot be traversed completely with the help of an intermediate pointer.
 Access to any element in a doubly circularly linked list is much easier than in a linearly linked
list since the particular element can be approached in two directions. For example to access an
element present in the fourth node of a circularly linked list having five elements, it is enough
to start from the last node and traverse the list in the reverse direction to get the value in the
fourth node.
11. Write difference between single and double linked list.
S. No. Singly linked list Doubly linked list
1 In case of singly linked lists, the In case of doubly linked lists, the complexity
complexity of insertion and deletion is O(n) of insertion and deletion is O(1)
2 The Singly linked list has two segments: The doubly linked list has three segments.
data and link. First is data and second, third are the
pointers.
3 It permits traversal components only in one It permits two way traversal.
way.
4 We mostly prefer a singly linked list for the We can use a doubly linked list to execute
execution of stacks. binary trees, heaps and stacks.
5 When we want to save memory and do not In case of better implementation, while
need to perform searching, we prefer a searching, we prefer a doubly linked list.
singly linked list.
6 A singly linked list consumes less memory The doubly linked list consumes more
as compared to the doubly linked list. memory as compared to the singly linked
list.

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

Algorithm to insert a node at a specific position


Step 1: If Ptr = Null
Write Overflow
Goto Step 12
End Of If
Step 2: Set New_Node = Ptr
Step 3: New_Node → Data = Val
Step 4: Set Temp = Head
Step 5: Set I = 0
Step 6: Repeat Step 5 And 6 Until I<Loc< Li=""></Loc<>
Step 7: Temp = Temp → Next
Step 8: If Temp = Null
Write "Desired Node Not Present"
Goto Step 12
End Of If
End Of Loop
Step 9: Ptr → Next = Temp → Next
Step 10: Temp → Next = Ptr
Step 11: Set Ptr = New_Node
Step 12: Exit
Algorithm to insert a node at the end
Step 1: If Ptr = Null
Write Overflow
Go To Step 1
[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 Ptr = Head
Step 7: Repeat Step 8 While Ptr - > Next != Null
Step 8: Set Ptr = Ptr - > Next
[End Of Loop]
Step 9: Set Ptr - > Next = New_Node
Step 10: Exit

2. Demonstrate the advantages and disadvantages of single linked list.


ADVANTAGES:
 Insertions and Deletions can be done easily.
 It does not need movement of elements for insertion and deletion.
 It space is not wasted as we can get space according to our requirements.
 Its size is not fixed.
 It can be extended or reduced according to requirements.
 Elements may or may not be stored in consecutive memory available, even then we can store
the data in computer.
 It is less expensive.
DISADVANTAGES:
 It requires more space as pointers are also stored with information.
 Different amount of time is required to access each element.
 If we have to go to a particular element then we have to go through all those elements that
come before that element.
 We cannot traverse it from last & only from the beginning.
 It is not easy to sort the elements stored in the linear linked list.

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.

Insert element at the end


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.

Delete the specified element :


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, defines 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).
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)).
Search the given element:
To check if an element is present in the linked list, start with a temp pointer pointing to the
head. This pointer will be used to traverse the list, ensuring that the reference to the starting
point is not lost. During the traversal, check if the data on the current node matches the
specified value val. If no match is found, move to the next node using the next pointer of the
current node.

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

Inserting a New Node in a Circular Linked List:


In this section, we will see how a new node is added into an already existing linked list. We
will take two cases and then see how insertion is done in each case.
Case 1: The new node is inserted at the beginning of the circular linked list.
Case 2: The new node is inserted at the end of the circular linked list.
Inserting a Node at the Beginning of a Circular Linked List:
Consider the linked list shown in Fig. 1.47. Suppose we want to add a new node with data 9 as
the first node of the list. Then the following changes will be done in the linked list.

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.

Deleting a Node from a Circular Linked List


In this section, we will discuss how a node is deleted from an already existing circular linked
list. We will take two cases and then see how deletion is done in each case. Rest of the cases of
deletion are same as that given for singly linked lists.
Case 1: The first node is deleted.
Case 2: The last node is deleted.
Deleting the First Node from a Circular Linked List:
Consider the circular linked list shown in Fig. 1.51. When we want to delete a node from the
beginning of the list, then the following changes will be done in the linked list.
Below is the algorithm to delete the first node from a circular linked list. In Step 1 of the
algorithm, we check if the linked list exists or not. If START = NULL, then it signifies that
there are no nodes in the list and the control is transferred to the last statement of the algorithm.
However, if there are nodes in the linked list, then we use a pointer variable PTR which will be
used to traverse the list to ultimately reach the last node. In Step 5, we change the next pointer
of the last node to point to the second node of the circular linked list. In Step 6, the memory
occupied by the first node is freed. Finally, in Step 7, the second node now becomes the first
node of the list and its address is stored in the pointer variable START.
Algorithm:
Step1: if start = NULL
Write underflow
Go to step 8
[end of if]
Step 2: Set ptr=start
Step 3: Repeat step4 while ptr->next!=start
Step 4:Set ptr=ptr->next
[end of loop]
Step 5: Set ptr->next=start->next
Step 6: free start
Step 7: Set start=ptr->next
Step 8: Exit
Deleting the Last Node from a Circular Linked List:
Consider the circular linked list shown in Fig. 1.53. Suppose we want to delete the last node from
the linked list, then the following changes will be done in the linked list.
Below is the algorithm to delete the last node from a circular linked list. In Step 2, 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 take another pointer variable PREPTR such that PREPTR
always points to one node before PTR. Once we reach the last node and the second last node,
we set the next pointer of the second last node to START, so that it now becomes the (new) last
node of the linked list. The memory of the previous last node is freed and returned to the free
pool.

Advantages of Circular Linked Lists


 Any node can be a starting point. We can traverse the whole list by starting from any point.
We just need to stop when the first visited node is visited again.
 Useful for the implementation of a queue. Unlike this implementation, we don’t need to
maintain two-pointers for the front and rear if we use a circular linked list. We can maintain
a pointer to the last inserted node and the front can always be obtained as next of last.
 Circular lists are useful in applications to repeatedly go around the list. For example, when
multiple applications are running on a PC, it is common for the operating system to put the
running applications on a list and then cycle through them, giving each of them a slice of
time to execute, and then making them wait while the CPU is given to another application. It
is convenient for the operating system to use a circular list so that when it reaches the end of
the list it can cycle around to the front of the list.
 Circular Doubly Linked Lists are used for the implementation of advanced data structures
like the Fibonacci Heap.
 Implementing a circular linked list can be relatively easy compared to other more complex
data structures like trees or graphs.
Applications of Circular Linked List
 This is used in multiplayer games to give each participant a chance to play.
 In an operating system, a circular linked list can be used to organize many running
applications.
 In resource allocation difficulties, circular linked lists might be useful.
 Circular linked lists are frequently used to build circular buffers.
 They can also be used in simulation and gaming.

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.

8. Write an algorithm to reverse a Single linked list.


Step 1: Initialise a ‘temp’ pointer at the head of the linked list. This pointer will be used to
traverse the linked list. And initialize the pointer ‘prev’ to ‘NULL’ to keep track of the previous
node. This will be used to reverse the direction of the ‘next’ pointers.

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 Linked list

An array is a collection of elements of a A linked list is a collection of objects known


similar data type. as a node where node consists of two parts,
i.e., data and address.

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.

Accessing any element in an array is faster Accessing an element in a linked list is


as the element in an array can be directly slower as it starts traversing from the first
accessed through the index. element of the linked list.

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.

Memory utilization is inefficient in the Memory utilization is efficient in the case of


array. For example, if the size of the array is a linked list as the memory can be allocated
6, and array consists of 3 elements only then or deallocated at the run time according to
the rest of the space will be unused. our requirement.

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy