[英]Implement a Stack with a circular singly linked list
我正在尝试使用循环单链表作为底层数据结构在 Java 中实现堆栈。 我放置了循环链表的插入函数来代替堆栈的推送函数等等。 我没有任何错误,但我在显示堆栈时遇到了麻烦。 如果有人能指出我如何显示堆栈的正确方向或出了什么问题,我将不胜感激!
这是我的堆栈类:
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(" ");
和
value.displayNode();
其次,在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():
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 + ">";
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.