Download as pdf or txt
Download as pdf or txt
You are on page 1of 61

List And Linked List

List
Definition :

The list is a collection of data,


elements, components or
objects of the same type.

#
List

Example :

i. List a group of student which will have


same data such as name, matric number.

ii. List a group of staff which will have same


data such as name, staff number, identity
card number.

#
The Operations Can Be Performed On The
List Are:
Num Operation Description

a. insertion Adding new items to the list

b. deletion • Removing an item from the list,


• This operation involves identifying the location
of the next item to delete, and
• Next item should be shifted upward.

c. retrieval Identify the item from the list and displays

d. traversal • All items will be listed in an orderly manner.


• This operation requires an algorithm repeats
#
An array would be boxes in a bookcase.

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).

A linked list, conversely, would be a necklace.

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

• The list can be implemented by arrays of


sequential items.

• First item in the input will be in first position


in the array, and so order the next item.

#
Implement Operations List As An Array

Senarai P0 P1 P2 ………….. Pn

Tatasusunan
X[0] X[1] X[2] X[3] ….. X[MAKS]

Illustration Of How Data Inserted Into List

#
Using The List Of Operations
Operations involved in implementing the list as an array is:

i.Create a list
ii.Check list

iii.Adding items on the list

iv.Deleting item from the list

#
Create List

• Create a list will involve the process of determining the


maximum number of items to be used in the list.

• The next process is to identify the types of items needed.

#
Create List

Initial
-number of items
-types of item Operation
-Create an empty list Output
-List is created

Operations To Create A List

#
Check List

• The process of reviewing the list is to identify


whether the list is empty or full.

• The process is divided into two ways:


a. Process to determine the list is empty or
not.
b. Process to determine the list is full or not.
#
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

Operations To Check A List Is


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

Operations To Check A List Is


Full

#
Adding Items On The List

• The process to add the item involves the


acquisition of new items.

• The last item in the list of positions identified


for the process of adding an item in 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)

Operations To Add Items On


The List
#
Deleting Item From The List

• List received must be checked to determine


the list is not empty.

• Items to be removed should be identified.

• Delete those items and the list updated.


#
Deleting Item From The List

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)

Operations To Delete Items


From The List
#
List
• Lists is a group of objects which is organized in sequence.

• List categories

i. Linear list

A list in which the data is organized in sequence, example:


array, linked list, stack and queue

ii. Non-linear list

A list in which the data is stored not sequence, example:


tree and graph

#
Introduction to Linear List

• Array and linked lists are linear lists that doesn’t


have any restrictions while implementing operations
such as, insertion, deletion and accessing data in the
lists.

#
Introduction to Linear List

• The operations can be done in any parts of the lists,


either in the front lists, in the middle or at the back of
the lists.

#
Introduction to Linear List

• An array named Pelajar which contains


attributes nama, pelajar, kursus and tahun
Pelajar. #
Introduction to Linear List
• The array is sorted and can only be accessed
based on the index or subscript of the array.

• Example: to access information for a student


named Mohd Saufi, we can use:
Pelajar[3].Nama, Pelajar[3].Kursus and
Pelajar[3].Tahun.

#
Introduction to Linear List

• Linked lists which contain several nodes which


is sorted in ascending order.

#
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.

Queue - Insert data in a queue can be done at the


back of the lists while to delete data from a queue
can only be done at the front list.
#
Linear List

Example code 1 (To Add Value In A List):


#include<iostream>
using namespace std;
void main(){
int Senarai[5];
int Penanda=0; //Pembolehubah yg menunjukan kpd ruangan kosong terakhir
int Nilai;
int Ulangan;
Senarai

0 1 2 3 4
Penanda

Continue…
#
Linear List

for(int s=0; s < 6; s++){


//check list is full or not
if(Penanda == 5){
cout<<"Senarai Telah Penuh\n";
cout<<"Kedudukan \t\t Nilai\n";
//display list
for (Ulangan = 0; Ulangan < Penanda; Ulangan ++){
cout<<Ulangan<<"\t\t\t "<<Senarai[Ulangan]<<"\n";
}

}else{

Continue…
#
Linear List

cout<<"Apakah nilai yang hendak dimasukan dalam senarai: ";


cin>>Nilai; Senarai 23
Senarai[Penanda]=Nilai;
0 1 2 3 4
Penanda++;
//display list
cout<<"Kedudukan \t\t Nilai\n"; Penanda++
for (Ulangan = 0; Ulangan < Penanda; Ulangan ++){
cout<<Ulangan<<"\t\t\t "<<Senarai[Ulangan]<<"\n";
}
}
}

#
Linear List

Example Code 2 (Delete Item From List):


#include<iostream>
using namespace std;
void main(){
int Senarai[5]={23,34,45,55,100};
int Penanda=5;
int Nilai;
int anjak;
int ulang; Senarai 23 34 45 55 100

0 1 2 3 4 5

Penanda

Continue…
#
Linear List

//check list is empty or not


//if list is empty
if (Penanda == 0){
cout<<"Senarai Kosong !!, Tidak Dapat Buang Nilai";

}else{ //if list is not empty


cout<<"Apakah nilai yang hendak dikeluarkan :";
cin>>Nilai;

Continue…
#
Linear List

for(ulang = 0; ulang < Penanda; ulang++){


if(Nilai == Senarai[ulang]){
//display position item to delete
cout<<"Kedudukan nilai ini adalah di kedudukan ke:"<<ulang<<endl;
//Anjakan nilai pada kedudukan dibelakangnya satu tempat hadapan
for (anjak=ulang; anjak < 5; anjak++)
{
Senarai[anjak]=Senarai[anjak+1];
}
Penanda--;
break;
}else{
cout<<"Nilai Tiada Dalam Senarai\n";
}}}
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

• Linked list consist of at least one head node.

• Head node acts as a pointer to the first node in


linked list and contains the address of the nod.

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

• The most important concept in linked list is


the instances that point/link to other
instances.

• In linked list this kind of instances usually


referred as nodes.

• Example : Grocery List


#
For example:

• A list of images that need to be burned to a CD in a


medical imaging application

• A list of users of a website that need to be emailed some


notification

• A list of objects in a 3D game that need to be rendered to


the screen

#
A linked list can be used to implement a queue.

The canonical real life example would be a line for a cashier.

A linked list can also be used to implement a stack.

The cononical real ife example would be one of those plate


dispensers at a buffet restaurant where pull the top plate off
the top of the stack.

#
Linked List

Name Matric
pointer
Data field Link field

Node

#
Linked List

• Linked list can be constructed using

i. A pointer or

ii. Array

#
Linked List Constructed Using Pointer

• Linked list constructed using pointers is a


dynamic data structure that does not limit the
list size, and node components can be added
and removed easily.

#
Linked List Constructed Using Pointer

• Figure 1 shows the linked list is constructed


using pointer which is contains four nodes.
10 24 40 50 NULL

Figure 1

#
Linked List Constructed Using Array

• Linked list built using the array is a static data


structure because the list size is limited.

• The maximum number of nodes in the linked


list depends on the size of the array that has
been declared.
#
Linked List Constructed Using Array

• Figure 2 shows linked list constructed using


Head node
an array.
0
index item link
3 50 -1
2 40 3
1 24 2
0 10 1
Figure 2

#
Linked List Constructed Using Array

• Head node contains value 0 indicates the first


node in linked list is at index 0.

• Link to the index value 0 is the value of 1


indicates a link to that node is at index 1 and
so on;
#
Linked List Constructed Using Array

• Head node contains value 0 indicates the first


node in linked list is at index 0.

• Link to the index value 0 is the value of 1


indicates a link to that node is at index 1 and
so on;
#
Linked List Constructed Using Array

• Link on the index value 3 is value -1 indicates


that the node is the last node in the list and
has no links.

#
Linked List Constructed Using
Array/Pointer

Head Nod Head Nod

NULL -1

Pointer Array

Figure 3 : Shows an empty linked list constructed using a pointer and array

#
Characteristics Of Non-empty Linked List

1. Must have a head node that is not


valued null. Starting from the head node,
all information in the linked list can be
achieved.
2. Each node in the linked list has at least
one data field.

#
Characteristics Of Non-empty Linked List

3. Each node in the linked list has at least


one field that stores link next node
address

#
Characteristics Of Non-empty Linked List

Head node Name Age cpa link


Fasehah 20 3.25 NULL

Data Field Link Field

Figure 4 : Shows a Non-empty linked list of a node

#
Characteristics Of Non-empty Linked List

Head node Name Age cpa link Name Age cpa link

Fasehah 20 3.25 Nina 25 3.5 NULL

Data Field Link Field Data Field Link Field

Figure 4 : Shows a Non-empty linked list with two nodes

#
Linked list variations

• Singly linked list

• Doubly linked list

• Circular linked list

• Circular doubly linked list

• Sorted linked list

• Unsorted linked list


#
Singly linked list

A B C ∅

• The last pointer of node points to NULL

#
Doubly linked lists
∅ A B C ∅

Head

• Each pointer of node points to not only successor


but the predecessor.
• There are two NULL: at the first and last nodes in
the list.
#
Doubly linked lists

• Advantage: given a node, it is easy to visit its


predecessor. Convenient to traverse lists
backwards

#
Circular linked lists

A B C

• The last pointer of node points to the first node


of the list.
• How do we know when we have finished
traversing the list? (Tip: check if the pointer of
the current node is equal to the head.)
#
Circular doubly linked list

• No NULL value at the first and last nodes in


the list.
• Convenient to traverse lists backwards and
forwards.
#
Sorted Linked list :

• The nodes in the lists is sorted in certain


order.

#
UnSorted Linked list :

• The nodes in the lists is not sorted in any order.

#
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?

The simplest and most straightforward is a train.

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.

Much like a linked list.

You might also like