COL106 Assignment 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

COL

106 Assignment 1

Due: Friday Aug 8, 11:59 pm

Total : 30 points

1 (10 pts). Implement the following interface for stack using a growable array as
discussed in class

template <typename Object>
class Stack {
public:
int size();
bool isEmpty();
Object& top()



throw(EmptyStackException);
void push(Object o);
Object pop()



throw(EmptyStackException);
};

2 (10 pts). Implement the following interface for queue using a circular linked list

template <typename Object>
class Queue {
public:
int size();
bool isEmpty();
Object& front()



throw(EmptyQueueException);
void enqueue(Object o);
Object dequeue()



throw(EmptyQueueException);
};

3 (10 pts). A queue can also be implemented using two stacks. One stack is used to
enqueue elements, while the other is used to dequeue elements. That is, when an
element is added to the queue it is pushed on the in stack. When an element is
removed from the queue it is popped off the out stack. If the out stack is empty, the
contents of the in stack is transferred to the out stack.

Implement the queue using two stacks (that you implemented in Problem 1). What
is the amortized run time of enqueue and dequeue operations?


4. (Need not be submitted but please solve) Alice has three array-based stacks, A, B,
and C, such that A has capacity 100, B has capacity 5, and C has capacity 3. Initially, A
is full, and B and C are empty. Unfortunately, the person who programmed the class

for these stacks made the push and pop functions private. The only function Alice
can use is a static function, transfer(S,T), which transfers (by iteratively applying the
private pop and push functions) elements from stack S to stack T until either S
becomes empty or T becomes full. So, for example, starting from our initial
configuration and performing transfer(A,C) results in A now holding 97 elements
and C holding 3. Describe a sequence of transfer operations that starts from the
initial configuration and results in B holding 4 elements at the end.

You might also like