Chapter 4 Linkedlist
Chapter 4 Linkedlist
1
OBJECTIVES
• Understand a linked list concept
• Explain the design, use and operation of a
linked list
• Apply basic linked list operations
• Design and implement linked list structures
• Write application programs using the linked
list concept
2
Introduction
char student_name[3];
index student_name
0 Ahmad
1 Jacky
2 Muthu
• What happen if we add one more student named Aminah, can we?
• What happen if we delete Jacky?
• Do you think that the computer memory is fully optimized? Why?
3
Linked List
• When we want to work with an unknown number of data values, we
use a linked list data structure to organize that data.
• A linked list consists of nodes where each node contains an element
and a link/tail to the next node in the list as shown below.
elements
9
Linked Lists
Arrays Array of
Linked Lists
Records
Data
connected?
No Yes Yes
Non-
Memory Contiguous Contiguous
Contiguous
Access Random Random Sequential
Size Fixed Fixed Dynamic
Linked List
Linked list provides the ability to execute the operations
below dynamically.
struct Node {
char key;
struct Node *next;
};
19
Example of A Simple Linked List with 3 nodes cont.
int main()
{
// Create 3 instances of struct Node
struct Node* head = NULL;
struct Node* second = NULL;
struct Node* third = NULL;
return 0;
}
next next next
key key key
head second third 20
Example of A Simple Linked List with 3 nodes cont.
• Currently, 3 nodes have been initialized with each of them is set to
NULL.
• Now we assign value to each node.
int main()
{
// Create 3 instances of struct Node
// Allocate 3 nodes in dynamic memory location using malloc
head->key = ‘a’;
head->next = second;
// Assign value to the second node
second->key = ‘b’;
// Link second node to the third node
second->next = third;
return 0;
}
next next next
a b key
head second third 22
Example of A Simple Linked List with 3 nodes cont.
• Lastly, assign value to the third node. Note that the next pointer in
the third node is set to NULL to indicate the list is terminated.
int main()
{
// Create 3 instances of struct Node
// Allocate 3 nodes in dynamic memory location using malloc
head->key = ‘a’;
head->next = second;
second->key = ‘b’;
second->next = third;
third->key = ‘c’;
third->next = NULL;
return 0;
} next next next
a b c null
head second third 23
Linked List
• Types of Linked Lists:
Singly Linked List
Doubly Linked List
Circularly Linked List
33
Singly Linked List
• Singly linked list is a basic linked list type linked together in a
sequential way.
• To perform any operation on a linked list we must keep track/reference
of the first node which may be referred by head pointer variable.
• In singly linked list address field of last node must contain a NULL
value specifying end of the list.
• A variable pointer (curr) that points to the element being accessed
Head
A B C
NULL 34
Inserting At Beginning of the list
struct Node *head = NULL; // a
a next
void insertAtBeginning(int value) null null
{
struct Node *newNode;
head
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value; // assume value = 1
if(head == NULL) b next
{ 1 null
newNode->next = NULL; // b
head = newNode; // c newNode
}
else
{ c next
newNode->next = head;
head = newNode;
1 null
} head
printf("\nOne node inserted!!!\n");
}
35
Inserting At Beginning of the list
cont.
struct Node *head = NULL;
d next
void insertAtBeginning(int value) 1 null
{
struct Node *newNode;
head
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value; // assume value = 2
if(head == NULL) e next next
{
newNode->next = NULL; 2 1 null
head = newNode; newNode head
}
else // d
{ f next next
newNode->next = head; // e
2 1 null
head = newNode; // f
} head newNode
printf("\nOne node inserted!!!\n");
}
36
Inserting At The End of the list
struct Node *head = NULL;
a next
void insertAtEnd(int value) null null
{
struct Node *newNode;
head
newNode = (struct Node*)malloc(sizeof(struct Node));b next
newNode->data = value; // assume value = 1
1 null
newNode->next = NULL;
if(head == NULL) // a head
head = newNode; // b
else
{ c next
struct Node *temp = head; // c
1 null
while(temp->next != NULL)
{ temp
temp = temp->next; next
}
temp->next = newNode;
1 null
} temp
printf("\nOne node inserted!!!\n");
} 37
Inserting At The End of the list
cont.
struct Node *head = NULL;
void insertAtEnd(int value)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value; // assume value = 3
newNode->next = NULL;
if(head == NULL) a
{ head = newNode; } next next
else 2 1 null
{
struct Node *temp = head; // a head newNode
while(temp->next != NULL)
{ b
next next
temp = temp->next; // b
} 2 1
temp->next = newNode; // c temp temp
}
printf("\nOne node inserted!!!\n");
} 38
Inserting At The End of the list
cont.
struct Node *head = NULL;
void insertAtEnd(int value)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value; // assume value = 3
newNode->next = NULL;
if(head == NULL) a
{ head = newNode; } next next next
else 2 1 3
{
struct Node *temp = head; // a head newNode temp
while(temp->next != NULL)
b
{
next next null
temp = temp->next; // b
} 2 1
temp->next = newNode; // c temp temp
}
printf("\nOne node inserted!!!\n");
} 39
Inserting in the middle of the list
struct Node *head = NULL;
void insertBetween(int value, int loc1, int loc2)
{ // assume loc1 = 1, loc2 = 4
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value; // assume value = 3 next
next next
if(head == NULL)
{
2 1 4
newNode->next = NULL; head newNode newNode
head = newNode; a next
} else
{ 2 b next
struct Node *temp = head; // a 1 null
temp
while(temp->data != loc1 && temp->data != loc2)
{ temp = temp->next;} // b
temp
newNode->next = temp->next;
temp->next = newNode;
}
printf("\nOne node inserted!!!\n"); }
40
Inserting in the middle of the list
cont. next
b
43
Deleting from Beginning of the
list cont.
struct Node *head = NULL;
void removeBeginning() c next next
{
2 1 null
if(head == NULL)
printf("\n\nList is Empty!!!"); head newNode
else
{ struct Node *temp = head; // a
a next
if(head->next == NULL) 2
{
head = NULL; temp
free(temp);
} else { // c
head = temp->next;
free(temp);
printf("\nOne node deleted!!!\n\n");
}
}
}
44
Deleting from Beginning of the
list cont.
struct Node *head = NULL;
void removeBeginning()
{
if(head == NULL)
printf("\n\nList is Empty!!!");
d
else next
{ struct Node *temp = head;
1 null
if(head->next == NULL)
{ head
head = NULL;
free(temp);
} else {
head = temp->next; // d
free(temp);
printf("\nOne node deleted!!!\n\n");
}
}
}
45
Add Node at Beginning
HEAD
A B C
NULL
52
Add Node in Middle
newNode->next = curr;
HEAD
prev->next = newNode;
A C
prev
curr
B
NULL
newNode = new Node(value);
53
Add Node at End
HEAD
A B C
NULL
54
Delete First Node
HEAD
A B C
NULL
55
Delete Node in Middle
HEAD
A B C
prev
curr
NULL
56
Delete Node at End
HEAD
A B C
NULL
57
Doubly Linked List
• The unique feature is each node has
two positions reserved for pointers
• The first position is used to point the
next node
• The second is used to point the
previous node
• This allows the list to be traversed in
two directions
• There is no need for a fixed head
pointer 58
Doubly Linked List
• One of the powerful implementations
• Each node has a pointer to both its
successor and its predecessor
HEAD
A B C
90 80 70 Mark
A B C Grade
*next
*prev
members
60
Create Double Linked List
//Create List
struct LLNode {
char key;
struct LLNode *next;
struct LLNode *prev;
}
61
Add Node
newNode.next = curr;
HEAD
prev.next = newNode;
A C
prev
newNode.prev
curr = prevNode; Curr.prev = newNode;
NULL
B
62
newNode = new Node(value);
Delete Node
succNode = curr.next;
HEAD
A B C
prev
curr
succ
NULL
prevNode.next = succNode;
63
Circularly Linked Lists
• The last node’s link points to the first
node of the list
• Allow access to nodes in the middle of
the list without starting at the beginning
• Insertion into and deletion from a
circularly follow the same logic patterns
used in a singly, except the last node
points to the first node
64
Circularly Linked Lists
• It only requires a single variable pointer,
which can move to different nodes
• The last element points back to the first
65
Circularly Linked List
90 80 70 Mark
A B C Grade
*next
members
66
Circularly Linked Lists
HEAD TAIL
A B C
67
Answer
Problem
2 Students list
Amia
1 3
Juana Siti
4
Roslan
Using Linked List
New Student List
2
Amia 1
1 2
Siti
3
Juana
3 4
4
Roslan