0% found this document useful (0 votes)
31 views50 pages

Chapter 4 Linkedlist

Uploaded by

Nurul Liyana
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)
31 views50 pages

Chapter 4 Linkedlist

Uploaded by

Nurul Liyana
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/ 50

LINKED LIST

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

• Previously, we have learned how to store data in an array.


• However, storing data in arrays has its own limitation.
• Consider the following array.

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

node node node


• The elements in the node are NOT stored at contiguous memory
locations.
• The elements in a linked list are linked using pointers.
• In the example above, we are using linked list to store integers (int).
• However, other objects/values could also be stored such as string,
characters, floating point numbers, etc.
Linked List
• These nodes could even point to other lists - but
that is something else.
• The element (or data compartment) is on the left,
with the pointer to the next node on the right.
• The last node has a null at the end.
• A 'stopper' is needed at the end, this is to stop
head pointer running off the end of the list when
running through the list.
Linked List
• In general, if you have a dynamic
collection, where elements are
frequently being added and deleted,
and the location of new elements added
to the list is significant, then benefits of
a linked list increase.
• Dynamic memory allocation

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.

• Insertion - Adding new record element to existing list


• Deletion - Deleting or removing an existing record
element from the list
• Retrieval - Search or find the location of records or
elements based on given key value or key field. Certain
conditions may be apply in searchers.
• Traversal - Accessing and processing each record items
or elements exactly once
Insertion
Deletion
Retrieval
Linked Lists
• A linked list is composed of three sections – a
head, nodes and tail.
• The first node is called the head. If the linked
list is empty, then the value of the head is
NULL.
• The head provides the handle by which one can
access all the members of the list, adding and
removing nodes
• The nodes contain the data/element
• The tail, marking the end of the linked list
Linked List

tail tail tail tail


Linked List
element elements
Example of A Simple Linked List with 3 nodes
next
key

//Declaration of a linked list node with data type char

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;

// Allocate 3 nodes in dynamic memory location using malloc


head = (struct Node*)malloc(sizeof(struct Node));
second = (struct Node*)malloc(sizeof(struct Node));
third = (struct Node*)malloc(sizeof(struct Node));

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

// Assign value to the first node (head)


head->key = ‘a’;
// Link first node to the second node
head->next = second;
return 0;
}
next next
a key
head second 21
Example of A Simple Linked List with 3 nodes cont.
• Now, assign value to the second 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

struct Node *head = NULL; 1


void insertBetween(int value, int loc1, int loc2)
{ // assume loc1 = 1, loc2 = 4 temp
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;
} else
{
c next
struct Node *temp = head; 3 null
while(temp->data != loc1 && temp->data != loc2)
{ temp = temp->next;}
newNode
newNode->next = temp->next; // c
temp->next = newNode;
}
printf("\nOne node inserted!!!\n"); }
41
Inserting in the middle of the list
cont.
struct Node *head = NULL;
d
void insertBetween(int value, int loc1, int loc2) next
{ // assume loc1 = 1, loc2 = 4
1
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));temp
newNode->data = value; // assume value = 3
if(head == NULL)
next next
{
newNode->next = NULL; 2 1 3
head = newNode;
head newNode newNode
} else
{ next
struct Node *temp = head;
4
while(temp->data != loc1 && temp->data != loc2)
{ temp = temp->next;} newNode
newNode->next = temp->next;
temp->next = newNode; // d null
}
printf("\nOne node inserted!!!\n"); }
42
Deleting from Beginning of the
list a
struct Node *head = NULL;
next
void removeBeginning() 1 null
{
if(head == NULL)
temp
printf("\n\nList is Empty!!!");
next
else
1 null
{ struct Node *temp = head; // a
if(head->next == NULL) head
{
head = NULL; // b
free(temp); // frees the memory space
} else { b next
head = temp->next;
null null
free(temp);
printf("\nOne node deleted!!!\n\n"); head
}
}
}

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

prev curr succ NULL 59


Doubly Linked List

Abdullah Badrul Hazimi Name


curr
070001 070002 070003 StudID

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

Abdullah Badrul Hazimi Name


curr
070001 070002 070003 StudID

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

You might also like