stack的java实现


import java.lang.Exception;

class MyStack
{
private static final int MAX_SIZE = 5;
private int[] t = new int[MAX_SIZE];
private int top = -1;

public void push(int x) throws Exception{

if(isFull()){
throw new Exception("栈已满");
}else{
t[++top] = x;
}

}

public int pop() throws Exception{

if(isEmpty()){
throw new Exception("栈为空");
}else{
return t[top--];
}
}

public boolean isFull(){
return top == MAX_SIZE - 1;
}

public boolean isEmpty(){
return top == -1;
}

public void printStack(){
for(int i = 0; i <= top; i++){
System.out.print(t[i]);
}
}
}

public class Temp
{
public static void main(String[] args) throws Exception{
MyStack ms = new MyStack();
ms.push(1);
ms.push(2);
ms.push(3);
ms.push(4);
ms.push(5);
System.out.println(ms.pop());
//ms.push(6);
ms.printStack();
}
}

顺序栈


class Node
{
public int data;
public Node next;

public Node(int data){
this.data = data;
}
}

class LinkedStack
{
private Node head;

public void push(int a){
Node node = new Node(a);
if(head == null){
head = node;
}else{
node.next = head;
head = node;
}
}

public int pop() throws Exception{
if(isEmpty()){
throw new Exception("栈为空");
}else{
Node temp = head;
head = head.next;
return temp.data;
}

}

public boolean isEmpty(){
return head == null;
}
}

public class TestLinkedStack
{
public static void main(String[] args) throws Exception{
LinkedStack ls = new LinkedStack();
ls.push(3);
ls.push(4);
System.out.println(ls.pop());
System.out.println(ls.pop());
//System.out.println(ls.pop());
}
}

链栈
### 如何在 Java实现栈数据结构 #### 使用数组实现栈 一种简单的方法是通过数组来创建栈。下面是一个简单的 `MyStack` 类,它展示了如何使用数组作为底层存储机制。 ```java public class MyStack { private int maxSize; private int[] stackArray; private int top; public MyStack(int size) { maxSize = size; stackArray = new int[maxSize]; top = -1; } public void push(int value) { if (top < maxSize - 1) { stackArray[++top] = value; } else { System.out.println("Stack is full"); } } public int pop() { if (top >= 0) { return stackArray[top--]; } else { throw new RuntimeException("Stack is empty"); } } public boolean isEmpty() { return (top == -1); } public int peek() { if (!isEmpty()) { return stackArray[top]; } else { throw new RuntimeException("Stack is empty"); } } } ``` 此代码定义了一个名为 `MyStack` 的类[^3],该类具有基本的栈操作方法:`push()` 添加新元素到栈顶;`pop()` 移除并返回栈顶元素;`peek()` 返回但不移除栈顶元素;以及 `isEmpty()` 判断栈是否为空。 #### 使用链表实现栈 另一种常见的做法是以单向链表为基础构建栈。这种方式允许动态调整大小而无需预先指定容量限制。 ```java class Node { int data; Node next; public Node(int d) { data = d; next = null; } } public class LinkedListBasedStack { private Node head; public LinkedListBasedStack(){ this.head=null; } public void push(int newData){ Node newNode=new Node(newData); newNode.next=head; head=newNode; } public int pop(){ if(head==null){ throw new RuntimeException("Stack underflow"); } int poppedValue= head.data; head=(head).next; return poppedValue; } public boolean isEmpty(){ return head == null ; } public int peek(){ if(isEmpty()){ throw new RuntimeException("Stack UnderFlow"); } return head.data; } } ``` 这段程序实现了基于链接节点列表的栈功能[^1]。每当我们调用 `push()` 方法时,会在链表头部插入一个新的节点;当执行 `pop()` 或者 `peek()` 操作的时候,则是从链表头读取或删除最上面的一个节点。 这两种方式都可以有效地模拟 LIFO(Last In First Out)行为,即最后进入的数据最先被取出,这是栈的核心特性之一[^2]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值