ADT Lists, Stacks, and Queues Instructor: Ahmed Eldawy 1 - - PowerPoint PPT Presentation

adt lists stacks and
SMART_READER_LITE
LIVE PREVIEW

ADT Lists, Stacks, and Queues Instructor: Ahmed Eldawy 1 - - PowerPoint PPT Presentation

ADT Lists, Stacks, and Queues Instructor: Ahmed Eldawy 1 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


slide-1
SLIDE 1

ADT Lists, Stacks, and Queues

Instructor: Ahmed Eldawy

1

slide-2
SLIDE 2

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

slide-3
SLIDE 3

Abstraction

3

slide-4
SLIDE 4

ADT

Abstract Data Types

4

Physical Memory Structure Algorithm Implementation Operations Application Programs

slide-5
SLIDE 5

Abstraction in C++

5

Class

Private member variables and constants Private methods Public methods Application Programs

slide-6
SLIDE 6

Example: Rational Numbers

6

Class

Numerator Denominator GCD(x, y) Normalize()

Create, +, -, *, /, print, …

Application Programs

slide-7
SLIDE 7

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

slide-8
SLIDE 8

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

slide-9
SLIDE 9

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

slide-10
SLIDE 10

Array Implementation of List

10

A1 A2 … Consecutive memory space N List size C List capacity

slide-11
SLIDE 11

Initialize

11

N=0 List size C=10 List capacity

initialize() { C=10 // Initial capacity N=0 // Initial size Allocate a memory space for C elements }

slide-12
SLIDE 12

push_back(x)

12

A1

A2 … AN x

N++ List size C List capacity

push_back(x) { if (N==C) the Expand A N = N + 1 AN = x }

slide-13
SLIDE 13

push_front(x)

13

x

A2 A3 … AN N++ List size C List capacity

push_front(x) { if (N==C) the Expand A Shift all elements A1 to AN by one position A1 = x N = N + 1 }

slide-14
SLIDE 14

insert(i, x)

14

A1

A2 … x … AN

N++ List size C List capacity

insert(i, x) { if (N==C) the Expand A Shift all elements Ai to AN by

  • ne position

Ai = x N = N + 1 }

slide-15
SLIDE 15

erase(i)

15

A1

A2 … Ai … AN

N-- List size C List capacity

erase(i) { Shift all elements Ai+1 to AN by one position N = N - 1 }

slide-16
SLIDE 16

pop_back()

16

A1

A2 … AN

N-- List size C List capacity

pop_back() { N = N - 1 }

slide-17
SLIDE 17

pop_front()

17

A1

A2 … AN

N-- List size C List capacity

pop_front() { Shift all elements A1 to AN by one position N = N - 1 }

slide-18
SLIDE 18

Linked-list Implementation

18

A1 … AN

Head T ail Null N List size

slide-19
SLIDE 19

Initialize

19

Head T ail N=0 List size Null Null

initialize() { N=0 Tail = Head = Null }

slide-20
SLIDE 20

push_back(x)

20

push_back(x) { N=N+1 n = Allocate new node n.next = null n.value = x if (Head is null) { Head = Tail = n } else { Tail.next = n Tail = n } } A1 … AN

Head T ail Null N++ List size

x

slide-21
SLIDE 21

push_front(x)

21

push_front(x) { N=N+1 n = Allocate new node n.next = Head n.value = x if (Head is null) { Head = Tail = n } else { Head = n } } A1 … AN

Head T ail Null N++ List size

x

slide-22
SLIDE 22

pop_front(x)

22

pop_front(x) { if N=0 then raise exception N=N-1

  • ld_node = Head

Head = Head.next delete old_node // Are we done? } A1 … AN

Head T ail Null N-- List size

slide-23
SLIDE 23

Array Vs Linked-list

Operation Array Linked-list Initialize push_back push_front pop_back pop_front find erase clear

23

slide-24
SLIDE 24

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