Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating an Iterator for a custom stack based on singly-linked nodes
    primarykey
    data
    text
    <p>For an assignment, I must create a custom stack class that implements the common functions of java.util.Stack. One of these stacks is based, by class extension, on the actual java.util.Stack class. The other, however, is based on creating 'Linkable' nodes that allow you to create a singly-linked stack that you can push or pop from (<code>Linkable</code>, in this instance, is an interface consisting of <code>getNext()</code> and <code>setNext()</code>). I have the basic code for that, and it is basically as follows:</p> <pre><code>public class LStack { Linkable head; public pop() { /* Pop element off of stack */ } public push() { /* Push element onto stack */ } public front() { /* Return tail of stack without removing it */ } public makeNull() { /* Clear entire stack */ } public empty() { /* Return true or false based on emptiness of stack */ } } </code></pre> <p>For the next part of the assignment, I must create an instance of <code>Iterator</code> using the <code>iterator()</code> method I will implement in <code>LStack</code>. Upon trying to implement the iterator, I have gotten stuck trying to figure out how to implement the <code>Iterator.next()</code> method override. Unfortunately, my professor is vehemently opposed to generics so I cannot use <code>Iterator&lt;E&gt;</code> or anything similar to provide context to what I am iterating over. Here is my current <code>Iterator</code> code:</p> <pre><code>class LIterator implements Iterator { Object current; LStack stack; LIterator(LStack stack) { current = null; this.stack = stack; } // .... unimplemented next(), hasNext(), remove() } </code></pre> <p>How can I retrieve the next object from the iterator? I have tried casting to a <code>Linkable</code> object so that I can use <code>getNext()</code>, but eclipse flags it as incompatible with <code>Iterator.next()</code>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload