Abstract Data Types. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Similar documents
Abstract Data Types 1

Abstract Data Types 1

Lists, Stacks, and Queues. (Lists, Stacks, and Queues ) Data Structures and Programming Spring / 50

List, Stack, and Queues

Queue Implementations

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

Purpose of Review. Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

Programming Abstractions

Dynamic Data Structures

Containers: Queue and List. Jordi Cortadella and Jordi Petit Department of Computer Science

Chapter 18: Stacks And Queues

the Stack stack ADT using the STL stack are parentheses balanced? algorithm uses a stack adapting the STL vector class adapting the STL list class

STACKS AND QUEUES. Problem Solving with Computers-II

Chapter 17: Linked Lists

CS350 - Exam 1 (100 Points)

Cpt S 122 Data Structures. Course Review FINAL. Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University

1 P age DS & OOPS / UNIT II

Top of the Stack. Stack ADT

Top of the Stack. Stack ADT

Chapter 17: Linked Lists

IV. Stacks. A. Introduction 1. Consider the 4 problems on pp (1) Model the discard pile in a card game. (2) Model a railroad switching yard

The Stack and Queue Types

CPSC 260 Data Structures and Algorithms for Computer Engineers Linked Lists!

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

DATA STRUCTURES AND ALGORITHMS LECTURE 08 QUEUES IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD

Containers: Stack. Jordi Cortadella and Jordi Petit Department of Computer Science

Chapter 18: Stacks And Queues

Standard Template Library

DataStruct 5. Stacks, Queues, and Deques

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

Containers: Stack. The Stack ADT. The Stack ADT. The Stack ADT

ADTs: Stacks and Queues

Chapter 18: Stacks And Queues. Pearson Education, Inc. Publishing as Pearson Addison-Wesley

CSC 222: Computer Programming II. Spring 2004

PA3 Design Specification

DHANALAKSHMI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING EC6301 OBJECT ORIENTED PROGRAMMING AND DATA STRUCTURES

1 Short Answer (7 Points Each)

Stacks and their Applications

CSCI-1200 Computer Science II Spring 2006 Test 3 Practice Problem Solutions

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

! A data type for which: ! In fact, an ADT may be implemented by various. ! Examples:

Programming Abstractions

Largest Online Community of VU Students

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

Stacks, Queues (cont d)

8. Fundamental Data Structures

A linear structure is an ordered (e.g., sequenced) arrangement of elements.

Doubly-Linked Lists

Unit 4 Basic Collections

CS24 Week 4 Lecture 2

Ch. 18: ADTs: Stacks and Queues. Abstract Data Type


811312A Data Structures and Algorithms, , Exercise 1 Solutions

Sequential Containers Cont'd

CMSC 341. Deques, Stacks and Queues 9/22/04 1

16. Dynamic Data Structures

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

CHAPTER 3 STACKS AND QUEUES. Iris Hui-Ru Jiang Fall 2008

Programming. Lists, Stacks, Queues

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

Name: 1 stack<int> s; 2 s.push(9) 3 s.push(5) 4 s.push(10) 5 s.push(1) 6 s.pop() 7 s.pop() 8 s.push(0) 9 s.pop() 10 s.pop() 11 s.

Templates and Vectors

CS 216 Exam 1 Fall SOLUTION

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

EEE2020 Data Structures and Algorithms Abstract Data Types: Stacks and Queues

CMSC 341. Linked Lists, Stacks and Queues. CMSC 341 Lists, Stacks &Queues 1

12 Abstract Data Types

CS 106B Lecture 5: Stacks and Queues

Object Oriented Programming COP3330 / CGS5409

Loop Invariants. while!done do // what is true at every step // Update/iterate // maintain invariant od CPS

Largest Online Community of VU Students

CSCI-1200 Data Structures Fall 2017 Lecture 10 Vector Iterators & Linked Lists

The Stack ADT. Stacks. The Stack ADT. The Stack ADT. Set of objects in which the location an item is inserted and deleted is prespecified.

CSCI-1200 Data Structures Fall 2010 Lecture 8 Iterators

Sequential Containers Cont'd

(8 1) Container Classes & Class Templates D & D Chapter 18. Instructor - Andrew S. O Fallon CptS 122 (October 8, 2018) Washington State University

CSCI-1200 Data Structures Fall 2014 Lecture 8 Iterators

CS197c: Programming in C++

DATA STRUCTURE UNIT I

How can we improve this? Queues 6. Topological ordering: given a sequence of. should occur prior to b, provide a schedule. Queues 5.

DATA STRUCUTRES. A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.

Programming Abstractions

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

Solution for Data Structure

Lecture Data Structure Stack

CS 206 Introduction to Computer Science II

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

Introduction p. 1 Pseudocode p. 2 Algorithm Header p. 2 Purpose, Conditions, and Return p. 3 Statement Numbers p. 4 Variables p. 4 Algorithm Analysis

list<t>::const_iterator it1 = lst.begin(); // points to first element list<t>::const_iterator it2 = lst.begin(); // points to second element it2++;

Linked List using a Sentinel

DNHI Homework 3 Solutions List, Stacs and Queues

the Queue queue ADT using the STL queue designing the simulation simulation with STL queue using STL list as queue using STL vector as queue

Data Structures and Algorithms

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 )

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Data Structures and Algorithms. Final Examination

CIS Fall Data Structures Midterm exam 10/16/2012

Transcription:

Abstract Data Types CptS 223 Advanced Data Structures Larry Holder School of Electrical Engineering and Computer Science Washington State University 1

Purpose Abstract Data Types (ADTs) Lists Stacks Queues 2

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++ class perfect for ADTs Can change ADT implementation details without breaking code using ADT 3

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

Lists Using Arrays Simple array vs. Vector class Estimating maximum size Operations insert(x,k) O(N) remove(k) O(N) find(x) O(N) findkth(k) O(1) printlist() O(N) 5

Linked Lists Elements not stored in contiguous memory Nodes in list consist of data element and next pointer node data next pointer null 6

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

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

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 9

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 10

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 11

C++ Standard Template Library (STL) Implementation of common data structures List, stack, queue, Generally called containers WWW resources www.sgi.com/tech/stl/ www.cppreference.com/cppstl.html 12

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 13

Container Methods int size() const Return number of elements in container void clear() Remove all elements from container bool empty() const Return true if container has no elements, otherwise return false 14

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 15

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

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) 17

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 18

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 19

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; } } 20

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 ) 21

Better printlist 22

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 23

Implementation of Vector copy constructor destructor operator= 24

Implementation of Vector Automatic resize 25

Implementation of Vector Iterators 26

Implementation of List Nested class 27

Implementation of List 28

Implementation of List Empty list 29

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

Implementation of List 31

Implementation of List 32

Implementation of List 33

Implementation of List 34

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) 35

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; } Running Times? 36

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(); } 37

Stack Applications Balancing symbols: ((()())(())) stack<char> 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 38

Stack Applications Postfix expressions ((1 * 2) + 3) + (4 * 5) = 1 2 * 3 + 4 5 * + HP calculators Infix to postfix conversion Output operands Push operators and left parentheses When right parenthesis, output operators up to left parenthesis 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; } 39

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 40

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 41

Queue Implementation Linked List Vector (?) 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; } Running time? 42

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(); } 43

Queue Applications Job scheduling Priority queues Graph traversals Queuing theory 44

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