Topic 2 Part 1
Topic 2 Part 1
DATA STRUCTURES
DFC 30233
DATA STRUCTURES By : Pn. Nor Anisah Binti Mohd Saad | JTMK PUO
TOPICS
Topic 1
Introduction to
data structures
Topic 6 Topic 2
Sorting &
List & Linked List
Searching
DFC 30233
Topic 5 Topic 3
Trees Stack
Topic 4
Queues
TOPIC 2 : LIST AND LINKED LIST
Before we proceed
with list and linked list,
let’s recap about
pointer first
RECAP : POINTER
Pointer variable, p
a 10 int a;
• is a variable itself
• does not hold the value of a 100XXD int *p;
• Its value is an address of a a = 10;
(100XXD)
p 100XXD p = &a;
• points to the memory location of
a 100XXA
• has its own memory location
(100XXA)
RECAP : POINTER
int a, b, c, *p;
p = &a;
p = &b;
p = &c;
RECAP : POINTER int *p,*q;
p = &x;
❑ Operation using pointers q = &x;
RECAP : POINTER
List
❑List is a type of data structures
❑It is a collection of data which is arranged sequentially.
Example:
❑A list of groceries to buy
Category of list:
❑Contiguous List (using Array)
❑Linked List (using Pointer)
LIST
[2]
[3]
[4]
[5]
Array Name
myArray [6]
Array Size
LIST: Insert
If the array is empty then we simply insert the value in the first index:
• Assume that the value to be inserted is 2.
value inserted
[0] 2
[1]
[2]
[3]
Syntax:
[4]
myArray [6]
LIST: Insertion at the beginning
If the array is not empty then we have to shift all the item inside the array to
the next index:
• Assume that the value to be inserted is 9 and the array is already have a
values like following:
[0] 2 [0] 9
[1] 4 [1] 2
[2] 6 [2] 4
[3] [3] 6
[4] [4]
[5] [5]
• Assume that the array is like the following and you want to insert 3
to myArray[2]
[0] 9
[0] 9 Target index is 2.
[1] 8 All the item after [1] 8
[2] 7
the target index [2] 3
will be shift to the
[3] 6 next index. [3] 7
[4] [4] 6
[5] [5]
• Assume that the array is like the following and you want to insert 5
at the end of the list
[0] 2
[1] 3
[2] 4
[3] 5
[4]
[5]
myArray [6]
LIST: Delete
[0] 6 [0] 7
[1] 7 [1] 8
[2] 8 [2] 9
[3] 9 [3]
[4] [4]
[5] [5]
[0] 9 [0] 9
[1] 8 [1] 8
Assume that [2] 7 [2] 6
this is the
[3] 6
target index [3] 5
[4] 5 [4]
[5] [5]
[0] 3 [0] 3
[1] 5 [1] 5
[2] 7 [2] 7
[3] 9 [3]
[4] [4]
[5] [5]
❑ Retrieval – Locate data in a list & present to the calling module without
changing the list’s content.
❑ A special case of retrieval – elements retrieved in a sequence.
❑ Requires a looping algorithm.
❑ Each loop processes one element.
❑ All elements have been processed – loop terminates.
LIST: Check FULL
❑ Check full is to see whether array is full or not. If array is full, new item cant be
inserted.
❑ Checking full can be done by traversing the array to find index equal to array
size
[0] 9
int n=0; [1] 8
while (myArray[n] != NULL && n<6){
[2] 6
n++; } LIST FULL
if (n==6) [3] 5
cout<< “Array is full!”; CANNOT
[4] 3
INSERT DATA
[5] 1
myArray [6]
LIST: Check EMPTY
❑ Check empty is to see whether array is empty or not. If empty, there are no item
to be remove.
❑ Checking empty can be done by traversing the array to find any item.
[0]
int n=5; [1]
while (myArray[n] == NULL && n>0){
[2]
n--; } LIST EMPTY
if (n==0) [3]
cout<< “Array is empty!”; CANNOT
[4]
DELETE DATA
[5]
myArray [6]
LIST: Destroy
❑ Syntax:
delete [] myArray;
LIST: Advantages and Disadvantages of LIST
❑ Linked List is a linear data structure and it is very common data structure which consists of group of node.
❑ Each node consists of its own data and link (the address of the next node)
❑ The link is used to chain the data.
Data link
One node
Data Link
20 45 75 85
LINKED LIST
head
classDNS3A
LINKED LIST
❑ Every list must have a pointer called “head” or “first node” or ”front”.
❑ If list is empty, head must be pointed to null.
❑ If list is not empty, head must be pointed to the first node and last node
must be pointed to null.
head
List is empty
head
20 45
add(75), add(85)
20 45 75 85
75
37
EXERCISE
• Based on the diagram 1, draw the memory diagram to show the changes after execute the
statement below. (Use the original diagram for each statement 1, 2 and 3)
1. Item 18 inserted between 7 and 21
45 7 21 8
45 7 21 8
45 7 21 8
LIST vs LINKED LIST
❑ They are a dynamic in nature which allocates the memory when required.
❑ Insertion and deletion operations can be easily implemented.
❑ Stacks and queues can be easily executed by using linked list.
❑ Linked List reduces the access time.
TOPIC 2 : LIST AND LINKED LIST
❑ A simple real life example is a Train, here each coach is connected to its previous and next coach
(Except first and last). In terms of programming consider coach body as node value and connectors as
links to previous and next nodes.
❑ Each coach bring several numbers of passengers (data), and all coaches connect together by using
chain (link).
APPLICATION OF LINKED LIST
EXERCISES
Last node of the list contains the address of the first node
and forms a circular chain. This statement represent:
A. Single Linked List
B. Double Linked List
C. Circular single Linked List
D. Circular Double Linked List
TOPIC 2 : LIST AND LINKED LIST