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



Similar documents
STACKS,QUEUES, AND LINKED LISTS

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

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

Universidad Carlos III de Madrid

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

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

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

1.00 Lecture 35. Data Structures: Introduction Stacks, Queues. Reading for next time: Big Java: Data Structures

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

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

Data Structures and Algorithms Stacks and Queues

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

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

Linked Lists Linked Lists, Queues, and Stacks

Sequential Data Structures

Programming with Data Structures

Analysis of a Search Algorithm

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!

Chapter 3: Restricted Structures Page 1

DATA STRUCTURE - STACK

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

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

Introduction to Data Structures and Algorithms

CmpSci 187: Programming with Data Structures Spring 2015

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

Common Data Structures

Unordered Linked Lists

Data Structures in the Java API

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

14 Stacks, Queues, And Linked Lists

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

Module 2 Stacks and Queues: Abstract Data Types

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

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

Introduction to Stacks

Data Structures and Algorithms Lists

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

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

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

ADTs,, Arrays, Linked Lists

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

DATA STRUCTURE - QUEUE

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

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

Linked List as an ADT (cont d.)

Cpt S 223. School of EECS, WSU

DATA STRUCTURES USING C

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

Stacks and queues. Algorithms and Data Structures, Fall Rasmus Pagh. Based on slides by Kevin Wayne, Princeton

\Mankinds's progress is measured by the number of. Elementary data structures such as stacks, queues,

D06 PROGRAMMING with JAVA

Java Software Structures

Abstract Data Types. Chapter 2

LINKED DATA STRUCTURES

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

Data Structures and Algorithms

Algorithms and Data Structures Written Exam Proposed SOLUTION

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

Big O and Limits Abstract Data Types Data Structure Grand Tour.

7.1 Our Current Model

Lecture Notes on Stacks & Queues

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

Data Structures UNIT III. Model Question Answer

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

Data Structures and Data Manipulation

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

10CS35: Data Structures Using C

Basic Data Structures and Algorithms

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

Atmiya Infotech Pvt. Ltd. Data Structure. By Ajay Raiyani. Yogidham, Kalawad Road, Rajkot. Ph : ,

List, Stack and Queue. Tom Chao Zhou CSC2100B Data Structures Tutorial 3

EE2204 DATA STRUCTURES AND ALGORITHM (Common to EEE, EIE & ICE)

Lecture 12 Doubly Linked Lists (with Recursion)

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

Level 3 Develop software using Java (7266/ )

Queue Implementations

Monitors, Java, Threads and Processes

Singly linked lists (continued...)

Java Collection Framework hierarchy. What is Data Structure? Chapter 20 Lists, Stacks, Queues, and Priority Queues

Data Structures Using C++

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

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

Data Structure [Question Bank]

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

Algorithms and Data Structures

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

Timing of a Disk I/O Transfer

Algorithms and Data Structures

Chapter 8: Bags and Sets

PES Institute of Technology-BSC QUESTION BANK

Queues. Manolis Koubarakis. Data Structures and Programming Techniques

Sequences in the C++ STL

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

Transcription:

Stacks and Queues What is a Stack? Stores a set of elements in a particular order Accessed in Last-In In-First-Out (LIFO) fashion Real life examples: Pile of books PEZ dispenser Cup trays in cafeteria CS examples: program execution stack, parsing/evaluating expressions Stack Abstract Data Type push(o): insert o on top of stack pop( ): remove object from top of stack top( ): look at object on top of stack (but don t remove) size( ): number of objects on the stack isempty( ( ): does (size = = 0)? Java Interface for Stack ADT public interface Stack { public int size(); public boolean isempty(); public Object top() throws StackEmptyException; public void push (Object element); public Object pop() throws StackEmptyException; Array-based Stack Implementation Allocate array of some size Maximum # of elements in stack Bottom stack element stored at index 0 first index tells which element is the top increment first when element pushed, decrement when pop d Array-based Implementation public class ArrayStack implements Stack { private Object[] S; private int topindex = -1; public void push(object obj) ) throws StackFull { if (size() == S.length) throw new StackFull( full ); S[++topIndex] ] = obj; ; public Object pop() throws StackEmpty { if (isempty( isempty()) throw new StackEmpty( empty ); Object elem = S[topIndex]; S[topIndex-- --]] = null; return elem; ;

Analysis Each operation is O(1) running time Independent of number of items in stack push, pop, top, size, isempty Space can be O(n) ) or may be much more depends if n is known at initialization time Linked List Stack Implementation Benefits Avoids maximum stack size restriction Only allocates memory for stack elements actually used How Allocate a node for each stack element Nodes are chained together by reference to next node in the chain Linked List Node public class Node { private Object element; private Node next; public Node(Object e, Node n) { element = e; next = n; Linked List Stack Implementation public class LinkedStack implements Stack { private Node top = null; private int size = 0; public void push(object elem) ) { Node v = new Node(); v.setelement(elem); v.setnext(top); top = v; public Object pop() throws StackEmpty { if (isempty( isempty()) throw new StackEmpty( empty ); Object temp = top.getelement(); top = top.getnext(); size --; return temp; Analysis All stack functions still O(1) push, pop, top, size, isempty What is a Queue Stores a set of elements in a particular order Accessed in First-In In-First-Out (FIFO) fashion Real life examples: Waiting in line at cafeteria Waiting on hold for technical support CS Example: Buffered I/O

Queue ADT Queue Interface in Java enqueue(o): Insert object o at rear of queue dequeue( ( ): remove object from front of queue size( ): number of elements isempty( ( ): size == 0? front( ): look at object at front of queue public interface Queue { public int size(); public boolean isempty(); public Object front() throws QueueEmpty; public void enqueue (Object element); public Object dequeue() throws QueueEmpty; Array-based Queue Implementation Array of fixed size Index array element for front and rear of queue Indices wrap around when they cross end of array Array Queue Implementation public class ArrayQueue implements Queue { private Object[] Q; private int size=0; private int front=0, rear = 0; public void enqueue(object o) { if (size( ) == Q.length) ) throw new QueueFull( full ); Q[rear] = o; rear = (rear + 1) % Q.length; List Queue Implementation List Queue Implementation Head and tail node references for front and rear of queue Insert at tail, remove from head Remove from tail too slow for singly linked list Updating tail reference with new tail takes full traversal So use tail of list for rear of queue public class ListQueue implements Queue { private Node head = null; private Node tail = null; private int size = 0;...

List Queue public void enqueue(object obj) ) { Node node = new Node(obj,, null); if (size == 0) head = node; else tail.setnext(node); tail = node; public Object dequeue() { Object obj = head.getelement(); head = head.getnext(); size-- --; if (size == 0) tail = null return obj; Analysis All queue operations are O(1) size( ), isempty( ( ) enqueue( ( ), dequeue( ( ), front( ) Double-ended ended Queue Sometimes called deque deque (dek( dek) Similar to stack and queue Allows insertion and removal at both ends of the queue Stack or queue is easily implemented using a deque Deque ADT insertfirst(e) ) : insert element at front insertlast(e) ) : insert element at rear removefirst( ( ) : remove first element removelast( ( ) : remove element at rear first( ) : examine first element last( ) : examine last element size(e) ) : number of elements in deque isempty( ( ) : size == 0? Doubly Linked List Singly linked list inefficient for removing from tail Has prev reference as well as next reference Can use sentinel nodes to reduce the special cases node has no element, just next or prev reference Deque Implementation public class MyDeque implements Deque { DLNode header, trailer; int size; public MyDeque() { header = new DLNode(); trailer = new DLNode(); header.setnext(trailer); trailer.setprev(header); size = 0;

Deque Implementation Deque (no sentinels) public void insertfirst(object o) { DLNode second = header.getnext(); DLNode first = new DLNode(o,header,, second); second.setprev(first); header.setnext(first); Could be null if no sentinels (trailer could also be null) public void insertfirst(object o) { DLNode second = header; DLNode first = new DLNode(o,, null, second); header = first; if (second == null) trailer = first; else second.setprev(first); Deque Analysis All operations still O(1) Doubly linked list nodes slightly larger more references to keep up to date