Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Note 4: Linked List Concept in Data Structure for Application
Linked list
Linked list is a data structure that allows sequential access to the elements. A list lays out
the sequence in a row, starting at the first element (called front) and proceeding in
successive order to the last element (called back). Each element in a list contains Links
that identify both the next and the preceding item in the sequence. We focused on the
fact that the Linked list provides efficient operations tot insert and delete an element at
any position in the sequence. This was very important, because we needed to understand
why a Linked list is sometimes used in an application rather than an array, which is
another basic sequence data structure. We concluded that programmers select a Linked
list as the data structure of choice when an application wants to maintain elements by
position and allow for frequent insertions node and deletions node. By the way, each
elements of Linked list called node.
Linked List Structure Types
Non-Circular Single Linked List
Circular Single Linked List
Non-Circular Double Linked List
Circular Double Linked List
Non-Circular with Circular Combine Double Linked List
Other Structure Using Linked List
Linked List Stack
Non-Circular Linked List Queue
Circular Linked List Queue
Circular Linked List Priority Queue
Linked List Array
Tree
Hash
Non-Circular Single Linked List
Graphical Picture:
Non-circular Single Linked List ends with NULL pointer
Head is NULL when list is empty
Head
5
10
Define Structure:
typedef struct node *ptr_node;
sturct node
{
short data;
ptr_node next;
15
20
NULL
25
30
Data Structures in C++ Note 4
22
}
Insert:
ptr_node Head, temp, curr, prev;
temp =new node
temp = NULL
T
F
temp->data=value
T
Head = NULL
F
value < Head->data
T
F
prev = Head
no memory temp->next=NULL
curr
=prev->next
return1
Head = temp
temp->next
=
Head
whilecurr&&value>curr->data
return0
Head = temp
prev=curr
return0
curr=curr->next
prev->next=temp
temp->next=curr
return0
Delete:
ptr_node Head, temp, curr, prev;
Head = NULL
T
F
value = Head->data
T
temp = Head
listempty Head = Head->next
return1
deletetemp
return0
F
prev = Head
curr=prev->next
whilecurr&&value>curr->data
prev=curr
curr=curr->next
curr = NULL or value!= curr->data
T
F
prev->next=curr->next
deletecurr
return0
return1
Circular Single Linked List
Graphical Picture:
Circular Single Linked List
Head
Head is NULL when list is empty
5
10
15
20
25
30
Data Structures in C++ Note 4
23
Define Structure:
typedef struct node *ptr_node;
sturct node
{
short data;
ptr_node next;
}
Insert:
ptr_node Head, temp, curr, prev;
temp = new node
temp = NULL
T
F
temp->data = value
T
Head = NULL
temp->next =
temp
Head = temp
return 0
no
memory
return 1
F
value
<
Head->data
T
F
temp->next = Head
prev = Head
curr = Head
curr = prev->next
while
curr
!= Head && value > curr->data
while curr->next != Head
prev = curr
curr = curr->next
curr = curr->next
prev->next = temp
curr->next = temp
temp->next = curr
Head = temp
return 0
return 0
Delete:
ptr_node Head, temp, curr, prev;
T
Head = NULL
value = Head->data
T
T
list
empty
return 1
F
temp = Head
Head = Head->next
F
F
prev = Head
curr = prev->next
curr = Head->next
while curr != Head && value > curr->data
prev = curr
while curr->next != Head
curr = curr->next
Head = NULL
curr = curr->next
value = curr->data
T
F
curr->next = Head->next
prev->next = curr->next
Head = Head->next
return 1
delete curr
delete temp
return 0
return 0
Data Structures in C++ Note 4
24
Circular Single Linked List
Ascending list contains Efficient Node
Graphical Picture:
Head
Empty linked list with efficient node
BIG
Head always points to efficient node
Head
Circular Single Linked List with efficient node
BIG is maximum value for field type
BIG
5
10
15
20
25
30
Define Structure:
typedef struct node *ptr_node;
struct node
{
short data;
ptr_node next;
}
Initialize the List:
ptr_node Head;
Head = new sturct node;
Head->Data = Max_value;
Head->next = Head;
//Max_value is stored maximum value of the field type
Insert:
ptr_node temp, curr, prev;
temp =new node
T
temp = NULL
F
temp->data=value
prev = Head
curr=prev->next
whilevalue>curr->data
no memory
return1
prev=curr
curr=curr->next
prev->next=temp
temp->next=curr
return0
Data Structures in C++ Note 4
25
Delete:
ptr_node temp, curr, prev;
prev = Head
curr=prev->next
whilevalue>curr->data
prev=curr
curr=curr->next
value=curr->data
T
F
listempty
or
valuenot inlist
return1
prev->next=curr->next
deletecurr
return0
Double Linked List with Efficient Node
Graphical Picture:
Circular Double Linked List with efficient node
Ascending and Descending links (pointers) for key value
Head
5
BIG
10
Empty list with
Efficient Node
BIG
ascending order
pointer field
input order
pointer field
15
20
25
30
Double Linked List (two different orders) with efficient node
Circular list using ascending pointer and efficient node
Non-circular list using input order pointer and end pointer, value inserted at
end of list
Head
NULL
pointer
BIG
15
5
30
10
25
20
End
pointer
Data Structures in C++ Note 4
26
Linked List Stack
Graphical Picture:
Linked Stack
Top is NULL when stack is empty
Top
Data
field
NULL
Pointer
field
Non-Circular Linked List Queue
Two Graphical Pictures:
Linked non-circular Queue
QFront and QRear are NULL when queue is empty
QFront
QRear
NULL
front of queue
Linked non-circular Queue
separate structure for QFront and QRear pointers
QFront and QRear pointers in structure are NULL when queue is empty
Queue_Pointers
QFront
QRear
NULL
Non-Circular Linked List Queue with 2 Pointers Algorithm:
Insert:
T
temp = new node
temp = NULL
no memory
return 1
T
F
temp->data = value
temp->next = NULL
qrear = NULL
qfront =
qrear = temp
F
qrear->next = temp
qrear = temp
return 0
Data Structures in C++ Note 4
27
Delete:
qfront = NULL
F
T
value = qfront->data
temp = qfront
queue
empty
return 1
qfront = qrear
T
qfront =
qrear =
NULL
F
qfront = qfront>next
delete temp
return 0
Circular Linked List Queue
Two Graphical Pictures:
QFront
Linked circular Queue
QFront and QRear are NULL when queue is empty
QRear
Linked circular Queue
with one pointer
front of queue
QRear
QRear is NULL when queue is empty
Circular Linked List Queue with 1 Pointer (QRear) Algorithm:
Insert:
temp = new node
T
temp = NULL
F
temp->data = value
no
memory
return 1
qrear = NULL
T
F
qrear = temp
qrear->next
= temp
temp->next = qrear->next
qrear->next = temp
qrear = temp
return 0
Data Structures in C++ Note 4
28
Delete:
qrear = NULL
T
queue
empty
return 1
F
qfront = qrear->next
value = qfront->data
qfront = qrear
T
F
qrear =
NULL
qrear->next = qfront->next
delete qfront
return 0
Circular Linked List Priority Queue
Algorithms:
Insert :
temp
temp
==
new
new
node
node
T
temp = NULL
T
F
temp->data = data; temp->priority = priority;
QRear = NULL
No Memory
Return 1
T
F
priority >= QRear->priority
temp->next = QRear->next
QRear = temp
QRear->next = temp
Temp->next = QRear
QRear = temp
prio = temp->priority
prev = QRear
curr = QRear->next
while (prio >= curr->priority)
prev = curr
curr = curr->next
temp->next = prev->next
prev->next = temp
Return 0
Delete:
It is using same algorithms as Circular Linked List Queue.
Linked List Array
Define Structure:
struct node
{
short data;
short link;
F
Data Structures in C++ Note 4
29
}
node list[12];
//list can be any size, a compile time or dynamically allocated structure
Graphical Picture:
data
link
0
32767
0 Efficient Node Initialize list:
Empty_cell is location (offset) of next
1
2
available element in structure
2
3
3
4
5
6
7
8
9
10
11
4
5
6
7
8
9
10
11
0
size = 12
empty_cell = 1
list[0].link = 0
list[0].data = 32767
for (i=1; i<size-1; i++)
list[i].link = i+1
list[size-1].link = 0
Insert:
temp, curr, prev are integer variables which contain offsets
empty_cell = 0
T
no
memory
return 1
F
temp = empty_cell
list[temp].data = value
empty_cell = list[temp].link
prev = 0
curr = list[0].link
while value > list[curr].data
prev = curr
curr = list[curr].link
list[prev].link = temp
list[temp].link = curr
return 0
Delete:
Data Structures in C++ Note 4
30
curr, prev are integer variables which contain offsets
prev = 0
curr=list[0].link
whilevalue>list[curr].data
prev=curr
curr=list[curr].link
value=list[curr].data
T
list[prev].link=list[curr].link
list[curr].link=empty_cell
empty_cell=curr
return0
F
listempty
or
valuenot inlist
return1
Array of Structures
Used to store independent Linked Lists (structures)
(structure contains Efficient Node for circular single Linked List only)
Define Structure:
Linkeded list structure is an array of type node
struct node
{
short data;
short link;
}
node list[15];
//list can be any size, a compile time or dynamically allocated structure
Graphical Picture:
data
0
1
32767
link
0
2
2
3
4
3
4
5
5
6
7
8
9
6
7
8
9
10
11
12
13
14
0
10
11
12
13
14
Efficient Node
table to maintain independent linked lists
(structures)
stored in array of structures
0
0
0
1
offset
of 1st
element
0
0
0
14
ascending single linked list
circular linked queue
linked stack
empty_cell (linked stack)
count of
nodes
in list
(optional)
Data Structures in C++ Note 4
31
Table & array of structures after insert and delete operations in independent structures
table to maintain independent linked lists
(structures)
stored in array of structures
0
4
ascending single linked list
10
3
circular linked queue
7
3
linked stack
2
4
empty_cell (linked stack)
offset
of 1st
element
count of nodes
in list
(optional)
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
data
32767
208
123
106
395
245
380
385
110
135
210
120
?
?
?
link
3 Efficient Node
5
12
8
0
10
4
6
11
0
1
9
13
14
0