0% found this document useful (0 votes)
25 views60 pages

Chapter 4

The document provides an overview of linked lists, comparing them with arrays and discussing their structure, advantages, and disadvantages. It details various types of linked lists, including single and double linked lists, along with operations such as creation, insertion, and deletion. The document also includes examples of code for implementing these operations in 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)
25 views60 pages

Chapter 4

The document provides an overview of linked lists, comparing them with arrays and discussing their structure, advantages, and disadvantages. It details various types of linked lists, including single and double linked lists, along with operations such as creation, insertion, and deletion. The document also includes examples of code for implementing these operations in 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
You are on page 1/ 60

Chapter -3

LINKED LISTS

1 06/10/2025
Introduction
 Linked lists and arrays are similar since they both

store collections of data.


 It a fundamental data structure to store homogenous

data items which linear relation ships.


 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.


2 06/10/2025
Cont…
The disadvantages of arrays are:
The size of the array is fixed.
Most often this size is specified at compile time.
This makes the programmers to allocate arrays,
which seems "large enough" than required.
Inserting new elements at the front is
potentially expensive
because existing elements need to be shifted over
to make room.
3 06/10/2025
Comparison between array and
linked list:

4 06/10/2025
Linked List Concepts:
 List is a collection of elements i.e organized sequentially (finite sequence)
 A linked list is a linear collection of data structure

 Its is a collection of data elements called nodes storing data and links to other

nodes.
 A node contains some information useful for a specific application and a pointer

to the next node.


 linked list is linear data structure that built from pointer and structure.

Head node is the first element in the list and key is a value in the
information field which contains the storage address of the succossor
node
 The data items in the linked list are not in consecutive memory locations.

Which reverse of array

5 06/10/2025
Cont…
Generally linked list is a linear collection
of connected nodes

Each node contains at least two component:


A piece of data (any type)

Pointer to the next node in the list

6 06/10/2025
Cont…
Head(start) :pointer to the first node

The last node points to NULL

7 06/10/2025
Composition of a Linked List
 A linked list is called “linked” because each node

in the series

(i.e. the chain) has a pointer to the next node in


the list, e.g.

a) The list head is a pointer to the first node in the list.

b) Each node in the list points to the next node in the


list.

c) The last node points to NULL (the usual way to


signify the end).
8 06/10/2025
If the pointer between any nodes (say B and C) is re-
assigned erroneously to null or anything else, the
access to the rest of the nodes ( C, D and E ) is then
lost.
If you loose the head pointer, you loose the entire list.

9 06/10/2025
Definition of link list (node of list)
Struct Node
{
data type data;
Node * next; // pointer to next node in the list
};
Example:
struct node
{ char name[20]; // Name of up to 20 letters
int age ;
float height;
node *nxt;// Pointer to next node
};
struct node *start_ptr = NULL;
10 06/10/2025
Advantages of linked lists:
Linked lists have many advantages:
 Linked lists are dynamic data structures.

i.e., they can grow or shrink during the execution


of a program.
 A linked list can easily grow and shrink in

size :The programmer doesn’t need to know how


many nodes will be in the list. They are created
in memory as needed.
 Speed of insertion or deletion from the

list :Inserting and deleting elements into and out


11 06/10/2025
of arrays requires moving elements of the array.
Disadvantages of linked lists:
 Searching a particular element in list is difficult and

also time consuming.


 It consumes more space because every node requires a

additional pointer to store address of the next node.


 Random access is not allowed. We have to access

elements sequentially starting from the first node. So

we cannot do binary search with linked lists.


 Extra memory space for a pointer is required with each

element of the list.

12 06/10/2025
Types of Linked Lists:

 Basically we can put linked lists into the

following five items depending on the number


of links in the node and their types, linked
list can be::

1. Single Linked List. /linear linked list

2. Double Linked List.

3. Circular Linked List.

4. Circular Double Linked List.

5.Header linked list


13 06/10/2025
Single Linked List:
Is linear collection of self-referential objects, called
nodes, connected by links
 linear: for every node in the list, there is one and only
one node that precedes it (except for possibly the first
node, which may have no predecessor,) and there is
one and only one node that succeeds it, (except for
possibly the last node, which may have no successor)
 self-referential: a node that has the ability to refer
to another node of the same type, or even to refer to
itself
14 06/10/2025
Single linked list
 node: contains data of any type, including a

reference to another node of the same data


type, or to nodes of different data types
 If a node of linked list structure has a link only to

its successor in the sequence of node, the list is


called a single linked list.
 Usually a list will have a beginning and an end;

the first element in the list is accessed by a


reference to that class, and the last node in the
15 list will have a reference that is set to null
Single Linked List
Linked list is called linear linked list in which
all node are linked together in sequential
manner.

Its dynamic data structure consists of a


sequence of node forming a linear ordering

Has single chain or link between items

Each node of linked list consists of :


 Element(data or information)

 Reference or address to next


16 06/10/2025
Single linked list

17 06/10/2025
Single Linked List
The beginning of the linked list is stored in a "start or

header" 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.


The start pointer is an ordinary local pointer

variable, so it is drawn separately on the left top to


show that it is in the stack.
18 06/10/2025
Single Linked list
The basic operations in a single linked
list are:

• Creation.

• Insertion.

• Deletion.

• Traversing.

19 06/10/2025
Creating a node for Single Linked List:
 Creating a singly linked list starts with creating a node.

Get the new node using getnode().


newnode = getnode();
If the list is empty, assign new node as start.
start = newnode;
If the list is not empty, follow the steps given below:
 The next field of the new node is made to point the
first node (i.e. start node) in the list by assigning the
address of the first node.
 The start pointer is made to point the new node by
assigning the address of the new node.
 Repeat the above steps ‘n’ times.

20 06/10/2025
Single linked list with 4 node

21 06/10/2025
The function createlist(), is used
to create ‘n’ number of nodes

22 06/10/2025
Insertion of a Node:
One of the most primitive operations that can be

done in a singly linked list is the insertion of a


node.
 Memory is to be allocated for the new before

reading the data.


The new node will contain empty data field and

empty next field.


 The data field of the new node is then stored with

the information read from the user.


23 06/10/2025
The next field of the new node is assigned to NULL.
Insertion of a Node:
The new node can then be inserted at three

different places namely:

Inserting a node at the beginning.

Inserting a node at the end.

Inserting a node at intermediate position.

24 06/10/2025
Inserting a node at the beginning:
The following steps are to be followed to insert a new
node at the beginning of the list:

Get the new node using getnode().

newnode = getnode();

If the list is empty then start = newnode.

If the list is not empty, follow the steps given


below:

newnode -> next = start;

start = newnode;
25 06/10/2025
shows inserting a node into the single linked
list at the beginning

26 06/10/2025
The function for inserting a node at the
beginning

27 06/10/2025
Inserting a node at the end:
Get the new node using getnode()

newnode = getnode();

If the list is empty then start = newnode.

If the list is not empty follow the steps given below:

temp = start;

while(temp -> next != NULL)

temp = temp -> next;

temp -> next = newnode;


28 06/10/2025
shows inserting a node into the single
linked list at the end.

29 06/10/2025
The function for inserting a node at the end.

30 06/10/2025
Inserting a node at middle
position:

31 06/10/2025
Deletion of a node:
 Another primitive operation that can be done in a

singly linked list is the deletion of anode.


 Memory is to be released for the node to be

deleted.
 A node can be deleted from the list from three

different places namely.

Deleting a node at the beginning.

Deleting a node at the end.

Deleting a node at intermediate /middle position.


32 06/10/2025
Deleting a node at the beginning:
The following steps are followed, to delete a node

at the beginning of the list:

If list is empty then display ‘Empty List’


message.

If the list is not empty, follow the steps given


below:

temp = start;

start = start -> next;

free(temp);
33 06/10/2025
shows deleting a node at the
beginning of a single linked list

34 06/10/2025
The function used for deleting the first
node in the list.

35 06/10/2025
Deleting a node at the end:
If list is empty then display ‘Empty List’ message.

If the list is not empty, follow the steps given below:

temp = prev = start;

while(temp -> next != NULL) {

prev=temp;

temp = temp -> next; }

prev -> next = NULL;

free(temp);
36 06/10/2025
shows deleting a node at the end
of a single linked list

37 06/10/2025
The function is used for deleting the
last node in the list.

38 06/10/2025
Double linked List
A double linked list is a two-way list in which all
nodes will have two links.
This helps in accessing both successor node and
predecessor node from the given node position.
 It provides bi-directional traversing. Each node
contains three fields:
• Left link.(Pointer to previous node)
• Data.
• Right link.(pointer to next node)
The left link points to the predecessor node and
the right link points to the successor node.
The data field stores the required data.

39 06/10/2025
Double linked list
 Many applications require searching forward
and backward through nodes of a list.
 For example searching for a name in a
telephone directory would need forward and
backward scanning through a region of the
whole list.

40 06/10/2025
Operation on double linked List
The basic operations in a double linked list are:
Creation.
Insertion.
Deletion.
Traversing

41 06/10/2025
Creating Doubly Linked Lists
The nodes for a doubly linked list would be defined as
follows:
struct node
{
node *nxt; // Pointer to next node

node *prv; // Pointer to previous node


};
node *current;
current = new node;
current->name = “x";
current->nxt = NULL;
current->prv = NULL;
42 06/10/2025
Example

43 06/10/2025
Node * temp= new Node;
// fill the information in temp
If (!tail)
{
Head= temp;
Tail = temp;
Temp->next=NULL;
Temp-> prev= NULL;
}

44 06/10/2025
Example

Else
{
Temp->next= head;
Head->prev = temp;
Head = temp;
Temp->prev= NULL;
}

45 06/10/2025
Inserting a node at the end:
Node * temp = new Node;
// store the information in new node
If (! tail)
{
Head = temp;
Tail = temp;
Temp-> next = NULL;
Temp->prev= NULL;
}
Else{
tail->next = temp;
Temp->prev= tail;
Temp->next= NULL;
Tail = temp;
}
46 06/10/2025
Inserting at end

47 06/10/2025
Deleting a node at the beginning:
The following steps are followed, to delete a
node at the beginning of the list:
• If list is empty then display ‘Empty List’
message.
• If the list is not empty, follow the steps given
below:
temp = start;
start = start -> right;
start -> left = NULL;
free(temp);

48 06/10/2025
Deleting at beginning

49 06/10/2025
Deleting a node at the end:
The following steps are followed to delete a node
at the end of the list:
• If list is empty then display ‘Empty List’ message
• If the list is not empty, follow the steps given
below:
temp = start;
while(temp -> right != NULL)
{
temp = temp -> right;
}
temp -> left -> right = NULL;
free(temp);
50 06/10/2025
Deleting at end

51 06/10/2025
Traversal and displaying a list (Left to Right):
 To display the information, you have to traverse the
list, node by node from the first node, until the end of
the list is reached.
 The following steps are followed, to traverse a list from
left to right:
• If list is empty then display ‘Empty List’ message.
• If the list is not empty, follow the steps given below:
temp = start;
while(temp != NULL)
{
print temp-> data;
temp = temp -> right;
}
52 06/10/2025
Traversal and displaying a list (Right to Left):
 To display the information from right to left, you have to traverse
the list, node by node from the first node, until the end of the list is
reached
 If list is empty then display ‘Empty List’ message.
 If the list is not empty, follow the steps given below:
temp = start;
while(temp -> right != NULL)
temp = temp -> right;
while(temp != NULL)
{
print temp -> data;
temp = temp -> left;
}
53 06/10/2025
Counting the Number of Nodes:
The following code will count the number of
nodes exist in the list (using recursion).
int countno de(no de *start)
{
if(start == NULL)
return 0;
else
return(1 + countno de(start ->right ));
}

54 06/10/2025
Lab exercise
Write A Complete Source Code for the
Implementation of Double Linked List
operation:

55 06/10/2025
Circular Linked List
In linear linked lists if a list is traversed (all the elements

visited) an external pointer to the list must be preserved in


order to be able to reference the list again.
Circular linked lists can be used to help the traverse the

same list again and again if needed. A circular list is very


similar to the linear list where in the circular list the
pointer of the last node points not NULL but the first
node.

56 06/10/2025
Linear linked List

57 06/10/2025
Circular linked list

58 06/10/2025
Cont…

In a circular linked list there are two methods to know if a

node is the first node or not.


Either a external pointer, list, points the first node or

A header node is placed as the first node of the circular

list.
The header node can be separated from the others by either

heaving a sentinel value as the info part or having a


dedicated flag variable to specify if the node is a header
node or not.
59 06/10/2025
PRIMITIVE FUNCTIONS IN CIRCULAR LISTS

The structure definition of the circular linked lists and

the linear linked list is the same:

struct node{

int info;

struct node *next;

};

60 06/10/2025

You might also like