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

Linked List

Linked list is a linear data structure consisting of nodes connected by pointers. Each node contains a data field storing an element and a pointer field containing the memory address of the next node. A linked list allows dynamic memory allocation and efficient insertion/deletion anywhere in the list. Common operations on linked lists include insertion, deletion, traversal, and search. Linked lists have advantages over arrays like dynamic size and efficient memory usage.

Uploaded by

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

Linked List

Linked list is a linear data structure consisting of nodes connected by pointers. Each node contains a data field storing an element and a pointer field containing the memory address of the next node. A linked list allows dynamic memory allocation and efficient insertion/deletion anywhere in the list. Common operations on linked lists include insertion, deletion, traversal, and search. Linked lists have advantages over arrays like dynamic size and efficient memory usage.

Uploaded by

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

LINKED LIST

Introduction

 inked list is a linear data structure that includes a series of connected nodes.
 It is a dynamic data structure.
 For every data item in a linked list, there is an associated pointer that gives the
memory location of the next data item in the linked list.
 The data items in the linked list are not in consecutive memory locations. They may
be anywhere, but the accessing of these data items is easier as each data item
contains the address of the next data item.
 Linked list can be defined as the nodes that are randomly stored in the memory.
 A node in the linked list contains two parts, i.e., first is the data part and second is
the address part.
 The last node of the list contains a pointer to the null.
 After array, linked list is the second most used data structure. In a linked list, every
link contains a connection to another link.

Linked List is a Series of Nodes, where each node Consist of two Parts viz Data Part &
Pointer Part, the Pointer Part stores the address of the next node

Example

In above figure the Linked List consists of three nodes. First Node1 has two-part one
data part which consists of the 5 as data and the second part which contain the
address of the next node. The last node3 has the address part null showing that no
more data in the list.
Linked List Vs Arrays

 Linked lists and arrays are similar since they both store linear collections of data.
 Array is the most common data structure used to store collections of elements. Arrays
are convenient to declare and provide the easy syntax to access any element by its index
number. Once the array is set up, access to any element is convenient and fast.

The disadvantages of arrays are:


 The size of the array is fixed and is specified at compile time. It is almost impossible to
expand the size of the array at run time.
 The size of the array must be known in advance before using it in the program.
 Inserting new elements at the front and deleting an element from an array is
potentially expensive because existing elements need to be shifted over to make
room.
 Array allocates the memory for all its elements in one block. That is all the elements
in the array need to be contiguously stored in the memory.

Advantages of linked lists:


Linked lists have many advantages over Arrays. Some of the very important advantages are:
 Linked lists are dynamic data structures. i.e., they can grow or shrink during the
execution of a program.
 Linked lists have efficient memory utilization. Here, memory is not pre-allocated.
Memory is allocated whenever it is required and it is de-allocated (removed) when it
is no longer needed.
 Insertion and Deletions are easier and efficient. Linked lists provide flexibility in
inserting a data item at a specified position and deletion of the data item from the
given position.
 Many complex applications can be easily carried out with linked lists.
 Linear Data Structures such as Stack, Queue can be easily implemented using Linked
list

Disadvantages of linked lists:


 It consumes more space because every node requires an additional pointer to store
address of the next node.
 Searching a particular element in list is difficult and also time consuming.
 No direct access to a particular element.
Representation of a Linked list

Linked list can be represented as the connection of nodes in which each node points to the
next node of the list. The representation of the linked list is shown below -
Types of Linked list
Basically, linked list is classified into the following types:
1. Single Linked List.
2. Double Linked List.
3. Circular Linked List

Singly-linked list - Singly linked list can be defined as the collection of an ordered set of
elements. A node in the singly linked list consists 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. These types of lists are
often referred to as linear linked list.

Doubly 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), and pointer to the previous node (previous pointer).
This helps to traverse in forward direction and backward direction.

Circular linked list - In a circular singly linked list, the last node of the list contains a pointer
to the first node of the list. We can have circular singly linked list as well as circular doubly
linked list.
Applications of Linked list
The applications of the Linked list are given as follows -

 Polynomials can be represented in linked list as well as can perform the operations
on the polynomial.
 A linked list can be used to represent the sparse matrix.
 The various operations like student's details, employee's details, or product details
can be implemented using the linked list as the linked list uses the structure data
type that can hold different data types.
 Using linked list, we can implement stack, queue, tree, and graph data structures.
 A linked list can be used to implement dynamic memory allocation. The dynamic
memory allocation is the memory allocation done at the run-time.
Operations performed on Linked list
The basic operations that are supported by a list are mentioned as follows –

Insertion - This operation is performed to add an element into the list.


Deletion - It is performed to delete an operation from the list.
Display - It is performed to display the elements of the list.
Search - It is performed to search an element from the list using the given key.

Complexity of Linked list


Worst-case Time complexity for Insertion and Deletion in linked list is O(1) and for Traversal
is O(n)
Where 'n' is the number of nodes in the given tree.
The Space Complexity of linked list for Insertion, Deletion and Search is O(n)
SINGLY LINKED LIST OR ONE WAY 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 consists 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.
 The beginning of the linked list is stored in a "START/ HEAD" pointer which points to the
first node
 The first node contains a pointer to the second node. The second node contains a
pointer to the third node, ... and so on.
 The last node in the list has its next field set to NULL to mark the end of the list.In other
words, we can say that each node contains only next pointer, therefore we cannot
traverse the list in the reverse direction.

The basic operations in a Singly linked list are:


 Insertion.
 Deletion.
 Traversing.

(1) Insert node at the beginning of the linked list


1. Create New Node

NEW= AVAIL
AVAIL= LINK[AVAIL]
2. Fill Data into “Data Field“
INFO[NEW]=ITEM
3. Fill it’s “Link Field” as NULL or the address pointed by the START

LINK[NEW]=START or LINK[NEW]= NULL


4. Newly Created node is now attached to Start
5. Make new node as Starting node
START=NEW
Algorithm to insert node at the beginning
1. If AVAIL=NULL, write “Overflow” & EXIT
2. Else
NEW=AVAIL ( Remove first node from AVAIL list)
AVAIL=LINK[AVAIL]
3 INFO[NEW] = ITEM (copies new data into new node)
st
4. If START= NULL then (1 node insertion)
START=NEW
LINK[NEW]= NULL
Else
LINK[NEW]=START (new node now points to original first node)
5. START=NEW (change START data so it points to the new node)
6. EXIT

(2) Algorithm to Insert node after a given node in LOC

INSLOC (INFO, LINK, START, AVAIL, LOC, ITEM)


1. If AVAIL=NULL, write “Overflow” & EXIT
Else
NEW=AVAIL (Remove first node from AVAIL list)
AVAIL=LINK[AVAIL]
2. INFO[NEW] = ITEM (copies new data into new node)
st
3 If LOC= NULL then (1 node insertion)
LINK[NEW]= START
START=NEW
Else
LINK[NEW]=LINK[LOC] (new node inserted after node with LOC)
LINK[LOC] =NEW
5. EXIT
(3)Algorithm to Insert node in sorted LinkedList
Find (INFO, LINK, START, ITEM, LOC)

(Finds LOC of the last node in a sorted list)


1. If START= NULL then (List is empty )
LOC= NULL
Return;
Else
If ITEM< INFO[START] Then (special situation, first node insertion)
LOC = NULL
Return;
2. SAVE=START (initialize the pointers)
3. PTR=LINK[START]
4. While PTR  NULL , repeat step 5 &6
5. IF ITEM < INFO[PTR] THEN
LOC=SAVE
Return;
6. SAVE= PTR
PTR=LINK[PTR] (Update the pointers)
End of loop)
7. LOC=SAVE
8. Return

INSORT (INFO, LINK, START, ITEM, LOC)


1. Call Find()
2. Call INSLOC()
3. Exit
Deletion from a Linked List

(1) Algorithm to delete a node from the beginning of the Linked List
1. If START=NULL, then write “Underflow”
EXIT
2. LOC=START
3. START=LINK[START]
4. LINK[LOC]=AVAIL (add to avail list)
AVAIL=LOC
5. EXIT

(2) Algorithm to Deleting the node following a given Node


Algorithm Deletes the node N with location LOC. SAVE is the location of the
node which precedes N
1. If SAVE= NULL then

START=LINK[START] (deletes first node)

else
LINK[SAVE]=LINK[LOC] (deletes node N)

2. LINK[LOC]=AVAIL (return deleted node to AVAIL List)

AVAIL=LOC
3. EXIT
DOUBLY LINKED LIST OR TWO-WAY 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.
 In a singly linked list, we could traverse only in one direction, because each node
contains address of the next node and it doesn't have any record of its previous nodes.
However, doubly linked list overcome this limitation of singly linked list.
 In doubly linked list each node of the list contains the address of its previous node and
the next node. This helps to traverse in forward direction and backward direction.
 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).
 Many applications require searching forward and backward thru nodes of a list. For
example, searching for a name in a telephone directory would need forward and
backward scanning thru a region of the whole list
A sample node in a doubly linked list is shown in the figure.

Two pointer variables can also use in the doubly linked list, namely FIRST –which points to
the first node in the list and LAST-which points to the last node in the list is also used

The basic operations in a double linked list are:


 Insertion.
 Deletion.
 Traversing.
DELETION OPERATION
Deletion in the middle of the doubly linked list.
Algorithm to Delete the node N given in LOC
1. FORW[BACK[LOC]] = FORW[LOC]

2. BACK[FORW[LOC]= BACK[LOC]

INSERTION OPERATION
1. Insertion in the middle of the doubly linked list
Algorithm to Insert a given ITEM between node A and B in LOCA and LOCB

1. FORW[LOCA]=NEW
2. FORW[NEW]=LOCB
3. BACK[LOCB]=NEW
4. BACK[NEW]=LOCA

2. Insertion at the beginning of doubly linked list


Algorithm to Insert at the beginning of doubly linked list
1. INFO[NEW]=ITEM
2. If START=NULL then
BACK[NEW] = FORW[NEW] = NULL (only node in the list)
START=NEW
else
BACK[NEW]=NULL
FORW[NEW]=START
START=NEW
3. EXIT
CIRCULAR LINKED LIST
 Circular Linked List is a special type of linked list in which all the nodes are linked in
continuous circle.
 It is a single linked list in which the link field of the last node points back to the
address of the first node.
 A circular linked list has no beginning and no end.
 But it is necessary to establish a special pointer called START pointer that points to
the first node of the list.
 Circular linked lists are frequently used instead of ordinary linked list because many
operations are much easier to implement in Circular Linked List
 In circular linked list no null pointers are used, hence all pointers contain valid
address.
 Circular list can be singly or doubly linked list.

Advantages:-

 Circular lists are frequency used instead of ordinary linked list because in circular list all
nodes contain a valid address.
 The important feature of circular list is as follows.
o In a circular list every node is accessible from a given node.
o Certain operations like concatenation and splitting becomes more efficient in
circular list.
Disadvantages:

 Without some conditions in processing, it is possible to get into an infinite Loop.


Operations on Circular linked list:
Inserting At Beginning of the list
1. If AVAIL=NULL, write “Overflow” & EXIT
Else
NEW=AVAIL (Remove first node from AVAIL list)
AVAIL=LINK[AVAIL]
2. INFO[NEW] = ITEM
3. PTR = START
4. Repeat Step 5 while LINK [PTR]! = START
5. PTR = LINK [PTR]
End of Loop
6. LINK[NEW]=START
7. LINK[PTR]= NEW
8. START=NEW
9. EXIT

Inserting a new node at the end of a circular linked list


1. If AVAIL=NULL, write “Overflow” & EXIT
Else
NEW=AVAIL (Remove first node from AVAIL list)
AVAIL=LINK[AVAIL]
2. INFO[NEW] = ITEM
3. LINK[NEW]=START
4. PTR = START
5. Repeat Step 6 while LINK [PTR]! = START
6. PTR = LINK [PTR]
End of Loop
7. LINK[PTR]= NEW
8. EXIT
Deleting the first node from a circular linked list

1. IF START = NULL;
Write UNDERFLOW;
EXIT
2. PTR = START
3. Repeat Step 4 while LINK[PTR]! = START
4. PTR = LINK[PTR]
[END OF LOOP]
5. LINK[PTR] = LINK [START]
6. START = LINK [PTR]
7. EXIT

Deleting the last node from a circular linked list

1: IF START = NULL; Write UNDERFLOW; EXIT


2: SET PTR = START
3: Repeat Steps 4 and 5 while LINK[PTR] != START
4: PREPTR = PTR
5: PTR = LINK[PTR]
END OF LOOP]
6: LINK[PREPTR] = START
7: EXIT

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