我正在尝试学习java中的泛型和迭代器并创建了以下代码 - >
import java.util.Iterator;
import java.util.NoSuchElementException;
public class Deque<Item> implements Iterable<Item>
{
private int N; // number of elements on list
private Node<Item> pre; // sentinel before first item
private Node<Item> post; // sentinel after last item
/*
* linked list node helper data type
*/
private static class Node<Item>
{
private Item item;
private Node<Item> next;
private Node<Item> prev;
}//class Node<Item> ends
/*
*construct an empty deque
*/
public Deque()
{
pre = new Node<Item>();
post = new Node<Item>();
pre.item = null;
post.item = null;
pre.prev = null;
post.next = null;
pre.next = post;
post.prev = pre;
N = 0;
}//Deque ends
/*
* is the deque empty?
*/
public boolean isEmpty()
{
//if( ((pre.next == post)&&(post.prev == pre)) || ( N == 0 ) ) return true;
if( N == 0 ) return true;
else return false;
}//isEmpty ends
/*
* return the number of items on the deque
*/
public int size()
{
return N;
}//size ends
/*
* insert the item at the front
* This is extension of Queue, so First is the location near Post Node
* pre-> <-post, pre->1<-post, pre->1,2<-post, pre->1,2,3<-post ...
* ^ ^ ^ ^ ^ ^ ^
* | | | | | | |
* initial these all are the effects of addFirst()
* condition
*/
public void addFirst(Item item)
{
//System.out.println("We are outside ListIterator - addFirst");
if( isEmpty() ) //here pre.next == post.prev
{
//System.out.println("We are inside isEmpty");
Node<Item> NewNode = new Node<Item>();
NewNode.item = item;
NewNode.next = post;
post.prev = NewNode;
NewNode.prev = pre;
pre.next = NewNode;
N++;
//System.out.println(NewNode.item);
}//if ends
else //here pre.next->1.prev & X.next->post
{
//System.out.println("We are inside !isEmpty");
Node<Item> NewNode = new Node<Item>();
Node<Item> last = post.prev;
NewNode.item = item;
NewNode.next = post;
post.prev = NewNode;
NewNode.prev = last;
last.next = NewNode;
N++;
//System.out.println(NewNode.item);
}//else ends
}//addFirst ends
/*
*insert the item at the end
* This is extension of Queue, so First is the location near Post Node
* pre-> <-post, pre->1<-post, pre->2,1<-post, pre->3,2,1<-post ...
* ^ ^ ^ ^ ^ ^ ^
* | | | | | | |
* initial these all are the effects of addLast()
* condition
*/
public void addLast(Item item)
{
//System.out.println("We are outside ListIterator - addLast");
if( isEmpty() ) //here pre.next == post.prev
{
//System.out.println("We are inside isEmpty");
Node<Item> NewNode = new Node<Item>();
NewNode.item = item;
NewNode.next = post;
post.prev = NewNode;
NewNode.prev = pre;
pre.next = NewNode;
N++;
System.out.println(NewNode.item);
}//if ends
else //here pre.next->1.prev & X.next->post
{
//System.out.println("We are inside !isEmpty");
Node<Item> NewNode = new Node<Item>();
Node<Item> last = pre.next;
NewNode.item = item;
NewNode.next = last;
pre.next = NewNode;
NewNode.prev = pre;
last.prev = NewNode;
N++;
System.out.println(NewNode.item);
}//else ends
}//addLast ends
/*
* delete and return the item at the front
* This is extension of Queue, so Last is the location near Pre Node
* pre->1,2,3<-post, pre->1,2<-post, pre->1<-post, pre-> <-post...
* ^ ^ ^ ^ ^ ^ ^
* | | | | | | |
* initial these all are the effects of removeFirst()
* condition
*/
public Item removeFirst()
{
if( isEmpty() ) throw new NoSuchElementException("Queue underflow EXIT" );
else
{
Item item = post.prev.item;
post.prev = post.prev.prev;
N--;
if( isEmpty() )
{
post.prev = pre;
pre.next = post;
N = 0;
}//if ends
return item;
}//else ends
}//removeFirst ends
/*
* delete and return the item at the end
* This is extension of Queue, so Last is the location near Pre Node
* pre->1,2,3<-post, pre->2,3<-post, pre->3<-post, pre-> <-post...
* ^ ^ ^ ^ ^ ^ ^
* | | | | | | |
* initial these all are the effects of removeLast()
* condition
*/
public Item removeLast()
{
if( isEmpty() ) throw new NoSuchElementException("Queue underflow EXIT" );
else
{
Item item = pre.next.item;
pre.next = pre.next.next;
N--;
if( isEmpty() )
{
pre.next = post;
post.prev = pre;
N = 0;
}//if ends
return item;
}//else ends
}//removeLast ends
/*
* return an iterator over items in order from front to end
* Returns an iterator that iterates over the items in this queue in FIFO order.
*/
public Iterator<Item> iterator()
{
return new ClassIterator<Item>(pre);
}//Iterator<Item> iterator() ends
// an iterator, doesn't implement remove() since it's optional
private static class ClassIterator<Item> implements Iterator<Item>
{
private Node<Item> current;
private int index = 0;
public ClassIterator(Node<Item> pre)
{
current = pre.next;
index = 1;
//System.out.println(current.item);
}
public boolean hasNext()
{
//System.out.println(current.item);
return current.next != null;
}
public void remove() { throw new UnsupportedOperationException(); }
public Item next()
{
if ( !hasNext() )
{
System.out.println("Queue is empty!!!");
throw new NoSuchElementException();
}
Item item = current.item;
current = current.next;
index++;
return item;
}
public void DisplayIndex( int indexVal )
{
if( index == indexVal ) System.out.println(current.item);
else {}
}//DisplayIndex ends
}//class ListIterator<Item> ends
public String toString()
{
StringBuilder s = new StringBuilder();
for (Item item : this) s.append(item + " ");
return s.toString();
}
/*
* Display at random based on the indices supplied from the main
*/
/*
* main function for unit testing
*/
public static void main(String[] args)
{
int N = Integer.parseInt(args[0]);
int K = Integer.parseInt(args[1]);
Deque<String> list = new Deque<String>();
System.out.println("Adding to the list by - addFirst");
for (int i = 0; i < N; i++) list.addFirst( StdIn.readString() );
StdOut.println(list);
StdOut.println();
Iterator<String> iter = list.iterator();
// print random index value
StdOut.println("Index Value = 1");
boolean a = iter.hasnext();
iter.DisplayIndex( 1 );
}//main ends
}
问题在于,即使我创建了hasnext()
的对象,我也无法使用DisplayIndex
访问ClassIterator
和Iterator
个函数。有什么帮助吗?
答案 0 :(得分:0)
Check out my implemenation .... it works
import java.util.Iterator;
import java.util.NoSuchElementException;
public class Deque<Item> implements Iterable<Item> {
private Node Head;
private Node Tail;
private int N;
private class Node{ // nested class to define nodes
Item item;
Node next;
Node previous;
}
public Deque() { // construct an empty deque
Head = null;
Tail = null;
}
public boolean isEmpty() { // is the deque empty?
return (Head == null && Tail == null);
}
public int size() { // return the number of items on the deque
return N;
}
public void addFirst(Item item) { // insert the item at the front
if(item == null)
throw new NullPointerException();
Node newNode = new Node();
newNode.item = item;
newNode.next = null;
newNode.previous = null;
if(Head == null && Tail == null) {
Head = newNode;
Tail = newNode;
}
else {
Node temp = Head;
Head = newNode;
newNode.next = temp;
temp.previous = newNode;
}
N++;
}
public void addLast(Item item) { // insert the item at the end
if(item == null)
throw new NullPointerException();
Node newNode = new Node();
newNode.item = item;
newNode.next = null;
newNode.previous = null;
if(Head == null && Tail == null) {
Head = newNode;
Tail = newNode;
}
else {
Tail.next = newNode;
newNode.previous = Tail;
Tail = newNode;
}
N++;
}
public Item removeFirst() { // delete and return the item at the front
if(isEmpty())
throw new NoSuchElementException();
Node temp = Head;
Head = Head.next;
Head.previous = null;
temp.next = null; // not required though its better to break the link
N--;
return temp.item;
}
public Item removeLast() { // delete and return the item at the end
if(isEmpty())
throw new NoSuchElementException();
Node temp = Tail;
Tail = Tail.previous;
Tail.next = null;
temp.previous = null;
N--;
return temp.item;
}
public Iterator<Item> iterator() { // return an iterator over items in order from front to end
return new dequeIterator<Item>();
}
@SuppressWarnings("hiding")
private class dequeIterator<Item> implements Iterator<Item> {
private Node current;
public dequeIterator() {
this.current = Head;
}
public boolean hasNext() {
//return ((current.next!=null)?true:false);
return current != null ;//&& current.next != null;
}
@SuppressWarnings("unchecked")
public Item next() {
if(!hasNext())
throw new NoSuchElementException();
Item temp = (Item) current.item;
current = current.next;
return temp;
}
public void remove() {
throw new UnsupportedOperationException();
}
}
public void dispForward(){
Node temp = Head; //here did not assign memory as only reference was required
while(temp!=null) {
System.out.print(temp.item+" ");
temp = temp.next;
}
}
public void dispBackward(){
Node temp = Tail; //here did not assign memory as only reference was required
while(temp!=null) {
System.out.print(temp.item+ " ");
temp = temp.previous;
}
}
public static void main(String[] args) { // unit testing
Deque<Integer> de = new Deque<Integer>();
de.addFirst(10);
de.addFirst(9);
de.addFirst(8);
de.addLast(7);
de.addLast(6);
de.dispForward();
System.out.println();
System.out.println(de.removeLast());
System.out.println(de.removeFirst());
de.dispBackward();
System.out.println();
System.out.println("usnig iterator");
// using iterator
Iterator<Integer> itr = de.iterator();
while(itr.hasNext()) {
Object element = itr.next();
System.out.print(element + " ");
}
}
}