CS24 Week 4 Lecture 2

Similar documents
CS24 Week 4 Lecture 1

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. elements of the same type. Week 9. Gaddis: Chapter 18

! A data type for which: ! An ADT may be implemented using various. ! Examples:

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. Week 9. elements of the same type.

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

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. elements of the same type. Week 9. Gaddis: Chapter 18

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

COSC160: Data Structures: Lists and Queues. Jeremy Bolton, PhD Assistant Teaching Professor

ADTs: Stacks and Queues

Stacks. Gaddis 18.1, Molly A. O'Neil CS 2308 :: Spring 2016


Chapter 18: Stacks And Queues

A linear-list Data Structure where - addition of elements to and - removal of elements from are restricted to the first element of the list.

1 P age DS & OOPS / UNIT II

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

.:: UNIT 4 ::. STACK AND QUEUE

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

Chapter 18: Stacks And Queues

Abstract Data Types. Abstract Data Types

Lecture Data Structure Stack

CMSC Introduction to Algorithms Spring 2012 Lecture 7

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

Introduction to the C programming language

Introduction to the C programming language

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

#06 More Structures LIFO FIFO. Contents. Queue vs. Stack 3. Stack Operations. Pop. Push. Stack Queue Hash table

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

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

Stack and Queue. Stack:

CSE 373 SEPTEMBER 29 STACKS AND QUEUES

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Chapter 9 STACK, QUEUE

! Mon, May 5, 2:00PM to 4:30PM. ! Closed book, closed notes, clean desk. ! Comprehensive (covers entire course) ! 30% of your final grade

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

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

CS 206 Introduction to Computer Science II

CS24 Week 8 Lecture 1

Name CPTR246 Spring '17 (100 total points) Exam 3

Basic Data Structures

Common Misunderstandings from Exam 1 Material

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

Separate Compilation and Namespaces Week Fall. Computer Programming for Engineers

Stacks and Queues. CSE 373 Data Structures Lecture 6

Stacks and Queues. CSE Data Structures April 12, 2002

CS24 Week 3 Lecture 1

CMP Points Total Midterm Spring Version (16 Points) Multiple Choice:

More Data Structures (Part 1) Stacks

ITI Introduction to Computing II

How to Win Coding Competitions: Secrets of Champions. Week 2: Computational complexity. Linear data structures Lecture 5: Stack. Queue.

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

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

Lists, Stacks and Queues in C. CHAN Hou Pong, Ken CSCI2100A Data Structures Tutorial 4

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0)

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

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

CS 1114: Implementing Search. Last time. ! Graph traversal. ! Two types of todo lists: ! Prof. Graeme Bailey.

Announcements. mp3.1 extra credit due tomorrow lab quacks due Saturday night (6/29) mp3 due Monday (7/1)

STACKS AND QUEUES. Problem Solving with Computers-II

CS S-04 Stacks and Queues 1. An Abstract Data Type is a definition of a type based on the operations that can be performed on it.

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

Lecture 2: Stacks and Queues

Extra Credit: write mystrlen1 as a single function without the second parameter int mystrlen2(char* str)

CMSC 132: Object-Oriented Programming II. Stack and Queue

Data Structures -- Introduction

Arrays and Linked Lists

Keeping Order:! Stacks, Queues, & Deques. Travis W. Peters Dartmouth College - CS 10

Data Structures and Algorithms

An Introduction to Queues With Examples in C++

16-Dec-10. Collections & Data Structures ABSTRACTION VS. IMPLEMENTATION. Abstraction vs. Implementation

CMSC 341 Lecture 6 STL, Stacks, & Queues. Based on slides by Lupoli, Dixon & Gibson at UMBC

Programming Abstractions

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

Stacks and Queues

CS 216 Exam 1 Fall SOLUTION

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

CS 206 Introduction to Computer Science II

Basic Data Structures 1 / 24

CSEN 301 Data Structures and Algorithms

CMP Points Total Midterm Spring Version (16 Points) Multiple Choice:

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

csci 210: Data Structures Stacks and Queues

Lecture No.04. Data Structures

Lecture 7: Data Structures. EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

Queues. Gaddis 18.4, Molly A. O'Neil CS 2308 :: Spring 2016

Abstract Data Types. Stack. January 26, 2018 Cinda Heeren / Geoffrey Tien 1

Data Structures and Object-Oriented Design III. Spring 2014 Carola Wenk

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

12 Abstract Data Types

Data Structures And Algorithms

Abstract Data Types. Definitions, Implementations, and Uses

CS350: Data Structures Stacks

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

Object Oriented Programming COP3330 / CGS5409

Abstract Data Types 1

Linked List. April 2, 2007 Programming and Data Structure 1

All the above operations should be preferably implemented in O(1) time. End of the Queue. Front of the Queue. End Enqueue

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

UNIT-2 Stack & Queue

Stacks, Queues and Hierarchical Collections

Transcription:

CS24 Week 4 Lecture 2 Kyle Dewey

Overview Linked Lists Stacks Queues

Linked Lists

Linked Lists Idea: have each chunk (called a node) keep track of both a list element and another chunk Need to keep track of only the head node List: 0, 1, 2, 3 0 1 2 3 X

Node Representation What might a node look like in C? struct Node { int item; struct Node* next; };

Node Representation What might a node look like in C++? class Node { public: Node(int i, Node* n); int getitem() const; void setitem(int i); Node* getnext() const; void setnext(node* n); private: int item; Node* node; };

C++ Implementation of Linked Lists

Stacks

Stack Like a linked list, these hold items So named because items are stacked on top of each other - can only access from one end Last in, first out (LIFO) order http://eli.thegreenplace.net/2011/02/04/where-the-top-of-the-stack-is-on-x86/

The Stack We have previously discussed the stack Local allocation How does local allocation work with the stack? Why is this called the stack?

Stack ADT Stacks can only be accessed from one end What sort of operations are applicable?

Stack ADT Stacks can only be accessed from one end What sort of operations are applicable? Create an empty stack push an element on the stack pop an element off the stack Look at the top element without popping, often called top

Stack ADT Logical Level in C++ Create an empty stack push an element on the stack pop an element off the stack Look at the top element without popping, often called top or peek --Constructors? Methods? Signatures?--

Logical Level class Stack { public: Stack(); // constructor void push(int item); int pop(); int top() const; };

Implementation Level How might we implement the stack? class Stack { public: Stack(); // constructor void push(int item); int pop(); int top() const; };

Implementation Level Two popular choices: arrays and linked lists Arrays: grow from left to right Linked lists: add and remove from the head

Stacks with Linked Lists Linked lists tend to work better for stacks. Why? Hint: what is problematic with an array representation?

Stacks with Linked Lists Linked lists tend to work better for stacks. Why? Easily push by adding an element to the front Easily pop by removing an element from the front No embedded maximum size

Stacks with Linked Lists IntStack stack;??? head

Stacks with Linked Lists IntStack stack; X head

Stacks with Linked Lists IntStack stack; stack.push(3);??? head

Stacks with Linked Lists IntStack stack; stack.push(3); 3 X head

Stacks with Linked Lists IntStack stack; stack.push(3); stack.push(4);??? head

Stacks with Linked Lists IntStack stack; stack.push(3); stack.push(4); 4 3 X head

Stacks with Linked Lists IntStack stack; stack.push(3); stack.push(4); stack.pop();??? head

Stacks with Linked Lists IntStack stack; stack.push(3); stack.push(4); stack.pop(); 3 X head

Queues

Motivation A grocery store has one cashier Ten people want to checkout People form a line based on when they arrived at the cashier First in, first out (FIFO) order

Queue ADT Queues have a concept of two ends - the front and back What sort of operations might be on a queue ADT?

Queue ADT Queues have a concept of two ends - the front and back What sort of operations might be on a queue ADT? Adding to the queue, often called enqueue Removing from the queue, often called dequeue

Queue ADT Logical Level in C++ Create an empty queue enqueue an item dequeue an item --Constructors? Methods? Signatures?--

Logical Level class Queue { public: Queue(); // constructor }; void enqueue(int item); int dequeue();

Implementing Queues How might we implement a queue?

Implementing Queues How might we implement a queue? Arrays: keep track of the front and back of the queue (hard!) Linked lists: add to the front or back of the list

Linked List Implementation How is the queue represented? What happens on enqueue? What happens on dequeue?

dequeue dequeue removes from the front of the line, AKA the front of the list Before 4 3 X head After 3 X head

enqueue enqueue adds to the back of the line, AKA the back of the list Before 7 X head After enqueue(8) 7 X 8 head

enqueue vs. dequeue enqueue is less efficient than dequeue. Why?

enqueue vs. dequeue enqueue is less efficient than dequeue. Why? Via head, dequeue has direct access to the front of the line In contrast, enqueue must walk the entire line to get to the end

Addressing Efficiency Problem Without diverging too much from a standard linked list, how might we address this efficiency problem? Hint: head essentially teleports us to the front of the list

Addressing Efficiency Problem Without diverging too much from a standard linked list, how might we address this efficiency problem? Hint: head essentially teleports us to the front of the list We could add a special end pointer to the end of the list

After Change Before enqueue(10) 7 8 9 X head end After enqueue(10) - add directly to end 7 8 9 10 X head end

Questions For a queue of empty length, what is head and end? For a queue of length one, what is head and end?

Questions For a queue of empty length, what is head and end? NULL For a queue of length one, what is head and end? The same element

Extra Bookkeeping Need to ensure that end always points to the last element