Professional Documents
Culture Documents
Chapter 2 - Intro To LIST LINKED LIST (v2)
Chapter 2 - Intro To LIST LINKED LIST (v2)
List
Definition :
#
List
Example :
#
The Operations Can Be Performed On The
List Are:
Num Operation Description
When you remove the box from the n-th row, all boxes from n+1 up need
to be moved one shelf down (so you don't have a troublesome empty
shelf).
When you find you don't like that blue jewel anymore, take it out of the
sequence and tie the resulting two ends together. No need to loop through each
pearl and displace it just so you can fix your necklace.
#
Implement Operations List As An Array
#
Implement Operations List As An Array
Senarai P0 P1 P2 ………….. Pn
Tatasusunan
X[0] X[1] X[2] X[3] ….. X[MAKS]
#
Using The List Of Operations
Operations involved in implementing the list as an array is:
i.Create a list
ii.Check list
#
Create List
#
Create List
Initial
-number of items
-types of item Operation
-Create an empty list Output
-List is created
#
Check List
Input
-receive a list
Operation
- Determining if the Output
first item exists or -if the first item
not. exists the list is
not empty
#
Check List
Input
-receive a list
Operation
- Determining if the Output
last item exists or -if the last item
not. exists the list is
full
#
Adding Items On The List
#
Adding Items On The List
Input
-receive a list Operation
-Make sure the list is not
full
-Receive new item
-The last item in the list of
Output
positions identified
- The list has
-Adding new item
been updated
(new items
added)
Input
-receive a list Operation
-Make sure the list is not
empty
-Identify items
-Items checked in the list
Output
to determine the position
- The list has
-Delete item and shifted
been updated
next item
(items have
been removed)
• List categories
i. Linear list
#
Introduction to Linear List
#
Introduction to Linear List
#
Introduction to Linear List
#
Introduction to Linear List
#
Introduction to Linear List
• Stack and queue is a linear lists that has
restrictions while implementing its operations.
Stack - to insert, delete and access data can only be
done at the top of the lists.
0 1 2 3 4
Penanda
Continue…
#
Linear List
}else{
Continue…
#
Linear List
#
Linear List
0 1 2 3 4 5
Penanda
Continue…
#
Linear List
Continue…
#
Linear List
//display list
cout<<"Kedudukan \t\t Nilai\n";
for (ulang = 0; ulang < Penanda; ulang ++){
cout<<ulang<<"\t\t\t "<<Senarai[ulang]<<"\n";
}
}
#
Linked List
• A linked list is a series of connected nodes
0110
Head Nod
#
Linked List
• Linked list is said to be empty when it does not
contain any node or head node contains the
value NULL.
NULL
Head Nod
#
Linked List
#
A linked list can be used to implement a queue.
#
Linked List
Name Matric
pointer
Data field Link field
Node
#
Linked List
i. A pointer or
ii. Array
#
Linked List Constructed Using Pointer
#
Linked List Constructed Using Pointer
Figure 1
#
Linked List Constructed Using Array
#
Linked List Constructed Using Array
#
Linked List Constructed Using
Array/Pointer
NULL -1
Pointer Array
Figure 3 : Shows an empty linked list constructed using a pointer and array
#
Characteristics Of Non-empty Linked List
#
Characteristics Of Non-empty Linked List
#
Characteristics Of Non-empty Linked List
#
Characteristics Of Non-empty Linked List
Head node Name Age cpa link Name Age cpa link
#
Linked list variations
A B C ∅
#
Doubly linked lists
∅ A B C ∅
Head
#
Circular linked lists
A B C
#
UnSorted Linked list :
#
Defining The Data Structure For A Linked
List
struct Node
{
char name[20]; // Name of up to 20 letters
int age; // D.O.B. would be better
float height; // In meters
Node *next; // Pointer to next node
};
Node *start_ptr = NULL; // Start Pointer (root)
#
Defining The Data Structure For A Linked
List
int main(){
if(start_ptr == NULL){
start_ptr = new Node;
}else{
Node *temp;
return 0;
}
#
Real world example of the Linked List?
Train cars are linked in a specific order so that they may be loaded, unloaded, transferred, dropped off, and
picked up in the most efficient manner possible.
For instance, the Jiffy Mix plant needs sugar, flour, cornmeal, etc. Just around the bend might be a paper
processing plant that needs chlorine, sulfuric acid, and hydrogen.
Now, we can stop the train, unload each car of its contents, then let the train go on, but then everything else on
the train has to sit while flour is sucked out of the caisson, then the sugar, etc.
Instead, the cars are loaded on the train in order so that a whole chunk of it can be detached, and the remainder of
the train moves on.
The end of the train is easier to detach than a portion in the middle, and vastly easier than detaching a few cars in
one spot, and a few cars in another spot.
If needed, however, you can insert and remove items at any point in the train.