Java双链表堆栈尾部错误
我搜索了很多,找到了很多相似的答案,但对我的问题毫无帮助 我正在为我的双链接列表执行一个push方法,虽然头部的指针工作正常,但尾部的下一个和上一个指针不工作,请帮助Java双链表堆栈尾部错误,java,linked-list,stack,push,Java,Linked List,Stack,Push,我搜索了很多,找到了很多相似的答案,但对我的问题毫无帮助 我正在为我的双链接列表执行一个push方法,虽然头部的指针工作正常,但尾部的下一个和上一个指针不工作,请帮助 public class MyStack<E> implements MyDeque { private Node<E> head; private Node<E> tail; private int size; public MyStack() {
public class MyStack<E> implements MyDeque {
private Node<E> head;
private Node<E> tail;
private int size;
public MyStack() {
head = null;
tail = null;
size = 0;
}
public void push(Object element) {
Node<E> newNode = new Node(element);
if(size == 0) {
Node temp = new Node(head);
head = newNode;
head.next = head;
head.previous = head;
tail = head;
tail.next = head;
tail.previous = temp;
}
else {
newNode.previous = head;
head = newNode;
newNode.next = tail;
(tail.next).previous = tail;
}//else statement
size++;
}//push()
public Object peek() {
if (size==0) return null;
else
return head;
}
public Object pop() {
size--;
if(size == 0)
return null;
else {
Node temp = new Node(head.previous);
head = head.previous;
head.next = tail;
head.previous = temp;
return head;
}//else
}//pop()
@Override
public int size() {
return size;
}
private class Node<E> {
private E data;
private Node<E> next;
private Node<E> previous;
public Node(E data) {
this.data = data;
this.next = null;
this.previous = null;
}
public Node(E data, Node<E> next, Node<E> previous) {
this.data = data;
this.next = next;
this.previous = previous;
}//constructor
public String toString() {
return data+"";
}
}//class Node<E>
public String toString() {
return (head+" Head\n" + head.next +" Head.Next\n" + head.previous+ " Head.previous\n"
+ tail+" Tail\n" + tail.next+" tail.next\n" + tail.previous+" tail.previous\n");
}
}
public类MyStack实现MyDeque{
专用节点头;
私有节点尾部;
私有整数大小;
公共MyStack(){
head=null;
tail=null;
尺寸=0;
}
公共无效推送(对象元素){
节点newNode=新节点(元素);
如果(大小==0){
节点温度=新节点(头部);
头=新节点;
head.next=head;
head.previous=头;
尾=头;
tail.next=头部;
tail.previous=温度;
}
否则{
newNode.previous=头;
头=新节点;
newNode.next=tail;
(tail.next).previous=tail;
}//else语句
大小++;
}//推
公共对象peek(){
如果(size==0),则返回null;
其他的
回流头;
}
公共对象pop(){
大小--;
如果(大小==0)
返回null;
否则{
节点温度=新节点(前一个节点的头部);
head=head.previous;
head.next=tail;
head.previous=温度;
回流头;
}//否则
}//流行音乐()
@凌驾
公共整数大小(){
返回大小;
}
私有类节点{
私人电子数据;
私有节点下一步;
前一个私有节点;
公共节点(E数据){
这个数据=数据;
this.next=null;
this.previous=null;
}
公共节点(E数据、下一个节点、上一个节点){
这个数据=数据;
this.next=next;
this.previous=先前;
}//建造师
公共字符串toString(){
返回数据+“”;
}
}//类节点
公共字符串toString(){
返回(head+“head\n”+head.next+“head.next\n”+head.previous+“head.previous\n”
+tail+“tail\n”+tail.next+“tail.next\n”+tail.previous+“tail.previous\n”);
}
}
堆栈为空时推送对象的情况看起来不正确。添加的第一个节点应分配给head。然后尾部成为新节点的头部,这个新节点成为尾部节点的尾部,新节点本身成为尾部
通常在堆栈的末尾(尾部)添加和删除元素。代码没有很清楚地说明您试图对head和tail成员执行什么操作。如果您将它们命名为firstNode和lastNode,可能会更清楚一些。我实际上看到了这段代码的一些问题
public class MyStack<E> implements MyDeque {
private Node<E> head;
private Node<E> tail;
private int size;
public MyStack() {
head = null;
tail = null;
size = 0;
}
在pop方法中,您可能希望在大小检查后移动大小减量,否则在弹出时1元素堆栈将返回null
编辑:使用单链接列表或至少不是循环双链接列表可能会更容易
而在最后添加可能是更传统的做事方式 堆栈不需要一个双链接列表,您只需要一个指向头部的指针和节点中的下一个变量。看起来你有一个环绕堆栈(头指向尾,反之亦然),我的问题是-为什么?在我看来,对于
size==0
,在push
中,除其他更改外,所有4个next
和previous
都应该null
。
public void push(Object element) {
Node<E> newNode = new Node(element);
if(size == 0) {
Node temp = new Node(head);
head = newNode;
head.next = head;
head.previous = head;
tail = head;
tail.next = head;
tail.previous = temp;
}
else {
newNode.previous = head;
head = newNode;
newNode.next = tail;
(tail.next).previous = tail;
}//else statement
size++;
}//push()
public void push(E element) {
Node newNode = new Node(E);
if(size == 0) {
head = newNode;
head.next = head;
head.previous = head;
tail = head;
} else {
newNode.previous = head;
head.next = newNode;
newNode.next = tail;
tail.previous = newNode;
head = newNode;
}
size++;
}