0% found this document useful (0 votes)
29 views6 pages

Stacks

The document discusses stacks, queues, and algorithms analysis questions. For question 8, a ticket counter simulation is run with 200 customers per day, a service time of 5 minutes per customer, and customers arriving every 20 seconds. The output shows that with 10 cashiers, the average customer wait time is below 10 minutes.

Uploaded by

moh543senob
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views6 pages

Stacks

The document discusses stacks, queues, and algorithms analysis questions. For question 8, a ticket counter simulation is run with 200 customers per day, a service time of 5 minutes per customer, and customers arriving every 20 seconds. The output shows that with 10 cashiers, the average customer wait time is below 10 minutes.

Uploaded by

moh543senob
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Stacks:

Q1: Please show how the following postfix expression can be evaluated using Stack as shown in

the class. Please show the stack contents and the operations.

4 7 5 3 + * 16 9 - / *

Ans: 32

Q2: A program performs the following operations on a stack S and Queue Q:

a. S.push(52);

b. S.push(52);

c. S.push(S.peek()*2);

d. System.out.println(S.size()); 104 36

e. System.out.println(S.pop()); 52 28

f. S.pop(); 52 0

g. System.out.println(S.size());

h. Q.enqueue(36); stack Queue

i. Q.enqueue(28);

j. Q.enqueue(Q.first()%3);

k. System.out.println(Q.first())

l. System.out.println(Q.dequeue());

m. System.out.println(S.pop());

The output from stack and Queue is:

104

36

36
Q3: What are the benefits of implementing stack using a LinkedList rather than an array based

implementation?

ANS: implementing stack implementation array

LinkedList<string> ArrayList<Integer>

LinkedList< Integer> ArrayStack<T>

LinkedList< T> ArrayList<String>

push()

pop() add ()

peek() remove()

isEmpty() set()

size() get()

toString() size()

Queue:

Q4: A program performs the following operations on an empty queue Q:

n. Q.enqueue(24)

o. Q.enqueue(74)

p. Q.enqueue(34)

q. Q.first()

r. Q.dequeue()

s. Q.enqueue(12)

t. Q.dequeue()

Please show the queue contents at the end of these operations. Clearly show the front and rear of

the queue.

24 74 34 12

34 12
Algorithm Analysis:

Q5: Please identify the time complexity of following code (using Big O notation). Assume that

eval method has a time complexity of O(n). Briefly explain why?

int val = 2*n;

for(int i = 1; i < val; i=i++)

eval(val);

Ans: O(n2)

Q6. What is the time complexity (in Big O) of the method test_code? Assume that test_1 has

complexity O(logn), test_2 has complexity O(n) and test_3 has complexity O(n 3). Briefly explain

why?

Ans: O(n3)

Q7. A program creates a queue. The program takes array of user names as String and then performs

following operations:

- If the user name starts with A to S, it will add (enqueue) to the queue

- If the user name starts with T to Z, it will remove (dequeue) one element from the queue and
then add both the user names to the queue.

What will the queue look like if the input array is as follows? Please clearly label front and rear of the

queue.

[“Saeed”, “Akram”, “Jamal”, “Tanveer”, “Usman”, “Ahmed”, “Dawood”, “Waheed”]

Ans: [“Tanveer”, “Saeed”, “Usman”, “Akram”, “Ahmed”, “Dawood”, “Waheed”, “Jamal”]

Q8. Please run the ticket counter simulation code with following parameters:

a. Number of customers per day = 200

b. Single Customer Service Time = 5 minutes

c. A customer arrives in the system every 20 seconds

How many cashiers are needed to keep the average customer wait time below 10 minutes?

Ans:

public class TicketCounterExample

private final static int PROCESS = 600;

private final static int MAX_CASHIERS = 200;

private final static int NUM_CUSTOMERS = 250;

public static void main(String[] args)

Customer customer;

Queue<Customer> customerQueue = new LinkedList<Customer>();

int[] cashierTime = new int[MAX_CASHIERS];

int totalTime, averageTime, departs, start;

for (int cashiers = 0; cashiers < MAX_CASHIERS; cashiers++)

{
for (int count = 0; count < cashiers; count++)

cashierTime[count] = 0;

for (int count = 1; count <= NUM_CUSTOMERS; count++)

customerQueue.add(new Customer(count * 20));

totalTime = 0;

while (!(customerQueue.isEmpty()))

for (int count = 0; count <= cashiers; count++)

if (!(customerQueue.isEmpty()))

customer = customerQueue.remove();

if (customer.getArrivalTime() > cashierTime[count])

start = customer.getArrivalTime();

else

start = cashierTime[count];

departs = start + PROCESS;

customer.setDepartureTime(departs);

cashierTime[count] = departs;

totalTime += customer.totalTime();

averageTime = totalTime / NUM_CUSTOMERS;

System.out.println("Number of customers per day : " + (cashiers + 1));

System.out.println("Average time: " + averageTime + "\n");

}
}

You might also like