Cpt S 223. School of EECS, WSU



Similar documents
Queue Implementations

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

Abstract Data Types. Chapter 2

Chapter 3: Restricted Structures Page 1

CSI33 Data Structures

Linked Lists Linked Lists, Queues, and Stacks

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

DATA STRUCTURES USING C

Common Data Structures

Integrating the C++ Standard Template Library Into the Undergraduate Computer Science Curriculum

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

Data Structures and Algorithms

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Data Structures and Algorithms Stacks and Queues

Stacks. Stacks (and Queues) Stacks. q Stack: what is it? q ADT. q Applications. q Implementation(s) CSCU9A3 1

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Linda Shapiro Spring 2016

Module 2 Stacks and Queues: Abstract Data Types

This lecture. Abstract data types Stacks Queues. ADTs, Stacks, Queues Goodrich, Tamassia

Answers to Review Questions Chapter 7

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Last not not Last Last Next! Next! Line Line Forms Forms Here Here Last In, First Out Last In, First Out not Last Next! Call stack: Worst line ever!

recursion, O(n), linked lists 6/14

Sequences in the C++ STL

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

What is a Stack? Stacks and Queues. Stack Abstract Data Type. Java Interface for Stack ADT. Array-based Implementation

Analysis of a Search Algorithm

Universidad Carlos III de Madrid

Application of Stacks: Postfix Expressions Calculator (cont d.)

Linked Lists: Implementation Sequences in the C++ STL

Cours de C++ Utilisations des conteneurs

7.1 Our Current Model

Data Structures and Algorithms V Otávio Braga

DATA STRUCTURE - QUEUE

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Outline. Computer Science 331. Stack ADT. Definition of a Stack ADT. Stacks. Parenthesis Matching. Mike Jacobson

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

QUEUES. Primitive Queue operations. enqueue (q, x): inserts item x at the rear of the queue q

Data Structures and Algorithms Lists

PROGRAMMING CONCEPTS AND EMBEDDED PROGRAMMING IN C, C++ and JAVA: Lesson-4: Data Structures: Stacks

Queues Outline and Required Reading: Queues ( 4.2 except 4.2.4) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Outline. The Stack ADT Applications of Stacks Array-based implementation Growable array-based stack. Stacks 2

Data Structures, Practice Homework 2, with Solutions (not to be handed in)

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

DATA STRUCTURE - STACK

Algorithms and Data S tructures Structures Stack, Queues, and Applications Applications Ulf Leser

Introduction to Data Structures and Algorithms

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

Data Structures Using C++ 2E. Chapter 5 Linked Lists

22c:31 Algorithms. Ch3: Data Structures. Hantao Zhang Computer Science Department

Algorithms and Data Structures

C++FA 3.1 OPTIMIZING C++

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

LINKED DATA STRUCTURES

C++ INTERVIEW QUESTIONS

Java Software Structures

Cpt S 223. School of EECS, WSU

Queues and Stacks. Atul Prakash Downey: Chapter 15 and 16

STACKS,QUEUES, AND LINKED LISTS

CmpSci 187: Programming with Data Structures Spring 2015

Stack & Queue. Darshan Institute of Engineering & Technology. Explain Array in detail. Row major matrix No of Columns = m = u2 b2 + 1

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

Course: Programming II - Abstract Data Types. The ADT Queue. (Bobby, Joe, Sue, Ellen) Add(Ellen) Delete( ) The ADT Queues Slide Number 1

Curriculum Map. Discipline: Computer Science Course: C++

Node-Based Structures Linked Lists: Implementation

13 Classes & Objects with Constructors/Destructors


Stacks. Linear data structures

! stack, queue, priority queue, dictionary, sequence, set e.g., a Stack is a list implements a LIFO policy on additions/deletions.

Abstract Data Type. EECS 281: Data Structures and Algorithms. The Foundation: Data Structures and Abstract Data Types

Linear ADTs. Restricted Lists. Stacks, Queues. ES 103: Data Structures and Algorithms 2012 Instructor Dr Atul Gupta

St S a t ck a ck nd Qu Q eue 1

Glossary of Object Oriented Terms

Introduction to Data Structures

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

CompuScholar, Inc. Alignment to Utah's Computer Programming II Standards

Moving from CS 61A Scheme to CS 61B Java

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

Data Structures and Algorithms

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Data Structure [Question Bank]

Data Structures Using C++

C++FA 5.1 PRACTICE MID-TERM EXAM

Sequential Data Structures

10CS35: Data Structures Using C

Data Structures and Data Manipulation

Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

COMPUTER SCIENCE. Paper 1 (THEORY)

Algorithms and Data Structures

Stacks. Data Structures and Data Types. Collections

PES Institute of Technology-BSC QUESTION BANK

Data Structures In Java

CS107L Handout 04 Autumn 2007 October 19, 2007 Custom STL-Like Containers and Iterators

Unordered Linked Lists

Transcription:

Abstract Data Types 1

Topics Abstract Data Types (ADTs) Some basic ADTs: Lists Stacks Queues 2

Primitive Data Type vs. Abstract Data Types Primitive DT: ADT: programmer progra ammer Interface (API) e.g., int, float Implementation (methods) Data 3

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 4

Specifications of basic ADTs List, Stack, Queue 5

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() 6

Stack ADT Stack = a list where insert and remove take place only at the top Operations Push (insert) element e e on top of stack Pop (remove) element from top of stack Top: return element at top of stack LIFO (Last In First Out) 7

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) Dequeue here 5 7 2 6 3 2 8 Enque here front back 8

Implementation ti for basic ADTs 9

List ADT using Arrays A 0 A 1 A 2 A 3 Operations Read as order N insert(x,k) : O(N) remove(k) : O(N) find(x) : O(N) findkth(k) (): O(1) printlist() : O(N) (means that t runtime is proportional to N) Read as order 1 (means that runtime is a constant i.e., not dependent on N) 10

List ADT using Linked Lists Elements not stored in contiguous memory Nodes in list consist of data element and next pointer node data next pointer null 11

Linked Lists Operations Insert(X,A) O(1) Remove(A) O(1) 12

Linked Lists Operations find(x) O(N) findkth(k) O(N) printlist() O(N) 13

Doubly-Linked List Singly-linked list insert(x,a) and remove(x) require pointer to node just before X Doubly-linked linked list Also keep pointer to previous node prev next 14

Doubly-Linked List Insert(X,A) Remove(X) 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; Problems with operations at ends of list 15

Sentinel Nodes Dummy head and tail nodes to avoid special cases at ends of list Doubly-linked linked list with sentinel nodes Empty doubly-linked li d list with sentinel nodes 16

C++ Standard Template Library (STL) Implementation of common data structures List, stack, queue, Generally called containers WWW references for STL www.sgi.com/tech/stl/ http://www.cplusplus.com/reference/stl/ www.cppreference.com/cppstl.html l 17

Implementing Lists using STL vector<object> Array-based implementation findkth O(1) insert and remove O(N) Unless change at end of vector list<object> 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 18

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 19

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 20

List-only Methods void push_front (const Object & x) Add x to front of list void pop_front () Remove object at front of list 21

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 boundschecking int capacity () const Return internal capacity of vector void reserve (int newcapacity) Set new capacity for vector (avoid expansion) 22

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 23

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 24

Example: printlist template <typename Container> void printlist (const Container & lst) { for (typename Container::const_iterator itr = lst.begin(); itr!= lst.end(); ++itr) { cout << *itr << endl; } } 25

Constant Iterators iterator begin () const_ iterator begin () const iterator end () const st_ 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 ) 26

Better printlist 27

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 28

Implementation of Vector constructor copy constructor destructor operator= 29

Implementation of Vector Automatic resize 30

Implementation of Vector Iterators (implemented using simple pointers) Iterator methods 31

Implementation of List Iterators implemented using nested class 32

Implementation of List 33

Implementation of List Empty list 34

Implementation of List Allows inheriting classes to access these. Gives List class access to constructor. 35

Implementation of List Allows inheriting classes to access these. Note: there is no const here Gives List class access to constructor. 36

Implementation of List 37

Implementation of List 38

Implementation of List 39

Stack ADT Stack is a list where insert and remove take place only at the top Operations Push (insert) element e e on top of stack Pop (remove) element from top of stack Top: return element at top of stack LIFO (Last In First Out) 40

Stack Implementation Linked List Vector template <typename Object> class stack { public: stack () {} void push (Object & x) {? s.push_front (x); } void pop () { s.pop_front (); } Object & top () { s.front (); } private: list<object> s; }???? template <typename Object> class stack { public: stack () {} void push (Object & x) { s.push_back? (x); } void pop () { s.pop_back (); } Object & top () { s.back (); } private: vector<object> s; } 41

Stack Implementation Linked List template <typename Object> class stack { public: stack () {} void push (Object & x) {? s.push_front (x); } void pop () {? s.pop_front (); } Object & top () {? s.front (); } private: list<object> s; } Vector template <typename Object> class stack { public: stack () {} void push (Object & x) { s.push_back (x); } void pop () { s.pop_back (); } Object & top () { s.back (); } private: vector<object> s; } 42

Running Times? Stack Implementation Linked List template <typename Object> class stack { public: stack () {} void push (Object & x) { s.push_front (x); } void pop () { s.pop_front (); } Object & top () { s.front (); } private: list<object> s; } Vector template <typename Object> class stack { public: stack () {} void push (Object & x) { s.push_back (x); } void pop () { s.pop_back (); } Object & top () { s.back (); } private: vector<object> s; } 43

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

Stack Applications Balancing symbols: ((()())(())) stack<char> s; How does this work? 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 45

Stack Applications Postfix expressions 1 2 * 3 + 4 5 * + Means ((1 * 2) + 3) + (4 * 5) HP calculators Unambiguous (no need for paranthesis) Infix needs paranthesis or else implicit precedence specification to avoid ambiguity Eg E.g., trya+(b*c)and 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<int> s; 46

Stack Applications Function calls Programming g languages g 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 47

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) Dequeue here 5 7 2 6 3 2 8 Enque here front back 48

Queue Implementation Linked List template <typename Object> 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<object> q; } How would the runtime change if vector is used in implementation? Running time? 49

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

Queue Applications Job scheduling Graph traversals Queuing theory 51

Summary Abstract Data Types (ADTs) Linked list Stack Queue C++ Standard Template Library (STL) Numerous applications Building blocks for more complex data structures 52