You are on page 1of 57

DFC 30233

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

❑ A pointer variable is a variable that stores a memory address.

❑ Pointers are declared using a “*” after the type.

❑ Example : int * ptr;

Data type Asterisk (Indentifier)


Pointer’s name
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

❑ Multiple pointers to one variable

int a, *p, *q, *r;


p = &a;
q = &a;
r = q;
RECAP : POINTER

❑ Multiple bindings : A pointer may be used to access different variables.

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

Array & Pointer


❑ Array name = Pointer
❑ The name of an array points only to the first element not to the whole array.
❑ The variable name of an array is a pointer variable
EXERCISES

Sila log in to https://classpoint.app/join

Pass Code: 82126


EXERCISES

Choose the CORRECT symbol to represent


address for a variable.
A. *
B. &
C. $
D. %
EXERCISES

Choose the CORRECT syntax to declare a


floating-point pointer named price.
A. int * price;
B. float price *;
C. float * price;
D. float &price;
TOPIC 2 : LIST AND LINKED LIST

Now, lets start with


LIST
COURSE LEARNING OUTCOME

List and a linked list

Advantages of linked lists

List and Linked Disadvantages of linked lists


List
Types of linked list
Topic 2
Applications of linked list

Operation of Basic operations of linked list


Linear Linked
List and linked list in problem
List solving
LIST

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

There are various operations can be perform on a list. The most


common are:
❑ Create
❑ Insert
Note that we will
❑ Delete use C++ language
❑ Retrieve in this course.
❑ Traverse
❑ Check Full
❑ Check Empty
❑ Destroy
LIST: Create List

To create a contiguous list, we need to declare an array.


Syntax:
[0]

Array Index [1]

[2]

[3]

[4]

[5]
Array Name
myArray [6]

Array Size
LIST: Insert

❑ Insertion is adding a value into the list.


❑ Can be made at the beginning, middle or end of the list.
❑ All of the data following the inserted element must be shifted to the rear
of the array. This caused computational time when the list is large.
LIST: Insertion at the beginning

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]

[5] myArray [0] = 2

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]

myArray [6] myArray [6]


LIST: Insertion at the middle

• 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]

myArray [6] myArray [6]


LIST: Insertion at the end

• 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

❑ Deletion is removing a value from the list.


❑ Can be made at beginning, middle or end of the list.
❑ All of the data following the inserted element must be shifted to the
front of the array. Also cause computational time.
LIST: Deletion at the beginning

[0] 6 [0] 7

[1] 7 [1] 8

[2] 8 [2] 9

[3] 9 [3]

[4] [4]

[5] [5]

myArray [6] myArray [6]


LIST: Deletion at the middle

[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]

myArray [6] myArray [6]


LIST: Deletion at the end

[0] 3 [0] 3

[1] 5 [1] 5

[2] 7 [2] 7

[3] 9 [3]

[4] [4]

[5] [5]

myArray [6] myArray [6]


LIST: Retrieval

❑ 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

❑ Array can be destroy/ removed completely by using operator delete[]

❑ Syntax:
delete [] myArray;
LIST: Advantages and Disadvantages of LIST

• The number of data are small


Advantages/ • The size of list is already known
Good • The operations of deletion and insertion
Implementation are happened rarely.
• When using random access

• The size of list is fixed


A better
• A lot of operation of deletion and insertion
Disadvantages - updating the record will involve shifting. approach: use a
/ problem Linked List to
• Waste memory when a space is not used
completely dynamically
allocate memory.
TOPIC 2 : LIST AND LINKED LIST

Now, lets look on


LINKED LIST
COURSE LEARNING OUTCOME

List and a linked list

Advantages of linked lists

List and Linked Disadvantages of linked lists


List
Types of linked list
Topic 2
Applications of linked list

Operation of Basic operations of linked list


Linear Linked
List and linked list in problem
List solving
LINKED 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

❑Linked list is a dynamic data structure.


• Memory will be allocated when a space is required.
• Memory will be released when a space is not required anymore.

head

Ibzal Haslin Wellson

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

List is not empty


2 8 9
LINKED LIST : Example

Linked list can grow and shrink during execution

20 45

add(75), add(85)

20 45 75 85

delete(85), delete(45), delete(20)

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

2. Item 7 deleted from the linked list

45 7 21 8

3. Item 16 inserted at the end of the linked list

45 7 21 8
LIST vs LINKED LIST

List Linked List


Memory Static Dynamic
Allocation (size is fixed and cant be (size can grow/shrink during
change) runtime)
Insertion & Insertion & deletion need a Insertion & deletion are
Deletion lot of computational time faster
Searching Can use random access, Searching and retrieval are
& Access searching and retrieval are slow because of sequential
faster access to item
Memory Occupies less memory than Occupies more memory than
Occupied linked list for the same list for the same amount of
amount of item item (because one node
consist of data and pointer)
TOPIC 2 : LIST AND LINKED LIST

List and a linked list

Advantages of linked lists

List and Linked Disadvantages of linked lists


List
Types of linked list
Topic 2
Applications of linked list

Operation of Basic operations of linked list


Linear Linked
List and linked list in problem
List solving
ADVANTAGES OF 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

List and a linked list

Advantages of linked lists

List and Linked Disadvantages of linked lists


List
Types of linked list
Topic 2
Applications of linked list

Operation of Basic operations of linked list


Linear Linked
List and linked list in problem
List solving
DISADVANTAGES OF LINKED LIST

❑ The memory is wasted as pointers require extra memory for storage.


❑ No element can be accessed randomly; it has to access each node sequentially.
❑ Reverse Traversing is difficult in linked list.
TOPIC 2 : LIST AND LINKED LIST

List and a linked list

Advantages of linked lists

List and Linked Disadvantages of linked lists


List
Types of linked list
Topic 2
Applications of linked list

Operation of Basic operations of linked list


Linear Linked
List and linked list in problem
List solving
TYPES OF LINKED LIST

Single Linked List


❑ Singly linked lists contain nodes which have a data part as well as an address part i.e. next, which points
to the next node in sequence of nodes.
❑ The operations we can perform on singly linked lists are insertion, deletion and traversal.
TYPES OF LINKED LIST

Double Linked List


❑ In a doubly linked list, each node contains two links the first link points to the previous node and the next
link points to the next node in the sequence.
TYPES OF LINKED LIST

Circular Single Linked List


❑ In the circular linked list the last node of the list contains the address of the first node and forms a
circular chain.
TYPES OF LINKED LIST

Circular Double Linked List


❑ In circular double inked list, the next pointer of the last node points to the first node and the previous
pointer of the first node points to the last node making the circular in both directions.
TOPIC 2 : LIST AND LINKED LIST

List and a linked list

Advantages of linked lists

List and Linked Disadvantages of linked lists


List
Types of linked list
Topic 2
Applications of linked list

Operation of Basic operations of linked list


Linear Linked
List and linked list in problem
List solving
APPLICATION OF 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

Sila log in to https://classpoint.app/join

Pass Code: 82126


EXERCISES

Nodes consist of:


A. Link
B. Null
C. Data
D. Value
EXERCISES

Pointer part in Last Node will always point to


A. Link
B. Null
C. Zero
D. Value
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

List and a linked list

Advantages of linked lists

List and Linked Disadvantages of linked lists


List
Types of linked list
Topic 2
Applications of linked list

Operation of Basic operations of linked list


Linear Linked
List and linked list in problem
List solving
THANKS!
Any questions?

You might also like