Hi. I have to resolve some homework question. By using a series of instruction pop(), push(), peek() and empty(, i need to carry out some instruction.

- For example, set x to the 2nd element of stack, leaving stack without top element so it will be pop(s) and x=peek(s).

- Then I have example 2: instruction - set x to third element of stack, leaving stackk unchanged.

What you think about this sequence: pop(s),pop(s), x=peek(s), push(s), push(s), pop(s), pop(s), push(s),push(s)?

# Set elements of stack java

Page 1 of 1## 3 Replies - 1640 Views - Last Post: 07 October 2012 - 10:25 AM

##
**Replies To:** Set elements of stack java

### #2

## Re: Set elements of stack java

Posted 05 October 2012 - 09:47 AM

Wait. How are we suppose to know what element we are on? We don't know the size of the stack.

Also. The peek and pop methods have no arguments so is S suppose to be the stack?

Also. The peek and pop methods have no arguments so is S suppose to be the stack?

### #3

## Re: Set elements of stack java

Posted 05 October 2012 - 04:54 PM

hi. but the answer for first example is correct for 100% becuase its a teacher example. I did not get the size of the stack. Also it doesnt say in which position x actually is. For these exercises I can use the stack 1 nd stack 2, push(s,d) pushes element 'd' onto the stack. there is one more example to solve: 'set x to the bottom, leave stack empty'.

pop(s),pop(s), x=peek(s), push(s,d), push(s,e), pop(s), pop(s), push(s,e),push(s,d)? is this answer better for second example?

pop(s),pop(s), x=peek(s), push(s,d), push(s,e), pop(s), pop(s), push(s,e),push(s,d)? is this answer better for second example?

pop(s),pop(s), x=peek(s), push(s,d), push(s,e), pop(s), pop(s), push(s,e),push(s,d)? is this answer better for second example?

pop(s),pop(s), x=peek(s), push(s,d), push(s,e), pop(s), pop(s), push(s,e),push(s,d)? is this answer better for second example?

### #4

## Re: Set elements of stack java

Posted 07 October 2012 - 10:25 AM

Hi. If someone can close this topic? I have found some explanation. Dnt need ny more answer. thanks anyway

Page 1 of 1