You are on page 1of 41

SINGLY LINKED LIST 1 of 14

Definition of linked list

A linked list is a linear collection of homogeneous data elements, called nodes, where
linear order is maintained by means of links or pointers.

Each node has two parts:


 The first part contains the data (information of the element) and
 The second part contains the address of the next node (link /next pointer field) in
the list.

Data part of the link can be an integer, a character, a String or an object of any kind.

Dr.L.Lakshmi 1
SINGLY LINKED LIST 2 of 14
NODE
node

link class Node:


def __init__(self, data):
self.data = data
self.link = None

data

LINKED LIST

Start Null

A B C D
Dr.L.Lakshmi 2
SINGLY LINKED LIST 3 of 14

• Linear collection of self-referential structures, called nodes, connected by pointer


links.

• Accessed via a pointer to the first node of the list.

• Subsequent nodes are accessed via the link-pointer member stored in each node.

• Link pointer in the last node is set to null to mark the end of the list.

• Data stored dynamically – each node is created as necessary.

• Length of a list can increase or decrease.

• Becomes full only when the system has insufficient memory to satisfy dynamic
storage allocation requests.

Dr.L.Lakshmi 3
SINGLY LINKED LIST 4 of 14

TYPES OF LINKED LISTS

1. Singly linked list


• Begins with a pointer to the first node
• Terminates with a null pointer
• Only traversed in one direction

2. Circular, singly linked list


• Pointer in the last node points back to the first node

3. Doubly linked list


• Two “start pointers”- first element and last element
• Each node has a forward pointer and a backward pointer
• Allows traversals both forwards and backwards

4. Circular, doubly linked list


• Forward pointer of the last node points to the first node and backward pointer
of the first node points to the last node

Dr.L.Lakshmi 4
SINGLY LINKED LIST 5 of 14

A singly linked list, or simply a linked list, is a linear collection of data items. The linear
order is given by means of POINTERS. These types of lists are often referred to as
linear linked list.

•Each item in the list is called a node.

•Each node of the list has two fields:

Information- contains the item being stored in the list.


Next address- contains the address to the next item in the list.

•The last node in the list contains NULL pointer to indicate that it is the end of the list.

Dr.L.Lakshmi 5
SINGLY LINKED LIST 6 of 14

OPERATIONS

• Creation of a node
• Insertions
• Deletions
• Traversing the list

Structure of a node: data link

class Node:
def __init__(self, data):
self.data = data
self.link = None

Dr.L.Lakshmi 6
SINGLY LINKED LIST 7 of 14

CREATION OF A NODE:

def create(self, data):


if self.tail is None:
self.head = Node(data)
self.tail = self.head

Dr.L.Lakshmi 7
SINGLY LINKED LIST 8 of 14
Function for creating a node

def create(self, data):


if self.tail is None:
self.head = Node(data)
self.tail = self.head
else:
self.tail.link = Node(data)
self.tail = self.tail.link

Dr.L.Lakshmi 8
SINGLY LINKED LIST 9 of 14
INSERTION:

How do we place elements in the list: Usually, there are 4 cases we can use:

•at the beginning

•at the end of the list

•after a given element

•before a given element

Dr.L.Lakshmi 9
SINGLY LINKED LIST 10 of 14
Case 1 : at the beginning Pseudo code:
1. def insert_at_begin(self,data):
2. x=Node(data)
3. x.link=self.head
4. self.head=x

Dr.L.Lakshmi 10
SINGLY LINKED LIST 11 of 14
Case 2 : end of the list Pseudo code:
1. def insert_at_end(self,data):
2. x=Node(data)
3. self.tail.link=x
4. self.tail=x

Dr.L.Lakshmi 11
SINGLY LINKED LIST 12 of 14
Case 3 : after a given element Pseudo code:
1. Inserting at end of list.
2. if (first! = NULL)
3. {
4. Cur=(struct node*) malloc
(sizeof(struct node))
5. Read cur->data
6. Next=first
7. while(next!=NULL)
8. {
9. if(next->data = = key)
10. break;
11. else
12. next=next->link;
13. }
14. cur -> link = next->link;
15. next->link = cur;
16. }

Dr.L.Lakshmi 12
SINGLY LINKED LIST 12 of 14
Case 4 : Before a given element
Pseudo code:
1. Inserting at end of list.
2. if (first! = NULL)
3. {
4. Cur=(struct node*) malloc
(sizeof(struct node))
5. Read cur->data
6. Next=first
7. while(next!=NULL)
8. {
9. if(next->data = = key)
10. break;
11. else
1. { prev=next
12. next=next->link; }
13. }
14. cur -> link = prev->link;
15. prev->link = cur;
16. }
Dr.L.Lakshmi 13
SINGLY LINKED LIST 12 of 14
DELETION: Removing an element from the list, without destroying the integrity of the list
itself.

When we delete a node we logically remove the node from the list by changing links and
physically remove it from heap

1)deletion from beginning of list


2)deletion at middle of list
3)deletion at end of list

Dr.L.Lakshmi 14
SINGLY LINKED LIST 13 of 14
DELETION at the beginning of list:

Pseudo code:
1. def delete_at_begin(self):
2. cur=self.head
3. self.head=self.head.link
4. cur.link=None
5. print("deleted data",
cur.data)

Dr.L.Lakshmi 15
SINGLY LINKED LIST 13 of 14
DELETION at the end of list:
Pseudo code:
1. def delete_at_end(self):
2. cur=self.head
3. while cur.link!=self.tail:
4. cur=cur.link
5. cur.link=None
6. print("deleted data",
self.tail.data)
7. self.tail=cur

Dr.L.Lakshmi 16
SINGLY LINKED LIST 13 of 14
DELETION at a given position other than first or last Pseudo code:
1. def
delete_at_pos(self,pos
):
2. cur=self.head
3. i=1
4. while i<pos:
5. prev=cur
6. cur=cur.link
7. i+=1
8.
prev.link=cur.link
9. print("deleted
data",cur.data)

Dr.L.Lakshmi 17
SINGLY LINKED LIST 14 of 14
TRAVERSAL:

Pseudo code:
1. def display(self):
2. cur = self.head
3. if cur==None:
4. print("Linked list is empty")
5. else:
6. while cur is not None:
7. print(cur.data, end = '-> ')
8. cur= cur.link
Dr.L.Lakshmi 18
SINGLY LINKED LIST 14 of 14
Concatenation of two single linked lists:
struct node *concat( struct node *start1,struct node *start2)
{
struct node *ptr;
if(start1==NULL)
{
start1=start2;
return start1;
}
if(start2==NULL)
return start1;
ptr=start1;
while(ptr->link!=NULL)
ptr=ptr->link;
ptr->link=start2;
return start1;
}

Dr.L.Lakshmi 19
CIRCULARLY LINKED LIST
Insertion at the front of Circular linked list
Procedure for insertion a node at the beginning of list
Step1. Create the new node
Step2. Set the new node’s next to itself (circular!)
Step3. If the list is empty, return new node.
Step4. Set our new node’s next to the front.
Step5. Set tail’s next to our new node.
Step6. Return the end of the list.

Dr.L.Lakshmi 20
CIRCULARLY LINKED LIST
Insertion in the middle of the Circular linked list

Dr.L.Lakshmi 21
CIRCULARLY LINKED LIST
Insertion at the end of Circular linked list
Procedure for insertion a node at the end of list
Step1. Create the new node
Step2. Set the new node’s next to itself (circular!)
Step3. If the list is empty,return new node.
Step4. Set our new node’s next to the front.
Step5. Set tail’s next to our new node.
Step6. Return the end of the list.

Dr.L.Lakshmi 22
CIRCULARLY LINKED LIST
Deletion In Circular linked list
There are three situation for Deleting element in list.
1.Deletion at beginning of the Circular linked list.
2.Deletion at the middle of the Circular linked list.
3.Deletion at the end of the Circular linked list.

Dr.L.Lakshmi 23
CIRCULARLY LINKED LIST
1.Deletion at beginning of the Circular linked list.

After Deletion

Dr.L.Lakshmi 24
CIRCULARLY LINKED LIST
Deletion at beginning in Circular linked list
Pseudo code:

node=start->next;
ptr=start;
if(i==0)
{
printf("\n List is empty");
exit(0);
}
ptr->next=node->next;
free(node);

Dr.L.Lakshmi 25
CIRCULARLY LINKED LIST
Deletion at the middle of the Circular linked list

After Deletion

Dr.L.Lakshmi 26
CIRCULARLY LINKED LIST
Deletion at location in Circular linked list
Pseudo code:

if(node_no==delete_no) {
ptr->next=node->next;
free(node);
flag=1;
break;
}
else {
ptr=ptr->next;
node=node->next;
}
node_no++;
count--; }
if(flag==0) {
printf("\n Position not found");

Dr.L.Lakshmi 27
CIRCULARLY LINKED LIST
Deletion at the end of the Circular linked list

After Deletion

Dr.L.Lakshmi 28
CIRCULARLY LINKED LIST
Deletion at Last Node
Pseudo code:

node=start->next;
ptr=start; count=i;
if(i==0) {
printf("\n List is empty");
exit(0);
}
while(count) {
node_no++;
ptr=ptr->next;
node=node->next;
count--; }
node=start->next;
ptr=start;
while(node_no!=1) {
node_no--;
ptr=ptr->next;
node=node->next;
} if(node_no==1) {
ptr->next=node->next;
free(node);}
Dr.L.Lakshmi 29
C program example: 2 , also show error simulation
CIRCULARLY LINKED LIST
A circularly linked list, or simply circular list, is a linked list in which the last
node is always points to the first node. This type of list can be build just by
replacing the NULL pointer at the end of the list with a pointer which points to the
first node. There is no first or last node in the circular list.

Advantages:
Any node can be traversed starting from any other node in the list.
There is no need of NULL pointer to signal the end of the list and hence, all
pointers contain valid addresses.
In contrast to singly linked list, deletion operation in circular list is simplified as
the search for the previous node of an element to be deleted can be started from
that item itself.

Dr.L.Lakshmi 30
DOUBLY LINKED LIST
 In a singly linked list one can move from the header node to any node in one
direction only (left-right).

A doubly linked list is a two-way list because one can move in either direction
i.e., either from left to right or from right to left.

It maintains two links or pointers. Hence, it is called as doubly linked list.

PREV DATA NEXT

Structure
Where, DATA field stores the element orofdata,
the node
PREV contains the address of its
previous node, NEXT contains the address of its next node.

Dr.L.Lakshmi 31
DOUBLY LINKED LIST
struct node
{
int data;
struct node *next; // Pointer to next node in DLL
struct node *prev; // Pointer to previous node in DLL
};

Dr.L.Lakshmi 32
DOUBLY LINKED LIST
Advantages over singly linked list

1) A DLL can be traversed in both forward and backward direction.

2) The delete operation in DLL is more efficient if pointer to the node to be deleted is
given.

In singly linked list, to delete a node, pointer to the previous node is needed. To get this
previous node, sometimes the list is traversed. In DLL, we can get the previous node using
previous pointer.

Disadvantages over singly linked list

1) Every node of DLL Require extra space for an previous pointer. It is possible to
implement DLL with single pointer though.

2) All operations require an extra pointer previous to be maintained. For example, in


insertion, we need to modify previous pointers together with next pointers. For example in
following functions for insertions at different positions, we need 1 or 2 extra steps to set
previous pointer.
Dr.L.Lakshmi 33
DOUBLY LINKED LIST
In this type of liked list each node holds two-pointer field. Pointers exist between
adjacent nodes in both directions. The list can be traversed either forward or backward

Dr.L.Lakshmi 34
DOUBLY LINKED LIST
Creating a New Node
Pseudo code:

ptr=(node*)malloc(sizeof(node));
ptr->info=item;
if(*start==NULL) {
ptr->prev = ptr->next = NULL ;
*start = *end = ptr ;
}
else {
ptr->prev = *end;
(*end)->next = ptr ;
ptr->next= NULL;
(*end)=ptr;
}

Dr.L.Lakshmi 35
DOUBLY LINKED LIST
Insertion in doubly linked list
There are three situation for inserting element in list.
1.Insertion at the front of list.
2.Insertion in the middle of the list.
3.Insertion at the end of the list.

1.Insertion at the front of list

Dr.L.Lakshmi 36
DOUBLY LINKED LIST
Insert at front of the list Algorithm
Pseudo code: InsertAtFrontDll(info,prev,next,start,end
)
ptr=(node*)malloc(sizeof(node)); 1.create a new node and address in
ptr->info=item; assigned to ptr.
if(*start==NULL) 2.check[overflow] if(ptr=NULL)
{ write:overflow and exit
*start=ptr; 3.set Info[ptr]=item;
*end=ptr; 4.if(start=NULL)
} set prev[ptr] = next[ptr] = NULL
else set start = end = ptr
{ else
ptr->prev = NULL; set prev[ptr] = NULL
ptr->next=*start; next[ptr] = start
(*start)->prev=ptr; set prev[start] = ptr
*start=ptr; set start = ptr [end if]
} 5.Exit.

Dr.L.Lakshmi 37
DOUBLY LINKED LIST
2.Insertion in the middle of the list

Dr.L.Lakshmi 38
DOUBLY LINKED LIST
2.Insertion in the middle of the list
Pseudo code:
ptr=(node*)malloc(sizeof(node));
ptr->info=item;
loc = *start ;
if(*start==NULL) {
ptr->prev = ptr->next = NULL ;
*start = *end = ptr ; }
else if(i<=size) {
while(n != i) {
loc=loc->next;
n++;
}
ptr->next = loc->next ;
ptr->prev = loc ;
(loc->next)->prev = ptr ;
loc->next = ptr ;
}
else{
ptr->prev = *end;
(*end)->next = ptr ;
ptr->next= NULL;
(*end)=ptr; }}
Dr.L.Lakshmi 39
DOUBLY LINKED LIST
3.Insertion at the end of the list

Dr.L.Lakshmi 40
DOUBLY LINKED LIST

Insert at the end of Dllist


Pseudo code:
ptr=(node*)malloc(sizeof(node));
ptr->info=item;
if(*start==NULL)
{
ptr->prev = ptr->next = NULL ;
*start = *end = ptr ;
}
else
{
ptr->prev = *end;
(*end)->next = ptr ;
ptr->next= NULL;
(*end)=ptr;
}
C program example: 3 , also show error
simulation
Dr.L.Lakshmi 41

You might also like