简体   繁体   中英

Stack and Queue with java

I was trying to solve the question below, but i was so unsure of the function of Q.insert(S.top()) Below is the question:
Fill the table showing the output of a series of priority queue and stack operations and their effect on an initially empty priority queue Q of integers and an empty stack S of integers. Make sure to indicate the orientation of each data structure you display (ie front, rear, top, bottom). (10p)

| Operation | Output | Stack S | Queue Q | rear | front |

Q.insert(9)

Q.insert(11)

Q.peek()

S.push(8)

Q.insert(S.top())

S.push(Q.remove())

Q.insert(Q.remove())

S.size() == Q.size()

Assuming the queue Q and the stack S are empty at the start of this series of operations...

S.push(8)

puts 1 element on the stack with value '8'

S.top()

then returns '8'

When calling a function like Q.insert(<param>) the parameter does not have to be a constant; it could be a variable or anything that will produce the right type of parameter.

So if you have methods such as

public void insert(int n);
public int top(); // returns an int(eger)

You can call insert any of these ways

int meaning = 42;
Q.insert( 10 );
Q.insert( meaning );
Q.insert( Math.round(3.14) ); // 'round' produces an int

so...

Q.insert(S.top())

inserts the '8' returned from S.top() into the queue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM