1111 Abstract Data Types Cpt S 223. School of EECS, WSU.

Slides:



Advertisements
Similar presentations
Chapter 25 Lists, Stacks, Queues, and Priority Queues
Advertisements

Copyright © 2003 Pearson Education, Inc. Slide 1.
Chapter 17 Linked Data Structures. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Nodes and Linked Lists Creating,
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Pointers and Data Structures 1 Yinzhi Cao Modified from slides made by Prof Goce Trajcevski.
Chapter 17 Linked Lists.
Lists Briana B. Morrison Adapted from Alan Eugenio & William J. Collins.
COMP171 Fall 2005 Lists.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Stacks, Queues, and Linked Lists
DATA STRUCTURES AND ALGORITHMS Prepared by İnanç TAHRALI
Linked Lists.
1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.
Linked Lists Chapter 4.
Queues and Linked Lists
Linear Lists – Linked List Representation
Data Structures: A Pseudocode Approach with C
Data Structures ADT List
Chapter 24 Lists, Stacks, and Queues
Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample.
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
Data Structures Using C++
Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010.
Double-Linked Lists and Circular Lists
FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Chapter 1 Object Oriented Programming 1. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
1 Joe Meehean. Ordered collection of items Not necessarily sorted 0-index (first item is item 0) Abstraction 2 Item 0 Item 1 Item 2 … Item N.
ABC Technology Project
1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be.
CSCI2100B Linked List Jeffrey
CSE Lecture 12 – Linked Lists …
Review Pseudo Code Basic elements of Pseudo code
The List ADT Textbook Sections
25 seconds left…...
Chapter 9: Data Structures I
We will resume in: 25 Minutes.
Stack & Queues COP 3502.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Pointers and Linked Lists.
Chapter 5.
Main Index Contents 11 Main Index Contents Model for a Queue Model for a Queue The Queue The Queue ADTQueue ADT (3 slides) Queue ADT Radix Sort Radix Sort.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Cousin of the Stack.  An abstract data type (container class) in which items are entered at one end and removed from the other end  First In First.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
CS 201 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - I Text: Read Weiss, §3.1 – 3.5 1Izmir University of Economics.
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Data Structure and Algorithm Analysis 03: Lists, Stacks and Queues Hongfei Yan School of EECS, Peking University.
Cpt S 122 – Data Structures Abstract Data Types
G64ADS Advanced Data Structures
Linked List Stacks, Linked List Queues, Dequeues
Data Structures and Algorithms
CMSC 341 Lecture 5 Stacks, Queues
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Object Oriented Programming COP3330 / CGS5409
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
Doubly Linked List Implementation
Data Structures and Algorithms
Queues Jyh-Shing Roger Jang (張智星)
Doubly Linked List Implementation
Chapter 3 Lists, Stacks, and Queues
Abstract Data Types Stacks CSCI 240
Data Structures & Programming
Presentation transcript:

1111 Abstract Data Types Cpt S 223. School of EECS, WSU

222 Topics Abstract Data Types (ADTs) Some basic ADTs: Lists Stacks Queues Cpt S 223. School of EECS, WSU

33 Primitive Data Type vs. Abstract Data Types e.g., int, float programmer Primitive DT: programmer ADT: Interface (API) Implementation (methods) Data Cpt S 223. School of EECS, WSU

44 Abstract Data Types (ADTs) ADT is a set of objects together with a set of operations. Abstract in that implementation of operations not specified in ADT definition E.g., List Insert, delete, search, sort C++ classes are perfect for ADTs Can change ADT implementation details without breaking code using ADT 4Cpt S 223. School of EECS, WSU

Specifications of basic ADTs List, Stack, Queue Cpt S 223. School of EECS, WSU5

66 The List ADT List of size N: A 0, A 1, …, A N-1 Each element A k has a unique position k in the list Elements can be arbitrarily complex Operations insert(X,k), remove(k), find(X), findKth(k), printList() Cpt S 223. School of EECS, WSU

7 Stack ADT Stack = a list where insert and remove take place only at the top Operations Push (insert) element on top of stack Pop (remove) element from top of stack Top: return element at top of stack LIFO (Last In First Out) 7Cpt S 223. School of EECS, WSU

8 Queue ADT Queue = a list where insert takes place at the back, but remove takes place at the front Operations Enqueue (insert) element at the back of the queue Dequeue (remove and return) element from the front of the queue FIFO (First In First Out) frontback Enque here Dequeue here 8Cpt S 223. School of EECS, WSU

Implementation for basic ADTs Cpt S 223. School of EECS, WSU9

10 List ADT using Arrays Operations insert(X,k) : O(N) remove(k) : O(N) find(X) : O(N) findKth(k) : O(1) printList() : O(N) Read as order N (means that runtime is proportional to N) Read as order 1 (means that runtime is a constant – i.e., not dependent on N) A0A0 A1A1 A2A2 A3A3 … Cpt S 223. School of EECS, WSU

11 List ADT using Linked Lists Elements not stored in contiguous memory Nodes in list consist of data element and next pointer node data next null pointer Cpt S 223. School of EECS, WSU

12 Linked Lists Operations Insert(X,A) – O(1) Remove(A) – O(1) Cpt S 223. School of EECS, WSU

13 Linked Lists Operations find(X) – O(N) findKth(k) – O(N) printList() – O(N) Cpt S 223. School of EECS, WSU

14 Doubly-Linked List Singly-linked list insert(X,A) and remove(X) require pointer to node just before X Doubly-linked list Also keep pointer to previous node prev next Cpt S 223. School of EECS, WSU

15 Doubly-Linked List Insert(X,A) Remove(X) Problems with operations at ends of list newA = new Node(A); newA->prev = X->prev; newA->next = X; X->prev->next = newA; X->prev = newA; X->prev->next = X->next; X->next->prev = X->prev; Cpt S 223. School of EECS, WSU

16 Sentinel Nodes Dummy head and tail nodes to avoid special cases at ends of list Doubly-linked list with sentinel nodes Empty doubly-linked list with sentinel nodes Cpt S 223. School of EECS, WSU

17 C++ Standard Template Library (STL) Implementation of common data structures List, stack, queue, … Generally called containers WWW references for STL Cpt S 223. School of EECS, WSU

18 Implementing Lists using STL vector Array-based implementation findKth – O(1) insert and remove – O(N) Unless change at end of vector list Doubly-linked list with sentinel nodes findKth – O(N) insert and remove – O(1) If position of change is known Both require O(N) for search 18Cpt S 223. School of EECS, WSU

19 Container Methods int size() const Return number of elements in container void clear() Remove all elements from container bool empty() Return true is container has no elements, otherwise returns false 19Cpt S 223. School of EECS, WSU

20 Vector and List Methods void push_back (const Object & x) Add x to end of list void pop_back () Remove object at end of list const Object & back () const Return object at end of list const Object & front () const Return object at front of list 20Cpt S 223. School of EECS, WSU

21 List-only Methods void push_front (const Object & x) Add x to front of list void pop_front () Remove object at front of list 21Cpt S 223. School of EECS, WSU

22 Vector-only Methods Object & operator[] (int idx) Return object at index idx in vector with no bounds-checking Object & at (int idx) Return object at index idx in vector with bounds- checking int capacity () const Return internal capacity of vector void reserve (int newCapacity) Set new capacity for vector (avoid expansion) 22Cpt S 223. School of EECS, WSU

23 Iterators Represents position in container Getting an iterator iterator begin () Return appropriate iterator representing first item in container iterator end () Return appropriate iterator representing end marker in container Position after last item in container 23Cpt S 223. School of EECS, WSU

24 Iterator Methods itr++ and ++itr Advance iterator itr to next location *itr Return reference to object stored at iterator itr s location itr1 == itr2 Return true if itr1 and itr2 refer to same location; otherwise return false itr1 != itr2 Return true if itr1 and itr2 refer to different locations; otherwise return false 24Cpt S 223. School of EECS, WSU

25 Example: printList 25 template void printList (const Container & lst) { for (typename Container::const_iterator itr = lst.begin(); itr != lst.end(); ++itr) { cout << *itr << endl; } Cpt S 223. School of EECS, WSU

26 Constant Iterators iterator begin () const_iterator begin () const iterator end () const_iterator end () const Appropriate version above returned based on whether container is const If const_iterator used, then *itr cannot appear on left-hand side of assignment (e.g., *itr=0 ) 26Cpt S 223. School of EECS, WSU

27 Better printList 27Cpt S 223. School of EECS, WSU

28 Container Operations Requiring Iterators iterator insert (iterator pos, const Object & x) Add x into list, prior to position given by iterator pos Return iterator representing position of inserted item O(1) for lists, O(N) for vectors iterator erase (iterator pos) Remove object whose position is given by iterator pos Return iterator representing position of item following pos This operation invalidates pos O(1) for lists, O(N) for vectors iterator erase (iterator start, iterator end) Remove all items beginning at position start, up to, but not including end 28Cpt S 223. School of EECS, WSU

29 Implementation of Vector 29 copy constructor destructor operator= constructor Cpt S 223. School of EECS, WSU

30 Implementation of Vector 30 Automatic resize Cpt S 223. School of EECS, WSU

31 Implementation of Vector 31 Iterator methods Iterators (implemented using simple pointers) Cpt S 223. School of EECS, WSU

32 Implementation of List 32 Iterators implemented using nested class Cpt S 223. School of EECS, WSU

33 Implementation of List 33Cpt S 223. School of EECS, WSU

34 Implementation of List 34 Empty list Cpt S 223. School of EECS, WSU

35 Implementation of List 35 Allows inheriting classes to access these. Gives List class access to constructor. Cpt S 223. School of EECS, WSU

36 Implementation of List 36 Allows inheriting classes to access these. Gives List class access to constructor. Note: there is no const here Cpt S 223. School of EECS, WSU

37 Implementation of List 37Cpt S 223. School of EECS, WSU

38 Implementation of List 38Cpt S 223. School of EECS, WSU

39 Implementation of List 39Cpt S 223. School of EECS, WSU

40 Stack ADT Stack is a list where insert and remove take place only at the top Operations Push (insert) element on top of stack Pop (remove) element from top of stack Top: return element at top of stack LIFO (Last In First Out) Cpt S 223. School of EECS, WSU

41 Stack Implementation template class stack { public: stack () {} void push (Object & x) { s.push_front (x); } void pop () { s.pop_front (); } Object & top () { s.front (); } private: list s; } Linked ListVector template class stack { public: stack () {} void push (Object & x) { s.push_back (x); } void pop () { s.pop_back (); } Object & top () { s.back (); } private: vector s; } ? ? ? ? ? ? Cpt S 223. School of EECS, WSU

42 Stack Implementation template class stack { public: stack () {} void push (Object & x) { s.push_front (x); } void pop () { s.pop_front (); } Object & top () { s.front (); } private: list s; } Linked ListVector template class stack { public: stack () {} void push (Object & x) { s.push_back (x); } void pop () { s.pop_back (); } Object & top () { s.back (); } private: vector s; } ? ? ? 42Cpt S 223. School of EECS, WSU

43 Stack Implementation template class stack { public: stack () {} void push (Object & x) { s.push_front (x); } void pop () { s.pop_front (); } Object & top () { s.front (); } private: list s; } Linked ListVector template class stack { public: stack () {} void push (Object & x) { s.push_back (x); } void pop () { s.pop_back (); } Object & top () { s.back (); } private: vector s; } Running Times ? 43 Cpt S 223. School of EECS, WSU

44 C++ STL Stack Class Methods Push, pop, top Empty, size #include stack s; for (int i = 0; i < 5; i++ ) { s.push(i); } while (!s.empty()) { cout << s.top() << endl; s.pop(); } Cpt S 223. School of EECS, WSU

45 Stack Applications Balancing symbols: ((()())(())) stack s; while not end of file { read character c if c = ( then s.push(c) if c = ) then if s.empty() then error else s.pop() } if (! s.empty()) then error else okay Cpt S 223. School of EECS, WSU How does this work?

46 Stack Applications Postfix expressions 1 2 * * + Means ((1 * 2) + 3) + (4 * 5) HP calculators Unambiguous (no need for paranthesis) Infix needs paranthesis or else implicit precedence specification to avoid ambiguity E.g., try a+(b*c) and (a+b)*c Postfix evaluation uses stack Class PostFixCalculator { public:... void Multiply () { int i1 = s.top(); s.pop(); int i2 = s.top(); s.pop(); s.push (i1 * i2); } private: stack s; } Cpt S 223. School of EECS, WSU

47 Stack Applications Function calls Programming languages use stacks to keep track of function calls When a function call occurs Push CPU registers and program counter on to stack (activation record or stack frame) Upon return, restore registers and program counter from top stack frame and pop Cpt S 223. School of EECS, WSU

48 Queue ADT Queue is a list where insert takes place at the back, but remove takes place at the front Operations Enqueue (insert) element at the back of the queue Dequeue (remove and return) element from the front of the queue FIFO (First In First Out) frontback Enque here Dequeue here Cpt S 223. School of EECS, WSU

49 Queue Implementation template class queue { public: queue () {} void enqueue (Object & x) { q.push_back (x); } Object & dequeue () { Object & x = q.front (); q.pop_front (); return x; } private: list q; } Linked List How would the runtime change if vector is used in implementation? Running time ? Cpt S 223. School of EECS, WSU

50 C++ STL Queue Class Methods Push (at back) Pop (from front) Back, front Empty, size #include queue q; for (int i = 0; i < 5; i++ ) { q.push(i); } while (!q.empty()) { cout << q.front() << endl; q.pop(); } Cpt S 223. School of EECS, WSU

51 Queue Applications Job scheduling Graph traversals Queuing theory 51Cpt S 223. School of EECS, WSU

52 Summary Abstract Data Types (ADTs) Linked list Stack Queue C++ Standard Template Library (STL) Numerous applications Building blocks for more complex data structures Cpt S 223. School of EECS, WSU