Linked List: Free Powerpoint Templates Free Powerpoint Templates
Linked List: Free Powerpoint Templates Free Powerpoint Templates
LIST
Free Powerpoint Templates
Page 1
Array Vs Linked List
• Array
– Arrays have fixed size
• Linked list
– Linked list is able to grow in size as needed
a b c d
Free Powerpoint Templates
Page 3
More terminology
• Null pointer
• External pointer
• Empty List
Free Powerpoint Templates
4 Page 4
Operations on Linked Lists
• Creation
• Traversal
Free Powerpoint Templates
Page 5
• Delete an item from the list
value
• Concatenation
Free Powerpoint Templates
Page 6
Types of Linked List
• Basically, we can put LL into following four types:
– Singly-Linked List
– Doubly-Linked List
Free Powerpoint Templates
Page 7
Singly-linked lists
• Here is a singly-linked list (SLL):
start
a b c d
Free Powerpoint Templates
Page 8
Creation
Free Powerpoint Templates
Page 9
Traversal
ptr
head
Free Powerpoint Templates
10 Page 10
Insertion at the beginning
new Zero
head
Free Powerpoint Templates
Page 11
Insertion at the end
new Three
head
one two
Free Powerpoint Templates
Page 12
Insertion at a specified
position
new three
head
keynode
Free Powerpoint Templates
Page 13
Deletion from Beginning
Free Powerpoint Templates
Page 14
Steps:
1. Ptr = head
Free Powerpoint Templates
Page 17
Steps:
1. Ptr = head
2. If (ptr = NULL) then
1. Print “The list is empty”
2. Exit
3. Else
1. While (ptr. link! = NULL) do
1. Ptr1 = ptr
2. Ptr = ptr. Link
2. End While
3. Ptr1. link = NULL
4. Free (ptr)
4. End if
5. Stop
Free Powerpoint Templates
Page 18
Function
void delete_from_end()
{
if(head==NULL)
{
printf("LINKED LIST IS EMPTY !!!");
return;
}
else
{
if(head->link==NULL)
{
temp = head;
printf ("THE DELETED ELEMENT IS %d", temp->data);
head=NULL;
free(temp);
}
Free Powerpoint Templates
Page 19
else
{
curr=head;
temp=head->link;
while(temp->link!=NULL)
{
curr=temp;
temp=temp->link;
}
printf("THE DELETED ELEMENT IS %d", temp->data);
curr->link=NULL;
free(temp);
}
}
}
Free Powerpoint Templates
Page 20
Deletion of any particular node
Free Powerpoint Templates
Page 21
Steps:
1. Ptr1 = head
2. If (ptr1 = NULL) then
1. Print “The list is empty”
2. Exit
3. Else
1. Ptr = ptr1
2. While (ptr ! = NULL) do
1. If (ptr. data! = KEY) then
1. Ptr1 = ptr
2. Ptr = ptr. Link
2. Else
1. Ptr1. link=ptr. Link
2. Free (ptr)
3. Exit
3. End If
3. End While
Free Powerpoint Templates
Page 22
4. If (ptr = NULL) then
5. End If
4. End if
5. Stop
Free Powerpoint Templates
Page 23
Function
void delete_a_value()
{
int val;
if(head==NULL)
{
printf("THE LINKED LIST IS EMPTY!!");
return;
}
else
{
printf("Enter the value to be deleted:");
scanf("%d",&val);
temp=head;
curr=temp;
Free Powerpoint Templates
Page 24
while(temp!=NULL)
{
if(temp->data == val)
{
curr->link=temp->link;
free (temp);
return;
}
curr=temp;
temp=temp->link;
}
}
}
Free Powerpoint Templates
Page 25