Week 6 Oliver Kullmann Dynamic sets Data structures Simple - - PowerPoint PPT Presentation

week 6
SMART_READER_LITE
LIVE PREVIEW

Week 6 Oliver Kullmann Dynamic sets Data structures Simple - - PowerPoint PPT Presentation

CS 270 Algorithms Week 6 Oliver Kullmann Dynamic sets Data structures Simple implementa- tion Dynamic sets 1 Special cases Simple implementation 2 Stacks Implementatio Special cases 3 Queues Implementatio Stacks 4 Tutorial


slide-1
SLIDE 1

CS 270 Algorithms Oliver Kullmann Dynamic sets Simple implementa- tion Special cases Stacks Implementatio Queues Implementatio Tutorial

Week 6 Data structures

1

Dynamic sets

2

Simple implementation

3

Special cases

4

Stacks

5

Implementation

6

Queues

7

Implementation

8

Tutorial

slide-2
SLIDE 2

CS 270 Algorithms Oliver Kullmann Dynamic sets Simple implementa- tion Special cases Stacks Implementatio Queues Implementatio Tutorial

General remarks

We start considering Part III “Data Structures” from CLRS. As a first example we consider two special cases of “buffers”, namely stacks and queues.

Reading from CLRS for week 5

The introduction to Part III on using sets on a computer. Chapter 10, Section 10.1.

slide-3
SLIDE 3

CS 270 Algorithms Oliver Kullmann Dynamic sets Simple implementa- tion Special cases Stacks Implementatio Queues Implementatio Tutorial

Sets

The most fundamental mathematical notion is that of a set: We have the possibility to determine the elements of a set. And we can form sets, either by some set-defining property,

  • r by using already given sets (e.g., unions, intersections,

differences). Now to bring the eternal and infinite world of mathematics to a computer, we need to take care of construction of “objects” destruction of “objects” naming (basically of functions)

  • rder issues (sets are unordered, but in computation there is

always order). For this, CLRS uses the (generic) ADT of “dynamic sets”. ADT: “abstract data type” — values (like “sets”) and how to operate with them.

slide-4
SLIDE 4

CS 270 Algorithms Oliver Kullmann Dynamic sets Simple implementa- tion Special cases Stacks Implementatio Queues Implementatio Tutorial

Elements of a dynamic sets

A “dynamic set” might contain pointers (or iterators) to objects,

  • r the objects themselves (in Java this can be only integers

and other primitive types, in C++ this is possible for every type of object). Whatever the objects in a set are, access to them (especially for changing them) is only possible via a pointer (or iterator). For insertion into a dynamic set, we must be given the

  • bject itself, and typically we obtain the pointer (iterator,

handle) to the copy of that object in the dynamic set back. For deletion from a dynamic set, we typically have the pointer of an object (already) in the dynamic set, and we want to delete that (very specific) object. For searching, we typically have only given some “key information”, and we want to search for some element in the dynamic set, which fits this (key) information.

slide-5
SLIDE 5

CS 270 Algorithms Oliver Kullmann Dynamic sets Simple implementa- tion Special cases Stacks Implementatio Queues Implementatio Tutorial

Keys

Besides objects (which become “elements” once they are in the set) and pointers, CLRS uses the notion of a key to identify an

  • bject:

If the object is for example a record of personal attributes, then the name or some form of ID can be used as a key. Often they keys are used for sorting. For example for that database of personal attributes, we might sort it according to alphabetical sorting of names.

slide-6
SLIDE 6

CS 270 Algorithms Oliver Kullmann Dynamic sets Simple implementa- tion Special cases Stacks Implementatio Queues Implementatio Tutorial

Dynamic sets: elementship and search

With sets S we can “ask” whether “x ∈ S” is true. This is the most basic set operation, and the equivalent for dynamic sets is the operation SEARCH(S, k) for key k and dynamic set S, returning either a pointer (iterator) to an object in S with key k, or NIL if there is no such object. We require the ability to extract the key from an object in S, and to compare keys for equality.

1 Storing S via an array (or a list), SEARCH can be

performed in O(|S|) time (that is, linear time) by simple sequential search.

2 To do faster than this, typically in time O(log(|S|))

(logarithmic time), under various circumstances, is a major aim of data structures for dynamic sets.

slide-7
SLIDE 7

CS 270 Algorithms Oliver Kullmann Dynamic sets Simple implementa- tion Special cases Stacks Implementatio Queues Implementatio Tutorial

Dynamic sets: modifying operations

With the following operations we can build and change dynamic sets: INSERT(S, x) inserts an object into dynamic set S, where x is either a pointer or the object itself. DELETE(S, x) deletes an object from dynamic set S, where here x is a pointer (iterator) into S. Note that the “x” in DELETE is of a different nature than the “x” in INSERT: It is a pointer (iterator!) into the (dynamic) set (and thus these two x are of different type). The most important application of INSERT is for creating a dynamic set: To create a set S of size n, call INSERT(S, −) n-times. We always have INSERT, while we might not have DELETE.

slide-8
SLIDE 8

CS 270 Algorithms Oliver Kullmann Dynamic sets Simple implementa- tion Special cases Stacks Implementatio Queues Implementatio Tutorial

Dynamic sets: using order

Often it is assumed that a linear order is given on the keys: So besides “k == k′ ?” we now can ask “k ≤ k′ ?”. In practice using strict orders “<” is more common, however this creates some (necessary) technical complications, which in this module we won’t be much concerned about (we discuss issues when the need arises).

(These complications have to do with the notion of “equality”, since “<” includes “not equal”. Considering Java, recall that there (lacking

  • perator overloading and lacking the ability to distinguish between

“object” and “pointer”) you have two operations for checking “equality”: “==” for object-identity and “.equals()” for

  • bject-equality, and now we needed appropriate object-equality

(consistent with the algorithms).)

slide-9
SLIDE 9

CS 270 Algorithms Oliver Kullmann Dynamic sets Simple implementa- tion Special cases Stacks Implementatio Queues Implementatio Tutorial

Dynamic sets: four further operations

Given a linear order on the objects (to be put into the set), we have the following additional operations: MINIMUM(S) returns a pointer (iterator) to the element with the smallest key MAXIMUM(S) returns a pointer (iterator) to the element with the largest key SUCCESSOR(S, x), where x is a pointer (iterator) into S, returns the next element in S w.r.t. the order on the keys PREDECESSOR(S, x), where x is a pointer (iterator) into S, returns the previous element in S. Operations for computing successors and predecessors can fail (there is no successor resp. predecessor iff we are already at the end resp. beginning), and in such cases we return NIL.

slide-10
SLIDE 10

CS 270 Algorithms Oliver Kullmann Dynamic sets Simple implementa- tion Special cases Stacks Implementatio Queues Implementatio Tutorial

Using sorting algorithms: static case

Dynamic sets can be realised using sorting, where we have to assume a linear order on the keys. If the set S with n elements is to be built only once, at the beginning, from a sequence of elements, then storing the elements in an array and sorting them, using for example MERGE-SORT with time complexity O(n · log n), is a good

  • ption:

SEARCH then takes time O(log n) (using binary search) while each of MINIMUM, MAXIMUM, SUCCESSOR, PREDECESSOR takes constant time. However we are concerned here with the dynamic case, where insertions and deletions are used in unpredictable ways. If we not assume that building the set is done (once and for all) at the beginning, but we want to have insertion and deletion, then the case is much less favourable.

slide-11
SLIDE 11

CS 270 Algorithms Oliver Kullmann Dynamic sets Simple implementa- tion Special cases Stacks Implementatio Queues Implementatio Tutorial

Using sorting algorithms: dynamic case

Now INSERTION and DELETION take time O(log(n) + n) = O(n), searching first for the right insertion/deletion place, and then shifting the other elements appropriately, while the five other (non-modifying) operations still take logarithmic resp. constant time. For practical applications, the linear complexity of insertion and deletion is not acceptable. And we also see that most of the intelligence of sophisticated searching algorithms is blown out of the window, and only some form of INSERTION-SORT (in combination with binary search) survived. It could be said that data structures for dynamic sets try to introduce some of the intelligent methods into the dynamic framework, making insertion and deletion more efficient (necessarily at the cost of making the other operations (somewhat) less efficient).

slide-12
SLIDE 12

CS 270 Algorithms Oliver Kullmann Dynamic sets Simple implementa- tion Special cases Stacks Implementatio Queues Implementatio Tutorial

Special cases of dynamic sets

Often not all of the operations for dynamic sets are needed,

  • pening up the possibilities of specialised and more efficient
  • implementations. Three important cases are as follows:

Buffer Only INSERTION, SHOW-SELECTED-ELEMENT (like MINIMUM and MAXIMUM, but not necessarily related to some order) and DELETE-SELECTED-ELEMENT; special cases are “stacks” and “queues”. Priority queue Only INSERTION, MINIMUM resp. MAXIMUM (for min- resp. max-priority queues) and DELETE-MIN resp. DELETE-MAX (special queues, where the selected element is given by a linear order on the keys). Dictionary Only INSERTION, DELETION and SEARCH (i.e., no order-requirements).

slide-13
SLIDE 13

CS 270 Algorithms Oliver Kullmann Dynamic sets Simple implementa- tion Special cases Stacks Implementatio Queues Implementatio Tutorial

Special cases of dynamic sets (cont.)

In other words: Buffers (stacks, queues, and priority queues) have the notion of a “selected element”, which they can show and delete (but general deletion is not possible); searching in the general sense is not possible. Dictionaries can perform arbitrary deletions and searches, but they have no order on the elements (and thus also no “special elements”). Insertion is always possible (given that there is enough space).

slide-14
SLIDE 14

CS 270 Algorithms Oliver Kullmann Dynamic sets Simple implementa- tion Special cases Stacks Implementatio Queues Implementatio Tutorial

Last in – first out

A stack is a buffer where the selected element is the element last entered. Thus a stack is characterised by the slogan “last in – first out” LIFO. One could also say “first in – last out” (FILO). So if you enter numbers 1, 2, 3 into a stack (in that order), then you get back 3, 2, 1 (in that order).

slide-15
SLIDE 15

CS 270 Algorithms Oliver Kullmann Dynamic sets Simple implementa- tion Special cases Stacks Implementatio Queues Implementatio Tutorial

Stack operations: PUSH, POP, TOP

For dynamic sets in general we talk about INSERT, but for stacks this is called PUSH. And the DELETE operation (which here deletes a selected element) is called POP. The operation for returning the selected element is called TOP. In the book the operation POP combines the above POP (just deleting the selected element) and the above TOP (just returning the selected element), which is the old style for a stack, which has been shown to have severe disadvantages: Amongst others, separation of concerns requires that we have two different operations POP and TOP.

slide-16
SLIDE 16

CS 270 Algorithms Oliver Kullmann Dynamic sets Simple implementa- tion Special cases Stacks Implementatio Queues Implementatio Tutorial

Examples

If we have a stack, and perform (in this order) PUSH(1), PUSH(2), PUSH(3), then TOP yields 3.

1 After POP then TOP yields 2. 2 And after another POP then TOP yields 1. 3 A final POP yields an empty stack.

Note that we have precisely as many PUSH as POP instructions (that holds for every buffer). What happens if on an empty stack we use POP or TOP ?!?

slide-17
SLIDE 17

CS 270 Algorithms Oliver Kullmann Dynamic sets Simple implementa- tion Special cases Stacks Implementatio Queues Implementatio Tutorial

The EMPTY operation

In order to check whether the stack is empty, the fourth stack-operation is EMPTY, returning a boolean which is true if and only if the stack is empty. It depends on the concrete stack-implementation (i.e., on the interface) what happens in case of an error. There are two principle error possibilities: stack overflow and stack underflow. Overflow means that a push-operation exceeds the stack-capacity. Underflow means the use of a top- or pop-operation on an empty stack.

slide-18
SLIDE 18

CS 270 Algorithms Oliver Kullmann Dynamic sets Simple implementa- tion Special cases Stacks Implementatio Queues Implementatio Tutorial

Implementation via an array

Using an array we can easily implement the ADT Stack:

1 The class (Java or, say, C++) contains as data member

(“instance variable”) a fixed-size array.

2 We fill that array from the left. 3 An index-variable points to the currently open slot in the

array.

4 Popping an element from the stack just means

decrementing that index. We consider an implementation via the Java-class Stack.

slide-19
SLIDE 19

CS 270 Algorithms Oliver Kullmann Dynamic sets Simple implementa- tion Special cases Stacks Implementatio Queues Implementatio Tutorial

Class Stack

class Stack { private f i n a l int [ ] stack ; private f i n a l int N; // max number elements private int n ; // number

  • f

elements public Stack ( f i n a l int N ) { // Standard e x c e p t i o n s r a i s e d i f N < 0 // or N i s too big f o r a v a i l a b l e memory . N = N ; stack = new int [N ] ; n = 0; } Remark: Note that n is the current number of elements as well as the index of the next open array-slot.

slide-20
SLIDE 20

CS 270 Algorithms Oliver Kullmann Dynamic sets Simple implementa- tion Special cases Stacks Implementatio Queues Implementatio Tutorial

Class Stack (cont.)

public boolean empty () { return n==0; } public int top () { // Standard exception r a i s e d i f n == 0. a s s e r t ( stack != n u l l && stack . length== N) ; a s s e r t (n >= 0 && n <= N) ; return stack [ n −1]; } Remark: assert is here used for conditions which are enforced by the internal logic of the class (and thus a violation would show that there is a bug in the class definition). The case n == 0 is absolutely possible according to the logic of the class Stack: Either the caller has to ensure that this never happens, or the exception has to be handled.

slide-21
SLIDE 21

CS 270 Algorithms Oliver Kullmann Dynamic sets Simple implementa- tion Special cases Stacks Implementatio Queues Implementatio Tutorial

Class Stack (cont.)

public boolean push ( f i n a l int x ) { a s s e r t (n >= 0 && n <= N) ; i f (n == N) return f a l s e ; a s s e r t ( stack != n u l l && stack . length== N) ; stack [ n++] = x ; return true ; } public boolean pop () { a s s e r t (n >= 0) ; a s s e r t (n <= N) ; i f (n == 0) return f a l s e ; −−n ; return true ; } Remarks: push and pop don’t throw (exceptions). Both

  • perations can fail (if we have already N elements, or there is no

element left), in which case they return false.

slide-22
SLIDE 22

CS 270 Algorithms Oliver Kullmann Dynamic sets Simple implementa- tion Special cases Stacks Implementatio Queues Implementatio Tutorial

Class Stack (cont.)

// a d d i t i o n a l f u n c t i o n a l i t y : public int s i z e () { return n ; } public int max size () { return N; } public boolean equals ( Stack S) { i f (n != S . n) return f a l s e ; for ( int i = 0; i < n ; ++i ) i f ( stack [ i ] != S . stack [ i ] ) return f a l s e ; return true ; } Remark: Recall that for Stack-variables a, b the comparison a == b checks whether these pointers are equal — if we wish to check for equal content we need to use a.equals(b).

slide-23
SLIDE 23

CS 270 Algorithms Oliver Kullmann Dynamic sets Simple implementa- tion Special cases Stacks Implementatio Queues Implementatio Tutorial

Class Stack (cont.)

public S t r i n g t o S t r i n g () { S t r i n g

  • ut = ” [ ” + n + ” , ” + N + ” ]\ n” ;

for ( int i = 0; i < n−1; ++i )

  • ut += stack [ i ] + ” ” ;

i f (n > 0)

  • ut += stack [ n −1];

return

  • ut ;

} } Remark: Here we take care to avoid a trailing space (after the last stack element).

slide-24
SLIDE 24

CS 270 Algorithms Oliver Kullmann Dynamic sets Simple implementa- tion Special cases Stacks Implementatio Queues Implementatio Tutorial

First in – first out

A queue is a buffer where the selected element is the element first entered. Thus a queue is characterised by the slogan “first in – first out” FIFO. One could also say “last in – last out” (“LILO”), but that is apparently not used. So if you enter numbers 1, 2, 3 into a queue (in that order), then you get back 1, 2, 3 (in that order).

slide-25
SLIDE 25

CS 270 Algorithms Oliver Kullmann Dynamic sets Simple implementa- tion Special cases Stacks Implementatio Queues Implementatio Tutorial

Queue operations

We will use very similar notions as for stacks: The only difference is that instead of TOP we use FRONT. Besides that we have PUSH, POP, EMPTY. The book uses ENQUEUE instead of PUSH, and DEQUEUE instead of POP. However our choice is more popular with programming languages.

slide-26
SLIDE 26

CS 270 Algorithms Oliver Kullmann Dynamic sets Simple implementa- tion Special cases Stacks Implementatio Queues Implementatio Tutorial

Examples

If we have a queue, and perform (in this order) PUSH(1), PUSH(2), PUSH(3), then FRONT yields 1.

1 After POP then FRONT yields 2. 2 And after another POP then FRONT yields 3. 3 A final POP yields an empty queue.

Note that we have precisely as many PUSH as POP instructions (that holds for every buffer).

slide-27
SLIDE 27

CS 270 Algorithms Oliver Kullmann Dynamic sets Simple implementa- tion Special cases Stacks Implementatio Queues Implementatio Tutorial

Implementation via an array

Using an array we can (relatively) easily implement the ADT Queue:

1 The class (Java or, say, C++) contains as data member

(instance variable) a fixed-size array.

2 We have furthermore index-variables for the left (“head”)

and the right (“tail”) end.

3 PUSH moves forward the right end, POP the left end. 4 We do not need to re-allocate the elements, when we reach

the right boundary of the array while actually not the whole array is filled, since we can “wrap around” the part of the array used for the queue.

5 For a real implementation, one needs to get the details

right, but the basic idea is very simple. We consider an implementation via the Java-class Queue.

slide-28
SLIDE 28

CS 270 Algorithms Oliver Kullmann Dynamic sets Simple implementa- tion Special cases Stacks Implementatio Queues Implementatio Tutorial

Class Queue

class Queue { private f i n a l int [ ] queue ; private f i n a l int N; // maximal number private int n ; // number

  • f

elements private int a , b ; // f i r s t and one−past−the −l a s t index

  • f

c u r r e n t elements public Queue ( f i n a l int N ) { // Standard e x c e p t i o n s r a i s e d i f N < 0 or N i s too big f o r a v a i l a b l e memory . N = N ; queue = new int [N ] ; n = a = b = 0; }

slide-29
SLIDE 29

CS 270 Algorithms Oliver Kullmann Dynamic sets Simple implementa- tion Special cases Stacks Implementatio Queues Implementatio Tutorial

Class Queue (cont.)

public boolean empty () { return n==0; } public int f r o n t () { // Queue underflow i f n==0 ( not detected ) a s s e r t ( queue!= n u l l && queue . length== N) ; a s s e r t ( a >= 0 && a < N) ; return queue [ a ] ; }

slide-30
SLIDE 30

CS 270 Algorithms Oliver Kullmann Dynamic sets Simple implementa- tion Special cases Stacks Implementatio Queues Implementatio Tutorial

Class Queue (cont.)

public boolean push ( f i n a l int x ) { a s s e r t (n >= 0 && n <= N) ; i f (n == N) return f a l s e ; a s s e r t ( queue!= n u l l && queue . length== N) ; a s s e r t (b >= 0 && b < N) ; queue [ b ] = x ; i f (b == N−1) b = 0; else ++b ; ++n ; return true ; }

slide-31
SLIDE 31

CS 270 Algorithms Oliver Kullmann Dynamic sets Simple implementa- tion Special cases Stacks Implementatio Queues Implementatio Tutorial

Class Queue (cont.)

public boolean pop () { a s s e r t (n >= 0 && n <= N) ; i f (n == 0) return f a l s e ; a s s e r t ( a >= 0 && a < N) ; i f ( a == N−1) a = 0; else ++a ; −−n ; return true ; }

slide-32
SLIDE 32

CS 270 Algorithms Oliver Kullmann Dynamic sets Simple implementa- tion Special cases Stacks Implementatio Queues Implementatio Tutorial

Class Queue (cont.)

// a d d i t i o n a l f u n c t i o n a l i t y : public int s i z e () { return n ; } public int max size () { return N; } public boolean equals ( Queue S) { i f (n != S . n) return f a l s e ; for ( int i=a , j=S . a , c=0; c<n ; i =( i== N−1) ?0: i +1, j =( j== N−1) ?0: j +1, ++c ) i f ( queue [ i ] != S . queue [ j ] ) return f a l s e ; return true ; } Note that c is used here as a counter.

slide-33
SLIDE 33

CS 270 Algorithms Oliver Kullmann Dynamic sets Simple implementa- tion Special cases Stacks Implementatio Queues Implementatio Tutorial

Class Queue (cont.)

public S t r i n g t o S t r i n g () { S t r i n g

  • ut = ” [ ” + n + ” , ” + N + ” ]\ n” ;

for ( int i = a , c = 0; c < n ; i = ( i== N−1) ? 0: i +1, ++c )

  • ut += queue [ i ] + ” ” ;

return

  • ut ;

} }

slide-34
SLIDE 34

CS 270 Algorithms Oliver Kullmann Dynamic sets Simple implementa- tion Special cases Stacks Implementatio Queues Implementatio Tutorial

Pushing and popping

From [Sedgewick, Exercise 4.6]: A letter means push and an asterisk means pop in the following

  • sequence. Give the sequence of values returned by the pop
  • perations when this sequence of operations is performed on an

initially empty stack: E A S * Y * Q U E * * * S T * * * I O * N * * * Solution: S Y E U Q T S A O N I E

slide-35
SLIDE 35

CS 270 Algorithms Oliver Kullmann Dynamic sets Simple implementa- tion Special cases Stacks Implementatio Queues Implementatio Tutorial

Pushing and popping

From [Sedgewick, Exercise 4.6]: A letter means push and an asterisk means pop in the following

  • sequence. Give the sequence of values returned by the pop
  • perations when this sequence of operations is performed on an

initially empty stack: E A S * Y * Q U E * * * S T * * * I O * N * * * Solution: S Y E U Q T S A O N I E

slide-36
SLIDE 36

CS 270 Algorithms Oliver Kullmann Dynamic sets Simple implementa- tion Special cases Stacks Implementatio Queues Implementatio Tutorial

Checking balancedness

Develop the idea for a program that reads in a sequence of characters, and determines whether its parentheses, braces, and curly braces are “balanced”. For example ( [ { ( [ ] ) ( [ ] ) } ] ) is balanced, while ( ( [ { } { } [ ] ) ) ) is not. Solution: When you read an opening bracket-symbol, push it onto the stack, when you read a closing bracket-symbol, check whether it’s the top-symbol, and pop it.

slide-37
SLIDE 37

CS 270 Algorithms Oliver Kullmann Dynamic sets Simple implementa- tion Special cases Stacks Implementatio Queues Implementatio Tutorial

Checking balancedness

Develop the idea for a program that reads in a sequence of characters, and determines whether its parentheses, braces, and curly braces are “balanced”. For example ( [ { ( [ ] ) ( [ ] ) } ] ) is balanced, while ( ( [ { } { } [ ] ) ) ) is not. Solution: When you read an opening bracket-symbol, push it onto the stack, when you read a closing bracket-symbol, check whether it’s the top-symbol, and pop it.

slide-38
SLIDE 38

CS 270 Algorithms Oliver Kullmann Dynamic sets Simple implementa- tion Special cases Stacks Implementatio Queues Implementatio Tutorial

Pushing and popping again

A letter means enqueue and an asterisk means dequeue in the sequence E A S * Y * Q U E * * * S T * * * I O * N * * * Give the sequence of values returned by the dequeue operations when this sequence of operations is performed on an initially empty queue. Solution: E A S Y Q U E S T I O N

slide-39
SLIDE 39

CS 270 Algorithms Oliver Kullmann Dynamic sets Simple implementa- tion Special cases Stacks Implementatio Queues Implementatio Tutorial

Pushing and popping again

A letter means enqueue and an asterisk means dequeue in the sequence E A S * Y * Q U E * * * S T * * * I O * N * * * Give the sequence of values returned by the dequeue operations when this sequence of operations is performed on an initially empty queue. Solution: E A S Y Q U E S T I O N

slide-40
SLIDE 40

CS 270 Algorithms Oliver Kullmann Dynamic sets Simple implementa- tion Special cases Stacks Implementatio Queues Implementatio Tutorial

A queue via two stacks

Can we implement a queue using two stacks in an efficient way ?! Solution: the basic idea is − · − = +

1 Use two stacks, IN and OUT. 2 The queue is empty iff both stacks are empty. 3 push(x) : IN.push(x). 4

front (): if OUT is not-empty, perform OUT.top(),

  • therwise pop everything from IN, push it to OUT, and

then perform OUT.top().

5 pop(): The same as with front, only replacing OUT.top()

with OUT.pop().

slide-41
SLIDE 41

CS 270 Algorithms Oliver Kullmann Dynamic sets Simple implementa- tion Special cases Stacks Implementatio Queues Implementatio Tutorial

A queue via two stacks

Can we implement a queue using two stacks in an efficient way ?! Solution: the basic idea is − · − = +

1 Use two stacks, IN and OUT. 2 The queue is empty iff both stacks are empty. 3 push(x) : IN.push(x). 4

front (): if OUT is not-empty, perform OUT.top(),

  • therwise pop everything from IN, push it to OUT, and

then perform OUT.top().

5 pop(): The same as with front, only replacing OUT.top()

with OUT.pop().