使用链表实现堆栈
在 Java 中使用链表实现堆栈的最佳方法是什么?
编辑:我将最佳定义为使用干净代码最有效。我已经使用数组来实现堆栈,但不熟悉链接列表,因此想知道是否有人可以帮助我实现类似于下面的内容:
public class StackArray{
private Object [] objArray;
private int stackSize;
public StackArray(){
objArray = new Object[50];
stackSize = 0;
}
public StackArray(int size){
objArray = new Object[size];
stackSize = 0;
}
//public interface methods - push, pop, top, empty & clear
public void push(Object o)throws StackArrayException{
if(stackSize < objArray.length){
objArray[stackSize] = o;
stackSize ++;
}else{
throw new StackArrayException("Stack Overflow");
}
}
public Object pop()throws StackArrayException{
if(stackSize != 0){
stackSize--;
return(objArray[stackSize]);
}else{
throw new StackArrayException("Stack Underflow");
}
}
public void top() throws StackArrayException{
if(stackSize != 0){
return(objArray[stackSize-1]);
}else{
throw new StackArrayException("Stack Underflow");
}
}
public boolean empty(){
return (stackSize == 0):
}
public void clear(){
stackSize = 0;
}
}
编辑:如果有人感兴趣,这里是链接列表实现。
public class StackList{
private Node listHead;
protected class Node{
protected Object datum;
protected Node next;
public Node(Object o, Node n){
datum = o;
next = n;
}
public StackList(){
listHead = null;
}
//public interface methods - push pop top empty clear
public void push(Object o){
listHead = new Node(o, listHead);
}
public Object pop() throws StackListException{
if(listHead!=null){
Object top = listHead.datum;
listHead = listHead.next;
return top;
}else{
throw new StackListException("Stack Underflow");
}
}
public Object top()throws StackListException{
if(listHead != null){
return(listHead.datum);
}else{
throw new StackListException("Stack Underflow");
}
}
public boolean empty(){
return (listHead == null);
}
public void clear(){
listHead = null;
}
}
What's the best way to implement a stack using linked lists in Java?
EDIT: I would define best as most efficient using clean code. I have already used an array to implement a stack, but am not familiar with link lists so was wondering if anyone could help me implement something similar to below:
public class StackArray{
private Object [] objArray;
private int stackSize;
public StackArray(){
objArray = new Object[50];
stackSize = 0;
}
public StackArray(int size){
objArray = new Object[size];
stackSize = 0;
}
//public interface methods - push, pop, top, empty & clear
public void push(Object o)throws StackArrayException{
if(stackSize < objArray.length){
objArray[stackSize] = o;
stackSize ++;
}else{
throw new StackArrayException("Stack Overflow");
}
}
public Object pop()throws StackArrayException{
if(stackSize != 0){
stackSize--;
return(objArray[stackSize]);
}else{
throw new StackArrayException("Stack Underflow");
}
}
public void top() throws StackArrayException{
if(stackSize != 0){
return(objArray[stackSize-1]);
}else{
throw new StackArrayException("Stack Underflow");
}
}
public boolean empty(){
return (stackSize == 0):
}
public void clear(){
stackSize = 0;
}
}
EDIT: Here is the linked list implementation if anyone is interested..
public class StackList{
private Node listHead;
protected class Node{
protected Object datum;
protected Node next;
public Node(Object o, Node n){
datum = o;
next = n;
}
public StackList(){
listHead = null;
}
//public interface methods - push pop top empty clear
public void push(Object o){
listHead = new Node(o, listHead);
}
public Object pop() throws StackListException{
if(listHead!=null){
Object top = listHead.datum;
listHead = listHead.next;
return top;
}else{
throw new StackListException("Stack Underflow");
}
}
public Object top()throws StackListException{
if(listHead != null){
return(listHead.datum);
}else{
throw new StackListException("Stack Underflow");
}
}
public boolean empty(){
return (listHead == null);
}
public void clear(){
listHead = null;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
假设您确实想从头开始执行此操作,而不是使用
Assuming you genuinely want to do this from scratch rather than using one of the perfectly good existing stack implementations then I would recommend:
为什么不直接使用 Stack 实施已经存在了吗?
或者更好(因为它实际上是一个链表,速度快且线程安全): LinkedBlockingDeque
Why don't you just use the Stack implementation already there?
Or better (because it really a linked list, its fast, and its thread safe): LinkedBlockingDeque
如果您谈论的是单个链表(一个节点具有对下一个对象的引用,但没有对前一个对象的引用),那么该类将如下所示:
当然,您需要编写其余方法的代码让它正常有效地工作,但你已经掌握了基础知识。
希望这有帮助!
If you're talking about a single linked list (a node has a reference to the next object, but not the previous one), then the class would look something like this :
Of course you will need to code the rest of the methods for it to work properly and effectively, but you've got the basics.
Hope this helps!
使用 LinkedList 实现堆栈 - 此 StackLinkedList 类在内部维护 LinkedList 引用。
StackLinkedList的push方法内部调用linkedList的
insertFirst()
方法StackLinkedList的方法内部调用linkedList的
deleteFirst()
方法完整程序
OUTPUT
礼貌:http:// www.javamadesoeasy.com/2015/02/implement-stack-using-linked-list.html
For implementing stack using using LinkedList- This StackLinkedList class internally maintains LinkedList reference.
StackLinkedList‘s push method internally calls linkedList’s
insertFirst()
methodStackLinkedList’s method internally calls linkedList’s
deleteFirst()
methodFull Program
OUTPUT
Courtesy : http://www.javamadesoeasy.com/2015/02/implement-stack-using-linked-list.html
使用 STL 适配器
std::stack
。为什么?因为您不必编写的代码是完成任务的最快方法。stack
经过充分测试,可能不需要您的任何关注。为什么不呢?因为您的代码需要一些特殊用途的要求,此处未记录。默认情况下,
stack
使用deque
双端队列,但它只需要底层容器支持“反向插入序列”,也称为.push_back.
Use the STL adapter
std::stack
. Why? Because the code you don't have to write is the fastest way to completion of your task.stack
is well-tested, and likely to not need any attention from you. Why not? Because there are some special-purpose requirements needed by your code, undocumented here.By default
stack
uses adeque
double-ended queue, but it merely requires the underlying container to support "Back Insertion Sequence", also known as.push_back
.这是一个使用数组和链表堆栈实现的教程实现。
这取决于具体情况。
数组:- 你不能调整它的大小(固定大小)
LinkedList:- 它比基于数组的列表需要更多的内存,因为它想将下一个节点保留在内存中。
Here is a tutorial implement using an array and linked list stack implementation.
It depends on the situation.
Array :- you can not resize it (fix size)
LinkedList :- it takes more memory than the array-based one because it wants to keep next node in memory.
我看到很多使用 LinkedList 的堆栈实现,最后我明白了什么是堆栈..并自己实现了堆栈(对我来说它是干净且高效的)。我希望您欢迎新的实施。代码如下。
这是它的主要类。
I saw many stack implementation using LinkedList, At the end I understand what stack is.. and implemented stack by myself(for me it's clean and efficient). I hope you welcome new implementations. Here the code follows.
And here the main class for it.