SlideShare a Scribd company logo
1 of 35
MODULE 3
LINKED LISTS
CONTENTS
 Definition
 Representation of linked lists in Memory
 Linked list operations
 Traversing
 Searching
 Insertion
 Deletion
 Doubly Linked lists
 Circular linked lists
 Header linked lists
 Linked Stacks and Queues
 Applications of Linked lists
 Polynomials
 Sparse matrix representation
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.
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.
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.
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.
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
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
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
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
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
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.
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.
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
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.
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.
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;
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
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
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
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
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
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
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;
}
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
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;
}
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
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;
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
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;
}
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
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
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
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
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

More Related Content

Similar to Module 3 Dara structure notes

Data structure and algorithms chapter three LINKED LIST
Data structure and algorithms chapter three LINKED LISTData structure and algorithms chapter three LINKED LIST
Data structure and algorithms chapter three LINKED LIST
binakasehun2026
 

Similar to Module 3 Dara structure notes (20)

data structures lists operation of lists
data structures lists operation of listsdata structures lists operation of lists
data structures lists operation of lists
 
unit 2- PPT.pdf
unit 2- PPT.pdfunit 2- PPT.pdf
unit 2- PPT.pdf
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)
 
Linked List.pptx
Linked List.pptxLinked List.pptx
Linked List.pptx
 
Deletion from single way linked list and search
Deletion from single way linked list and searchDeletion from single way linked list and search
Deletion from single way linked list and search
 
Linked lists 1
Linked lists 1Linked lists 1
Linked lists 1
 
Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm
 
Unit II Data Structure 2hr topic - List - Operations.pptx
Unit II  Data Structure 2hr topic - List - Operations.pptxUnit II  Data Structure 2hr topic - List - Operations.pptx
Unit II Data Structure 2hr topic - List - Operations.pptx
 
Operations on linked list
Operations on linked listOperations on linked list
Operations on linked list
 
Data structure and algorithms chapter three LINKED LIST
Data structure and algorithms chapter three LINKED LISTData structure and algorithms chapter three LINKED LIST
Data structure and algorithms chapter three LINKED LIST
 
csc211_lecture_21.pptx
csc211_lecture_21.pptxcsc211_lecture_21.pptx
csc211_lecture_21.pptx
 
Team 10
Team 10Team 10
Team 10
 
Data Structure and Algorithm Lesson 2.pptx
Data Structure and Algorithm Lesson 2.pptxData Structure and Algorithm Lesson 2.pptx
Data Structure and Algorithm Lesson 2.pptx
 
Linked lists a
Linked lists aLinked lists a
Linked lists a
 
ds 4Linked lists.ppt
ds 4Linked lists.pptds 4Linked lists.ppt
ds 4Linked lists.ppt
 
4.linked list(contd.)
4.linked list(contd.)4.linked list(contd.)
4.linked list(contd.)
 
Chapter 3 Linkedlist Data Structure .pdf
Chapter 3 Linkedlist Data Structure .pdfChapter 3 Linkedlist Data Structure .pdf
Chapter 3 Linkedlist Data Structure .pdf
 
Link list
Link listLink list
Link list
 
lect 2-DS ALGO(online).pdf
lect 2-DS  ALGO(online).pdflect 2-DS  ALGO(online).pdf
lect 2-DS ALGO(online).pdf
 
Unit7 C
Unit7 CUnit7 C
Unit7 C
 

Recently uploaded

Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
amitlee9823
 
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
amitlee9823
 
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
amitlee9823
 
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts ServiceCall Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...
Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...
Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...
amitlee9823
 
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
amitlee9823
 
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get CytotecAbortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Riyadh +966572737505 get cytotec
 
FESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfFESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdf
MarinCaroMartnezBerg
 
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAl Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
AroojKhan71
 
Probability Grade 10 Third Quarter Lessons
Probability Grade 10 Third Quarter LessonsProbability Grade 10 Third Quarter Lessons
Probability Grade 10 Third Quarter Lessons
JoseMangaJr1
 

Recently uploaded (20)

Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
 
VidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptxVidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptx
 
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
 
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
 
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts ServiceCall Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
 
BigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxBigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptx
 
April 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's AnalysisApril 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's Analysis
 
Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...
Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...
Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...
 
ELKO dropshipping via API with DroFx.pptx
ELKO dropshipping via API with DroFx.pptxELKO dropshipping via API with DroFx.pptx
ELKO dropshipping via API with DroFx.pptx
 
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
 
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort Service
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort ServiceBDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort Service
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort Service
 
Invezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signals
 
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get CytotecAbortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
 
FESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfFESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdf
 
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAl Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
 
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
 
Accredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdfAccredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdf
 
Generative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and MilvusGenerative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and Milvus
 
Probability Grade 10 Third Quarter Lessons
Probability Grade 10 Third Quarter LessonsProbability Grade 10 Third Quarter Lessons
Probability Grade 10 Third Quarter Lessons
 

Module 3 Dara structure notes

  • 2. CONTENTS  Definition  Representation of linked lists in Memory  Linked list operations  Traversing  Searching  Insertion  Deletion  Doubly Linked lists  Circular linked lists  Header linked lists  Linked Stacks and Queues  Applications of Linked lists  Polynomials  Sparse matrix representation
  • 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