Stacks and Queues. stacks dynamic resizing queues generics iterators applications. Stacks and queues

Size: px
Start display at page:

Download "Stacks and Queues. stacks dynamic resizing queues generics iterators applications. Stacks and queues"

Transcription

1 Stacks and queues Stacks and Queues stacks dynamic resizing queues generics iterars applications Fundamental data types. Values: sets of objects Operations: insert, remove, test if empty. Intent is clear when we insert. Which item do we remove? Stack. Remove the item most recently added. Analogy. Cafeteria trays, Web surfing. Queue. Remove the item least recently added. Analogy. Registrar's line. LIFO = "last in out" FIFO = " in out" push pop Reference: Algithms in Java, Chapter 3, 4 enqueue dequeue Algithms in Java, 4 th Edition Rt Sedgewick and Kevin Wayne Copyright 2008 February 11, :54:15 PM 2 Client, implementation, interface Client, Implementation, Interface Separate interface and implementation so as : Build layers of abstraction. Reuse software. Ex: stack, queue, symbol table, union-find,... Benefits. Client can't know details of implementation client has many implementation from which choose. Implementation can't know details of client needs many clients can re-use the same implementation. Design: creates modular, reusable libraries. Perfmance: use optimized implementation where it matters. Client: program using operations defined in interface. Implementation: actual code implementing operations. Interface: description of data type, basic operations. Client: program using operations defined in interface. Implementation: actual code implementing operations. Interface: description of data type, basic operations. 3 4

2 Stacks Stack operations. push() Insert a new item on stack. pop() Remove and return the item most recently added. isempty() Is the stack empty? push pop stacks dynamic resizing queues generics iterars applications public static void main(string[] args) StackOfStrings stack = new StackOfStrings(); while (!StdIn.isEmpty()) String item = StdIn.readString(); if (item.equals("-")) StdOut.print(stack.pop()); else stack.push(item); % me.txt not that is % java StackOfStrings <.txt not that 5 6 Stack pop: linked-list implementation Stack push: linked-list implementation st the was it of st the was it String item =.item; old st the was it Node old = ; st the was it =.next; old st the was it Node = new Node(); st the was it return item; old of st the was it.item = "of";.next = old; 7 8

3 Stack: linked-list implementation Stack: linked-list trace public class StackOfStrings private Node = ; StdIn StdOut private class Node String item; Node next; "inner class" not not return == ; - not not public void push(string item) Node old = ; = new Node();.item = item;.next = old; - - that not not that not public String pop() if (isempty()) throw new RuntimeException(); String item =.item; =.next; return item; is that is 9 10 Stack: array implementation Stack: array implementation Array implementation of a stack. Use array s[] sre N items on stack. push(): add new item at s[n]. pop(): remove item from s[n-1]. s[] it was the st of times N capacity = 10 public class StackOfStrings private String[] s; private int N = 0; public StackOfStrings(int capacity) s = new String[capacity]; return N == 0; public void push(string item) s[n++] = item; public String pop() String item = s[--n]; s[n] = ; return item; this version avoids "loitering" garbage collecr only reclaims memy if no outstanding references public String pop() return s[--n]; 11 12

4 Stack: dynamic array implementation Q. How grow array? Q. How shrink array? First try. push(): increase size of s[] by 1. pop(): decrease size of s[] by 1. stacks dynamic resizing queues generics iterars applications Too expensive. Need copy all item a new array. Inserting N items takes time proptional N ~ N 2 /2. infeasible f large N Goal. Ensure that array resizing happens infrequently Stack: dynamic array implementation Q. How grow array? A. If array is full, create a new array of twice the size, and copy items. public StackOfStrings() s = new String[1]; public void push(string item) if (N == s.length) resize(2 * s.length); s[n++] = item; private void resize(int capacity) String[] dup = new String[capacity]; f (int i = 0; i < N; i++) dup[i] = s[i]; s = dup; "repeated doubling" Stack: dynamic array implementation Q. How shrink array? First try. push(): double size of s[] when array is full. pop(): halve size of s[] when array is half full. Too expensive Consider push-pop-push-pop- sequence when array is full. Time proptional N per operation. N = 5 N = 4 "thrashing" it was the st of it was the st N/2 + N ~ 2N N = 5 it was the st of Consequence. Inserting N items takes time proptional N (not N 2 ). N = 4 it was the st 15 16

5 Stack: dynamic array implementation Stack: dynamic array implementation trace Q. How shrink array? Efficient solution. push(): double size of s[] when array is full. pop(): halve size of s[] when array is one-quarter full. public String pop() String item = s[n-1]; s[n-1] = ; N--; if (N == s.length/4) resize(s.length / 2); s[n++] = item; return item; Invariant. Array is always tween 25% and 100% full. Consequence. Starting from empty data structure, any sequence of M ops takes time proptional M. "amtized" bound a StdIn StdOut N a.length not 4 4 not 5 8 not not 5 8 not not - not 3 8 that 4 8 that - that is 2 2 is Stack implementations: dynamic array vs. linked List Tradeoffs. Can implement with either array linked list; client can use interchangeably. Which is tter? Linked list. Every operation takes constant time in wst-case. Uses extra time and space deal with the links. Array. Every operation takes constant amtized time. Less wasted space. stacks dynamic resizing queues generics iterars applications 19 20

6 Queues Queue dequeue: linked list implementation Queue operations. enqueue() dequeue() isempty() Insert a new item on queue. Delete and return the item least recently added. Is the queue empty? it last was the st of String item =.item; public static void main(string[] args) QueueOfStrings q = new QueueOfStrings(); while (!StdIn.isEmpty()) String item = StdIn.readString(); if (item.equals("-")) StdOut.print(q.dequeue()); else q.enqueue(item); else % me.txt not that is % java QueueOfStrings <.txt not last was the st of =.next; last was the st of return item; Queue enqueue: linked list implementation Queue: linked list implementation last public class QueueOfStrings private Node, last; it was the st private class Node String item; Node next; oldlast last return == ; it was the st of oldlast last it was the st of oldlast last it was the st of Node oldlast = last; Node last = new Node(); last.item = "of"; last.next = ; oldlast.next = last; public void enqueue(string item) Node oldlast = last; last = new Node(); last.item = item; last.next = ; if (isempty()) = last; else oldlast.next = last; public String dequeue() String item =.item; =.next; if (isempty()) last = ; return item; 23 24

7 Queue: dynamic array implementation Array implementation of a queue. Use array q[] sre items in queue. enqueue(): add new item at q[tail]. dequeue(): remove item from q[head]. Update head and tail modulo the capacity. Add repeated doubling and shrinking. q[] the st of times head tail capacity = 10 stacks dynamic resizing queues generics iterars applications Parameterized stack Parameterized stack We implemented: StackOfStrings. We also want: StackOfURLs, StackOfCusmers, StackOfInts, etc? We implemented: StackOfStrings. We also want: StackOfURLs, StackOfCusmers, StackOfInts, etc? Attempt 1. Implement a separate stack class f each type. Rewriting code is tedious and err-prone. Maintaining cut-and-pasted code is tedious and err-prone. Attempt 2. Implement a stack with items of type Object. Casting is required in client. Casting is err-prone: run-time err if types most reasonable approach until Java 1.5. [hence, used in AlgsJava] StackOfObjects s = new StackOfObjects(); Apple a = new Apple(); Orange b = new Orange(); s.push(a); s.push(b); a = (Apple) (s.pop()); run-time err 27 28

8 Parameterized stack Generic stack: linked list implementation We implemented: StackOfStrings. We also want: StackOfURLs, StackOfCusmers, StackOfInts, etc? public class StackOfStrings private Node = ; public class Stack<Item> private Node = ; Attempt 3. Java generics. Avoid casting in both client and implementation. Discover type mismatch errs at compile-time instead of run-time. Stack<Apple> s = new Stack<Apple>(); Apple a = new Apple(); Orange b = new Orange(); s.push(a); s.push(b); a = s.pop(); type parameter compile-time err Guiding principles. Welcome compile-time errs; avoid run-time errs. private class Node String item; Node next; return == ; public void push(string item) Node old = ; = new Node();.item = item;.next = old; public String pop() String item =.item; =.next; return item; private class Node Item item; Node next; return == ; public void push(item item) Node old = ; = new Node();.item = item;.next = old; public Item pop() Item item =.item; =.next; return item; generic type name Generic stack: array implementation Generic stack: array implementation public class StackOfStrings private String[] s; private int N = 0; public class Stack<Item> private Item[] s; private int N = 0; public class StackOfStrings private String[] s; private int N = 0; public class Stack<Item> private Item[] s; private int N = 0; public StackOfStrings(int capacity) s = new String[capacity]; public Stack(int capacity) s = new Item[capacity]; public StackOfStrings(int capacity) s = new String[capacity]; public Stack(int capacity) s = (Item[]) new Object[capacity]; return N == 0; return N == 0; return N == 0; return N == 0; public void push(string item) s[n++] = item; public void push(item item) s[n++] = item; public void push(string item) s[n++] = item; public void push(item item) s[n++] = item; public String pop() return s[--n]; public Item pop() return s[--n]; public String pop() return s[--n]; public Item pop() return s[--n]; the way it should the way it generic array creation not allowed in Java the ugly cast 31 32

9 Generic data types: auboxing Q. What do about primitive types? Wrapper type. Each primitive type has a wrapper object type. Ex: Integer is wrapper type f int. Auboxing. Aumatic cast tween a primitive type and its wrapper. Syntactic sugar. Behind-the-scenes casting. Stack<Integer> s = new Stack<Integer>(); s.push(17); // s.push(new Integer(17)); int a = s.pop(); // int a = s.pop().intvalue(); stacks dynamic resizing queues generics iterars applications Botm line. Client code can use generic stack f any type of data Iteration Iterars Design challenge. Suppt iteration over stack items by client, without revealing the internal representation of the stack. Q. What is an Iterable? A. Has a method that returns an Iterar. public interface Iterable<Item> Iterar<Item> iterar(); s[] i N it was the st of times current of st the was it Q. What is an Iterar? A. Has methods hasnext() and next(). Q. Why make data structures Iterable? A. Java suppts elegant client code. public interface Iterar<Item> boolean hasnext(); Item next(); void remove(); optional; use at your own risk feach statement equivalent code Java solution. Make stack Iterable. f (String s : stack) StdOut.println(s); Iterar<String> i = stack.iterar(); while (i.hasnext()) String s = i.next(); StdOut.println(s); 35 36

10 Stack iterar: linked list implementation Stack iterar: array implementation impt java.util.iterar; public class Stack<Item> implements Iterable<Item>... public Iterar<Item> iterar() return new ListIterar(); private class ListIterar implements Iterar<Item> private Node current = ; public boolean hasnext() return current!= ; public void remove() /* not suppted */ public Item next() Item item = current.item; current = current.next; return item; impt java.util.iterar; public class Stack<Item> implements Iterable<Item> public Iterar<Item> iterar() return new ArrayIterar(); private class ArrayIterar implements Iterar<Item> private int i = N; public boolean hasnext() return i > 0; public void remove() /* not suppted */ public Item next() return s[--i]; i N current s[] it was the st of times of st the was it Stack applications stacks dynamic resizing queues generics iterars applications Real wld applications. Parsing in a compiler. Java virtual machine. Undo in a wd process. Back butn in a Web browser. PostScript language f printers. Implementing function calls in a compiler

11 Function calls Arithmetic expression evaluation How a compiler implements a function. Function call: push local environment and return address. Return: pop return address and local environment. Recursive function. Function that calls itself. Note. Can always use an explicit stack remove recursion. p = 216, q = 192 p = 192, q = 24 gcd (216, 192) static int gcd(int p, int q) if (q == 0) return p; else return gcd(q, p % q); gcd (192, 24) p = 24, q = 0 static int gcd(int p, int q) if (q == 0) return p; else return gcd(q, p % q); gcd (24, 0) static int gcd(int p, int q) if (q == 0) return p; else return gcd(q, p % q); Goal. Evaluate infix expressions. ( 1 + ( ( ) * ( 4 * 5 ) ) ) operand Two-stack algithm. [E. W. Dijkstra] Value: push on the value stack. Operar: push on the operar stack. Left parens: igne. Right parens: pop operar and two values; push the result of applying that operar those values on the operand stack. Context. An interpreter! operar value stack operar stack * * * * * * * ( 1 + ( ( ) * ( 4 * 5 ) ) ) + ( ( ) * ( 4 * 5 ) ) ) ( ( ) * ( 4 * 5 ) ) ) + 3 ) * ( 4 * 5 ) ) ) 3 ) * ( 4 * 5 ) ) ) ) * ( 4 * 5 ) ) ) * ( 4 * 5 ) ) ) ( 4 * 5 ) ) ) * 5 ) ) ) 5 ) ) ) ) ) ) ) ) ) Arithmetic expression evaluation Crectness public class Evaluate public static void main(string[] args) Stack<String> ops = new Stack<String>(); Stack<Double> vals = new Stack<Double>(); while (!StdIn.isEmpty()) String s = StdIn.readString(); if (s.equals("(")) ; else if (s.equals("+")) ops.push(s); else if (s.equals("*")) ops.push(s); else if (s.equals(")")) String op = ops.pop(); if (op.equals("+")) vals.push(vals.pop() + vals.pop()); else if (op.equals("*")) vals.push(vals.pop() * vals.pop()); else vals.push(double.parsedouble(s)); StdOut.println(vals.pop()); % java Evaluate ( 1 + ( ( ) * ( 4 * 5 ) ) ) Q. Why crect? A. When algithm encounters an operar surrounded by two values within parentheses, it leaves the result on the value stack. as if the iginal input were: Repeating the argument: ( 1 + ( ( ) * ( 4 * 5 ) ) ) ( 1 + ( 5 * ( 4 * 5 ) ) ) ( 1 + ( 5 * 20 ) ) ( ) 101 Extensions. Me ops, precedence der, associativity

12 Stack-based programming languages PostScript Observation 1. The 2-stack algithm computes the same value if the operar occurs after the two values. ( 1 ( ( ) ( 4 5 * ) * ) + ) Observation 2. All of the parentheses are redundant! * * + Jan Lukasiewicz Page description language. Explicit stack. Full computational model Graphics engine. Basics. %!: I am a PostScript program. Literal: push me on the stack. Function calls take arguments from stack. Turtle graphics built in. a PostScript program %! move 0 72 rline 72 0 rline 0-72 rline rline 2 setlinewidth stroke its output Botm line. Postfix "reverse Polish" notation. Applications. Postscript, Fth, calculars, Java virtual machine, PostScript PostScript Data types. basic: integer, floating point, boolean,... Graphics: font, path, curve,... Full set of built-in operars. Text and strings. Full font suppt. System.out.print() show (display a string, using current font). cvs (convert anything a string). String() Variables (and functions). Identifiers start with /. def operar associates id with value. Braces. args on stack. function definition %! /box /sz exch def 0 sz rline sz 0 rline 0 sz neg rline sz neg 0 rline def %! /Helvetica-Bold findfont 16 scalefont setfont move (Square root of 2:) show move 2 sqrt 10 string cvs show Square root of 2: function calls move 72 box move 144 box 2 setlinewidth stroke 47 48

13 PostScript PostScript F loop. from, increment, on stack. Loop body in braces. f operar. If-else conditional. Boolean on stack. Alternatives in braces. if operar.... (hundreds of operars) mul dup 2 add move 72 box f Application 1. All figures in Algithms in Java Application 2. Deluxe version of StdDraw also saves PostScript f vecr graphics. %! translate /kochr 2 copy ge dup 0 rline 3 div 2 copy kochr 60 rotate 2 copy kochr -120 rotate 2 copy kochr 60 rotate 2 copy kochr ifelse pop pop def 0 0 move kochr 0 81 move kochr move kochr move kochr stroke See page Queue applications M/M/1 queuing model Familiar applications. itunes playlist. Data buffers (ipod, TiVo). Asynchronous data transfer (file IO, pipes, sockets). Dispensing requests on a shared resource (printer, process). M/M/1 queue. Cusmers arrive accding Poisson process at rate of λ per minute. Cusmers are serviced with rate of µ per minute. interarrival time has exponential distribution Pr[X x] = 1 - e - λ x service time has exponential distribution Pr[X x] = 1 - e - µ x Simulations of the real wld. Traffic analysis. Waiting times of cusmers at call center. Determining numr of cashiers have at a supermarket. Arrival rate Infinite queue Server Departure rate Q. What is average wait time W of a cusmer in system? Q. What is average numr of cusmers L in system? 51 52

14 M/M/1 queuing model: example simulation M/M/1 queuing model: event-based simulation time (seconds) 0 public class MM1Queue public static void main(string[] args) double lambda = Double.parseDouble(args[0]); // arrival rate double mu = Double.parseDouble(args[1]); // service rate double nextarrival = StdRandom.exp(lambda); double nextservice = nextarrival + StdRandom.exp(mu); arrival departure wait Queue<Double> queue = new Queue<Double>(); Hisgram hist = new Hisgram("M/D/1 Queue", 60); while (true) // next event is an arrival while (nextarrival < nextservice) queue.enqueue(nextarrival); nextarrival += StdRandom.exp(lambda); // next event is a service completion double arrival = queue.dequeue(); double wait = nextservice - arrival; hist.adddatapoint(math.min(60, (int) (Math.round(wait)))); if (queue.isempty()) nextservice = nextarrival + StdRandom.exp(mu); else nextservice = nextservice + StdRandom.exp(mu); 54 M/M/1 queuing model: experiments M/M/1 queuing model: experiments Observation. If service rate µ is much larger than arrival rate λ, cusmers gets good service. Observation. As service rate µ approaches arrival rate λ, services goes h***. % java MM1Queue % java MM1Queue

15 M/M/1 queuing model: experiments M/M/1 queuing model: analysis Observation. As service rate µ approaches arrival rate λ, services goes h***. M/M/1 queue. Exact fmulas known. % java MM1Queue.2.21 wait time W and queue length L approach infinity as service rate approaches arrival rate W = λ 2µ (µ λ) + 1 µ, L = λ W Little s Law Me complicated queueing models. Event-based simulation essential! Queueing they. See ORFE

Stacks and Queues. !stacks!dynamic resizing!queues!generics!applications. Stacks and Queues

Stacks and Queues. !stacks!dynamic resizing!queues!generics!applications. Stacks and Queues Stacks and Queues Stacks and Queues!stacks!dynamic resizing!queues!generics!applications Fundamental data types. Values: sets of objects Operations: insert, remove, test if empty. Intent is clear when

More information

Algorithms. Algorithms. Algorithms 1.3 BAGS, QUEUES, AND STACKS. stacks resizing arrays queues generics iterators applications

Algorithms. Algorithms. Algorithms 1.3 BAGS, QUEUES, AND STACKS. stacks resizing arrays queues generics iterators applications Algithms Stacks and queues 1.3 BAGS, QUEUES, AND STACKS Fundamental data types. Value: collection of objects. Operations: insert, remove, iterate, test if empty. Intent is clear when we insert. Which item

More information

Algorithms. Algorithms 1.3 BAGS, QUEUES, AND STACKS. stacks resizing arrays. queue. generics. applications. Stacks and queues

Algorithms. Algorithms 1.3 BAGS, QUEUES, AND STACKS. stacks resizing arrays. queue. generics. applications. Stacks and queues ROBERT SEDGEWICK KEVIN WAYNE Algithms ROBERT SEDGEWICK KEVIN WAYNE Stacks and queues Algithms F O U R T H E D I T I O N http://algs4.cs.princen.edu 1.3 BAGS, QUEUES, AND STACKS stacks resizing arrays queues

More information

! Set of operations (add, remove, test if empty) on generic data. ! Intent is clear when we insert. ! Which item do we remove? Stack.

! Set of operations (add, remove, test if empty) on generic data. ! Intent is clear when we insert. ! Which item do we remove? Stack. Stacks and Queues 4.3 Stacks and Queues Fundamental data types.! Set of operations (add, remove, test if empty) on generic data.! Intent is clear when we insert.! Which item do we remove? Stack.! Remove

More information

Algorithms. Algorithms. Algorithms 1.3 BAGS, QUEUES, AND STACKS. stacks resizing arrays queues generics iterators applications

Algorithms. Algorithms. Algorithms 1.3 BAGS, QUEUES, AND STACKS. stacks resizing arrays queues generics iterators applications Algithms ROBERT SEDGEWICK KEVIN WAYNE Stacks and queues 1.3 BAGS, QUEUES, AND STACKS Fundamental data types. Value: collection of objects. Operations: add, remove, iterate, test if empty. Intent is clear

More information

4.3 Stacks and Queues. Stacks. Stacks and Queues. Stack API

4.3 Stacks and Queues. Stacks. Stacks and Queues. Stack API Stacks and Queues 4.3 Stacks and Queues Fundamental data types. Set of operations (add, remove, test if empty) on generic data. Intent is clear when we insert. Which item do we remove? Stack. Remove the

More information

2.6 Stacks and Queues

2.6 Stacks and Queues Stacks and Queues 2.6 Stacks and Queues Fundamental data types.! Set of operations (add, remove, test if empty) on generic data.! Intent is clear when we insert.! Which item do we remove? Stack.! Remove

More information

Algorithms. Algorithms 1.3 BAGS, QUEUES, AND STACKS. stacks resizing arrays queues generics iterators applications. Is Polleverywhere working?

Algorithms. Algorithms 1.3 BAGS, QUEUES, AND STACKS. stacks resizing arrays queues generics iterators applications. Is Polleverywhere working? Is Polleverywhere wking? Algithms ROBERT SEDGEWICK KEVIN WAYNE 1.3 BAGS, QUEUES, AND STACKS Algithms F O U R T H E D I T I O N ROBERT SEDGEWICK KEVIN WAYNE http://algs4.cs.princen.edu stacks resizing arrays

More information

TSP assignment next lecture

TSP assignment next lecture 3/18/12 Data Types and Data Structures CS 112 Introduction to Programming Data types. Set of values and operations on those values. Some are built into the Java language: int, double[], String, Most are

More information

4.3 Stacks and Queues

4.3 Stacks and Queues 4.3 Stacks and Queues Introduction to Programming in Java: An Interdisciplinary Approach Robert Sedgewick and Kevin Wayne Copyright 2002 2010 03/30/12 04:33:08 PM Data Types and Data Structures Data types.

More information

Algorithms. Algorithms 1.3 STACKS AND QUEUES. stacks resizing arrays queues generics iterators applications ROBERT SEDGEWICK KEVIN WAYNE.

Algorithms. Algorithms 1.3 STACKS AND QUEUES. stacks resizing arrays queues generics iterators applications ROBERT SEDGEWICK KEVIN WAYNE. Algorithms ROBERT SEDGEWICK KEVIN WAYNE 1.3 STACKS AND QUEUES Algorithms F O U R T H E D I T I O N ROBERT SEDGEWICK KEVIN WAYNE http://algs4.cs.princeton.edu stacks resizing arrays queues generics iterators

More information

14. Stacks and Queues

14. Stacks and Queues Section 4.3 http://introcs.cs.princen.edu Data types and data structures Data types APIs Clients Strawman implementation Linked lists Implementations http://introcs.cs.princen.edu Set of values. Set of

More information

14. Stacks and Queues

14. Stacks and Queues 1 COMPUTER SCIENCE S E D G E W I C K / W A Y N E 14. Stacks and Queues Section 4.3 http://introcs.cs.princen.edu COMPUTER SCIENCE S E D G E W I C K / W A Y N E 14. Stacks and Queues APIs Clients Strawman

More information

14. Stacks and Queues

14. Stacks and Queues 1 COMPUTER SCIENCE S E D G E W I C K / W A Y N E 14. Stacks and Queues Section 4.3 http://introcs.cs.princen.edu COMPUTER SCIENCE S E D G E W I C K / W A Y N E 14. Stacks and Queues APIs Clients Strawman

More information

COMPUTER SCIENCE. Computer Science. 12. Stacks and Queues. Computer Science. An Interdisciplinary Approach. Section 4.3

COMPUTER SCIENCE. Computer Science. 12. Stacks and Queues. Computer Science. An Interdisciplinary Approach. Section 4.3 COMPUTER SCIENCE S E D G E W I C K / W A Y N E PA R T I I : A L G O R I T H M S, T H E O R Y, A N D M A C H I N E S Computer Science Computer Science An Interdisciplinary Approach Section 4.3 ROBERT SEDGEWICK

More information

CS2012 Programming Techniques II

CS2012 Programming Techniques II 27 January 14 Lecture 6 (continuing from 5) CS2012 Programming Techniques II Vasileios Koutavas 1 27 January 14 Lecture 6 (continuing from 5) 2 Previous Lecture Amortized running time cost of algorithms

More information

4.3 Stacks, Queues, and Linked Lists

4.3 Stacks, Queues, and Linked Lists 4.3 Stacks, Queues, and Linked Lists Introduction to Programming in Java: An Interdisciplinary Approach Robert Sedgewick and Kevin Wayne Copyright 2002 2010 3/21/2013 8:59:11 PM Data Types and Data Structures

More information

Week 4 Stacks & Queues

Week 4 Stacks & Queues CPSC 319 Week 4 Stacks & Queues Xiaoyang Liu xiaoyali@ucalgary.ca Stacks and Queues Fundamental data types. Value: collection of objects. Operations: insert, remove, iterate, test if empty. Intent is clear

More information

Basic Data Structures

Basic Data Structures Basic Data Structures Some Java Preliminaries Generics (aka parametrized types) is a Java mechanism that enables the implementation of collection ADTs that can store any type of data Stack s1

More information

Basic Data Structures 1 / 24

Basic Data Structures 1 / 24 Basic Data Structures 1 / 24 Outline 1 Some Java Preliminaries 2 Linked Lists 3 Bags 4 Queues 5 Stacks 6 Performance Characteristics 2 / 24 Some Java Preliminaries Generics (aka parametrized types) is

More information

Lecture 4 Stack and Queue

Lecture 4 Stack and Queue Lecture 4 Stack and Queue Bo Tang @ SUSTech, Spring 2018 Our Roadmap Stack Queue Stack vs. Queue 2 Stack A stack is a sequence in which: Items can be added and removed only at one end (the top) You can

More information

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

CMSC 132, Object-Oriented Programming II Summer Lecture 9: CMSC 132, Object-Oriented Programming II Summer 2018 Lecturer: Anwar Mamat Lecture 9: Disclaimer: These notes may be distributed outside this class only with the permission of the Instructor. 9.1 QUEUE

More information

4.3: Linked Structures

4.3: Linked Structures Linked vs. Sequential Allocation 4.3: Linked Structures "The name of the song is called 'Haddocks' Eyes.' " "Oh, that's the name of the song, is it?" Alice said, trying to feel interested. "No, you don't

More information

CSE 214 Computer Science II Stack

CSE 214 Computer Science II Stack CSE 214 Computer Science II Stack Spring 2018 Stony Brook University Instructor: Shebuti Rayana shebuti.rayana@stonybrook.edu http://www3.cs.stonybrook.edu/~cse214/sec02/ Random and Sequential Access Random

More information

csci 210: Data Structures Stacks and Queues

csci 210: Data Structures Stacks and Queues csci 210: Data Structures Stacks and Queues 1 Summary Topics Stacks and Queues as abstract data types ( ADT) Implementations arrays linked lists Analysis and comparison Applications: searching with stacks

More information

Programming Abstractions

Programming Abstractions Programming Abstractions C S 1 0 6 B Cynthia Lee Today s Topics ADTs Stack Example: Reverse-Polish Notation calculator Queue Example: Mouse Events Stacks New ADT: Stack stack.h template

More information

Stacks, Queues (cont d)

Stacks, Queues (cont d) Stacks, Queues (cont d) CSE 2011 Winter 2007 February 1, 2007 1 The Adapter Pattern Using methods of one class to implement methods of another class Example: using List to implement Stack and Queue 2 1

More information

Stacks and queues. CSCI 135: Fundamentals of Computer Science Keith Vertanen.

Stacks and queues. CSCI 135: Fundamentals of Computer Science Keith Vertanen. Stacks and queues http://www.flickr.com/photos/mac-ash/4534203626/ http://www.flickr.com/photos/thowi/182298390/ CSCI 135: Fundamentals of Computer Science Keith Vertanen Terminology Overview Abstract

More information

Lecture P5: Abstract Data Types

Lecture P5: Abstract Data Types Review Lecture P5: Abstract Data Types Data type: Set of values and collection of operations on those values. Example: int Set of values: between -32,767 and 32,767 (minimum limits). Operations: +, -,

More information

-The Hacker's Dictionary. Friedrich L. Bauer German computer scientist who proposed "stack method of expression evaluation" in 1955.

-The Hacker's Dictionary. Friedrich L. Bauer German computer scientist who proposed stack method of expression evaluation in 1955. Topic 15 Implementing and Using "stack n. The set of things a person has to do in the future. "I haven't done it yet because every time I pop my stack something new gets pushed." If you are interrupted

More information

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

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO) Outline stacks stack ADT method signatures array stack implementation linked stack implementation stack applications infix, prefix, and postfix expressions 1 Stacks stacks of dishes or trays in a cafeteria

More information

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures Computer Science 210 Data Structures Siena College Fall 2017 Topic Notes: Linear Structures The structures we ve seen so far, Vectors/ArrayLists and linked lists, allow insertion and deletion of elements

More information

Stacks and Queues. Fundamentals of Computer Science.

Stacks and Queues. Fundamentals of Computer Science. Stacks and Queues http://www.flickr.com/photos/mac-ash/4534203626/ http://www.flickr.com/photos/thowi/182298390/ Fundamentals of Computer Science Outline Terminology Abstract Data Types ADT) Data structures

More information

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 )

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 ) ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 ) Unit - 2 By: Gurpreet Singh Dean Academics & H.O.D. (C.S.E. / I.T.) Yamuna Institute of Engineering & Technology, Gadholi What is a Stack? A stack is a

More information

Lab 6: P.S. it s a stack Due 11:59 pm Monday, Mar 31, 2008

Lab 6: P.S. it s a stack Due 11:59 pm Monday, Mar 31, 2008 1 Assignment Lab 6: P.S. it s a stack Due 11:59 pm Monday, Mar 31, 2008 An interpreter is a program that executes other programs. For example, the Java Virtual Machine that you run using the java program

More information

Programming Abstractions

Programming Abstractions Programming Abstractions C S 1 0 6 B Cynthia Lee Today s Topics HW Tips QT Creator dos & don ts ADTs Stack Example: Reverse-Polish Notation calculator Queue Event queues QT Creator A F E W W A R N I N

More information

Algorithms and Data Structures

Algorithms and Data Structures Algorithms and Data Structures PD Dr. rer. nat. habil. Ralf Peter Mundani Computation in Engineering / BGU Scientific Computing in Computer Science / INF Summer Term 2018 Part 2: Data Structures PD Dr.

More information

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

CMSC 132: Object-Oriented Programming II. Stack and Queue CMSC 132: Object-Oriented Programming II Stack and Queue 1 Stack Allows access to only the last item inserted. An item is inserted or removed from the stack from one end called the top of the stack. This

More information

infix expressions (review)

infix expressions (review) Outline infix, prefix, and postfix expressions queues queue interface queue applications queue implementation: array queue queue implementation: linked queue application of queues and stacks: data structure

More information

Advanced Java Concepts Unit 3: Stacks and Queues

Advanced Java Concepts Unit 3: Stacks and Queues Advanced Java Concepts Unit 3: Stacks and Queues Stacks are linear collections in which access is completely restricted to just one end, called the top. Stacks adhere to a last-in, first-out protocol (LIFO).

More information

Postfix (and prefix) notation

Postfix (and prefix) notation Postfix (and prefix) notation Also called reverse Polish reversed form of notation devised by mathematician named Jan Łukasiewicz (so really lü-kä-sha-vech notation) Infix notation is: operand operator

More information

Building Java Programs

Building Java Programs Building Java Programs Appendix Q Lecture Q-1: stacks and queues reading: appendix Q 2 Runtime Efficiency (13.2) efficiency: measure of computing resources used by code. can be relative to speed (time),

More information

Postscript Intro. References. What is Postscript? Hello World! in Ghostscript. CSE 413, Autumn 2005 Programming Languages. Page description language

Postscript Intro. References. What is Postscript? Hello World! in Ghostscript. CSE 413, Autumn 2005 Programming Languages. Page description language References Postscript Intro CSE 413, Autumn 2005 Programming Languages http://www.cs.washington.edu/education/courses/413/05au/ Postscript Language Reference, Adobe Postscript Language Tutorial and Cookbook,

More information

Abstract Data Types and Java Generics. Fundamentals of Computer Science

Abstract Data Types and Java Generics. Fundamentals of Computer Science Abstract Data Types and Java Generics Fundamentals of Computer Science Outline Abstract Data Types (ADTs) A collection of data and operations on that data Data Structure How we choose to implement an ADT

More information

Evaluation of postfix

Evaluation of postfix Postfix Infix notation: Operator appears between operands: 2 + 3 5 3 + 6 9 Implied precedence: 2 + 3 * 4 2 + (3 * 4 ), not (2 + 3 ) * 4 Prefix notation: Operator precedes operands: + 2 3 5 + 2 * 3 5 (+

More information

Some Applications of Stack. Spring Semester 2007 Programming and Data Structure 1

Some Applications of Stack. Spring Semester 2007 Programming and Data Structure 1 Some Applications of Stack Spring Semester 2007 Programming and Data Structure 1 Arithmetic Expressions Polish Notation Spring Semester 2007 Programming and Data Structure 2 What is Polish Notation? Conventionally,

More information

DEEPIKA KAMBOJ UNIT 2. What is Stack?

DEEPIKA KAMBOJ UNIT 2. What is Stack? What is Stack? UNIT 2 Stack is an important data structure which stores its elements in an ordered manner. You must have seen a pile of plates where one plate is placed on top of another. Now, when you

More information

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows: STACKS A stack is a linear data structure for collection of items, with the restriction that items can be added one at a time and can only be removed in the reverse order in which they were added. The

More information

STACKS AND QUEUES. Problem Solving with Computers-II

STACKS AND QUEUES. Problem Solving with Computers-II STACKS AND QUEUES Problem Solving with Computers-II 2 Stacks container class available in the C++ STL Container class that uses the Last In First Out (LIFO) principle Methods i. push() ii. iii. iv. pop()

More information

Java generics. h"p:// h"p://

Java generics. hp://  hp:// Java generics h"p://www.flickr.com/photos/pdxdiver/4917853457/ h"p://www.flickr.com/photos/paj/4002324674/ CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2014 Overview Abstract

More information

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 165] Postfix Expressions

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 165] Postfix Expressions CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 165] Postfix Expressions We normally write arithmetic expressions using infix notation: the operator (such as +) goes in between the

More information

Data Structures and Algorithms

Data Structures and Algorithms Data Structures and Algorithms Alice E. Fischer Lecture 6: Stacks 2018 Alice E. Fischer Data Structures L5, Stacks... 1/29 Lecture 6: Stacks 2018 1 / 29 Outline 1 Stacks C++ Template Class Functions 2

More information

CSE Data Structures and Algorithms... In Java! Stacks. CSE2100 DS & Algorithms 1

CSE Data Structures and Algorithms... In Java! Stacks. CSE2100 DS & Algorithms 1 CSE 2100 Data Structures and Algorithms... In Java Stacks 1 What is Stack A stack is a collection of objects that are inserted and removed according to the last-in, first-out (LIFO) principle. Internet

More information

The Stack ADT. Stacks. The Stack ADT. The Stack ADT. Set of objects in which the location an item is inserted and deleted is prespecified.

The Stack ADT. Stacks. The Stack ADT. The Stack ADT. Set of objects in which the location an item is inserted and deleted is prespecified. The Stack ADT Stacks Set of objects in which the location an item is inserted and deleted is prespecified Stacks! Insert in order! Delete most recent item inserted! LIFO - last in, first out Stacks 2 The

More information

Stacks CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008

Stacks CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008 Chapter 7 s CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008 The Abstract Data Type: Specifications of an abstract data type for a particular problem Can emerge during the design of the

More information

Computer Science First Exams Pseudocode Standard Data Structures Examples of Pseudocode

Computer Science First Exams Pseudocode Standard Data Structures Examples of Pseudocode Computer Science First Exams 2014 Pseudocode Standard Data Structures Examples of Pseudocode Candidates are NOT permitted to bring copies of this document to their examinations. 1 Introduction The purpose

More information

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

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University CS 11 Introduction to Computing II Wayne Snyder Department Boston University Today Object-Oriented Programming Concluded Stacks, Queues, and Priority Queues as Abstract Data Types Reference types: Basic

More information

Stack Abstract Data Type

Stack Abstract Data Type Stacks Chapter 5 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop, peek, and empty To understand how Java implements a stack To learn how to implement a

More information

The Stack and Queue Types

The Stack and Queue Types The Stack and Queue Types Hartmut Kaiser hkaiser@cct.lsu.edu http://www.cct.lsu.edu/ hkaiser/fall_2012/csc1254.html 2 Programming Principle of the Day Do the simplest thing that could possibly work A good

More information

Queues Fall 2018 Margaret Reid-Miller

Queues Fall 2018 Margaret Reid-Miller Queues 15-121 Fall 2018 Margaret Reid-Miller Today Exam 2 is next Tuesday, October 30 Writing methods various classes that implement Lists. Methods using Lists and Big-O w/ ArrayList or LinkedLists Prove

More information

Complexity, General. Standard approach: count the number of primitive operations executed.

Complexity, General. Standard approach: count the number of primitive operations executed. Complexity, General Allmänt Find a function T(n), which behaves as the time it takes to execute the program for input of size n. Standard approach: count the number of primitive operations executed. Standard

More information

ADTs: Stacks and Queues

ADTs: Stacks and Queues Introduction to the Stack ADTs: Stack: a data structure that holds a collection of elements of the same type. - The elements are accessed according to LIFO order: last in, first out - No random access

More information

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES CH4.2-4.3. ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN JAVA, GOODRICH, TAMASSIA AND GOLDWASSER

More information

CS 106B Lecture 5: Stacks and Queues

CS 106B Lecture 5: Stacks and Queues CS 106B Lecture 5: Stacks and Queues Monday, July 3, 2017 Programming Abstractions Summer 2017 Stanford University Computer Science Department Lecturer: Chris Gregg reading: Programming Abstractions in

More information

Loop Invariants. while!done do // what is true at every step // Update/iterate // maintain invariant od CPS

Loop Invariants. while!done do // what is true at every step // Update/iterate // maintain invariant od CPS Loop Invariants Want to reason about the correctness of a proposed iterative solution Loop invariants provide a means to effectively about the correctness of code while!done do // what is true at every

More information

Programming Abstractions

Programming Abstractions Programming Abstractions C S 1 0 6 X Cynthia Lee Today s Topics ADTs Stack Example: Reverse-Polish Notation calculator Queue Example: Mouse Events Stacks New ADT: Stack stack.h template

More information

3.3: Encapsulation and ADTs

3.3: Encapsulation and ADTs Abstract Data Types 3.3: Encapsulation and ADTs Data type: set of values and operations on those values. Ex: int, String, Complex, Card, Deck, Wave, Tour,. Abstract data type. Data type whose representation

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Abstract Data Type Stack Version of February 2, 2013 Abstract These lecture notes are meant

More information

Summer Final Exam Review Session August 5, 2009

Summer Final Exam Review Session August 5, 2009 15-111 Summer 2 2009 Final Exam Review Session August 5, 2009 Exam Notes The exam is from 10:30 to 1:30 PM in Wean Hall 5419A. The exam will be primarily conceptual. The major emphasis is on understanding

More information

Java generics. CSCI 136: Fundamentals of Computer Science II Keith Vertanen

Java generics. CSCI 136: Fundamentals of Computer Science II Keith Vertanen Java generics h"p://www.flickr.com/photos/pdxdiver/4917853457/ h"p://www.flickr.com/photos/paj/4002324674/ CSCI 136: Fundamentals of Computer Science II Keith Vertanen Overview Abstract Data Types (ADTs)

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Abstract Data Type Stack Version of February 2, 2013 Abstract These lecture notes are meant

More information

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

Stacks. Stacks. Main stack operations. The ADT Stack stores arbitrary objects. Insertions and deletions follow the last-in first-out (LIFO) principle. Stacks 1 Stacks The ADT Stack stores arbitrary objects. Insertions and deletions follow the last-in first-out (LIFO) principle. 2 Main stack operations Insertion and removal are defined by: push(e): inserts

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

Data Structure. Recitation VII

Data Structure. Recitation VII Data Structure Recitation VII Recursion: Stack trace Queue Topic animation Trace Recursive factorial Executes factorial(4) Step 9: return 24 Step 8: return 6 factorial(4) Step 0: executes factorial(4)

More information

The Bucharest University of Economic Studies. Data Structures. ADTs-Abstract Data Types Stacks and Queues

The Bucharest University of Economic Studies. Data Structures. ADTs-Abstract Data Types Stacks and Queues The Bucharest University of Economic Studies Data Structures ADTs-Abstract Data Types Stacks and Queues Agenda Definition Graphical representation Internal interpretation Characteristics Operations Implementations

More information

LIFO : Last In First Out

LIFO : Last In First Out Introduction Stack is an ordered list in which all insertions and deletions are made at one end, called the top. Stack is a data structure that is particularly useful in applications involving reversing.

More information

CS251 Programming Languages Handout # 29 Prof. Lyn Turbak March 7, 2007 Wellesley College

CS251 Programming Languages Handout # 29 Prof. Lyn Turbak March 7, 2007 Wellesley College CS5 Programming Languages Handout # 9 Prof. Lyn Turbak March, 00 Wellesley College Postfix: A Simple Stack Language Several exercises and examples in this course will involve the Postfix mini-language.

More information

CSCI 136 Data Structures & Advanced Programming. Lecture 14 Fall 2018 Instructor: Bills

CSCI 136 Data Structures & Advanced Programming. Lecture 14 Fall 2018 Instructor: Bills CSCI 136 Data Structures & Advanced Programming Lecture 14 Fall 2018 Instructor: Bills Announcements Mid-Term Review Session Monday (10/15), 7:00-8:00 pm in TPL 203 No prepared remarks, so bring questions!

More information

Computer Science 501 Data Structures & Algorithms The College of Saint Rose Fall Topic Notes: Linear Structures

Computer Science 501 Data Structures & Algorithms The College of Saint Rose Fall Topic Notes: Linear Structures Computer Science 501 Data Structures & Algorithms The College of Saint Rose Fall 2015 Topic Notes: Linear Structures The structures we ve seen so far, Vectors and linked lists, allow insertion and deletion

More information

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures Computer Science 210 Data Structures Siena College Fall 2018 Topic Notes: Linear Structures The structures we ve seen so far, Vectors/ArrayLists and linked list variations, allow insertion and deletion

More information

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

Cpt S 122 Data Structures. Course Review Midterm Exam # 1 Cpt S 122 Data Structures Course Review Midterm Exam # 1 Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Midterm Exam 1 When: Friday (09/28) 12:10-1pm Where:

More information

An Introduction to Trees

An Introduction to Trees An Introduction to Trees Alice E. Fischer Spring 2017 Alice E. Fischer An Introduction to Trees... 1/34 Spring 2017 1 / 34 Outline 1 Trees the Abstraction Definitions 2 Expression Trees 3 Binary Search

More information

Stacks Fall 2018 Margaret Reid-Miller

Stacks Fall 2018 Margaret Reid-Miller Stacks 15-121 Fall 2018 Margaret Reid-Miller Today Exam 2 is next Tuesday, October 30 Today: Quiz 5 solutions Recursive add from last week (see SinglyLinkedListR.java) Stacks ADT (Queues on Thursday) ArrayStack

More information

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

Linked List. April 2, 2007 Programming and Data Structure 1 Linked List April 2, 2007 Programming and Data Structure 1 Introduction head A linked list is a data structure which can change during execution. Successive elements are connected by pointers. Last element

More information

CS 211. Project 5 Infix Expression Evaluation

CS 211. Project 5 Infix Expression Evaluation CS 211 Project 5 Infix Expression Evaluation Part 1 Create you own Stack Class Need two stacks one stack of integers for the operands one stack of characters for the operators Do we need 2 Stack Classes?

More information

Stacks and Queues. Chapter Stacks

Stacks and Queues. Chapter Stacks Chapter 18 Stacks and Queues 18.1 Stacks The stack abstract data type allows access to only one element the one most recently added. This location is referred to as the top of the stack. Consider how a

More information

Stacks Goodrich, Tamassia, Goldwasser Stacks

Stacks Goodrich, Tamassia, Goldwasser Stacks Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Stacks Stacks 1 The Stack ADT The Stack ADT

More information

Linear Data Structure

Linear Data Structure Linear Data Structure Definition A data structure is said to be linear if its elements form a sequence or a linear list. Examples: Array Linked List Stacks Queues Operations on linear Data Structures Traversal

More information

An Introduction to Data Structures

An Introduction to Data Structures An Introduction to Data Structures Advanced Programming ICOM 4015 Lecture 17 Reading: Java Concepts Chapter 20 Fall 2006 Adapded from Java Concepts Companion Slides 1 Chapter Goals To learn how to use

More information

Motivation for Queues

Motivation for Queues CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 178] Motivation for Queues Some examples of first-in, first-out (FIFO) behavior: æ waiting in line to check out at a store æ cars on

More information

Stacks and Queues. EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG

Stacks and Queues. EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG Stacks and Queues EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG What is a Stack? A stack is a collection of objects. Objects in a stack are inserted and removed according to the

More information

Problem with Scanning an Infix Expression

Problem with Scanning an Infix Expression Operator Notation Consider the infix expression (X Y) + (W U), with parentheses added to make the evaluation order perfectly obvious. This is an arithmetic expression written in standard form, called infix

More information

Parsing Scheme (+ (* 2 3) 1) * 1

Parsing Scheme (+ (* 2 3) 1) * 1 Parsing Scheme + (+ (* 2 3) 1) * 1 2 3 Compiling Scheme frame + frame halt * 1 3 2 3 2 refer 1 apply * refer apply + Compiling Scheme make-return START make-test make-close make-assign make- pair? yes

More information

Data structure and algorithm in Python

Data structure and algorithm in Python Data structure and algorithm in Python Stacks, Queues and Deques Xiaoping Zhang School of Mathematics and Statistics, Wuhan University Table of contents 1. Stacks 2. Queue 3. Double-Ended Queues 1 Stacks

More information

G Programming Languages - Fall 2012

G Programming Languages - Fall 2012 G22.2110-003 Programming Languages - Fall 2012 Lecture 3 Thomas Wies New York University Review Last week Names and Bindings Lifetimes and Allocation Garbage Collection Scope Outline Control Flow Sequencing

More information

CSEN 202 Introduction to Computer Programming

CSEN 202 Introduction to Computer Programming CSEN 202 Introduction to Computer Programming Lecture 4: Iterations Prof. Dr. Slim Abdennadher and Dr Mohammed Abdel Megeed Salem, slim.abdennadher@guc.edu.eg German University Cairo, Department of Media

More information

List, Stack, and Queues

List, Stack, and Queues List, Stack, and Queues R. J. Renka Department of Computer Science & Engineering University of North Texas 02/24/2010 3.1 Abstract Data Type An Abstract Data Type (ADT) is a set of objects with a set of

More information

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved Stacks Chapter 5 Copyright 2012 by Pearson Education, Inc. All rights reserved Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions A Problem Solved: Checking for Balanced

More information

Postscript Control Flow

Postscript Control Flow Postscript Control Flow CSE 413, Autumn 2005 Programming Languages http://www.cs.washington.edu/education/courses/413/05au/ Variables Postscript uses dictionaries to associate a name with an object value»

More information

The Stack ADT. Stacks and Queues. Stack: Illustration. What is a Stack? Accessors top size isempty

The Stack ADT. Stacks and Queues. Stack: Illustration. What is a Stack? Accessors top size isempty The Stack ADT Stacks and Queues ADT Data Structure Interface add() remove() find() request result EECS200: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG Accessors top Mutators push pop of

More information