Unit-3-Linked-List-DSU
Unit-3-Linked-List-DSU
Unit-3-Linked-List-DSU
com/data-structure-
using-c-for-msbte-3k-scheme/
Node structure
Example : Suppose we want to maintain the node containing name of student and
call number. Here two data field of different types are required. Hence, we need to
use a structure. The next field is a pointer to another node of same type so node
definition will be,
Ans.
Struct node
{
Char name [15] :
int rollno ;
sturct node * next;
};
The statement :
Struct node * next ;
Means next is a pointer to the next structure node. And it can be represented as :
Data Link
A linked list, or one-way list is a linear collection of data elements, called nodes,
where the linear order is given by means of pointers
Node structure
Example : Suppose we want to maintain the node containing name of student and
call number. Here two data field of different types are required. Hence, we need to
use a structure. The next field is a pointer to another node of same type so node
definition will be,
Struct node
{
Char name [15] :
int rollno ;
sturct node * next;
Ans };
The statement :
Struct node * next ;
Means next is a pointer to the next structure node. And it can be represented as :
Null Pointer : The pointer of the last node contains a special value, called the null
pointer, which is any invalid address. The null pointer, denoted by X in the diagram,
signals the end of list.
Empty List : The linked list also contains a list pointer variable – called START or
NAME – which contains the address of the first node in the list : hence there is an
arrow drawn from the START to the first node. We need only this address is START
to trace through the list. A special case is the list that has no nodes, such a list
called as empty list or null list and is denoted by the null pointer in the variable
START.
Write an algorithm to insert a new node at the beginning in S - 24
3 04
linear list W - 22
Algorithm : Inserting an element at the beginning :
Suppose the original Linked list as shown in Fig
3 nodes are present is the given list. Now insert a new node with data 5, in the
beginning of the list :
Step 1 : Create a space for new node and P will point to this empty node.
Step 2 : Read information and to be inserted at P and store it at p
Put the value 5 in INFO field of new node.
INTO (D) = 5
Step 3 : Check whether this is the first element (i.e. START = NULL) if so then
point ‘START’ to ‘P’ (START = P)
Else
Ans
New connect ‘P’ in front of the list, so its next field should point to the beginning of
liset (P Next = LIST)
P Next = START
Now the address filed or next pointer of ‘P’ is pointing to start, where START
pointing is stored in ‘P’ next field. This establishes a desired link between a new
node ‘P’ and the LIST.
Now LIST will have 4 elements. The last step is change starting address of list (i.e.
START = P)
4 S -24
Explain the operation on a singly linked list 04
W – 23
A linked list, or one-way list is a linear collection of data elements, called nodes, where the
linear order is given by means of pointers.
The various operations that can be performed on the linked list are given below :
b. Insertion :
Add ( ) function :
Delete ( ) function :
Display ( )
Step 1 : If list is not empty, visit each node display the information.
[i.e. it (START ! = NULL)
{
q = START ;
while (q ! = NULL)
{
print (“% d”, q into) ;
q = q Next ;
}
}
Step 2 : Return.
S -24
5 Compare linear list with circular list (Any four points) 04
W – 22
Sr.
Linear linked list Circular linked list
No
A linear linked list, or one-way
list is a linear collection of data A circular linked list is a list in which
elements, called nodes, where the last node pointer to the first
1 the linear order is given by node in a list.
means of pointers.
P is node to be deleted
Ans
Free (P)
Insertion:
current head.
Move the head to point to the new node.
Deletion:
Delete from the beginning :
Check if the head is NULL.
o If it is, return NULL (the list is empty).
Store the current head node in a temporary variable temp.
Move the head pointer to the next node.
Delete the temporary node.
Return the new head of the linked list.
o Delete from the end :
Check if the head is NULL.
o If it is, return NULL (the list is empty).
Check if the head's next is NULL (only one node in the
list).
o If true, delete the head and return NULL.
Traverse the list to find the second last node
(second_last).
Delete the last node (the node after second_last).
Set the next pointer of the second last node to NULL.
Return the head of the linked list.
Corel 3
Corel 4
To remove the first node of a linked list, store the current head in a temporary
variable (temp), move the head pointer to the next node, delete the temporary head
node and finally , return the new head of the linked list.
Step 1 : Check if the list is empty
if (head == NULL)
return NULL;.
Step 2 : Store the current head in a temporary variable
Ans struct Node* temp = head
Step 3 : Move the head pointer to the next node
head = head->next
Step 4 : free (temp);
Step 5 : Return.
Create a singly Linked list using data fields 10,20,30,40,50 and
S – 23
13 show procedure step by step with help of diagram from start to 04
W – 23
end
Initially liked list will be empty
Start = NULL ;
Insert node 10
Node 10 will be first node.
Start Null
10
Start
10 20 Null
Start Null
10 20 30
Insert node 50
Singly Linked list using data fields 10, 20, 30, 40, 50
Start 10 20 30 40 50
/Head
Step 2 :
Start 10 20 30 40 50
/Head
Step 4 :
Start 10 20 30 40 50
/Head
A linked list, or one-way list is a linear collection of data elements, called nodes, where the
linear order is given by means of pointers.
Each node is divided into two pacts :
1. First part contains information of the element.
2. Second part contains address of the next node is the list, which is called as linked
field or next pointer field.
Information Next
: Node structure
Example : Suppose we want to maintain the node containing name of student and
call number. Here two data field of different types are required. Hence, we need to
use a structure. The next field is a pointer to another node of same type so node
definition will be,
Struct node
{
Ans Char name [15] :
int rollno ;
sturct node * next;
};
The statement :
Struct node * next ;
Means next is a pointer to the next structure node. And it can be represented as :
Linked List: Linked lists are less rigid in their storage structure and elements are usually
not stored in contiguous locations, hence they need to be stored with additional
tags giving a reference to the next element.
count ← 0
If (head != NULL) then
temp ← head
count ← count + 1
temp ← temp.next
End while
End if
End
W – 22 03
17 Write the ‘C’ function for searching a node in single linked list
Step 1 : START
Step 2 : Accept linked list & data
Step 3 : Set Temp := Start
Step 4 : While link [Temp] not equal to Null [Check till end]
If Info [Temp] = data, then
Write: “Found Successfully"
EXIT [If found, Stop]
Ans.
[End of If]
Set Temp := Link [Temp] [Pointer is
incremented to next]
[End of while]
Step 5 : Write: "Not Found"
Step 6 : EXIT [stop]
Input: 10->20->30->40->50->null
Output: 10 20 30 40 50
Explanation: Every element of each node from head node to last node is
printed which means we have traversed each node successfully.
W – 19 04
Write an algorithm to insert a new node at the beginning and
21 W – 18 06
end of the singly linked list.
S - 22 04
Algorithm to insert a new node at the beginning and end of the singly linked
list.
Construct a singly linked list using data fields 21, 25, 96, 58,
22 74 and show procedure step-by-step with the help of diagram W – 19 04
start to end.
Singly Linked list using data fields 21, 25, 96, 58, 74
Initially liked list will be empty
Start = NULL ;
Insert node 21
Node 21 will be first node.
Start Null
21
Start
21 25 Null
Start Null
21 25 96
Null
Start 21 25 96 58
/Head
Start 21 25 96 58 74
/Head
Linked list
Array
int main() {
return 0;
}
Create a singly linked list using data fields 15, 20, 22, 58, 60.
W – 19 04
26 Search a node 22 from the SLL and show procedure step-by-
S – 24 06
step with the help of diagram from start to end.
Singly Linked list using data fields 15, 20, 22, 58, 60
Start 15 20 22 58 60
/Head
Step 2 :
Start 15 20 22 58 60
/Head
To delete the first node of a circular linked list, we first check if the list is
empty. If it is then we print a message and return NULL. If the list
contains only one node (the head is the same as the last) then we delete
that node and set the last pointer to NULL. If there are multiple nodes
then we update the last->next pointer to skip the head node and
effectively removing it from the list. We then delete the head node to
free the allocated memory. Finally, we return the updated last pointer,
which still points to the last node in the list.
Algorithm
Step 1 : Check if List is Empty:
Ans If last is nullptr, print “List is empty” and return nullptr.
Else goto step 2
Step 2 : Get Head Node (Start)
Set Start = q
Start = start-> next
q->next = Null
last - > next = start
Step 3 Return q
Delete ( ) :
Corel 9
Corel 10
Move r to the last node and connect back to start.
Corel 11
Create a singly linked list using data fields 90, 25, 46, 39, 56.
28 Search a node 40 from the SLL and show procedure step-by- S – 19 06
step with the help of diagram from start to end.
Singly Linked list using data fields 10, 20, 30, 40, 50
Start 10 20 30 40 50
/Head
Step 4 :
Start 10 20 30 40 50
/Head
Delete ( ) function :
Describe advantages of circular link list over linear link list with
30 W – 23 04
example
Advantages of Circular Linked Lists over linear linked list:
Efficient operations: Since the last node of the circular list points back to
the first node, circular linked lists can be traversed quickly and efficiently. This
makes them useful for applications that require frequent traversal, such as
queue and hash table implementations. Where as in linear linked list
sequential traversing of linear traversing is done. To visit last node all nodes
must be visited.
Space efficiency: Circular linked lists can be more space-efficient than linear
linked lists because they do not require a separate pointer to keep track of the
end of the list. This means that circular linked lists can be more compact and
take up less memory than linear linked list.
Flexibility: The circular structure of the list allows for greater flexibility in
Ans certain applications. For example, a circular linked list can be used to
represent a ring or circular buffer, where new elements can be added and old
elements can be removed without having to shift the entire list. Whereas in
linear linked list node can be inserted at the end , in between and beginning.
Dynamic size: Circular linked lists can be dynamically sized, which means
that nodes can be added or removed from the list as needed. This makes
them useful for applications where the size of the list may change frequently,
such as memory allocation and music or media players.
Ease of implementation: Implementing circular linked lists is often simpler
than implementing linear linked list. This is because circular linked lists have a
simple, circular structure that is easy to understand and implement.
Thank You
https://shikshamentor.com/data-structure-
using-c-for-msbte-3k-scheme/
Visit
https://shikshamentor.com/