IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture6.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Stack & Queues COP 3502.
§3 The Stack ADT 1. ADT A stack is a Last-In-First-Out (LIFO) list, that is, an ordered list in which insertions and deletions are.
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Data Structures and Algorithms (60-254)
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture24.
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.
E.G.M. Petrakislists, stacks, queues1 Stacks Stack: restricted variant of list –Elements may by inserted or deleted from only one end  LIFO lists –Top:
CHAPTER 7 Queues.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
 Balancing Symbols 3. Applications
CS Data Structures II Review COSC 2006 April 14, 2017
Stack and Queue COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
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.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
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.
Stack: Linked List Implementation Push and pop at the head of the list New nodes should be inserted at the front of the list, so that they become the top.
Stacks, Queues, and Deques
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
Chapter 16 Stacks and Queues Saurav Karmakar Spring 2007.
Chapter 6.6, (event-driven simulation) Queues 1CSCI 3333 Data Structures.
Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.
Definition Stack is an ordered collection of data items in which access is possible only at one end (called the top of the stack). Stacks are known.
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
Topic 3 The Stack ADT.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
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.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
Lists, Stacks, and Queues Abstract Data Types (ADTs) The List ADT The Stack ADT The Queue ADT.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture8.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Chapter 16 Stacks & Queues. Objective In this chapter we will learn:  Stacks  Queues  Different implementations (arrays and linked list) of both 
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
18-1 Queues Data Structures and Design with Java and JUnit © Rick Mercer.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
Lecture Objectives  To understand how Java implements a stack  To learn how to implement a stack using an underlying array or linked list  Implement.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
M180: Data Structures & Algorithms in Java Queues Arab Open University 1.
April 27, 2017 COSC Data Structures I Review & Final Exam
Linear Data Structures
Stacks & Queues CSC 172 SPRING 2002 LECTURE 4 Agenda  Stack  Definition  Implementation  Analysis  Queue  Definition  Implementation  Analysis.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
3/3/20161 Stacks and Queues Introduction to Data Structures Ananda Gunawardena.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Data.
Stacks This presentation shows – how to implement the stack – how it can be used in real applications.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture1.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture13.
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
Cinda Heeren / Geoffrey Tien
Stacks Stack: restricted variant of list
Stacks Chapter 5 Adapted from Pearson Education, Inc.
CSC 1052 Stacks 1/11/2019.
The List, Stack, and Queue ADTs
Stacks and Queues 1.
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
Stacks.
Stacks, Queues, and Deques
Getting queues right … finally (?)
Presentation transcript:

IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture6 th Mar 2007 Implementing Stacks & Queues

2 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 6 th Mar 2007 ADT Stacks Basic operations Examples of use Implementations Array-based and linked list-based ADT Queues Basic operations Examples of use Implementations Array-based and linked list-based Stack Applications Balanced Symbol Checker Postfix Machines Summary Outline

3 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 6 th Mar 2007 Linear Data Structures A collection of components that are arranged along one dimension, i.e in a straight line, or linearly. Stack: a linear data structure where access is restricted to the most recently inserted item. Queue: a linear data structure where access is restricted to the least recently inserted item. Both of these abstract data types can be implemented at a lower level using a list: either an array or a linked list.

4 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 6 th Mar 2007 Stack The last item added is pushed (added) to the stack. The last item added can be popped (removed) from the stack. The last item added can be topped (accessed) from the stack. These operations all take constant time: O(1). A typical stack interface: void push(Thing newThing); void pop(); Thing top();

5 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 6 th Mar 2007 Stack Implementation: Array A stack can be implemented as an array A and an integer top that records the index of the top of the stack. For an empty stack, set top to -1. When push(X) is called, increment top, and write X to A[top]. When pop() is called, decrement top. When top() is called, return A[top].

6 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 6 th Mar 2007 Example top(-1) Atop(0) A Btop(1) Push A Push B MyStack myStack = new MyStack(); myStack.push(A); myStack.push(B);

7 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 6 th Mar 2007 Array Doubling When array-based stack is constructed, instantiate an array with a “default” size. When the array underlying the stack is full (not the stack itself!), we can increase the array through array doubling. Allocate a new array twice the size, and copy the old array to the first half of the new array: Thing[] newA = new Thing[oldA.length*2]; for(int ii=0; ii<oldA.length; ii++) newA[ii] = oldA[ii]; oldA = newA;

8 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 6 th Mar 2007 Running Time Without array doubling, all stack operations take constant time – O(1). With array doubling, push() may be O(N), but this happens quite rarely: array doubling due to data size N must be preceded by N/2 push() non-doubling calls. Effectively, still constant time  Amortization.

9 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 6 th Mar 2007 Stack Implementation: Array public class MyArrayStack { private T[] array; private int topOfStack; private static final int DEFAULT_CAPACITY = 10; public MyArrayStack() … public boolean isEmpty() … public void makeEmpty() … public T top() … public void pop() … public T topAndPop() … public void push(T x) … private void doubleArray() … }

10 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 6 th Mar 2007 Stack Implementation: Linked List First item in list = top of stack (if empty: null ) push(Thing x) : Create a new node containing x Insert it as the first element pop() : Delete first item (i.e. move “top” to the second item) top() : Return the data of the first element dcba topOfStack

11 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 6 th Mar 2007 Stack Implementation: Linked List public class MyLinkedListStack { private ListNode topOfStack; public MyLinkedListStack() … public boolean isEmpty() … public void makeEmpty() … public T top() … public void pop() … public T topAndPop() … public void push(T x) … }

12 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 6 th Mar 2007 Queue Last item added is enqueued (added) to the back. First item added is dequeued (removed) from the front. First item added can be accessed: getFront. These operations all take constant time – O(1). A typical queue interface: void enqueue(Thing newThing); void dequeue(); Thing getFront();

13 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 6 th Mar 2007 Queue Implementation: Simple Idea Store items in an array A Maintain index: back Front of queue = A[0] Back of queue = A[back] Enqueue is easy & fast: store at A[back], back++ Dequeue is inefficient: A[1] to A[back] needs to be shifted (and back-- )  O(N) X back XY XYZ YZ enqueue(X)enqueue(Y)enqueue(Z)dequeue()

14 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 6 th Mar 2007 Queue Implementation: Better Idea Add another index: front, which records the front of the queue Dequeue is now done by incrementing front Both enqueue and dequeue are now O(1). But what happens if enqueue and dequeue array.length-1 items? XYZ back YZ enqueue(X) enqueue(Y) enqueue(Z) dequeue() front Z back dequeue() front

15 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 6 th Mar 2007 Queue Implementation: “Circular” Array After the array.length-1 -th item is enqueued, the underlying array is full, even though the queue is not  logically, it should be (almost?) empty. Solution: wraparound Re-use cells at beginning of array that are ‘empty’ due to dequeue. When either front or back is incremented and points “outside array” ( ≥array.length ), reset to 0.

16 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 6 th Mar 2007 Circular Example Both front and back indexes “wraparound” the array. Think of the array as a circle… PQR frontback PQRS frontback PQRS front T back QRS front T back RS front T back S front T back T frontback

17 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 6 th Mar 2007 Java Implementation Fairly straightforward. Basically, maintain Front Back Number of items in queue When is the underlying array really full? How do we do array doubling?

18 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 6 th Mar 2007 Queue Implementation: Array public class MyArrayQueue { private T[] array; private int front,back,currentSize; private static final int DEFAULT_CAPACITY = 10; public MyArrayQueue() … public boolean isEmpty() … public void makeEmpty() … public T getFront() … public void dequeue() … public T getFrontAndDequeue() … public void enqueue(T x) … private void doubleQueue() … private int increment(int x) … }

19 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 6 th Mar 2007 Queue Implementation: Linked List Maintain 2 node references: front & back An empty queue: front = back = null. enqueue(Thing X) : Create a new node N containing X If queue empty: front = back = N Else append N and update back dequeue() : Delete first item (referenced by front ) getFront() : Return data of first element abcd frontback

20 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 6 th Mar 2007 Queue Implementation: Linked List public class MyLinkedListQueue { private ListNode front; private ListNode back; public MyLinkedListQueue() … public boolean isEmpty() … public void makeEmpty() … public T getFront() … public void dequeue() … public T getFrontAndDequeue() … public void enqueue(T x) … }

21 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 6 th Mar 2007 Stack Application: Balanced Symbols Java uses pairs of symbols to denote blocks: ( and ) { and } [ and ] An opening symbol must be matched with a closing symbol: { [ ( ) ] } is OK { ( [ ) ] } is NOT OK A closing symbol, e.g. ‘)’, must match the most recently seen opening symbol, e.g. ‘(‘. This can be done using a stack.

22 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 6 th Mar 2007 Balanced Symbol Checker Algorithm Make an empty stack Read symbols: If symbol is an opening symbol, push onto stack. If symbol is a closing symbol: If stack is empty, report error! Else, (top and) pop stack. If popped symbol is not a matching opening symbol, report error! At the end, if the stack is not empty, report error!

23 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 6 th Mar 2007 Say we are given an arithmetic expression, and we are asked to evaluate it. Typically, we use infix expressions, of the form operand1 OPERATOR operand2 Example: * 3 To process infix expressions we need precedence and associativity: * ^ 3 ^ ^ 5 * 3 * 6 / 7 ^ 2 ^ 2 We use brackets: (1-2)-((((4^5)*3)*6)/(7^(2^2))) Stack Application: Simple Calculator

24 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 6 th Mar 2007 Postfix Notation Typically, we use postfix expressions, of the form operand1 operand2 OPERATOR Example: * With postfix expressions, precedence is unambiguous: ^ 5 * 3 * 6 / 7 ^ 2 ^ 2, or (1-2)-((((4^5)*3)*6)/(7^(2^2))), becomes ^ 3 * 6 * ^ ^ / -

25 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 6 th Mar 2007 Postfix Machines A postfix machine evaluates a postfix expression: If an operand is seen, push it onto stack If an operator is seen, pop appropriate number of operands, evaluate operator, push result onto stack When the complete postfix expression is evaluated, the stack should contain exactly one item: the result.

26 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 6 th Mar 2007 Both versions, array and linked-list, run in O(1) Linked-list implementation requires extra overhead due to next reference at each node (Circular) array implementation of queues can be quite tricky Array space doubling needs memory at least 3x size of actual data. Summary