adt lists, stacks, and queues · lists, stacks, and queues instructor: ahmed eldawy 1. objectives...
TRANSCRIPT
![Page 1: ADT Lists, Stacks, and Queues · Lists, Stacks, and Queues Instructor: Ahmed Eldawy 1. Objectives Understand the importance of ADT Learn how to implement ADT in C++ Recognize the](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b00bac23501697025add0/html5/thumbnails/1.jpg)
ADT
Lists, Stacks, and
Queues
Instructor: Ahmed Eldawy
1
![Page 2: ADT Lists, Stacks, and Queues · Lists, Stacks, and Queues Instructor: Ahmed Eldawy 1. Objectives Understand the importance of ADT Learn how to implement ADT in C++ Recognize the](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b00bac23501697025add0/html5/thumbnails/2.jpg)
Objectives
Understand the importance of ADT
Learn how to implement ADT in C++
Recognize the difference between ADT
definition and implementation
Build an ADT for lists
2
![Page 3: ADT Lists, Stacks, and Queues · Lists, Stacks, and Queues Instructor: Ahmed Eldawy 1. Objectives Understand the importance of ADT Learn how to implement ADT in C++ Recognize the](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b00bac23501697025add0/html5/thumbnails/3.jpg)
Abstraction
3
![Page 4: ADT Lists, Stacks, and Queues · Lists, Stacks, and Queues Instructor: Ahmed Eldawy 1. Objectives Understand the importance of ADT Learn how to implement ADT in C++ Recognize the](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b00bac23501697025add0/html5/thumbnails/4.jpg)
ADT
Abstract Data Types
4
Physical
Memory
Structure
Algorithm
Implementation
Operations
Application Programs
![Page 5: ADT Lists, Stacks, and Queues · Lists, Stacks, and Queues Instructor: Ahmed Eldawy 1. Objectives Understand the importance of ADT Learn how to implement ADT in C++ Recognize the](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b00bac23501697025add0/html5/thumbnails/5.jpg)
Abstraction in C++
5
Class
Private member
variables and
constants
Private
methods
Public methods
Application Programs
![Page 6: ADT Lists, Stacks, and Queues · Lists, Stacks, and Queues Instructor: Ahmed Eldawy 1. Objectives Understand the importance of ADT Learn how to implement ADT in C++ Recognize the](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b00bac23501697025add0/html5/thumbnails/6.jpg)
Example: Rational Numbers
6
Class
Numerator
Denominator
GCD(x, y)
Normalize()
Create, +, -, *, /, print, …
Application Programs
![Page 7: ADT Lists, Stacks, and Queues · Lists, Stacks, and Queues Instructor: Ahmed Eldawy 1. Objectives Understand the importance of ADT Learn how to implement ADT in C++ Recognize the](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b00bac23501697025add0/html5/thumbnails/7.jpg)
ADT Design
What is ADT design?
Defining the public interface
Who designs an ADT?
You!
With your users
Sometimes, YOU are your own user
7
![Page 8: ADT Lists, Stacks, and Queues · Lists, Stacks, and Queues Instructor: Ahmed Eldawy 1. Objectives Understand the importance of ADT Learn how to implement ADT in C++ Recognize the](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b00bac23501697025add0/html5/thumbnails/8.jpg)
Lists
List: A sequence of zero or more elements
A1, A2, …, AN
N: Size or length of the list
A1: First element
AN: Last element
The order of items should be preserved
8
![Page 9: ADT Lists, Stacks, and Queues · Lists, Stacks, and Queues Instructor: Ahmed Eldawy 1. Objectives Understand the importance of ADT Learn how to implement ADT in C++ Recognize the](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b00bac23501697025add0/html5/thumbnails/9.jpg)
List ADT
initialize(): Creates an empty list
push_back(x): Appends the item x to the end of the list
pop_back(): Removes the last element
push_front(x): Prepends the item x at the beginning of
the list
pop_front(): Removes the first element
insert(x, i): Inserts item x at position i
erase(i): Deletes item at position i
find(x): Finds the position of the element with value x
size(): Returns the number of elements
9
![Page 10: ADT Lists, Stacks, and Queues · Lists, Stacks, and Queues Instructor: Ahmed Eldawy 1. Objectives Understand the importance of ADT Learn how to implement ADT in C++ Recognize the](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b00bac23501697025add0/html5/thumbnails/10.jpg)
Array Implementation of List
10
A1
A2
…Consecutive
memory space
NList size
CList capacity
![Page 11: ADT Lists, Stacks, and Queues · Lists, Stacks, and Queues Instructor: Ahmed Eldawy 1. Objectives Understand the importance of ADT Learn how to implement ADT in C++ Recognize the](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b00bac23501697025add0/html5/thumbnails/11.jpg)
Initialize
11
N=0List size
C=10List capacity
initialize() {C=10 // Initial capacity
N=0 // Initial sizeAllocate a memory space for C elements
}
![Page 12: ADT Lists, Stacks, and Queues · Lists, Stacks, and Queues Instructor: Ahmed Eldawy 1. Objectives Understand the importance of ADT Learn how to implement ADT in C++ Recognize the](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b00bac23501697025add0/html5/thumbnails/12.jpg)
push_back(x)
12
A1
A2
…
AN
x
N++List size
CList capacity
push_back(x) {if (N==C) the Expand A
N = N + 1AN = x
}
![Page 13: ADT Lists, Stacks, and Queues · Lists, Stacks, and Queues Instructor: Ahmed Eldawy 1. Objectives Understand the importance of ADT Learn how to implement ADT in C++ Recognize the](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b00bac23501697025add0/html5/thumbnails/13.jpg)
push_front(x)
13
x
A2
A3
…
AN
N++List size
CList capacity
push_front(x) {if (N==C) the Expand A
Shift all elements A1 to AN
by one positionA1 = x
N = N + 1}
![Page 14: ADT Lists, Stacks, and Queues · Lists, Stacks, and Queues Instructor: Ahmed Eldawy 1. Objectives Understand the importance of ADT Learn how to implement ADT in C++ Recognize the](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b00bac23501697025add0/html5/thumbnails/14.jpg)
insert(i, x)
14
A1
A2
…
x
…
AN
N++List size
CList capacity
insert(i, x) {if (N==C) the Expand A
Shift all elements Ai to AN by one positionAi = x
N = N + 1}
![Page 15: ADT Lists, Stacks, and Queues · Lists, Stacks, and Queues Instructor: Ahmed Eldawy 1. Objectives Understand the importance of ADT Learn how to implement ADT in C++ Recognize the](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b00bac23501697025add0/html5/thumbnails/15.jpg)
erase(i)
15
A1
A2
…
Ai
…
AN
N--List size
CList capacity
erase(i) {Shift all elements Ai+1 to AN
by one positionN = N - 1
}
![Page 16: ADT Lists, Stacks, and Queues · Lists, Stacks, and Queues Instructor: Ahmed Eldawy 1. Objectives Understand the importance of ADT Learn how to implement ADT in C++ Recognize the](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b00bac23501697025add0/html5/thumbnails/16.jpg)
pop_back()
16
A1
A2
…
AN
N--List size
CList capacity
pop_back() {N = N - 1
}
![Page 17: ADT Lists, Stacks, and Queues · Lists, Stacks, and Queues Instructor: Ahmed Eldawy 1. Objectives Understand the importance of ADT Learn how to implement ADT in C++ Recognize the](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b00bac23501697025add0/html5/thumbnails/17.jpg)
pop_front()
17
A1
A2
…
AN
N--List size
CList capacity
pop_front() {Shift all elements A1 to AN
by one positionN = N - 1
}
![Page 18: ADT Lists, Stacks, and Queues · Lists, Stacks, and Queues Instructor: Ahmed Eldawy 1. Objectives Understand the importance of ADT Learn how to implement ADT in C++ Recognize the](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b00bac23501697025add0/html5/thumbnails/18.jpg)
Linked-list Implementation
18
A1 … ANHead
Tail
Null
NList size
![Page 19: ADT Lists, Stacks, and Queues · Lists, Stacks, and Queues Instructor: Ahmed Eldawy 1. Objectives Understand the importance of ADT Learn how to implement ADT in C++ Recognize the](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b00bac23501697025add0/html5/thumbnails/19.jpg)
Initialize
19
Head
Tail
N=0List size
Null
Null
initialize() {N=0
Tail = Head = Null}
![Page 20: ADT Lists, Stacks, and Queues · Lists, Stacks, and Queues Instructor: Ahmed Eldawy 1. Objectives Understand the importance of ADT Learn how to implement ADT in C++ Recognize the](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b00bac23501697025add0/html5/thumbnails/20.jpg)
push_back(x)
20
push_back(x) {N=N+1
n = Allocate new noden.next = nulln.value = x
if (Head is null) {Head = Tail = n
} else {Tail.next = nTail = n
}} A1 … AN
Head
Tail
Null
N++List size
x
![Page 21: ADT Lists, Stacks, and Queues · Lists, Stacks, and Queues Instructor: Ahmed Eldawy 1. Objectives Understand the importance of ADT Learn how to implement ADT in C++ Recognize the](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b00bac23501697025add0/html5/thumbnails/21.jpg)
push_front(x)
21
push_front(x) {N=N+1
n = Allocate new noden.next = Headn.value = x
if (Head is null) {Head = Tail = n
} else {Head = n
}
}
A1 … ANHead
Tail
Null
N++List size
x
![Page 22: ADT Lists, Stacks, and Queues · Lists, Stacks, and Queues Instructor: Ahmed Eldawy 1. Objectives Understand the importance of ADT Learn how to implement ADT in C++ Recognize the](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b00bac23501697025add0/html5/thumbnails/22.jpg)
pop_front(x)
22
pop_front(x) {if N=0 then raise exception
N=N-1old_node = HeadHead = Head.next
delete old_node// Are we done?
}
A1 … ANHead
Tail
Null
N--List size
![Page 23: ADT Lists, Stacks, and Queues · Lists, Stacks, and Queues Instructor: Ahmed Eldawy 1. Objectives Understand the importance of ADT Learn how to implement ADT in C++ Recognize the](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b00bac23501697025add0/html5/thumbnails/23.jpg)
Array Vs Linked-list
Operation Array Linked-list
Initialize
push_back
push_front
pop_back
pop_front
find
erase
clear
23
![Page 24: ADT Lists, Stacks, and Queues · Lists, Stacks, and Queues Instructor: Ahmed Eldawy 1. Objectives Understand the importance of ADT Learn how to implement ADT in C++ Recognize the](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b00bac23501697025add0/html5/thumbnails/24.jpg)
Array Vs Linked-list
Operation Array Linked-list
Initialize O(1) O(1)
push_back O(1) O(1)
push_front O(n) O(1)
pop_back O(1) O(1)
pop_front O(n) O(1)
find O(n) O(n)
erase O(n) O(n)
clear O(1) O(n)
24