Elementary Data Structures CS 110: Data Structures and Algorithms First Semester, 2010-2011.

Slides:



Advertisements
Similar presentations
Queues and Linked Lists
Advertisements

Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Data Structures and Algorithms Lecture (Queues) Instructor: Quratulain.
Today’s Agenda  Stacks  Queues  Priority Queues CS2336: Computer Science II.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 The Stack ADT (§4.2) The Stack ADT stores arbitrary objects Insertions and deletions.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
E.G.M. Petrakisstacks, queues1 Stacks  Stack: restricted variant of list  elements may by inserted or deleted from only one end : LIFO lists  top: the.
1 Lecture 25 Abstract Data Types –II Overview  A stack Interface in Java  StackEmptyException  Stack ADT(A Simple Array-Based Implementation  Queue.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
Stacks.
Queues.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5) Java.util.Stack class Java.util.Vector.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Applications of Stacks (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5)
Implementing and Using Stacks
Stacks, Queues, and Deques
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Stacks, Queues, and Deques
Stacks. week 2a2 Outline and Reading The Stack ADT (§4.1) Applications of Stacks Array-based implementation (§4.1.2) Growable array-based stack Think.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Data Structures and Algorithms
Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.
Problem of the Day  What do you get when you cross a mountain climber and a grape?
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
STACKS AND QUEUES 1. Outline 2  Stacks  Queues.
Mohammad Amin Kuhail M.Sc. (York, UK) University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Computer Science.
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
Stacks And Queues Chapter 18.
AITI Lecture 18 Introduction to Data Structure, Stack, and Queue Adapted from MIT Course 1.00 Spring 2003 Lecture 23 and Tutorial Note 8 (Teachers: Please.
CS 367 Introduction to Data Structures Lecture 5.
Lecture Objectives  To understand how Java implements a stack  To learn how to implement a stack using an underlying array or linked list  Implement.
Parasol Lab, Dept. CSE, Texas A&M University
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Stack. ADS2 Lecture 1010 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in.
1 Stacks Abstract Data Types (ADTs) Stacks Application to the analysis of a time series Java implementation of a stack Interfaces and exceptions.
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
Stacks (and Queues).
Elementary Data Structures
Stacks.
Stacks and Queues.
Queues Queues Queues.
CMSC 341 Lecture 5 Stacks, Queues
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Stacks, Queues, and Deques
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Stacks, Queues, and Deques
Stacks.
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Stacks and Queues 1.
Stacks Abstract Data Types (ADTs) Stacks
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Stacks, Queues, and Deques
Stacks and Linked Lists
Presentation transcript:

Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,

Learning Objectives ► Explain and implement Stacks and Queues ► Analyze implementations in terms of efficiency

Elementary Data Structures ► Stack ► container of elements that are inserted and removed last-in first-out (LIFO) ► Queue ► container of elements that are inserted and removed first-in first-out (FIFO)

Stack ► Last-in, First-out (LIFO) structure ► Operations ► push: add element into the stack ► pop: remove & return topmost element ► top: return topmost element ► isEmpty: check if the stack has no elements ► size: return number of elements in the stack ► Sample uses ► "Back" button of a browser, "Undo" operation, function/method calls

Stack Interface public interface Stack { public int size(); public boolean isEmpty(); public void push( Object o ); public Object top() throws EmptyStackException; public Object pop() throws EmptyStackException; } public class EmptyStackException extends RuntimeException { }

Array Implementation public class ArrayStack implements Stack { private int top = -1; private Object S[];... } xyzw 3 top...

Array Implementation Details ► An array of objects stores the elements ► An integer field points to the topmost element ► Value of top is –1 when the stack is empty ► A constant indicates the size/capacity of the array ► Throw a StackFullException when a push is attempted on a full array

ArrayStack Class public class ArrayStack implements Stack { public static final int CAPACITY = 1000; private Object S[]; private int top; public ArrayStack() { S = new Object[CAPACITY]; top = -1; } public boolean isEmpty() { return (top < 0); } … }

ArrayStack Class continued public class ArrayStack implements Stack { … public int size() { return (top + 1); } public void push(Object obj) throws FullStackException { if (size() == CAPACITY) throw new FullStackException(); S[++top] = obj; } … } public class FullStackException extends RuntimeException { }

ArrayStack Class continued public class ArrayStack implements Stack { … public Object top() throws EmptyStackException { if (isEmpty()) throw new EmptyStackException(); return S[top]; } public Object pop() throws EmptyStackException { if (isEmpty()) throw new EmptyStackException(); return S[top--]; } … }

Garbage Collection ► After a pop() operation, array still contains reference to popped element ► Succeeding push() operations will override such references but it is not certain whether pushes will occur after the pops ► Better to set the reference to null so that the object is garbage-collected when no longer in use

Improved pop() Method public class ArrayStack implements Stack { … public Object pop() throws EmptyStackException { Object elem; if (isEmpty()) throw new EmptyStackException(); elem = S[top]; S[top--] = null; // dereference S[top] for garbage collection. return elem; } … }

Using the Stack Stack s1 = new ArrayStack(); String temp; s1.push( "easy" ); s1.push( "this" ); temp = (String) s1.pop(); System.out.print( temp ); s1.push( "is" ); s1.push( "class" ); while ( !s1.isEmpty() ) { temp = (String) s1.pop(); System.out.print( " "+ temp ); } System.out.println(); Cast object to String OK because Strings are Objects

Stack of ints Stack s2 = new ArrayStack(); s2.push( 5 ); s2.push( 2 ); s2.push( 3 ); int num = (Integer) s2.pop(); System.out.println( num ); Allowed in Java 1.5 because primitive type values are "auto-boxed" Cast object to Integer type (not int) Note: In previous Java versions, s2.push( new Integer( 2 ) ); num = ( (Integer) s2.pop() ).intValue();

Time Complexity Analysis ► push() :O(1) ► pop() :O(1) ► isEmpty() :O(1) ► size() :O(1) ► top() :O(1)

Array Implementation Alternative ► Make top variable point to next available array position instead of actual topmost element ► top = 0 when empty ► top represents size x 4 top yzw...

Problems with ArrayStack ► CAPACITY needs to be specified ► Consequences ► stack may fill up (when size() == MAX ) ► memory is wasted if actual stack consumption is way below maximum ► Need a more "dynamic" implementation

Linked List Implementation y top zw null A stack as a sequence of nodes

The Node Class public class Node { private Object element; private Node next; public Node( Object e, Node n ) { element = e; next = n; } public Object getElement() … public Node getNext() … public void setElement( Object newElem ) … public void setNext( Node newNext ) … } y

Linked List Implementation ► Stack is represented by a Node reference (called top) ► This reference is null when stack is empty ► Top refers to the top element only but links in each node keep the elements together ► An integer field represents the number of elements in the stack

NodeStack Class public class NodeStack implements Stack { private Node top; private int size; public NodeStack() { top = null; size = 0; } public boolean isEmpty() { return (top == null); } … }

NodeStack Class continued public class NodeStack implements Stack { … public int size() { return size; } public void push( Object obj ) { Node v = new Node( obj, top ); top = v; size++; } … }

Push Operation y top zw null 3 size

Push Operation y top zw null 3 size x Create node

Push Operation y top zw null 4 size x Update top and size

NodeStack Class continued public class NodeStack implements Stack { … public Object top() throws EmptyStackException { if ( isEmpty() ) throw new EmptyStackException(); return top.getElement(); } public Object pop() throws EmptyStackException { if ( isEmpty() ) throw new EmptyStackException(); Object temp = top.getElement(); top = top.getNext(); size--; return temp; } … }

Pop Operation y top zw null 4 size x

Pop Operation y top zw null 4 size x Get top element temp

Pop Operation y top zw null 3 size x Update top and size temp

Pop Operation y top zw null 3 size x Node automatically disposed temp

x Pop Operation y top zw null 3 size Return element

Using the NodeStack Stack s2 = new NodeStack(); s2.push( 5 ); s2.push( 2 ); s2.push( 3 ); int num = (Integer) s2.pop(); System.out.println( num ); Only this line is changed

Time Complexity Analysis ► push() :O(1) ► pop() :O(1) ► isEmpty() :O(1) ► size() :O(1) ► top() :O(1)

ArrayStack versus NodeStack ► NodeStack uses only the memory that it needs at any given time ► NodeStack has no size limit (just the system’s memory) – FullStackException not thrown ► ArrayStack’s implementation is simpler ► Which implementation is more efficient?

Managing Multiple Implementations ► Note that we now have two implementations of a Stack: public class ArrayStack implements Stack { … } public class NodeStack implements Stack { … } ► Consider what code needs to be changed if we shift between implementations ► It would be preferable if the code that uses the stack does not need to be updated

A StackFactory Class ► Use a separate class that produces Stack objects public class StackFactory { public static Stack createStack() { return new ArrayStack(); // or return new NodeStack(); } ► Advantage: ► if you want to change your implementation, you just need to change StackFactory ► you don’t need to change all calls to new ArrayStack in all your code!

Using a StackFactory Stack s2 = StackFactory.createStack(); s2.push( 5 ); s2.push( 2 ); s2.push( 3 ); int num = (Integer) s2.pop(); System.out.println( num ); this line need not be changed even if the stack implementation changes

Queue ► First-in, First-out (FIFO) structure ► Operations ► enqueue: insert element at rear ► dequeue: remove & return front element ► front: return front element ► isEmpty: check if the queue has no elements ► size: return number of elements in the queue ► Sample use ► handling requests and reservations

The Queue Interface public interface Queue { public int size(); public boolean isEmpty(); public void enqueue( Object o ); public Object front() throws EmptyQueueException; public Object dequeue() throws EmptyQueueException; } public class EmptyQueueException extends RuntimeException { }

Array Implementation Possibilities ► On enqueue, place element in the next available slot; on dequeue, remove element at position 0 and move all other elements to the left ► Dequeue takes O(n) time ► Have integer pointers to front and rear, increment rear on enqueue, increment front on dequeue, so that both operations are O(1)

Array Implementation of a Queue ► An Object array and two integers ► front: index of first element in queue ► rear: index of first FREE element in queue 4 rear... 0 front

ArrayQueue public class ArrayQueue implements Queue { public static final int CAPACITY = 1000; private Object s[]; private int front, rear; public ArrayQueue() { s = new Object[CAPACITY]; front = rear = 0; }... }

isEmpty and Enqueue public class ArrayQueue implements Queue {... public boolean isEmpty() { return ( front == rear ); } public void enqueue( Object o ) throws FullQueueException { if ( rear == CAPACITY ) throw new FullQueueException(); s[rear++] = o; }... } public class FullQueueException extends RuntimeException { }

Enqueue Operation 3 rear... 0 front

Enqueue Operation 4 rear... 0 front Enqueued object

Dequeue public class ArrayQueue implements Queue {... public Object dequeue() throws EmptyQueueException { if ( isEmpty() ) throw new EmptyQueueException(); return s[front++]; } … }

Dequeue Operation 4 rear... 1 front Return this object

Dequeue Operation 4 rear... 1 front Remember to set reference in array to null null

Dequeue with Garbage Collection public class ArrayQueue implements Queue {... public Object dequeue() throws EmptyQueueException { if ( isEmpty() ) throw new EmptyQueueException(); Object data = s[front]; s[front] = null; front++; return data; }

Circular Array ► Suppose many enqueue operations followed by many dequeue operations ► Result: rear approaches CAPACITY but the queue is not really full ► Solution: Circular Array ► allow rear (and front) to "wrap around" the array (if rear = CAPACITY-1, incrementing rear means resetting it to 0)

Circular Array continued ► When is the array full? ► Simple answer: when (rear == front) ► Problem: this is the same condition as empty ► Solution: Reserve a slot ► full: when ( (rear+1) % CAPACITY == front) (one free slot left) ► empty: when ( rear == front ) ► Note: "wastes" a slot ► alternative: have a boolean field called hasElements ► full: when ( hasElements && (rear == front)) ► But not really better ► hasElements takes up extra space too ► Also, need to take care of hasElements in enqueue and dequeue

Revised Enqueue public class ArrayQueue implements Queue {... public void enqueue( Object o ) throws FullQueueException { if ((rear+1) % CAPACITY == front) throw new FullQueueException(); s[rear] = o; rear = (rear + 1) % CAPACITY; }... }

Revised Dequeue public class ArrayQueue implements Queue {... public Object dequeue() throws EmptyQueueException { if ( isEmpty() ) throw new EmptyQueueException(); Object data = s[front]; s[front] = null; front = (front + 1) % CAPACITY; return data; } … }

Completing the ArrayQueue Class public class ArrayQueue implements Queue {... public int size() { return (CAPACITY + rear – front) % CAPACITY; } … public Object front() throws EmptyQueueException { if ( isEmpty() ) throw new EmptyQueueException(); return s[front]; } … }

Time Complexity Analysis ► enqueue() :O(1) ► dequeue() :O(1) ► isEmpty() :O(1) ► size() :O(1) ► front() :O(1)

Dynamic Implementation ► Queue is represented by a linked sequence of nodes ► Two node references refer to front and rear element, respectively ► Use a size field to monitor number of elements

public class NodeQueue implements Queue { private Node front; private Node rear; private int size; } Linked List Implementation

Enqueue front null rear

Enqueue front rear null

Dequeue front null rear Return this object

NodeQueue Considerations ► Exercise: complete the NodeQueue class ► Note that the queue is empty when both front and rear are null ► Need to watch out for special cases ► Enqueue from an empty queue ► Dequeue from a single-element queue

Exercise: Balanced Parenthesis ► Write a program which takes in a string then outputs whether ( YES ) or not ( NO ) the parentheses are balanced. ► Only consider (),[],{} and <>. Ignore everything else. InputOutput ((A))YES ( c)YES { ]}NO (()NO ())NO

Exercise: Postfix Calculator ► Write a program which computes then prints the result of a postfix string or prints ERROR for an invalid string. ► Input numbers are single-digit for simplicity. InputOutput *20 1+2ERROR 90*2--ERROR