0% found this document useful (0 votes)
50 views

Linked List1

A linked list is a linear data structure where elements are linked using pointers. Each node contains a data field and a pointer to the next node. The last node points to null. Linked lists allow dynamic memory allocation and efficient insertion/deletion without shifting other elements. They are useful for implementing other data structures like stacks and queues. Common operations on linked lists include insertion, deletion, traversal, and searching by adjusting the pointers between nodes.

Uploaded by

focaba4283
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Linked List1

A linked list is a linear data structure where elements are linked using pointers. Each node contains a data field and a pointer to the next node. The last node points to null. Linked lists allow dynamic memory allocation and efficient insertion/deletion without shifting other elements. They are useful for implementing other data structures like stacks and queues. Common operations on linked lists include insertion, deletion, traversal, and searching by adjusting the pointers between nodes.

Uploaded by

focaba4283
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Linked List:

 Linked List is a linear data structure, in which elements are not stored at a contiguous
location, rather they are linked using pointers.
 Linked List forms a series of connected nodes, where each node stores the data and
the address of the next node.
 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.

Node Structure: A node in a linked list typically consists of two components:


1. Data: It holds the actual value or data associated with the node.
2. Next Pointer: It stores the memory address (reference) of the next node in the
sequence.
Head and Tail: The linked list is accessed through the head node, which points to the first
node in the list. The last node in the list points to NULL or nullptr, indicating the end of the
list. This node is known as the tail node.

Representation of Linked List:


Each node consists of:
 A data item
 An address of another node
We wrap both the data item and the next node reference in a struct as:
struct node{
int data;
struct node *next;
};
Understanding the structure of a linked list node is the key to having a grasp on it.
Each struct node has a data item and a pointer to another struct node. Let us create a
simple Linked List with three items to understand how this works.
/* Initialize nodes */
struct node *head;
struct node *one = NULL;
struct node *two = NULL;
struct node *three = NULL;
/* Allocate memory */
one = malloc(sizeof(struct node));
two = malloc(sizeof(struct node));
three = malloc(sizeof(struct node));
/* Assign data values */
one->data = 1;
two->data = 2;
three->data=3;
/* Connect nodes */
one->next = two;
two->next = three;
three->next = NULL;
/* Save address of first node in head */
head = one;
In just a few steps, we have created a simple linked list with three nodes.

Fig: Linked list Representation


The power of a linked list comes from the ability to break the chain and rejoin it. For
example if you wanted to put an element 4 between 1 and 2, the steps would be:
 Create a new struct node and allocate memory to it.
 Add its data value as 4
 Point its next pointer to the struct node containing 2 as the data value
 Change the next pointer of "1" to the node we just created.
Doing something similar in an array would have required shifting the positions of all the
subsequent elements.

Why use linked list over array?

Till now, we were using array data structure to organize the group of elements that are to
be stored individually in the memory. However, Array has several advantages and
disadvantages which must be known in order to decide the data structure which will be
used throughout the program.
Array contains following limitations:

1. The size of array must be known in advance before using it in the program.
2. Increasing size of the array is a time taking process. It is almost impossible to expand
the size of the array at run time.
3. All the elements in the array need to be contiguously stored in the memory. Inserting
any element in the array needs shifting of all its predecessors.

Linked list is the data structure which can overcome all the limitations of an array. Using
linked list is useful because,

1. It allocates the memory dynamically. All the nodes of linked list are non-contiguously
stored in the memory and linked together with the help of pointers.
2. Sizing is no longer a problem since we do not need to define its size at the time of
declaration. List grows as per the program's demand and limited to the available
memory space.

Difference between array and linked list:

Why linked list data structure needed?


Here are a few advantages of a linked list that is listed below, it will help you understand why
it is necessary to know.
 Dynamic Data structure: The size of memory can be allocated or de-allocated at run
time based on the operation insertion or deletion.

 Ease of Insertion/Deletion: The insertion and deletion of elements are simpler than
arrays since no elements need to be shifted after insertion and deletion, Just the
address needed to be updated.
 Efficient Memory Utilization: As we know Linked List is a dynamic data structure the
size increases or decreases as per the requirement so this avoids the wastage of
memory. The list is not required to be contiguously present in the memory. The node
can reside anywhere in the memory and linked together to make a list. This achieves
optimized utilization of space.
 Implementation: Various advanced data structures can be implemented using a
linked list like a stack, queue, graph, hash maps, etc.
 List size is limited to the memory size and doesn't need to be declared in advance.

Example:
In a system, if we maintain a sorted list of IDs in an array id[] = [1000, 1010, 1050, 2000,
2040].
If we want to insert a new ID 1005, then to maintain the sorted order, we have to move all
the elements after 1000 (excluding 1000).
Deletion is also expensive with arrays until unless some special techniques are used. For
example, to delete 1010 in id[], everything after 1010 has to be moved due to this so much
work is being done which affects the efficiency of the code.

There are mainly three types of linked lists:


 Singly-linked list
 Doubly linked list
 Circular linked list

1. Singly-linked list:
In a singly linked list, each node contains a reference to the next node in the sequence.
Traversing a singly linked list is done in a forward direction.

A node in singly linked list is represented as:

struct node
{
int data;
struct node *next;
};

Operations on Singly Linked List:


The insertion into a singly linked list can be performed at different positions. Based on the
position of the new node being inserted, the insertion is categorized into the following
categories.
SN Operation Description

1 Insertion at It involves inserting any element at the front of the list. We just need
beginning to a few link adjustments to make the new node as the head of the
list.

2 Insertion at end It involves insertion at the last of the linked list. The new node can be
of the list inserted as the only node in the list or it can be inserted as the last
one. Different logics are implemented in each scenario.

3 Insertion after It involves insertion after the specified node of the linked list. We
specified node need to skip the desired number of nodes in order to reach the node
after which the new node will be inserted.

Deletion: The Deletion of a node from a singly linked list can be performed at different
positions. Based on the position of the node being deleted, the operation is categorized into
the following categories.

SN Operation Description

1 Deletion at It involves deletion of a node from the beginning of the list. This is the
beginning simplest operation among all. It just need a few adjustments in the
node pointers.

2 Deletion at the It involves deleting the last node of the list. The list can either be empty
end of the list or full. Different logic is implemented for the different scenarios.

3 Deletion after It involves deleting the node after the specified node in the list. we
specified node need to skip the desired number of nodes to reach the node after which
the node will be deleted. This requires traversing through the list.

4 Traversing In traversing, we simply visit each node of the list at least once in order
to perform some specific operation on it, for example, printing data
part of each node present in the list.

5 Searching In searching, we match each element of the list with the given element.
If the element is found on any of the location then location of that
element is returned otherwise null is returned.

2. Doubly-linked list:
In a doubly linked list, each node contains references to both the next and previous nodes.
This allows for traversal in both forward and backward directions, but it requires additional
memory for the backward reference.
A node in doubly linked list is represented as:
struct node {
int data;
struct node *next;
struct node *prev;
};

3. Circular linked list:


In a circular linked list, the last node points back to the head node, creating a circular
structure. It can be either singly or doubly linked.

Operations on Linked Lists:

Insertion: Adding a new node to a linked list involves adjusting the pointers of the existing
nodes to maintain the proper sequence. Insertion can be performed at the beginning, end,
or any position within the list.

Deletion: Removing a node from a linked list requires adjusting the pointers of the
neighbouring nodes to bridge the gap left by the deleted node. Deletion can be performed
at the beginning, end, or any position within the list.

Searching: Searching for a specific value in a linked list involves traversing the list from the
head node until the value is found or the end of the list is reached.

Advantages of Linked Lists:


Dynamic Size: Linked lists can grow or shrink dynamically, as memory allocation is done at
runtime.
Insertion and Deletion: Adding or removing elements from a linked list is efficient, especially
for large lists.
Flexibility: Linked lists can be easily reorganized and modified without requiring a
contiguous block of memory.
Disadvantages of Linked Lists:
Random Access: Unlike arrays, linked lists do not allow direct access to elements by index.
Traversal is required to reach a specific node.
Extra Memory: Linked lists require additional memory for storing the pointers, compared to
arrays.
Conclusion:
Linked lists are versatile data structures that provide dynamic memory allocation and
efficient insertion and deletion operations. Understanding the basics of linked lists is
essential for any programmer or computer science enthusiast. With this knowledge, you can
implement linked lists to solve various problems and expand your understanding of data
structures and algorithms.

Algo for traversing a Singly Linked List:


1. [initialize] Set ptr = head
2. Repeat steps 3 and 4 while ptr != NULL
3. Apply process to ptr-> data
4. Set ptr = ptr-> next
[END OF LOOP]
5. Exit

Algo to print number of nodes in a Singly Linked List:


1. [Initialize] Set count = 0
2. [initialize] Set ptr = head
3. Repeat steps 4 and 5 while ptr != NULL
4. Set count = count + 1
5. Set ptr = ptr-> next
[END OF LOOP]
6. Write count
7. Exit

Algo to search val in a Singly Linked List:


1. [initialize] Set ptr = head
2. Repeat step 3 while ptr != NULL
3. If val = ptr->data
Set pos = ptr
Goto step 5
Else
Set ptr = ptr-> next
[END OF IF]
[END OF LOOP]
4. Set pos = null
5. Exit

Algo to insert a new node at the beginning of Singly Linked List:


1. Create new_node
2. Allocate memory to new_node
3. Set new_node-> data = val
4. Set new_node-> next = head
5. Set head = new_node
6. Exit

Algo to insert new node at the end of Singly Linked List:


1. Create new_node
2. Allocate memory to new_node
3. Set new_node-> data = val
4. Set new_node-> next = NULL
5. Set ptr = head
6. Repeat step 7 while ptr-> next != NULL
7. Set ptr = ptr-> next
[END OF LOOP]
8. Set ptr-> next = new_node
9. Exit

Algo to delete first node of Singly Linked List:


1. If head = NULL
Write Underflow
Goto step 5
[End of if]
2. Set ptr = head
3. Set head = head->next
4. Free ptr
5. exit

You might also like