Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Question about loop the queque elements to get the totals using peek()  RSS feed

 
Peter Cong
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am using java linkedquque. I want to loop the elements in the queque to get the totals.
so I use while loop and peek(). but this codes only return the first element of peek, how can I change the next element after the first peek?
---------------------------
while(! myShares.isEmpty() && myShares.peek() != null) {
tran = myShares.peek();
total = total + tran.getShares() * tran.getSharePrice();
//needs code here to point to next element

}
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which LinkedQueue are you referring to? Because there is no such thing in Java SE. Anyway have you look at the documentation for peek()?
 
Peter Cong
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the linked queque I am refering,
OK, let's make the question simple. For java queque, let's assume the elements of queque is integer, How do you get the total of the elements in the queque?
Yes, I read doc peek(), which return the head element of queque, now I want to loop to get next element until to the end of the queque.
something like peek().next. but I can not find it. Are there any other way?

-------------------------------------------
public class LinkedQueue<E> implements Queue<E> {

private static class Elem<F> {

private F value;
private Elem<F> next;

private Elem( F value, Elem<F> next ) {
this.value = value;
this.next = next;
}
}

private Elem<E> front;
private Elem<E> rear;

public E peek() {

// pre-conditions:

if ( front == null ) {
throw new EmptyQueueException();
}

return front.value;
}

public void enqueue( E o ) {

// pre-conditions:

if ( o == null ) {
throw new IllegalArgumentException( "null" );
}

Elem<E> newElem;
newElem = new Elem<E>( o, null );

if ( rear == null ) {
front = rear = newElem;
} else {
rear.next = newElem;
rear = rear.next;
}

}

public E dequeue() {

// pre-conditions:

if ( front == null ) {
throw new EmptyQueueException();
}

E result = front.value;

if ( front == rear ) {
front = rear = null;
} else {
front = front.next;
}

return result;
}

public boolean isEmpty() {
return front == null;
}

}
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags when posting code. It will highlight your code making it much easier to read. You can edit your post with the button. Please do so for both posts in this topic.

I'm afraid that you're using the wrong data-structure for your problem. A Queue is designed for holding elements prior to processing. Therefore you can only take elements from the head of the Queue. You can't iterate it (although there are collections which can because they also implement another interface such as List which allows you to do so).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!