0% found this document useful (0 votes)
12 views13 pages

Unit 2

The document provides an overview of linked lists, including singly linked lists, doubly linked lists, and circular linked lists, detailing their structure, operations (insertion, deletion, search, display), and comparisons with arrays. It highlights the advantages of linked lists such as dynamic sizing and efficient memory allocation. Additionally, it discusses applications of linked lists in various data structures and dynamic memory management.

Uploaded by

PrasadGunde
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)
12 views13 pages

Unit 2

The document provides an overview of linked lists, including singly linked lists, doubly linked lists, and circular linked lists, detailing their structure, operations (insertion, deletion, search, display), and comparisons with arrays. It highlights the advantages of linked lists such as dynamic sizing and efficient memory allocation. Additionally, it discusses applications of linked lists in various data structures and dynamic memory management.

Uploaded by

PrasadGunde
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/ 13

UNIT-2

Linked Lists: Singly linked lists: representation and operations, doubly linked lists and
circular linked lists, Comparing arrays and linked lists, Applications of linked lists.

1) Linked List:
Linked list is a linear collection of data elements that store in a
sequence called nodes. For each node will have 2-fields. Such as

Data field

Address field

Data Field: it is used to store the element/data of list

Address Field: it is used to store the address of next node in list (i.e
link)

Example of linked list

Representation of linked list:

As in linked list the data/element is stored in the form of node , where


it consist of 2 fields . it is represented by struct data type (user
define data type). i.e

The starting node in linked list is called Head node which is used as start of linked list
Operations of linked list

The different operations of linked list are:

 Insert
 Delete
 Search
 Display

Insertion: This operation is used to insert an element into the


list. Inserting at the end involves traversing the entire list
until we reach the last node. We then set the last node’s next
reference to point to the new node, making the new node the last
element in the list.

Here data 20 is inserted at the end of list

Algorithm

 Go to the last node of the Linked List

 Change the address pointer of last node from NULL to the new
node

 Make the address pointer of new node as NULL to show the end
of Linked List
Deletion: This operation is used to delete an element from linked list.
Deleting a node in a Linked List is an important operation and can be
done by removing the first node in list.

Algorithm

 Check if the list is empty: If the head is NULL, the list is empty,
and there’s nothing to delete.

 Update the head node: Set the head to the second node (head = head-
>addr.

 Delete the original head node: The original head node is now free

Display: This operation displays all the node/elements of list.


Display of Singly Linked List is one of the fundamental operations,
where we traverse or visit each node of the linked list.

Algorithm
 We will initialize a temporary pointer to the head node of the
singly linked list.
 After that, we will check if that pointer is null or not null, if it
is null, then return.
 While the travel pointer is not null, we will access and print the
data of the current node, then we move the pointer to next address
node.

Search: This operation is used to find the given user element exist in
the list or not.

Algorithm

 We will initialize a temporary pointer to the head node of the


singly linked list.
 After that, we will check if that pointer is null or not null, if it
is null, then return.
 While the travel pointer is not null, we will access and print the
data of the current node and check with the user element both are
equal search is terminated or then we move the pointer to next
address node, repeat this.
Otherwise the element not found in the list.

-------------------------------------------------------------------------
-------

2) Double Linked List


A doubly linked list is a another data structure that store elements in a
sequence linked manner. It is more advanced than singly linked list.

The main advantage of a doubly linked list is that it allows for efficient
traversal of the list in both directions. This is because each node in the
list contains a pointer to the previous node and a pointer to the next
node.
This allows for quick and easy insertion and deletion of nodes from the
list, as well as efficient traversal of the list in both directions.

Representation of Doubly Linked List in Data Structure

The double linked list is represented using nodes that have three fields:

 Data field
 A pointer address to the next node (next_addr)
 A pointer address to the previous node (prev_addr)

Its Data structure is defined by using struct user defined data type

Each node in a Doubly Linked List contains the data it holds, a pointer to
the next node in the list, and a pointer to the previous node in the list.

By linking these nodes together through the next and prev pointers, we can
traverse the list in both directions (forward and backward), which is a
key feature of a Doubly Linked List.
Note: In double linked list Start node is called as Head and last Node is
called as tail

Operations of Double linked list includes

Insertion

Deletion

Search

Traversal

Insertion: This operation is used to insert an element into linked list.


Since each node contains a pointer to both the previous and next node,
insertion requires adjusting these pointers carefully.

Algorithm

 Create a new node with the given data.


 If the list is empty, set the new node as the head.
 Traverse the list until the last node is found.
 Set the next address pointer of the last node to the new node.
 Set the prev address pointer of the new node to the last node.

Deletion: This operation is used to delete the node from linked list.
Removing of node can done only when there is DLL. Each node in DLL
contains pointers to both its previous and next nodes, deletion requires
careful pointer adjustments to ensure no broken links occur.

Algorithm
 Check if the list is empty; if it is, return as there is nothing to
delete.
 Store the current head node in a temporary variable.
 Move the head pointer to the next node.
 If the new head exists, update its prev pointer to NULL.
 Delete the old head node to free memory.

Searching: This operation is used to search the given elelemnt in double


linked list. It can perform in both the directions i.e from head to tail
and also from tail to head. As DLL consists of chaining in 2-ways.

Example finding an elelemnt 8 in the DLL is

Given element found at 3 rd node in DLL

Algorithm

 Initialize a node variable travel to store the position of the node


containing data value x in the doubly linked list.
 Initialize a pointer travel node to store the head node of the
doubly linked list.
 Iterate over the linked list and for every node, check if data value
of that node is equal to x or not.
 If found return pos.
 Else return -1 as the node with value x is not present in DLL.
Traversal: This operation is used to travel the DLL from head node to
tail node and also tail node to head node. It is possible only if DLL
exists only.

Example: DLL is

For the above the DLL of the 2-way traversal is :

Forward Traversal : A ->B ->C ->D

Backward Traversal : D ->C ->B ->A

Note: With DLL Data structure it is easy to reverse the list without
complexity.

-------------------------------------------------------------------------
-------

3) Circular Linked List


A circular linked list is a special type of linked list where all
the nodes are connected to form a circle. Unlike a regular linked
list, which ends with a node pointing to NULL, the last node in a
circular linked list points back to the first node.

This means that you can keep traversing the list without ever
reaching a NULL value.

Types of Circular Linked Lists


We can create a circular linked list from both singly linked lists
and doubly linked lists. So, circular linked lists are basically of
two types:

1. Circular Singly Linked List


In Circular Singly Linked List, each node has just one pointer
called the “next” pointer. The next pointer of the last node points
back to the first node and this results in forming a circle. In this
type of Linked list, we can only move through the list in one
direction.
2. Circular Doubly Linked List:
In circular doubly linked list, each node has two pointers prev and
next, similar to doubly linked list. The prev pointer points to the
previous node and the next points to the next node. Here, in
addition to the last node storing the address of the first node,
the first node will also store the address of the last node.

Representation of a Circular Singly Linked List


Let’s take a look on the structure of a circular linked list.

NOTE: Node structure of CLL is represented as same as SLL or DLL

Example
Operations on the Circular Linked list
We can do some operations on the circular linked list similar to
the singly and doubly linked list which are:

1. Insertion
2. Deletion
3. Search
4. Display/Traversal

Insertion

This operation in Circular linked lists that involves adding a new


node to the list. The only extra step is connecting the last node
to the first one

Deletion

This operation involves removing a node from the Circular linked


list. The main difference is that we need to ensure the list
remains circular after the deletion. We can delete a node in a
circular linked list.
Search

Searching in a circular linked list is similar to searching in a


regular linked list. We start at a given node and traverse the list
until you either find the target value or return to the starting
node. Since the list is circular, make sure to keep track of where
you started to avoid an infinite loop.

The element to search is 3. So given element found in CLL at 2-


node.

Traversal

This operation is used to travel the CLL from head node to the
again head .Because here the list is maintained in circularly.

For the above CLL the traversal of nodes is : 2-> 3-> 4.

-------------------------------------------------------------------
------

4) Comparing Arrays and Linked List


Arrays Linked List
Arrays store list of elements Linked list store list of elements
in a sequence of memory with in a sequence of memory with
fixed size unlimited size

Array stores limited of Linked list stores unlimited of


elements only depending on size elements in the form of link
An array is static, i.e. memory The linked list is a dynamic data
size is fixed and cannot be structure whose size can be changed
updated at the run time at run time
It is defined by derived data It is defined by user defined data
type type called struct
An array is static, i.e. memory The linked list is a dynamic data
size is fixed and cannot be structure whose size can be changed
updated at the run time at run time
The elements of an array are The elements of a linked list are
independent of each other dependent as the address of the
next node is stored in the previous
node
Memory allocation is done at Memory allocation is done at run
compile time time
Since memory regions are In a linked list, insertion and
consecutive and fixed, deletion operations are quick and
inserting or removing data is uncomplicated
more expensive
Size remains constant Size grows or shrinks when elements
are inserted/deleted

5) Applications of linked lists

 Using linked list we can implement data structures such as


linear and non linear data structures (i.e Stack, Queue, Trees
and Graphs)

 Using linked list we can represent the adjacency list of graph

 Dynamic memory allocation implementation is possible


 Maintaining a directory of names
 Polynomial equations can able to store
 Sparse matrices are representated.

You might also like