0% found this document useful (0 votes)
89 views78 pages

CSC 221 DSA Linked List Single Double Circular 10032023 053401pm

The document discusses linked lists, including single linked lists. It explains that a linked list is a linear data structure composed of nodes, where each node contains a data field and a pointer to the next node. The document compares linked lists to arrays, noting that linked lists can dynamically grow and shrink in size more easily than arrays. It also describes the basic operations of linked lists like traversal, insertion, and deletion of nodes.

Uploaded by

Kumail Raza
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)
89 views78 pages

CSC 221 DSA Linked List Single Double Circular 10032023 053401pm

The document discusses linked lists, including single linked lists. It explains that a linked list is a linear data structure composed of nodes, where each node contains a data field and a pointer to the next node. The document compares linked lists to arrays, noting that linked lists can dynamically grow and shrink in size more easily than arrays. It also describes the basic operations of linked lists like traversal, insertion, and deletion of nodes.

Uploaded by

Kumail Raza
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/ 78

CSC-221 DATA STRUCTURES AND ALGORITHMS

Instructor: Ayesha Jamal


Lecture : Linked List (Single, Double, Circular)
LINKED LIST
LINKED LIST

 A linked list is a linear data structure.


 It consists of nodes where each node contains a data field and a
reference(link) to the next node in the list.
 Each node holds its own data and the address of the next
node hence forming a chain like structure.
 Elements in a linked list are not stored at contiguous memory
locations.
 Elements in a linked list are linked using pointers.
ARRAY VS. LINKED LIST

 Linked list does not require explicit mention of size at declaration


 It can easily shrink and grow as per requirement

5
An Array A Linked List

6
ARRAYS VS LISTS

• Arrays are lists that have a fixed size in memory.


• The programmer must keep track of the length of the array
• No matter how many elements of the array are used in a program,
the array has the same amount of allocated space.
• Array elements are stored in successive memory locations. Also,
order of elements stored in array is same logically and physically.

7
ARRAYS VS LISTS

• A linked list takes up only as much space in memory as is needed


for the length of the list.
• The list expands or contracts as you add or delete elements.
• In linked list the elements are not stored in successive memory
location
• Elements can be added to (or deleted from) either end, or added to
(or deleted from)the middle of the list.
8
ARRAY VERSUS LINKED LISTS

 Linked lists are more complex to code and manage than arrays, but they
have some distinct advantages.
 Dynamic: a linked list can easily grow and shrink in size.
 We don’t need to know how many nodes will be in the list. They are created in memory as needed.
 In contrast, the size of a C++ array is fixed at compilation time.

 Easy and fast insertions and deletions


 To insert or delete an element in an array, we need to copy to temporary variables to make room for
new elements or close the gap caused by deleted elements.
 With a linked list, no need to move other nodes. Only need to reset some pointers. 9
LINKED LIST

A B C 

Head
 A linked list is a series of connected nodes
 Each node contains at least
node
 A piece of data (any type)
A
 Pointer to the next node in the list
data pointer
 Head: pointer to the first node
 The last node points to NULL
10
REPRESENTATION OF LINKED LIST

 A linked list is represented by a pointer to the first node of the linked list.
 The first node is called the head.
 If the linked list is empty, then the value of the head is NULL. 
 Each node in a list consists of at least two parts: 
1) Data 
2) Pointer (Or Reference) to the next node 
LINKED LIST ACTUAL PICTURE IN MEMORY
1051 6
1052 1063
current 1053 1063
1054 2
head 1055 1051
1056
2 6 8 7 1 1057 7
1058 1060
current 1059
1060 1
1061 0
head 1062 1054
1063 8
1064 1057
1065
OPERATIONS IN LINKED LIST

 Traversing Linked List


 Insert a Node in Linked List
 Deleting a Node from Linked List
TYPES OF LINKED LIST
 Single Linked List:
Navigation is forward only.
 Double Linked List:
Navigation is forward and backward.
 Circular Linked List:
Last element is linked to first element.
 Circular Doubly Linked List:
Combination of circular & doubly linked list.
Single Linked List
SINGLE LINKED LIST

 Single Linked list is the default linked list and a linear data structure in which each
data node is connected to the next data node via a pointer, hence forming a chain.
 The element in such a linked list can be inserted in 3 ways:
 Insertion at beginning of the list.
 Insertion at the end of the list.
 Insertion at the particular position in the list.
WHAT IS A NODE?

 A Node in a linked list holds the data value and the pointer which points
to the location of the next node in the linked list.
 Why we need to store the location of the next node?
 Because the memory locations allocated to these nodes are not contiguous
hence each node should know where the next node is stored.
 As the node is a combination of multiple information, hence we will be
defining a class for Node which will have a variable to store data and
another variable to store the pointer.
CREATING A NODE
To avoid get/set methods
Class Node {
Public:
int data; // data in node
Node *next; // Pointer to next node
};

//Driver.cpp:
Node *p; p 10
p = new Node;
p -> data = 10;
p -> next = NULL; 18
DELETING A NODE

p 10
//Driver.cpp:
delete p;
p = NULL;
p

19
AN INTEGER (SINGLY) LINKED LIST

First Node of List Last Node of List

head
10 13 5 2

data next NULL

class Node {
public:
int data;
Node *next;
};
20
JOINING TWO NODES
Node *p, *q;
p 10
p = new Node;
p - > data = 10;
p - > next = NULL;

q = new Node; q 6
q - > data = 6;
q - > next = NULL;

p 10 6
p - > next = q; 21

q
ACCESSING LIST DATA

p 10 6

Expression Value
p Pointer to first node (head)
p - > data 10
p - > next Pointer to next node
p - > next - > data 6
p - > next - > next NULL pointer
22
Basic Concepts

BUILDING A LIST FROM 1 TO N


class Node {
public:
int data;
Node *next;
};

Node *head = NULL; // pointer to the list head

head

23
Basic Concepts

CREATING THE FIRST NODE


Node *ptr; // declare pointer to Node
ptr = new Node; // create a new Node
ptr - > data = 1;
ptr - > next = NULL;

head = ptr; // new node is first

head 1
ptr

24
ADDING MORE NODES

Node *ptr2 = new Node;


head 1 2
ptr2 - > data = 2;
ptr2 - > next = NULL; ptr
ptr - > next = ptr2; // order is
ptr = ptr2; // important ptr2
head 1 2
ptr2 = new Node;
ptr2 - > data = 3;
ptr2 - > next = NULL;
ptr
ptr - > next = ptr2; // order is
ptr = ptr2; // important
ptr2 3

•Create a new node with data field set to 3


•Its next pointer should point to NULL
26
head 1 2

ptr

ptr2 3
The next pointer of the node which was
previously last should now point to newly
created node “ptr->next=ptr2”
27
head 1 2

ptr

ptr2 3

ptr should now point to the newly created Node


“ptr = ptr2;”
28
RE-ARRANGING THE VIEW

head 1 2 3
ptr
ptr2

29
TRAVERSING THE LIST
void printList()
{
Node *pointer = head;
while (pointer != NULL)
{
cout<< pointer->data;
pointer = pointer->next;
}
} 30
ANALYSIS OF LINKED LIST
 add
 we simply insert the new node after the current node.
 remove
 remove is also a one-step operation
 search
 You may have to search the entire list
 back
 moving the current pointer back one node requires traversing the list from the start
until the node whose next pointer points to current node.
NODE INSERTION

 Insertion at the beginning of the list


 Insertion at the end of the list
 Insertion in arbitrary location in a the list

32
NODE INSERTION AT THE BEGINNING
Steps:
 Create a node
 Set the node data values
 Connect the pointers
Initial list head 48 17 142 //
Step 1 Step 2

List after Step 3

head 93
NODE INSERTION AT THE BEGINNING
Initial list head 48 17 142 //
ptr
Node *ptr; ptr
ptr = new Node; 93
head
ptr - > data = 93;
ptr - > next = head;
head = ptr;

head
Rearrange:

head 93
NODE INSERTION AT THE END

Steps:
 Create a Node
Initial list head 48 17 142 //
 Set the node data values Step 1 Step 2
 Connect the pointers

List after Step 3


NODE INSERTION AT THE END

Initial list head 48 17 142 //


Need a pointer at the last node
ptr

Node * ptr;
ptr = head;
while (ptr->next != NULL)
{
ptr = ptr->next;
}
NODE INSERTION AT THE END

Initial list head 48 17 142 //

ptr

Node * p;
p = new Node;
p -> data = 150;
p -> next = NULL;
150 //

p
NODE INSERTION AT THE END

Node * p;
p = new Node;
p -> data = 150; ptr

p -> next = NULL;


ptr -> next = p; head 48 17 142

150 //
p
NODE INSERTION AT ARBITRARY POSITION
Steps:
 Create a Node
 Set the node data values Step 1 Step 2

 Break pointer connection


 Re-connect the pointers
Step 3

Step 4
NODE INSERTION AT ARBITRARY POSITION
Steps:
 Create a Node
 Set the node data values
 Break pointer connection
 Re-connect the pointers
Need a pointer on the node after which a new node is to be
Inserted. For instance, if new node is to be inserted after the node
having value ‘17’, we need a pointer at this node

ptr
How to get pointer on desired node?
NODE INSERTION AT ARBITRARY POSITION
Suppose we want to insert a node after the node having value ‘x’:

Node * ptr;
ptr = head;
while (ptr->data != x)
{
ptr = ptr->next;
}

ptr
NODE INSERTION AT ARBITRARY POSITION
Node * ptr; Node * p;
p = new Node;
ptr = head;
p -> data = 100;
while (ptr->data != x) p -> next = NULL;
{
ptr = ptr->next;
}

ptr 150 //
p
NODE INSERTION AT ARBITRARY POSITION
Node * ptr; Node * p;

ptr = head; p = new Node;


p -> data = 150;
while (ptr->data != x)
p -> next = NULL;
{ p -> next = ptr -> next;
ptr = ptr->next; ptr -> next = p;
}

ptr
150
p
NODE DELETION

 Deleting from the beginning of the list

 Deleting from the end of the list

 Deleting from the middle of the list


DELETING FROM THE BEGINNING
Steps:
 Break the pointer connection
 Re-connect the nodes
 Delete the node
head 6 4 17 42

head
6 4 17 42

head 4 17 42
DELETING FROM THE BEGINNING
head
ptr 6 4 17 42

head
head 6 4 17 42
ptr

head 4 17 42

Node * ptr;
ptr = head;
head = head ->next;
delete ptr;
DELETING FROM THE END
Steps:
 Break the pointer connection
 Set previous node pointer to NULL
 Delete the node

head 6 4 17 42

head 6 4 17 42

head 6 4 17
DELETING FROM THE END

head 6 4 17 42

predPtr ptr

Node * ptr; We need a pointer one node before


Node * predPtr; the node to be deleted
ptr = head;
predPtr = NULL;
while (ptr->next != NULL)
{
predPtr = ptr;
ptr = ptr->next;

}
DELETING FROM THE END
head 6 4 17 42

predPtr ptr

head 6 4 17 42

predPtr ptr

head 6 4 17 42

predPtr -> next = NULL; predPtr ptr


delete ptr;
head 6 4 17
DELETING FROM AN ARBITRARY POSITION

Steps:
 Set previous Node pointer to next node
 Break Node pointer connection
head 4 17 42
 Delete the node

head 4 17 42

head 4 42
DELETING FROM AN ARBITRARY
POSITION
head 4 17 42

predPtr ptr
Node * ptr; We need a pointer on the node to be
Node * predPtr; deleted (as well a pointer to one node
before the node to be deleted)
ptr = head;
predPtr = NULL;
while (ptr->data != x)
Given the value of the node to be
deleted, assume this to be variable ‘x’
{ Keep moving a pointer until the
predPtr = ptr; required node is reached
ptr = ptr->next;
}
DELETING FROM AN ARBITRARY POSITION

head 4 17 42

predPtr -> next = ptr -> next; predPtr ptr


delete ptr;
head 4 17 42

predPtr ptr

head 4 17 42

predPtr ptr
Circular Linked List
CIRCULARLY-LINKED LISTS

 The next field in the last node in a singly-linked list is set to NULL.
 Moving along a singly-linked list has to be done in a watchful manner.
 Doubly-linked lists have two NULL pointers: prev in the first node and
next in the last node.
 A way around this potential hazard is to link the last node with the first
node in the list to create a circularly-linked list.
CIRCULARLY-LINKED LISTS

 The next field in the last node in a singly-linked list is set to NULL.
 Moving along a singly-linked list has to be done in a watchful manner.
 Doubly-linked lists have two NULL pointers: prev in the first node and
next in the last node.
 A way around this potential hazard is to link the last node with the first
node in the list to create a circularly-linked list.
CIRCULARLY-LINKED LISTS

 The next field in the last node in a singly-linked list is set to NULL.
 Moving along a singly-linked list has to be done in a watchful manner.
 Doubly-linked lists have two NULL pointers: prev in the first node and
next in the last node.
 A way around this potential hazard is to link the last node with the first
node in the list to create a circularly-linked list.
CIRCULARLY-LINKED LISTS

 The next field in the last node in a singly-linked list is set to NULL.
 Moving along a singly-linked list has to be done in a watchful
manner.
 Doubly-linked lists have two NULL pointers: prev in the first node
and next in the last node.
 To create a circularly-linked list, link the last node with the first
node in the list.
CIRCULARLY LINKED LIST
 Two views of a circularly linked list:

current

head 2 6 8 7 1 size=5

current
6
8
size=5
head 2

1
APPLICATIONS OF LINKED LIST

1. Images are linked with each other. So, an image viewer software uses a
linked list to view the previous and the next images using the previous
and next buttons.
2. Web pages can be accessed using the previous and the next URL links
which are linked using linked list.
3. The music players also use the same technique to switch between music.
4. To keep the track of turns in a multi player game, a circular linked list is
used. 
CIRCULAR LINKED LISTS

60
CIRCULAR LINKED LIST
 A linked list in which the last node points to the first node is called a
circular linked list

head 9 17 22 26 34

61
CIRCULAR LINKED LIST

head Empty list

head

data
One node in list
next

head
Multiple nodes in list

data data data

next next next 62


CIRCULAR LINKED LIST - IMPLEMENTATION

class Node{ class CircularList{


public: public:
int data; Node *head;
Node* next; CircularList();
}; --------
--------
};
TRAVERSING CIRCULAR LINKED LIST

void CircularList::traverse()
{
Node *temp = head;
if (head!= NULL)
{
do
{
cout<<temp->data<<" ";
temp=temp->next;
}while (temp != head);
}
} 64
ADD NODE TO BEGINNING OF LIST
void CircularList::add_begin(int data)
else
{
{
Node *ptr = new Node;
Node *temp = head;
ptr->data = data;
while(temp->next!=head)
ptr->next = NULL;
{
if (head == NULL)
temp=temp->next;
{
}
head = ptr;
temp->next = ptr;
ptr->next = head;
ptr->next = head;
}
head = ptr;
}
}
65
ADD NODE TO END OF LIST

void CircularList::add_end(int data) else


{ {
Node *ptr = new Node; Node *temp = head;
ptr->data = data; while(temp->next!=head)
ptr->next = NULL; {
if (head == NULL) temp=temp->next;
{ }
head = ptr; temp->next = ptr;
ptr->next = head; ptr->next = head;
} }
}

66
DELETE NODE FROM BEGINNING OF LIST

void CircularList::del_beg() }
{ else{
Node *temp; Node *t=head;
temp=head; while(temp->next!=head)
if(head==NULL){ {
cout<<"\nList has no nodes"; temp=temp->next;
return; }
} head=head->next;
//Only one node in list temp->next=head;
if(head->next==head){ delete t;
head=NULL; }
delete temp; }
return; 67
DELETE NODE FROM END OF LIST
void CircularList::del_end()
else
{
{
Node *temp;
Node *prevtemp;
temp=head;
while(temp->next!=head)
if(head==NULL)
{
{
prevtemp=temp;
cout<<"\nList has no nodes";
temp=temp->next;
}
}
//Only one node in list
prevtemp->next=temp->next;
if(head->next==head)
delete temp;
{
}
head=NULL;
}
delete temp;
} 68
DOUBLY LINKED LISTS

69
INTRODUCTION

 The singly linked list contains only one pointer field i.e. every node holds an address
of the next node.
 The singly linked list is uni-directional i.e. we can only move from one node to its
successor.
 Doubly linked list: Each node has two pointers, next and previous
 Each node has one pointer to its successor (NULL if there is none) and one pointer
to its predecessor (NULL if there is none).

70
INTRODUCTION
prev

data
predecessor successor

next

first 9 17 22 26 34

last

71
Node of a doubly linked list
*Previous Data *Next

A SINGLY LINKED LIST


Head

A DOUBLY LINKED LIST

Head 72
INTRODUCTION
 Linked list

class Node
{
int data;
Node* next;

};
 Doubly linked list

class Node {
Node *previous;
int data;
73
Node *next;

};
DOUBLY LINKED LIST : INSERTION
predptr class Node
{
public:
int data;
Node * next;
15 20 Node * prev;
};

void insert(Node * predPtr, int data)


{
17 Node* newptr = new Node;
newptr->data = data;
newptr->prev = predptr;
newptr newptr->next = predptr->next;
predptr->next->prev = newptr;
predptr->next = newptr;
} 74
DOUBLY LINKED LIST : DELETION
ptr

15 17 20

void delete(Node * ptr)


{ free
ptr->next->prev = ptr->prev;
ptr->prev->next = ptr->next; 75

delete ptr;
ADD NODE AT THE END OF LIST

// List already has element(s)


void DoublyList::add_end(int value){
else
node *temp, *ptr;
{
ptr= new node;
temp = head;
ptr>data = value;
while (temp->next != NULL)
ptr->next = NULL;
{ temp = temp->next;}
ptr->prev = NULL;
temp->next = ptr;
// If list has no elements
ptr->prev = temp;
if (head == NULL)
}
{
}
head = ptr;
}
76
ADD NODE AT THE BEGINNING OF LIST
void DoublyList::add_begin(int value){

node *temp;
temp = new node;
temp->prev = NULL;
temp->next = NULL;
temp->data = value;
// If list has no elements
if (head == NULL)
{
head = temp;
}
// List has element(s)
temp->next = head;
head->prev = temp;
77
head = temp;
}
DELETION OF NODE
//Last node to be deleted
if(p->next==NULL){
p->prev->next=NULL;
delete p;
return;
}

//Node in between other nodes to be deleted


p->prev->next = p->next;
p->next->prev = p->prev;
delete p;
} 78

You might also like