More Data Structures (Part 1) Stacks

Similar documents
from inheritance onwards but no GUI programming expect to see an inheritance question, recursion questions, data structure questions

csci 210: Data Structures Stacks and Queues

ITI Introduction to Computing II

Stacks and Queues. David Greenstein Monta Vista

Data Abstraction and Specification of ADTs

C Sc 127B Practice Test 2 Section Leader Your Name 100pts

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

Queues Fall 2018 Margaret Reid-Miller

CS24 Week 4 Lecture 2

CMSC 132, Object-Oriented Programming II Summer Lecture 9:

Precept 2: Data structures, Searching, and Sorting. Qian Zhu Feb 8, 2011

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

Stack and Queue. Stack:

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

Outline. Example stack Version 1 -- int stack with fixed array Version 2 -- int stack with flexible array Version 3 -- with interface

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

infix expressions (review)

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

Queues. Stacks and Queues

CSE 373 Data Structures and Algorithms. Lecture 2: Queues

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

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

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

Data Structure. Recitation VII

Queue ADT. January 31, 2018 Cinda Heeren / Geoffrey Tien 1

1. Introduction. Lecture Content

A queue is a linear collection whose elements are added on one end and removed from the other

1.00 Lecture 26. Data Structures: Introduction Stacks. Reading for next time: Big Java: Data Structures

1. Stack Implementation Using 1D Array

Stacks (5.1) Abstract Data Types (ADTs) CSE 2011 Winter 2011

CS350: Data Structures Stacks

CSC 1052 Algorithms & Data Structures II: Queues

1/22/13. Queue? CS 200 Algorithms and Data Structures. Implementing Queue Comparison implementations. Photo by David Jump 9. Colorado State University

1/22/13. Queue. Create an empty queue Determine whether a queue is empty Add a new item to the queue

Announcements Queues. Queues

Basic Data Structures

Name Section Number. CS210 Exam #2 *** PLEASE TURN OFF ALL CELL PHONES*** Practice

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

Queue? Part 4. Queues. Photo by David Jump. Create an empty queue Items leave a queue from its front.

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12

Linked Structures. See Section 3.2 of the text.

IT 4043 Data Structures and Algorithms. Budditha Hettige Department of Computer Science

Queues. CITS2200 Data Structures and Algorithms. Topic 5

Basic Data Structures 1 / 24

Stacks Fall 2018 Margaret Reid-Miller

CSE 143 SAMPLE MIDTERM

push(d), push(h), pop(), push(f), push(s), pop(), pop(), push(m).

Midterm Exam 2 CS 455, Spring 2011

CSC 273 Data Structures

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

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

Stacks Goodrich, Tamassia, Goldwasser Stacks

Stacks. Access to other items in the stack is not allowed A LIFO (Last In First Out) data structure

EXAMINATIONS 2012 Trimester 1, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

Lists. CSC212 Lecture 8 D. Thiebaut, Fall 2014

Interfaces & Generics

PA3 Design Specification

Lecture Data Structure Stack

Data Structures Lecture 5

Building Java Programs

n Data structures that reflect a temporal relationship q order of removal based on order of insertion n We will consider:

Priority Queue ADT. Revised based on textbook author s notes.

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

Adam Blank Lecture 1 Winter 2017 CSE 332. Data Abstractions

STACKS AND QUEUES. Problem Solving with Computers-II

Stacks. Stacks. Main stack operations. The ADT Stack stores arbitrary objects. Insertions and deletions follow the last-in first-out (LIFO) principle.

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

ADTs: Stacks and Queues

INFO1x05 Tutorial 6. Exercise 1: Heaps and Priority Queues

Introduction to Computer Science II (CSI 1101) Final Examination

Introduction to Computer Science II (ITI 1121) Final Examination

Data Structures G5029

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

CS 216 Exam 1 Fall SOLUTION

More Group HW. #ifndef Stackh #define Stackh. #include <cstdlib> using namespace std;

Queues. Queue ADT Queue Implementation Priority Queues

Associate Professor Dr. Raed Ibraheem Hamed

Lecture 3: Stacks & Queues

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

BlockingArrayQueue.java. Page 1

EXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

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

Chapter 18: Stacks And Queues

Stacks and Queues. Introduction to abstract data types (ADTs) Stack ADT. Queue ADT. Time permitting: additional Comparator example

Chapter 2. Stack & Queues. M hiwa ahmad aziz

EXAMINATIONS 2016 TRIMESTER 2

CMPSCI 187: Programming With Data Structures. Lecture #11: Implementing Stacks With Arrays David Mix Barrington 28 September 2012

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

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

CS200: Queues. Prichard Ch. 8. CS200 - Queues 1

10/26/2017 CHAPTER 3 & 4. Stacks & Queues. The Collection Framework

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

WITH SOLUTIONS!!! WARNING: THIS COPY CONTAINS SOLUTIONS!!! COMP103 Introduction to Data Structures and Algorithms

Model Solutions. COMP 103: Test April, 2013

Where does the insert method place the new entry in the array? Assume array indexing starts from 0(zero).

CSC148 Week 2. Larry Zhang

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue

CE204 Data Structures and Algorithms Part 2

CSE 143. Lecture 4: Stacks and Queues

Queue rear tail front head FIFO

Transcription:

More Data Structures (Part 1) Stacks 1

Stack examples of stacks 2

Top of Stack top of the stack 3

Stack Operations classically, stacks only support two operations 1. push 2. pop add to the top of the stack remove from the top of the stack throws an exception if there is nothing on the stack 4

Push 1. st.push("a") 2. st.push("b") 3. st.push("c") 4. st.push("d") 5. st.push("e") top top top top top "E" "D" "C" "B" "A" 5

Pop 1. String s = st.pop() 2. s = st.pop() 3. s = st.pop() 4. s = st.pop() 5. s = st.pop() top top top top top "E" "D" "C" "B" "A" 6

Applications stacks are used widely in computer science and computer engineering undo/redo widely used in parsing a call stack is used to store information about the active methods in a Java program convert a recursive method into a non-recursive one 7

Example: Reversing a sequence a silly and usually inefficient way to reverse a sequence is to use a stack 8

Don't do this public static <E> List<E> reverse(list<e> t) { List<E> result = new ArrayList<E>(); Stack<E> st = new Stack<E>(); for (E e : t) { st.push(e); } while (!st.isempty()) { result.add(st.pop()); } return result; } 9

Implementation with ArrayList ArrayList can be used to efficiently implement a stack the end of the list becomes the top of the stack adding and removing to the end of an ArrayList usually can be performed in O(1) time 10

public class Stack<E> { private ArrayList<E> stack; public Stack() { } this.stack = new ArrayList<E>(); public void push(e element) { } this.stack.add(element); } public E pop() { } return this.stack.remove(this.stack.size() - 1); 11

Implementation with Array the ArrayList version of stack hints at how to implement a stack using a plain array however, an array always holds a fixed number of elements you cannot add to the end of the array without creating a new array you cannot remove elements from the array without creating a new array instead of adding and removing from the end of the array, we need to keep track of which element of the array represents the current top of the stack we need a field for this index 12

import java.util.arrays; import java.util.emptystackexception; public class IntStack { // the initial capacity of the stack private static final int DEFAULT_CAPACITY = 16; // the array that stores the stack private int[] stack; // the index of the top of the stack (equal to -1 for an empty stack) private int topindex; 13

/** * Create an empty stack. */ public IntStack() { this.stack = new int[intstack.default_capacity]; this.topindex = -1; } 14

Implementation with Array IntStack t = new IntStack(); this.topindex == -1 this.stack index 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 15

Implementation with Array pushing a value onto the stack: increment this.topindex set the value at this.stack[this.topindex] 16

Implementation with Array IntStack t = new IntStack(); t.push(7); this.topindex == 0 this.stack index 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 17

Implementation with Array IntStack t = new IntStack(); t.push(7); t.push(-5); this.topindex == 1 this.stack index 7-5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 18

Implementation with Array popping a value from the stack: get the value at this.stack[this.topindex] decrement this.topindex return the value notice that we do not need to modify the value stored in the array 19

Implementation with Array IntStack t = new IntStack(); t.push(7); t.push(-5); int value = t.pop(); // value == -5 this.topindex == 0 this.stack index 7-5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 20

/** * Pop and return the top element of the stack. * * @return the top element of the stack * @throws EmptyStackException if the stack is empty */ public int pop() { // is the stack empty? if (this.topindex == -1) { throw new EmptyStackException(); } // get the element at the top of the stack int element = this.stack[this.topindex]; // adjust the top of stack index this.topindex--; } // return the element that was on the top of the stack return element; 21

Implementation with Array // stack state when we can safely do one more push this.topindex == 14 this.stack index 7-5 6 3 2 1 0 0 9-3 2 7 1-2 1 0 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 22

/** * Push an element onto the top of the stack. * * @param element the element to push onto the stack */ public void push(int element) { // is there capacity for one more element? if (this.topindex < this.stack.length - 1) { // increment the top of stack index and insert the element this.topindex++; this.stack[this.topindex] = element; } else { 23

Adding Capacity if we run out of capacity in the current array we need to add capacity by doing the following: make a new array with greater capacity how much more capacity? copy the old array into the new array set this.stack to refer to the new array push the element onto the stack 24

else { // make a new array with double previous capacity int[] newstack = new int[this.stack.length * 2]; // copy the old array into the new array for (int i = 0; i < this.stack.length; i++) { newstack[i] = this.stack[i]; } } } // refer to the new array and push the element onto the stack this.stack = newstack; this.push(element); 25

Adding Capacity when working with arrays, it is a common operation to have to create a new larger array when you run out of capacity in the existing array you should use Arrays.copyOf to create and copy an existing array into a new array 26

else { // make a new array with greater capacity int[] newstack = new int[this.stack.length * 2]; // copy the old array into the new array int[] newstack = Arrays.copyOf(this.stack, this.stack.length * 2); for (int i = 0; i < this.stack.length; i++) { newstack[i] = this.stack[i]; } } } // refer to the new array and push the element onto the stack this.stack = newstack; this.push(element); 27

More Data Structures (Part 2) Queues 28

Queue 29

Queue front back 30

Queue Operations classically, queues only support two operations 1. enqueue add to the back of the queue 2. dequeue remove from the front of the queue 31

Queue Optional Operations optional operations 1. size 2. isempty 3. peek 4. search 5. isfull 6. capacity number of elements in the queue is the queue empty? get the front element (without removing it) find the position of the element in the queue is the queue full? (for queues with finite capacity) total number of elements the queue can hold (for queues with finite capacity) 32

Enqueue 1. q.enqueue("a") 2. q.enqueue("b") 3. q.enqueue("c") 4. q.enqueue("d") 5. q.enqueue("e") A B C D E F B B B B B B 33

Dequeue 1. String s = q.dequeue() A B C D E F B 34

Dequeue 1. String s = q.dequeue() 2. s = q.dequeue() B C D E F B 35

Dequeue 1. String s = q.dequeue() 2. s = q.dequeue() 3. s = q.dequeue() C D E F B 36

Dequeue 1. String s = q.dequeue() 2. s = q.dequeue() 3. s = q.dequeue() 4. s = q.dequeue() D E F B 37

Dequeue 1. String s = q.dequeue() 2. s = q.dequeue() 3. s = q.dequeue() 4. s = q.dequeue() 5. s = q.dequeue() E F B 38

FIFO queue is a First-In-First-Out (FIFO) data structure the first element enqueued in the queue is the first element that can be accessed from the queue 39

Implementation with LinkedList a linked list can be used to efficiently implement a queue as long as the linked list keeps a reference to the last node in the list required for enqueue the head of the list becomes the front of the queue removing (dequeue) from the head of a linked list requires O(1) time adding (enqueue) to the end of a linked list requires O(1) time if a reference to the last node is available java.util.linkedlist is a doubly linked list that holds a reference to the last node 40

public class Queue<E> { private LinkedList<E> q; public Queue() { } this.q = new LinkedList<E>(); public enqueue(e element) { } this.q.addlast(element); } public E dequeue() { } return this.q.removefirst(); 41

Implementation with LinkedList note that there is no need to implement your own queue as there is an existing interface the interface does not use the names enqueue and dequeue however 42

java.util.queue public interface Queue<E> extends Collection<E> boolean add(e e) Inserts the specified element into this queue... E remove() Retrieves and removes the head of this queue... E peek() plus other methods Retrieves, but does not remove, the head of this queue... http://docs.oracle.com/javase/7/docs/api/java/util/queue. html 43

java.util.queue LinkedList implements Queue so if you ever need a queue you can simply use: e.g. for a queue of strings Queue<String> q = new LinkedList<String>(); 44