如何将数据返回后节点?我试图创建一个Dequeue后部功能
如您所见,这是我的Deque。我认为我没有正确使用我的后指针。由于目前,它仅返回列表的正面值。我只需要帮助尝试弄清楚如何返回后方的值即可。它正确地将其删除,但是 它返回错误的值。抱歉,我试图尽可能保持紧凑。但是我想确保它可以运行
public class LinkedDeque <T> implements DequeInterface<T>
{
protected LLNode<T> front; // reference to the front of this queue
protected LLNode<T> rear; // reference to the rear of this queue
protected int numElements = 0; // number of elements in this queue
@Override
public T dequeueRear ()throws QueueUnderflowException
{
if (isEmpty())
throw new QueueUnderflowException("Dequeue attempted on empty queue.");
LLNode<T> lastNode = front;
LLNode<T> previoustolast = null;
while(lastNode.getLink()!= null)
{
previoustolast = lastNode;
lastNode= lastNode.getLink();
}
previoustolast.setLink(null);
return lastNode.getInfo();
}
@Override
public boolean isEmpty()
{
return (front == null);
}
public String toString() {
String result = "";
LLNode<T> temp = front;
while(temp!=null) {
result += temp.getInfo()+" ";
temp = temp.getLink();
}
return result;
}
}
public class LLNode<T>
{
protected LLNode<T> link;
protected T info;
public LLNode(T info)
{
this.info = info;
link = null;
}
public void setInfo(T info){
this.info = info;
}
public T getInfo(){
return info;
}
public void setLink(LLNode<T> link) {
this.link = link;
}
public LLNode<T> getLink(){
return link;
}
}
public class LinkedDequeTestDriver
{
public static void main(String[] args)
{
LinkedDeque<Integer> myLinkedDeque = new LinkedDeque();
myLinkedDeque.enqueueRear(10);
myLinkedDeque.enqueueRear(20);
myLinkedDeque.enqueueRear(30);
myLinkedDeque.enqueueRear(40);
myLinkedDeque.enqueueRear(50);
System.out.println("Queue");
System.out.println(myLinkedDeque);
myLinkedDeque.dequeueRear();
System.out.println(myLinkedDeque);
myLinkedDeque.dequeueRear();
System.out.println(myLinkedDeque);
System.out.println(myLinkedDeque.dequeueRear());
}
public interface DequeInterface<T>
{
void enqueueFront(T element) throws QueueOverflowException;
// Throws QueueOverflowException if this queue is full;
// otherwise, adds element to the front of this queue.
void enqueueRear(T element) throws QueueOverflowException;
// Throws QueueOverflowException if this queue is full;
// otherwise, adds element to the rear of this queue.
T dequeueFront() throws QueueUnderflowException;
// Throws QueueUnderflowException if this queue is empty;
// otherwise, removes front element from this queue and returns it.
T dequeueRear() throws QueueUnderflowException;
// Throws QueueUnderflowException if this queue is empty;
// otherwise, removes rear element from this queue and returns it.
boolean isFull();
// Returns true if this queue is full; otherwise, returns false.
boolean isEmpty();
// Returns true if this queue is empty; otherwise, returns false.
int size();
// Returns the number of elements in this queue.
String toString();
}
So as you can see this is my deque. I dont think im correctly using my rear pointer. Cause right now its only returning the value of the front of the list. I just need help trying to figure out how to return the value of the rear. its removing it correctly but
its returning the wrong value. Sorry i tried to keep it as compact as I possibly could. But i wanted to ensure it was runable
public class LinkedDeque <T> implements DequeInterface<T>
{
protected LLNode<T> front; // reference to the front of this queue
protected LLNode<T> rear; // reference to the rear of this queue
protected int numElements = 0; // number of elements in this queue
@Override
public T dequeueRear ()throws QueueUnderflowException
{
if (isEmpty())
throw new QueueUnderflowException("Dequeue attempted on empty queue.");
LLNode<T> lastNode = front;
LLNode<T> previoustolast = null;
while(lastNode.getLink()!= null)
{
previoustolast = lastNode;
lastNode= lastNode.getLink();
}
previoustolast.setLink(null);
return lastNode.getInfo();
}
@Override
public boolean isEmpty()
{
return (front == null);
}
public String toString() {
String result = "";
LLNode<T> temp = front;
while(temp!=null) {
result += temp.getInfo()+" ";
temp = temp.getLink();
}
return result;
}
}
public class LLNode<T>
{
protected LLNode<T> link;
protected T info;
public LLNode(T info)
{
this.info = info;
link = null;
}
public void setInfo(T info){
this.info = info;
}
public T getInfo(){
return info;
}
public void setLink(LLNode<T> link) {
this.link = link;
}
public LLNode<T> getLink(){
return link;
}
}
public class LinkedDequeTestDriver
{
public static void main(String[] args)
{
LinkedDeque<Integer> myLinkedDeque = new LinkedDeque();
myLinkedDeque.enqueueRear(10);
myLinkedDeque.enqueueRear(20);
myLinkedDeque.enqueueRear(30);
myLinkedDeque.enqueueRear(40);
myLinkedDeque.enqueueRear(50);
System.out.println("Queue");
System.out.println(myLinkedDeque);
myLinkedDeque.dequeueRear();
System.out.println(myLinkedDeque);
myLinkedDeque.dequeueRear();
System.out.println(myLinkedDeque);
System.out.println(myLinkedDeque.dequeueRear());
}
public interface DequeInterface<T>
{
void enqueueFront(T element) throws QueueOverflowException;
// Throws QueueOverflowException if this queue is full;
// otherwise, adds element to the front of this queue.
void enqueueRear(T element) throws QueueOverflowException;
// Throws QueueOverflowException if this queue is full;
// otherwise, adds element to the rear of this queue.
T dequeueFront() throws QueueUnderflowException;
// Throws QueueUnderflowException if this queue is empty;
// otherwise, removes front element from this queue and returns it.
T dequeueRear() throws QueueUnderflowException;
// Throws QueueUnderflowException if this queue is empty;
// otherwise, removes rear element from this queue and returns it.
boolean isFull();
// Returns true if this queue is full; otherwise, returns false.
boolean isEmpty();
// Returns true if this queue is empty; otherwise, returns false.
int size();
// Returns the number of elements in this queue.
String toString();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论