Data Structure: Content

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

Content

• List
• Abstraction data type List
DATA STRUCTURE • Mathematical model of List
• List operations
(LIST – ARRAY IMPLEMENTATION) • Array-based implementation
• Data structure
Phạm Nguyên Khang
• Implementation of basic list operators
pnkhang@cit.ctu.edu.vn
• Application

CAN THO, 7/7/2022


Page 2

ADT List ADT List

• What is a List? • What is a List?


• Sequence of zero or more elements (of a given • Elements can be accessed at any position.
type a.k.a ElementType)
My name is
Bunny.

a1, a2, …, an a1, a2, …, an

Hey the second one,


what’s your name?

Thank you Khánh Vân for the so cute rabbits! Page 3 Page 4
ADT List ADT List

• What is a List? • What is a List?


• A flexible structure • A flexible structure
• Can grow or shrink on demand • Can grow or shrink on demand
• New elements can be inserted at any position • New elements can be inserted at any position.
I want to be at
the 3rd place. Now, I’m at the
3rd place!

a1, a2, a3 a1, a2, a3 a4


Page 5 Page 6

ADT List ADT List

• What is a List? • What is a List?


• Flexible structure • Flexible structure
• Can grow or shrink on demand • Can grow or shrink on demand
• Elements can also be deleted at any position • Elements can also be deleted at any position
I want to Bye bye!
go out!

a1, a2, a3 a1, a2


Page 7 Page 8
ADT List ADT List

• n (n ≥ 0): length of the list • An important property


• If n > 0 • Elements can be linearly ordered according their
– a1: the first element position. So, lists are a linear structure.
– an: the last element • ai is at position i
• If n = 0 • ai precedes ai+1
– We have an empty list (which has no elements) • ai follows ai-1
• end(L): position following the last element on L

a1, a2, …, an a1, a2, …, an


Page 9 Page 10

ADT List ADT List

• List operations • List operations


Operator Meaning Operator Meaning
end(L) Return the position following the last element on L next(p, L) Return the position following position p on list L. If p is the last
insert(x, p, L) Insert x at the position p in list L, moving elements at p and position on L, then next(p, L) returns end(L).
following positions to the next higher position. previous(p, L) Return the position preceding position p on list L. If p is the first
locate(x, L) Return the position of x in list L. If x appears more than once, position on L, then previous(p, L) is undefined.
then the position of the first occurrence is returned. If x does not first(L) Return the first position of L. If L is empty, return end(L).
appear at all, then end(L) is returned. makeNull(L) Cause L to become empty and return position end(L).
retrieve(p, L) Return the element at position p on list L. If p is an invalid printList(L) Print elements of L in the order of occurrence.
position, the result is undefined.
In practice, we can always modify retrieve() to return a pointer to
an object of type ElementType.
delete(p, L) Delete the element at position p of list L. If p is an invalid position,
do nothing or raise an error.
Page 11 Page 12
Array-based Implementation Array-based Implementation

• ElementType: type of elements


• Data structure “List” Position = Array index + 1
• elements: A (fixed) array to store elements Array index = Position - 1
• last: position of the last element in the array
• last is also the length of list.

Array Array
elements 0 1 2 3 4 5 6 index
elements 0 1 2 3 4 5 6 index
1 2 3 4 5 6 7 1 2 3 4 5 6 7
last last

4 List position 4 List position


Page 13 Page 14

Array-based Implementation Array-based Implementation

• Implementation in C – Type definition • Implementation in C – Type definition


#define MAXLENGTH 100 #define MAXLENGTH 100
typedef ... ElementType; //Type of elements typedef ... ElementType;
typedef int Position; //Position is an integer typedef int Position;

typedef struct { typedef struct {


//Quiz: write your code here ElementType elements[MAXLENGTH];
Position last;
} List; } List;

Page 15 Page 16
Array-based Implementation Array-based Implementation

• Implementation in C – end(L) • Implementation in C – first(L)


Position end(List *pL) { Position first(List *pL) {
return pL->last + 1; //Return the first position of L
} return 1;
}

Page 17 Page 18

Array-based Implementation Array-based Implementation

• Implementation in C – next(p, L) • Implementation in C – next(p, L)


Position next(Position p, List *pL) { Position next(Position p, List *pL) {
//1. Check the validity of p (1 ≤ p ≤ last) //1. Check the validity of p (1 ≤ p ≤ last)

//2. Return position following p //2. Return position following p


} return p + 1;
}

Page 19 Page 20
Array-based Implementation Array-based Implementation

• Implementation in C – previous(p, L) • Implementation in C – makeNull(p, L)


Position previous(Position p, List *pL) { Position makeNull(List *pL) {
//1. Check the validity of p (2 ≤ p ≤ end(L)) //1. Make L become empty

//2. Return the position preceding p //2. Return end(L)


return p - 1; }
}

Page 21 Page 22

Array-based Implementation Array-based Implementation

• Implementation in C – makeNull(p, L) • Implementation in C – retrieve(p, L) – version 1


Position makeNull(List *pL) { ElementType retrieve(Position p, List *pL) {
//1. Make L become empty //1. Check the validity of p (1 ≤ p ≤ last)
pL->last = 0;
//2. Return the element at p position
//2. Return end(L) }
return end(pL);
}

Page 23 Page 24
Array-based Implementation Array-based Implementation

• Implementation in C – retrieve(p, L) – version 1 • Implementation in C – retrieve(p, L) – version 1


ElementType retrieve(Position p, List *pL) { ElementType retrieve(Position p, List *pL) {
//1. Check the validity of p (1 ≤ p ≤ last) //1. Check the validity of p (1 ≤ p ≤ last)
if (p < 1 || p > pL->last) {
//print error message //2. Return the element at p position
ElementType dummy; return pL->elements[p - 1];
return dummy; //an arbitrary value }
}

//2. Return the element at p position


}

Page 25 Page 26

Array-based Implementation Array-based Implementation

• Implementation in C – retrieve(p, L) – version 2 • Implementation in C – retrieve(p, L) – version 2


ElementType *retrieve(Position p, List *pL) { ElementType *retrieve(Position p, List *pL) {
//1. Check the validity of p (1 ≤ p ≤ last) //1. Check the validity of p (1 ≤ p ≤ last)
if (p < 1 || p > pL->last) {
//2. Return the address of the element at p //print error message
} return 0; //NULL address
}

//2. Return the address of the element at p


}

Page 27 Page 28
Array-based Implementation Array-based Implementation

• Implementation in C – retrieve(p, L) – version 2 • Implementation in C – locate(x, L)


ElementType *retrieve(Position p, List *pL) { Position locate(ElementType x, List *pL) {
//1. Check the validity of p (1 ≤ p ≤ last) // Sequentially scans the array to look for x

//2. Return the address of the element at p // If x is not found return end(L)
return &pL->elements[p - 1];
}

Page 29 Page 30

Array-based Implementation Array-based Implementation

• Implementation in C – locate(x, L) • Implementation in C – insert(x, p, L)


Position locate(ElementType x, List *pL) {
1. Check the validity of p (1 ≤ p ≤ end(L))
// Sequentially scans the array to look for x
for (int i = 1; i <= pL->last; i++)
p is valid.
if (pL->elements[i-1] == x)
return i;

// If x is not found, return end(L)


return end(pL);
}
elements 0 1 2 3 4 5 6
1 2 3 4 5 6 7

p=2 last = 4
Page 31 Page 32
Array-based Implementation Array-based Implementation

• Implementation in C – insert(x, p, L) • Implementation in C – insert(x, p, L)


1. Check the validity of p (1 ≤ p ≤ end(L)) 1. Check the validity of p (1 ≤ p ≤ end(L))
2. Move the elements at location p, p+1, …, last 2. Move the elements at location p, p+1, …, last
into p+1, p+2, …, last+1 into p+1, p+2, …, last+1
We need to be moved to Ok, I go first.
the next location

elements 0 1 2 3 4 5 6 elements 0 1 2 3 4 5 6
1 2 3 4 5 6 7 1 2 3 4 5 6 7

p=2 last = 4 p=2 last = 4


Page 33 Page 34

Array-based Implementation Array-based Implementation

• Implementation in C – insert(x, p, L) • Implementation in C – insert(x, p, L)


1. Check the validity of p (1 ≤ p ≤ end(L)) 1. Check the validity of p (1 ≤ p ≤ end(L))
2. Move the elements at location p, p+1, …, last 2. Move the elements at location p, p+1, …, last
into p+1, p+2, …, last+1 into p+1, p+2, …, last+1
Then, it’s my turn. I’m the last one, hic L

elements 0 1 2 3 4 5 6 elements 0 1 2 3 4 5 6
1 2 3 4 5 6 7 1 2 3 4 5 6 7

p=2 last = 4 p=2 last = 4


Page 35 Page 36
Array-based Implementation Array-based Implementation

• Implementation in C – insert(x, p, L) • Implementation in C – insert(x, p, L)


1. Check the validity of p (1 ≤ p ≤ end(L)) 1. Check the validity of p (1 ≤ p ≤ end(L))
2. Move the elements at location p, p+1, …, last 2. Move the elements at location p, p+1, …, last
into p+1, p+2, …, last+1 into p+1, p+2, …, last+1
3. Place the new element x at the location p 3. Place the new element x at the location p
4. Increase last

elements 0 1 2 3 4 5 6 elements 0 1 2 3 4 5 6
1 2 3 4 5 6 7 1 2 3 4 5 6 7

p=2 last = 4 p=2 last = 5


Page 37 Page 38

Array-based Implementation Array-based Implementation

• Implementation in C – insert(x, p, L) • Implementation in C – insert(x, p, L)


void insert(ElementType x, Position p, List *pL) { void insert(ElementType x, Position p, List *pL) {
//1. Check the validity of p //1. Check the validity of p
//2. Move elements at p, p+1, ...,last if (p < 1 || p > end(pL)) {
//3. Place x at p //print an error message
//4. Increase last return;
} }
//2. Move elements at p, p+1, ..., last
//3. Place x at p
//4. Increase last
}
• Please keep in mind that
array index = position - 1
Page 39 Page 40
Array-based Implementation Array-based Implementation

• Implementation in C – insert(x, p, L) • Implementation in C – insert(x, p, L)


void insert(ElementType x, Position p, List *pL) { void insert(ElementType x, Position p, List *pL) {
//1. Check the validity of p //1. Check the validity of p
//2. Move elements at p, p+1, ..., last //2. Move elements at p, p+1, ..., last
int i; //3. Place x at p (array index = position - 1)
for (i = pL->last; i >= p; i--) pL->elements[p - 1] = x;
pL->elements[i] = pL->elements[i-1];
//4. Increase last
//3. Place x at p }
//4. Increase last
}

Page 41 Page 42

Array-based Implementation Array-based Implementation

• Implementation in C – insert(x, p, L) • Implementation in C – delete(p, L)


void insert(ElementType x, Position p, List *pL) {
1. Check the validity of p (1 ≤ p ≤ last)
//1. Check the validity of p
//2. Move elements at p, p+1, ..., last
//3. Place x at p p is valid.
//4. Increase last
pL->last++;

elements 0 1 2 3 4 5 6
1 2 3 4 5 6 7

p=2 last = 4
Page 43 Page 44
Array-based Implementation Array-based Implementation

• Implementation in C – delete(p, L) • Implementation in C – delete(p, L)


1. Check the validity of p (1 ≤ p ≤ last) 1. Check the validity of p (1 ≤ p ≤ last)
2. Move the elements at location p+1, …, last into 2. Move the elements at location p+1, …, last into
p, p+1, …, last-1 p, p+1, …, last-1
We need to be moved to I should go first and take
thethe
previous location
next location his location.

elements 0 1 2 3 4 5 6 elements 0 1 2 3 4 5 6
1 2 3 4 5 6 7 1 2 3 4 5 6 7

p=2 last = 4 p=2 last = 4


Page 45 Page 46

Array-based Implementation Array-based Implementation

• Implementation in C – delete(p, L) • Implementation in C – delete(p, L)


1. Check the validity of p (1 ≤ p ≤ last) 1. Check the validity of p (1 ≤ p ≤ last)
2. Move the elements at location p+1, …, last into 2. Move the elements at location p+1, …, last into
p, p+1, …, last-1 p, p+1, …, last-1
Now, it’s my turn.
3. Decrease last

elements 0 1 2 3 4 5 6 elements 0 1 2 3 4 5 6
1 2 3 4 5 6 7 1 2 3 4 5 6 7

p=2 last = 4 p=2 last = 3


Page 47 Page 48
Array-based Implementation Array-based Implementation

• Implementation in C – delete(p, L) • Implementation in C – delete(p, L)


void delete(Position p, List *pL) { void delete(Position p, List *pL) {
//1. Check the validity of p //1. Check the validity of p
//2. Move elements at p+1, p+2, ..., last if (p < 1 || p > pL->last) {
//3. Decrease last //print an error message
} return;
}

//2. Move elements at p+1, p+2, ..., last


//3. Decrease last
}
• Please keep in mind that
array index = position - 1
Page 49 Page 50

Array-based Implementation Array-based Implementation

• Implementation in C – delete(p, L) • Implementation in C – printList(L) – version 1


void delete(Position p, List *pL) { void printList(List *pL) {
//1. Check the validity of p //Direct access using array index
//2. Move elements at p+1, p+2, ..., last for (int i = 0; i < pL->last; i++) {
//Remember: array index = position - 1 //print pL->elements[i]
int i;
for (i = p + 1; i <= pL->last; i++) }
pL->elements[i-2] = pL->elements[i-1]; }

//3. Decrease last


}

Page 51 Page 52
Array-based Implementation Array-based Implementation

• Implementation in C – printList(L) – version 2 • Implementation in C – printList(L) – version 2


void printList(List *pL) { //Use while loop instead of for
//Iterates over L using list operators
for (Position p = first(pL); void printList(List *pL) {
p != end(pL); //Iterates over L using list operators
p = next(p, pL)) { Position p = first(pL);
while (p != end(pL)) {
//print retrieve(p, pL) //print retrieve(p, pL)
p = next(p, pL);
} }
} }

Page 53 Page 54

Array-based Implementation Array-based Implementation

• Application – remove duplicate elements • Quiz: locate(x, L) - revisited


void purge(List *pL) { Position locate(ElementType x, List *pL) {
//removes duplicate elements from list L // Sequentially scans the array to look for x
Position p = first(pL); for (int i = 1; i <= pL->last; i++)
while (p != end(pL)) { if (pL->elements[i-1] == x)
Position q = next(p, pL); return i;
while (q != end(pL))
if (retrieve(p, pL) == retrieve(q, pL)) // If x is not found, return end(L)
delete(q, pL); return end(pL); Rewrite this
else } function using basic
q = next(q, pL);
list operators
p = next(p, pL);
}
}

Page 55 Page 56
Array-based Implementation Array-based Implementation

• Practice: Playing with numbers • Practice: Playing with numbers


– Define a List to store integers (max: 100) – main() function
• Implement some basic operators: end(), insert(), • Create an empty list L
delete(), makeNull(), first(), next(), locate(), printList() • Read n integers from stdin and append them to L
– main() function • Print elements of L in format: a1, a2, a3
• Create an empty list L • Insert 20 at the 4th position
• Read n integers from stdin and append them to L • Print elements of L
• Print elements of L in format: a1, a2, a3 • Delete the 1st element
• Insert 20 at the 4th position • Print elements of L
• Print elements of L • Print the position of x (x is read from stdin),
Sample output:
• Delete the 1st element Sample input: 2, 4, 7, 8, 3
• Print elements of L 5 2, 4, 7, 20, 8, 3
• Print the position of x (x is read from stdin) 2 4 7 8 3 4, 7, 20, 8, 3
4 1
Page 57 Page 58

Array-based Implementation Array-based Implementation

• Practice: A very simple system • Practice: A very simple system


of student management of student management
– Define a DS ElementType to # Name Mark – main() function # Name Mark

store Student Info. 1 Harry Potter 10 • Make an empty list L 1 Harry Potter 10

• name: string (max: 50 characters) 2 Hermione Granger 8 • Insert 5 elements into L in ordered 2 Hermione Granger 8
• mark: integer of its occurrence
– Define a DS List to store
3 Draco Malfoy 7
• Print student List in format
3 Draco Malfoy 7

4 Luna Lovegood 8 Name 1: mark 1 4 Luna Lovegood 8


Students (max: 100) 5 Ron Weasley 9 Name 2: mark 2 5 Ron Weasley 9
• Implement some basic operators: • Insert a new student (Ginny
end(), insert(), delete(), Weasley, 6) at the 2nd position
makeNull(), first(), next(), • Print the student list
printList() • Delete the 3rd element
• Print the student list

Page 59 Page 60
THANK YOU

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy