Java 使用循环单链表实现堆栈

Java 使用循环单链表实现堆栈,java,linked-list,stack,Java,Linked List,Stack,我试图在Java中实现a堆栈,使用循环单链表作为底层数据结构。我为循环链表放置了insert函数,以替换堆栈的push函数等等。我没有任何错误,但显示堆栈时遇到问题。如果有人能给我指出如何显示堆栈的正确方向,或者哪里出了问题,我将非常感激 这是我的堆栈类: public class Stack { private int maxSize; // size of stack array private long[] stackArray; private int top; // top of st

我试图在Java中实现a堆栈,使用循环单链表作为底层数据结构。我为循环链表放置了insert函数,以替换堆栈的push函数等等。我没有任何错误,但显示堆栈时遇到问题。如果有人能给我指出如何显示堆栈的正确方向,或者哪里出了问题,我将非常感激

这是我的堆栈类:

public class Stack {
private int maxSize; // size of stack array
private long[] stackArray;
private int top; // top of stack

private Node current = null;           // reference to current node
private int count = 0;              // # of nodes on list
private long iData;


public Stack(int s) // constructor
{
    maxSize = s; // set array size
    stackArray = new long[maxSize]; // create array
    top = -1; // no items yet
}
public void push(long j) // put item on top of stack
{
    Node n = new Node(j);
    if(isEmpty()){
        current = n;
    }
    n.next = current;
    current = n;
    count++;
}
//--------------------------------------------------------------
public Node pop() // take item from top of stack
{
    if(isEmpty()) {
        return null;
    }
    else if(count == 1){
        current.next = null;
        current = null;
        count--;
        return null;
    }else{
        Node temp = current;
        current = current.next;
        temp.next = null;
        temp = null;
        count--;
    }
    return current;
}
//--------------------------------------------------------------
public Node peek(long key) // peek at top of stack
{
    Node head = current;
    while(head.iData != key){
        head = head.next;
    }
    return head;
}
//--------------------------------------------------------------
public boolean isEmpty() // true if stack is empty
{
    return (count == 0);
}
//--------------------------------------------------------------
public boolean isFull() // true if stack is full
{
    return (count == maxSize-1);
}
//--------------------------------------------------------------
这是我的构造函数类

public class Node{
public long iData;          // data item (key)
public Node next;           // next node in the list

public Node(long id){        // constructor
    iData = id;             // next automatically nulls
}

public void displayNode(){
    System.out.print(iData + " ");
}

public static void main(String[] args) {
    Stack newlist = new Stack(3);
    newlist.push(1);
    newlist.push(2);
    newlist.push(3);
    newlist.push(4);

    newlist.pop();
    newlist.pop();

    newlist.push(4);

    newlist.pop();

    newlist.peek(1);

    newlist.push(5);

    while( !newlist.isEmpty() ) // until it’s empty,
    { // delete item from stack
        Node value = newlist.pop();
        System.out.print(value); // display it
        System.out.print(" ");
    } // end while
    System.out.println("");
}


//newlist.displayList();

}

首先,在主函数中,使用
System.out.print
函数打印值。这将显示对象的类名表示形式,然后显示“@”及其哈希代码

替换以下行

System.out.print(value); // display it
System.out.print(" ");

其次,在
pop
方法中,当
count
为1时,返回
null
。它应该返回列表中的最后一个元素。另外,在最后一个
else if
子句中,您应该返回
temp
。用这个替换你的代码

public Node pop() // take item from top of stack
{
    if (isEmpty()) {
        return null;
    }
    Node temp = current;
    if (count == 1) {
        current = null;
    } else {
        current = current.next;
    }
    count--;
    temp.next = null;
    return temp;
}

关于您的实现的一些注意事项:

1) stackArray成员似乎是另一个基于数组的堆栈实现的遗留物

2) 最大尺寸真的是一个要求吗?如果是这样的话,就不会在push(..)中强制执行堆栈大小限制

3) 您的push(…)方法无法使列表保持循环。您应该将循环关闭回新节点

4) 添加虚拟节点可以使链表保持循环,而与堆栈大小无关。这可以使push(..)方法更简单(以及任何用于打印目的的迭代)

5) peek()方法契约不清楚。通常,您希望peek方法返回堆栈顶部的值,而不删除它。另外,为什么返回类型Node?这个类应该对调用方隐藏—它是一个内部实现细节,而不是您希望在API中公开的内容

以下是另一种实现,它也支持toString()

公共类堆栈{
专用节点EOS;
私有整数计数=0;
公共堆栈(){
EOS=新节点(0);
EOS.next=EOS;
}
公共空间推送(长j){
节点newNode=新节点(j);
节点tmp=EOS.next;
EOS.next=newNode;
newNode.next=tmp;
计数++;
}
公共长条{
if(isEmpty()){
返回null;
}否则{
计数--;
Node=EOS.next;
EOS.next=node.next;
返回node.iData;
}
}
公共长视{
if(isEmpty()){
返回null;
}否则{
Node=EOS.next;
返回node.iData;
}
}
公共布尔值为空(){
返回(计数=0);
}
@凌驾
公共字符串toString(){
StringBuilder sb=新的StringBuilder();
节点p=EOS.next;
while(p!=EOS){
sb.append(p).append(“\n”);
p=p.next;
}
使某人返回字符串();
}
私有静态类节点{
公共长iData;
公共节点下一步;
公共节点(长id){
iData=id;
}
@凌驾
公共字符串toString(){
返回“”;
}
}
}

这不是循环链接列表(循环链接列表也不适合堆栈)。另外,如果元素不在堆栈中,
peek()
将为您提供一个NPE。这样做没有多大意义。这两种结构具有非常不同的行为。此外,“堆栈”更多的是一个概念,而不是一个实际的具体数据结构,它可以通过几种方式实现。循环链表只是。。。循环链表。
public Node pop() // take item from top of stack
{
    if (isEmpty()) {
        return null;
    }
    Node temp = current;
    if (count == 1) {
        current = null;
    } else {
        current = current.next;
    }
    count--;
    temp.next = null;
    return temp;
}
public class Stack {
  private Node EOS; 
  private int count = 0; 

  public Stack() {
    EOS = new Node(0);
    EOS.next = EOS;
  }

  public void push(long j) {
    Node newNode = new Node(j);
    Node tmp = EOS.next;
    EOS.next = newNode;
    newNode.next = tmp;
    count++;
  }

  public Long pop() {
    if (isEmpty()) {
      return null;
    } else {
      count--;
      Node node = EOS.next;
      EOS.next = node.next;
      return node.iData;
    }
  }

  public Long peek() {
    if (isEmpty()) {
      return null;
    } else {
      Node node = EOS.next;
      return node.iData;
    }
  }

  public boolean isEmpty() {
    return (count == 0);
  }

  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder();
    Node p = EOS.next;
    while (p != EOS) {
      sb.append(p).append("\n");
      p = p.next;
    }
    return sb.toString();
  }

  private static class Node {
    public long iData; 
    public Node next; 

    public Node(long id) { 
      iData = id; 
    }

    @Override
    public String toString() {
      return "<" + iData + ">";
    }
  }
}