ADT Lists, Stacks, and Queues · Lists, Stacks, and Queues Instructor: Ahmed Eldawy 1. Objectives...

24
ADT Lists, Stacks, and Queues Instructor: Ahmed Eldawy 1

Transcript of ADT Lists, Stacks, and Queues · Lists, Stacks, and Queues Instructor: Ahmed Eldawy 1. Objectives...

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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