adt lists stacks and
play

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


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

  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

  3. Abstraction 3

  4. Abstract Data Types Application Programs Operations ADT Physical Algorithm Memory Implementation Structure 4

  5. Abstraction in C++ Application Programs Public methods Class Private member Private variables and methods constants 5

  6. Example: Rational Numbers Application Programs Create, +, - , *, /, print, … Class Numerator GCD(x, y) Denominator Normalize() 6

  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

  8. Lists List: A sequence of zero or more elements A 1 , A 2 , …, A N N: Size or length of the list A 1 : First element A N : Last element The order of items should be preserved 8

  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

  10. Array Implementation of List List capacity C List size N A 1 A 2 … Consecutive memory space 10

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

  12. push_back(x) List capacity C List size N++ push_back(x) { A 1 if (N==C) the Expand A A 2 N = N + 1 … A N = x } A N x 12

  13. push_front(x) List capacity C List size N++ push_front(x) { x if (N==C) the Expand A A 2 Shift all elements A 1 to A N A 3 by one position … A 1 = x A N N = N + 1 } 13

  14. insert(i, x) List capacity C List size N++ insert(i, x) { A 1 if (N==C) the Expand A A 2 Shift all elements A i to A N by … one position A i = x x … N = N + 1 } A N 14

  15. erase(i) List capacity C List size N-- erase(i) { A 1 Shift all elements A i+1 to A N A 2 by one position … N = N - 1 } A i … A N 15

  16. pop_back() List capacity C List size N-- pop_back() { A 1 N = N - 1 A 2 } … A N 16

  17. pop_front() List capacity C List size N-- pop_front() { A 1 Shift all elements A 1 to A N A 2 by one position … N = N - 1 } A N 17

  18. Linked-list Implementation List size N T ail … A 1 A N Head Null 18

  19. Initialize List size N=0 Null T ail Head Null initialize() { N=0 Tail = Head = Null } 19

  20. push_back(x) push_back(x) { N=N+1 n = Allocate new node n.next = null n.value = x List size N++ if (Head is null) { Head = Tail = n } else { x T ail Tail.next = n Tail = n } Null } … A 1 A N Head 20

  21. push_front(x) push_front(x) { N=N+1 n = Allocate new node n.next = Head n.value = x List size N++ if (Head is null) { Head = Tail = n } else { Head = n } T ail } … x A 1 A N Head Null 21

  22. pop_front(x) pop_front(x) { if N=0 then raise exception N=N-1 old_node = Head Head = Head.next List size N-- delete old_node // Are we done? } T ail … A 1 A N Head Null 22

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

  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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend