3. INTRODUCTION
One way by which we can store a list of items is using arrays,
in which every item is stored in consecutive memory locations.
However, the disadvantage of arrays is operations like
insertion and deletion are expensive to perform.
Also changing the size of an array is not a straightforward task.
An alternate way of storing a list of items in the memory is by
considering each item as an individual entity and storing it.
Also every item will have a field called link, that contains the
address of the next item in the list.
4. INTRODUCTION
This means, we need not store successive items of the list in
successive memory locations.
Such an arrangement also makes insertion and deletion of items
very easy. This is accomplished using a data structure called Linked
List.
“A Linked List or One-Way List is a linear collection of data
elements, called nodes, where the linear order is given by means of
pointers.”
Hence, for a given list of items, if array is used to store the items,
then the sequence of items in the array must be same as the
sequence mentioned in the list.
However, with linked list, the sequence need not be the same as
mentioned.
5. INTRODUCTION
Each node in a linked list is divided into two parts:
info - contains the information about the element
link – contains the address of the next node in the list
A linked list of 6 nodes is diagrammatically presented below.
X
STAR
T info link
The info part of a node can either contain a single data item or a
group of data items.
The link part holds the address of the next node in the list.
The linked list also has a list pointer, called START, that points to the
first node of the list. START indicates the beginning of the list.
6. REPRESENTATION OF LINKED LISTS IN MEMORY
Since a simple linked list is made up of two parts(info and link), a
linked list is maintained using two linear arrays in the memory.
One array is called the INFO array and it contains the information of
all linked list nodes.
The second array is called the LINK array and it contains the
addresses of next nodes in the linked list.
The index positions in the array correspond to the list nodes.
For an index position k, INFO[k] and LINK[k] will contain the data in
node k and address of next node to k respectively.
The list also requires a variable, called START, that points to the first
node of the list.
7. REPRESENTATION OF LINKED LISTS IN MEMORY
Consider the following linked list.
N O E X I
STAR
T T X
The memory representation of the above list is,
1
2
3
4
5
6
7
8
9
10
11
INFO LINK
E
N
_
T
O
I
X
10
8
1
0
5
6
9
STAR
T 3
8. REPRESENTATION OF LINKED LISTS IN MEMORY
Array representation of two lists
88 74 93 82 X
AL
G
84 62 74 99 74 78 X
GEO
M
1
2
3
4
5
6
7
8
9
10
11
INFO LINK
93
88
82
74
7
10
0
2
ALG
6
99
93
62
74
88
82
74
78
74
84
4
7
8
9
10
0
1
0
2
3
GEO
M11
9. REPRESENTATION OF LINKED LISTS IN MEMORY
Array representation of multiple lists
Gran
t
Scott Vit
o
Kat
z
X
BOND
Hunte
r
McBrid
e
Evan
s
X
KELL
Y
HALL
X
Telle
r
Jone
s
Adam
s
NELSO
N Roger
s
Westo
n
X
10. REPRESENTATION OF LINKED LISTS IN MEMORY
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Student Link
1
2
3
4
Faculty Link
BOND
KELLY
HALL
NELSON
Vito
Katz
Grant
Scott
4
0
16
1
12
Vito
Hunter
Katz
Evans
Grant
McBride
Scott
4
14
0
0
16
6
1
12
3
12
3
0
Vito
Hunter
Katz
Evans
Rogers
Teller
Jones
Grant
McBride
Weston
Scott
Adams
4
14
0
0
15
10
17
16
6
0
1
8
12
3
0
9
11. REPRESENTATION OF LINKED LISTS IN MEMORY
Representation of lists with multiple data items in each node.
Brow
n
178521065 Femal
e
14700
STAR
T Cohe
n
177444557 Male 19000
Davis 192387282 Femal
e
22800 Evan
s
168568113 Male 34200 X
1
2
3
4
5
6
7
Evans
Cohen
Davis
Brown
168568113
177444557
192387282
178521065
Male
Male
Female
Female
34200
19000
22800
14700
0
4
1
2
Name SSN Sex Salary Link
STAR
T
6
12. REPRESENTATION OF LINKED LISTS IN MEMORY
Defining a linked list node
From the discussion so far, we can conclude that a node in a
list can have any type of data.
Along with the data, there is a link part that contains the
address/index of the next node.
The link part will only have integer values as it holds an
address or index, both of which are integers.
Hence, to define a linked list node, we need to have a facility
by which we can group data belonging to different data types.
This can be accomplished using Structures.
13. REPRESENTATION OF LINKED LISTS IN MEMORY
Structures are used to define a linked list node.
The nodes are then created using dynamic memory allocation
functions.
Defining a node using structures
Consider the following structure definition,
typedef struct node{
char info;
node* link;
};
The above structure definition results in a node that holds
character data and pointer to another node.
14. REPRESENTATION OF LINKED LISTS IN MEMORY
Node creation using malloc()
With the node definition ready, we can now create a linked list
node.
For this, we first declare a pointer variable as follows,
node* ptr;
We then use dynamic memory allocation functions to create a
node.
ptr = (node*)malloc(sizeof(node));
1000 1001 1002 1003 1004 1005 1006
Info link
1000 1001 1002 1003 1004 1005 1006
1002
ptr
15. REPRESENTATION OF LINKED LISTS IN MEMORY
Assigning values to a node
Once a node has been created, we next need to fill the node
with appropriate values.
The node that we have created can hold a character in the info
field and address in the link field.
Here the info and link fields are members of a structure.
Hence, to assign values to info and link fields, we need to
access these structure members.
We know that a structure member can be accessed using a
structure variable and the DOT operator.
16. REPRESENTATION OF LINKED LISTS IN MEMORY
However, the node that we have created is not represented by
a normal structure variable.
It is represented by a pointer.
node* ptr;
ptr = (node*)malloc(sizeof(node));
1000 1001 1002 1003 1004 1005 1006
Info link
1000 1001 1002 1003 1004 1005 1006
1002
ptr
The DOT operator cannot be used by a pointer variable to
access structure members.
Instead, a special operator, , is used by pointers for this
purpose.
17. REPRESENTATION OF LINKED LISTS IN MEMORY
The use of DOT and is illustrated below. Consider the
structure,
typedef struct sample{
int a;
};
sample var, *ptr;
var . a = 10;
ptr a = 20;
We now see how we assign values for the linked list node
using the pointer ptr.
ptr info = ‘A’;
ptr link = NULL;
18. REPRESENTATION OF LINKED LISTS IN MEMORY
This will result in,
ptr
A 0
1002
We now see how to create a linked list of two nodes.
node* create2(){
node *first, *second;
} first
1002
node* create2(){
node *first, *second;
first = (node *) malloc(sizeof(node));
}
node* create2(){
node *first, *second;
first = (node *) malloc(sizeof(node));
second = (node *)
malloc(sizeof(node));
}
secon
d
2002
node* create2(){
node *first, *second;
first = (node *) malloc(sizeof(node));
second = (node *)
malloc(sizeof(node));
first info = ‘A’;
second info = ‘B’;
}
A
B
node* create2(){
node *first, *second;
first = (node *) malloc(sizeof(node));
second = (node *)
malloc(sizeof(node));
first info = ‘A’;
second info = ‘B’;
first link = second;
}
A 2002
node* create2(){
node *first, *second;
first = (node *) malloc(sizeof(node));
second = (node *)
malloc(sizeof(node));
first info = ‘A’;
second info = ‘B’;
first link = second;
second link = NULL;
return first;
B 0
19. LINKED LIST OPERATIONS
Traversing
Traversing a list involves visiting all the linked list nodes.
A 78 B 32 C 19 D 66 E 21 F 0
STAR
T
05
PTR
Algorithm: (Traversing a Linked List) This algorithm visits all nodes of a
linked list, applying an operation PROCESS on each node of the list. The
variable PTR points to the node currently being processed.
1. Set PTR := START.
2. Repeat steps 3 and 4 while PTR ≠ NULL:
3. Apply PROCESS to PTR info.
4. Set PTR := PTR link.
[End of Step 2 loop.]
5. Exit
PTR PTR
05 78 32 19 66 21
PTR PTR PTR
20. LINKED LIST OPERATIONS
Algorithm: PRINT(START) This algorithm prints the information at each node
of the list.
1. Set PTR := START.
2. Repeat steps 3 and 4 while PTR ≠ NULL:
3. Write : PTR info.
4. Set PTR := PTR link.
[End of Step 2 loop.]
5. Exit
Algorithm: COUNT(START, NUM) This algorithm finds the number of
elements in the list.
1. Set NUM := 0.
2. Set PTR := START.
3. Repeat steps 4 and 5 while PTR ≠ NULL:
4. Set NUM := NUM + 1.
5. Set PTR := PTR link.
[End of Step 3 loop.]
5. Exit
21. LINKED LIST OPERATIONS
Searching
Here we will see how a search for an item can be performed in an
ordered and unordered list.
Algorithm: SEARCH(START, ITEM, LOC) This algorithm finds the location
LOC of the node where ITEM first appears in an unordered linked list, or
sets LOC = NULL.
1. Set PTR := START.
2.Repeat steps 3 and 4 while PTR ≠ NULL:
3. If ITEM = PTR info, then:
Set LOC := PTR and Exit.
Else
Set PTR := PTR link.
[End of If structure.]
[End of Step 2 loop.]
4. Set LOC := NULL.
5. Exit
22. LINKED LIST OPERATIONS
Algorithm: SEARCH(START, ITEM, LOC) This algorithm finds the location
LOC of the node where ITEM first appears in an ordered linked list, or sets
LOC = NULL. The list elements are arranged in descending order.
1. Set PTR := START.
2.Repeat step 3 while PTR ≠ NULL:
3. If ITEM < PTR info, then:
Set PTR := PTR link.
Else if ITEM = PTR info, then:
Set LOC := PTR and Exit.
Else
Set LOC := NULL, and Exit.
[End of If structure.]
[End of Step 2 loop.]
4. Set LOC := NULL.
5. Exit
23. LINKED LIST OPERATIONS
Insertion
Different ways of inserting a node in a linked list are:
Insert at the beginning of the list
Insert after a specified node
Insert at the end of the list
Insert at the beginning
This operation is illustrated as follows
A 2000 B 0
1000
STAR
T
1000 2000
STEP 1 – Create a new node
A 2000 B 0
1000
STAR
T
1000 2000
C 0
3000
NEW
3000
STEP 1 – Create a new node
STEP 2 – Make the new
node point to the current
first node.
A 2000 B 0
1000
STAR
T
C 1000
3000
NEW
3000
1000 2000 STEP 1 – Create a new node
STEP 2 – Make the new
node point to the current
first node.
STEP 3 - Make START point
to the new node
A 2000 B 0
3000
STAR
T
C 1000
3000
NEW
3000
1000 2000
C 1000 A 2000 B 0
3000
START
3000 1000 2000
24. LINKED LIST OPERATIONS
Function to insert a node at the beginning of a list
3000
STAR
T
A 2000 B 0
1000 2000
3000
NEW
C 1000
3000
node* insertBeg(node* START, char data){
}
node* insertBeg(node* START, char data){
node* NEW;
NEW = (node*)malloc(sizeof(node));
NEW info = data;
}
node* insertBeg(node* START, char data){
node* NEW;
NEW = (node*)malloc(sizeof(node));
NEW info = data;
NEW link = START;
}
node* insertBeg(node* START, char data){
node* NEW;
NEW = (node*)malloc(sizeof(node));
NEW info = data;
NEW link = START;
START = NEW;
return START;
}
25. LINKED LIST OPERATIONS
Insertion at the
end
A 2000 B 0
1000
1000 2000
STAR
T
STEP 1 – Create a new node
C 0
3000
3000
NEW
STEP 1 – Create a new node
STEP 2 – Reach the end of the list
A 2000 B 0
1000
1000 2000
STAR
T
C 0
3000
3000
NEW
TEMP
A 2000 B 0
1000
1000 2000
STAR
T
C 0
3000
3000
NEW
TEMP
A 2000 B 0
1000
1000 2000
STAR
T
C 0
3000
3000
NEW
TEMP
STEP 1 – Create a new node
STEP 2 – Reach the end of the list
STEP 3 – Make TEMP point to the NEW
node
A 2000 B 3000
1000
1000 2000
STAR
T
C 0
3000
3000
NEW
TEMP
A 2000 B 3000
1000
1000 2000
STAR
T
C 0
3000
26. LINKED LIST OPERATIONS
Function to insert a node at the end of
a list
A 2000 B 3000
1000
1000 2000
STAR
T
C 0
3000
3000
NEW
TEMP
node* insertEnd(node* START, char
data){
}
node* insertEnd(node* START, char
data){
node* NEW;
NEW =
(node*)malloc(sizeof(node));
NEW info = data;
NEW link = NULL;
}
node* insertEnd(node* START, char
data){
node* NEW, *TEMP;
NEW =
(node*)malloc(sizeof(node));
NEW info = data;
NEW link = NULL;
TEMP = START;
while(TEMP link != NULL)
TEMP link = NEW;
return START;
}
27. LINKED LIST OPERATIONS
Insertion after a specified
node
A 2000 B 3000
1000
1000 2000
STAR
T
C 0
3000
4000
NEW
TEMP
STEP 1 – Create a new node
D 0
4000
STEP 1 – Create a new node
STEP 2 – Reach the specified node
A 2000 B 3000
1000
1000 2000
STAR
T
C 0
3000
4000
NEW
TEMP
D 0
4000
A 2000 B 3000
1000
1000 2000
STAR
T
C 0
3000
4000
NEW
TEMP
D 0
4000
STEP 1 – Create a new node
STEP 2 – Reach the specified node
STEP 3 – Make the link field of NEW node
point to the last node.
A 2000 B 3000
1000
1000 2000
STAR
T
C 0
3000
4000
NEW
TEMP
D 3000
4000
STEP 1 – Create a new node
STEP 2 – Reach the specified node
STEP 3 – Make the link field of NEW node
point to the last node.
STEP 4 – Make the link field of TEMP node
point to the NEW node.
A 2000 B 4000
1000
1000 2000
STAR
T
C 0
3000
4000
NEW
TEMP
D 3000
4000
A 2000 B 4000
1000
1000 2000
STAR
T
C 0
3000
D 3000
4000
28. LINKED LIST OPERATIONS
Function to insert a node after a specified
node
A 2000 B 4000
1000
1000 2000
STAR
T
C 0
3000
D 3000
4000
TEMP
NEW
4000
NEW link = TEMP link;
node* insertAfter(node* START, char data, char
key){
}
node* insertAfter(node* START, char data, char
key){
node* NEW, *TEMP;
NEW = (node*)malloc(sizeof(node));
NEW info = data;
NEW link = NULL;
}
node* insertAfter(node* START, char data, char
key){
node* NEW, *TEMP;
NEW = (node*)malloc(sizeof(node));
NEW info = data;
NEW link = NULL;
TEMP = START;
while(TEMP != NULL){
if(TEMP info = = key)
break;
TEMP= TEMP link;
NEW link = TEMP link;
TEMP link = NEW;
return START;
29. LINKED LIST OPERATIONS
Deletion
Different ways of deleting a node from a linked list are:
Deleting the first node of the list
Deleting a specified node
Deleting the last node of the list
Deleting the first node of the list
A 2000 B 3000 C 0
1000
STA
RT
STEP 1 – Set a TEMP
pointer to the first node.
TEM
P
STEP 1 – Set a TEMP
pointer to the first node.
STEP 2 – Make START point
to the second node.
A 2000 B 3000 C 0
2000
STA
RT
TEM
P
STEP 1 – Set a TEMP
pointer to the first node.
STEP 2 – Make START point
to the second node.
STEP 3 – Delete the node
pointed by TEMP
B 3000 C 0
2000
STA
RT
30. LINKED LIST OPERATIONS
Function to delete the first node of
the list
B 3000 C 0
1000
STAR
T
A 2000
TEMP
node* deletFront(node* start){
}
node* deletFront(node* start){
node* temp;
temp = start;
}
node* deletFront(node* start){
node* temp;
temp = start;
start = start link;
}
2000
node* deletFront(node* start){
node* temp;
temp = start;
start = start link;
free(temp);
return start;
}
31. LINKED LIST OPERATIONS
Deleting a specified
node
STAR
T
1000
1000
A 2000
2000
B 3000
3000
C 0
STEP 1 – Create two pointers TEMP and PREV.
STEP 2 – Set TEMP to START and PREV to NULL..
TEMP
STEP 1 – Create two pointers TEMP and PREV.
STEP 2 – Set TEMP to START and PREV to NULL..
STEP 3 – Search for the specified node using TEMP. Also
PREV must be one step behind TEMP.
TEMP
PREV
STEP 1 – Create two pointers TEMP and PREV.
STEP 2 – Set TEMP to START and PREV to NULL..
STEP 3 – Search for the specified node using TEMP. Also
PREV must be one step behind TEMP.
STEP 4 – Make PREV node point to the node next to
TEMP.
A 3000
STEP 1 – Create two pointers TEMP and PREV.
STEP 2 – Set TEMP to START and PREV to NULL..
STEP 3 – Search for the specified node using TEMP. Also
PREV must be one step behind TEMP.
STEP 4 – Make PREV node point to the node next to
TEMP.
STEP 5 – Delete the node pointed by TEMP.
STAR
T
1000
1000 3000
C 0
A 3000
32. LINKED LIST OPERATIONS
Function to delete a specified node from
the list
STAR
T
1000
1000 2000
B 3000
3000
C 0
node* deleteSpecific(node* start, char
key){
}
node* deleteSpecific(node *start, char
key){
node *temp, *prev;
temp = start; prev = NULL;
}
TEMP
node* deleteSpecific(node *start, char
key){
node *temp, *prev;
temp = start; prev = NULL;
while(temp != NULL){
if(temp info == key)
break;
prev = temp;
temp = temp link;
}
PREV TEMP
A 2000
prev link = temp link;
A 3000
prev link = temp link;
free(temp);
prev link = temp link;
free(temp);
return start;
}
STAR
T
1000
1000 3000
C 0
A 3000
33. LINKED LIST OPERATIONS
Deleting the last node of the
list
B 3000 C 0
STAR
T
A 2000
1000
1000 2000 3000
STEP 1 – Create two pointers TEMP and
PREV
STEP 2 – Set TEMP to START and PREV to
NULL
TEMP
STEP 1 – Create two pointers TEMP and
PREV
STEP 2 – Set TEMP to START and PREV to
NULL
STEP 3 – Reach the end of the list using
TEMP. Also PREV must be one
step before TEMP.
B 3000 C 0
STAR
T
A 2000
1000
1000 2000 3000
TEMP
PREV
STEP 1 – Create two pointers TEMP and
PREV
STEP 2 – Set TEMP to START and PREV to
NULL
STEP 3 – Reach the end of the list using
TEMP. Also PREV must be one
step before TEMP.
STEP 4 – Delete the node pointed by TEMP.
STEP 1 – Create two pointers TEMP and
PREV
STEP 2 – Set TEMP to START and PREV to
NULL
STEP 3 – Reach the end of the list using
TEMP. Also PREV must be one
step before TEMP.
STEP 4 – Delete the node pointed by TEMP.
STEP 5 – Set the link field of the node pointed
by PREV to NULL.
B 0
STAR
T
A 2000
1000
1000 2000
B 0
34. LINKED LIST OPERATIONS
Function to delete the last node from
the list
C 0
STAR
T
1000
1000 2000 3000
node* deleteEnd(node*
start){
}
node* deleteEnd(node* start){
node *temp, *prev;
temp = start; prev =
NULL;
}
TEMP
node* deleteEnd(node* start){
node *temp, *prev;
temp = start; prev =
NULL;
while(temp link !=
NULL){
prev = temp;
temp = temp
link;
}
}
PREV
A 2000
TEMP
B 3000
STAR
T
1000
1000 2000 3000
PREV
A 2000
TEMP
B 3000 C 0
node* deleteEnd(node* start){
node *temp, *prev;
temp = start; prev =
NULL;
while(temp link !=
NULL){
prev = temp;
temp = temp
link;
}
free(temp);
}
node* deleteEnd(node* start){
node *temp, *prev;
temp = start; prev =
NULL;
while(temp link !=
NULL){
prev = temp;
temp = temp
link;
}
free(temp);
prev link = NULL;
B 0
STAR
T
A 2000
1000
1000 2000
B 0
35. LINKED LIST OPERATIONS
Displaying contents of a list
B 3000 C 0
STAR
T
A 2000
TEMP
1000
1000 2000 3000
void display(node
*start){
}
void display(node
*start){
node *temp;
temp = start;
}
void display(node *start){
node *temp;
temp = start;
while(temp != NULL){
printf(“%c”,temp
info);
temp = temp link;
}
}
B 3000 C 0
STAR
T
A 2000
TEMP
1000
1000 2000 3000
B 3000 C 0
STAR
T
A 2000
TEMP
1000
1000 2000 3000