0% found this document useful (0 votes)
21 views32 pages

Linked Lists: Types and Operations

This document provides an overview of Linked Lists, including their definition, types (singly, doubly, and circular), and basic operations such as insertion, deletion, and display. It explains the structure of linked lists, how they are represented, and the specific operations associated with each type. The document also includes diagrams to illustrate the processes of insertion, deletion, and reversal of linked lists.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views32 pages

Linked Lists: Types and Operations

This document provides an overview of Linked Lists, including their definition, types (singly, doubly, and circular), and basic operations such as insertion, deletion, and display. It explains the structure of linked lists, how they are represented, and the specific operations associated with each type. The document also includes diagrams to illustrate the processes of insertion, deletion, and reversal of linked lists.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Data Structures and Algorithms

(DSAs)

Linked Lists

Lecture 4
Outline
List
List Operations
Linked List
Linked List Representation
Types of Linked Lists
 Doubly Linked List
 Singly Linked List
 Circular Linked List

Linked List Operations


 Insertion, Deletion, Display, Search, Delete.
List
A list, also called a sequence, is a container that
stores elements in a certain linear order, which
is imposed by the operations performed.
The basic operations supported are retrieving,
inserting, and removing an element given
its position.
Special types of lists include stacks and
queues, where insertions and deletions can be
done only at the head or the tail of the
sequence.
The basic realization of sequences is by means
of arrays and linked lists.
List
A list can involve virtually anything, for example, a
list of integers [3; 2; 4; 2; 5], a shopping list [apples;
butter; bread; cheese], or a list of web pages each
containing a picture and a link to the next web page.

When considering lists, we can speak about-them on


different levels, on a very abstract level (on which
we can define what we mean by a list), on a level on
which we can depict lists and communicate as
humans about them, on a level on which computers
can communicate, or on a machine level in which
they can be implemented.
List Operations
Following are the basic operations supported by a list.

Insertion: Adds an element at the beginning of the list.


Deletion: Deletes an element at the beginning of the list.
Insert Last: Adds an element at the end of the list.
Delete Last: Deletes an element from the end of the list.
Insert After: Adds an element after an item of the list.
Delete: Deletes an element from the list using the key.
Display forward: Displays the complete list in a forward
manner.
Display backward: Displays the complete list in a backward
manner.
Linked Lists
A linked list is a sequence of data structures, which are
connected together via links.
Linked List is a sequence of links which contains items.
Each link contains a connection to
another link. Linked list is the second most-used data
structure after array.
Following are the important terms to understand the
concept of Linked List.
 Link − Each link of a linked list can store a data called an
element.
 Next − Each link of a linked list contains a link to the next
link called Next.
 Linked List − A Linked List contains the connection link to
the first link called First.
Linked List Representation
Linked list can be visualized as a chain of
nodes, where every node points to the next
node.
Linked List Representation
Following the illustration on the previous
slide, following are the important points to be
considered;

- Linked List contains a link element called


first.
- Each link carries a data field(s) and a link
field called next.
- Each link is linked with its next link using
its next link.
- Last link carries a link as null to mark the
end of the list.
Linked List Graphical Representation
Non-empty lists can be represented by two-cells,
in each of which the rst cell contains a pointer to
a list element and the second cell contains a
pointer to either the empty list or another two-
cell.
We can depict a pointer to the empty list by a
diagonal bar or cross through the cell. For
instance, the list [3; 1; 4; 2; 5] can be
represented as:
Types of Linked Lists
Following are the various types of linked lists.
Simple Linked List : Item navigation is
forward only.

Doubly Linked List : Items can be


navigated forward and backward.

Circular Linked List : Last item contains


link of the first element as next and the first
element has a link to the last element as
previous.
Linked List Operations
Following are the basic operations supported
by a list.

Insertion : Adds an element at the beginning


of the list.
Deletion : Deletes an element at the
beginning of the list.
Display : Displays the complete list.
Search : Searches an element using the
given key.
Delete : Deletes an element using the given
key.
Insertion Operation
Adding a new node in linked list is a more
than one step activity. We shall learn this with
diagrams here.
First, create a node using the same structure
and find the location where it has to be
inserted.
Insertion Operation
Imagine that we are inserting a node B
(NewNode), between A (LeftNode) and C
(RightNode). Then point B.next to C;
NewNode.next −> RightNode;

It should look like this


Insertion Operation
Now, the next node at the left should point to the
new node.
LeftNode.next −> NewNode;
Insertion Operation
This will put the new node in the middle of the two.
The new list should look like this

Similar steps should be taken if the node is being


inserted at the beginning of the list. While inserting it
at the end, the second last node of the list should point
to the new node and the new node will point to NULL.
Deletion Operation
Deletion is also a more than one step process.
We shall learn with pictorial representation.
First, locate the target node to be removed,
by using searching algorithms.
Deletion Operation
The left (previous) node of the target node
now should point to the next node of the
target node
LeftNode.next −> TargetNode.next;
Deletion Operation
This will remove the link that was pointing to
the target node. Now, using the following
code, we will remove what the target node is
pointing at.
TargetNode.next −> NULL;
Deletion Operation
We need to use the deleted node. We can
keep that in memory otherwise we can simply
de-allocate memory and wipe off the target
node completely.
Reverse Operation
This operation is a thorough one. We need to
make the last node to be pointed by the head
node and reverse the whole linked list.

First, we traverse to the end of the list. It


should be pointing to NULL. Now, we shall
make it point to its previous node;
Reverse Operation
We have to make sure that the last node is
not the lost node. So we'll have some temp
node, which looks like the head node pointing
to the last node. Now, we shall make all left
side nodes point to their previous nodes one
by one.
Reverse Operation
Except the node (first node) pointed by the
head node, all nodes should point to their
predecessor, making them their new
successor. The first node will point to NULL.
Reverse Operation
We'll make the head node point to the new
first node by using the temp node.

The linked list is now reversed.


Doubly Linked List
Doubly Linked List is a variation of Linked list in which
navigation is possible in both ways, either forward and
backward easily as compared to Single Linked List.
Following are the important terms to understand the
concept of doubly linked list;

Link: Each link of a linked list can store a data called an


element.
Next: Each link of a linked list contains a link to the next
link called Next.
Prev: Each link of a linked list contains a link to the
previous link called Prev.
Linked List: A Linked List contains the connection link to
the first link called First and to the last link called Last.
Doubly Linked List Representation
Doubly Linked List Representation
As per the illustration on the previous slide, following
are the important points to be considered;

Doubly Linked List contains a link element called first


and last.
Each link carries a data field(s) and a link field called
next.
Each link is linked with its next link using its next link.
Each link is linked with its previous link using its
previous link.
The last link carries a link as null to mark the end of
the list.
Doubly Linked List Operations
Following are the basic operations supported by a list;

Insertion − Adds an element at the beginning of the list.


Deletion − Deletes an element at the beginning of the list.
Insert Last − Adds an element at the end of the list.
Delete Last − Deletes an element from the end of the list.
Insert After − Adds an element after an item of the list.
Delete − Deletes an element from the list using the key.
Display forward − Displays the complete list in a forward
manner.
Display backward − Displays the complete list in a
backward manner.
Circular Linked List
Circular Linked List is a variation of Linked
list in which the first element points to the
last element and the last element points to
the first element.

Both Singly Linked List and Doubly


Linked List can be made into a circular
linked list.
Singly Linked List as Circular
In singly linked list, the next pointer of the
last node points to the first node.
Doubly Linked List as Circular
In doubly linked list, the next pointer of the last
node points to the first node and the previous
pointer of the first node points to the last node
making the circular in both directions.

As per the above illustration, following are the


important points to be considered.
The last link's next points to the first link of the
list in both cases of singly as well as doubly linked
list.

Basic Circular List Operations
Following are the important operations
supported by a circular list;

insert − Inserts an element at the start of


the list.

delete – Deletes an element from the start of


the list.

display − Displays the list.


Questions

You might also like