数组模拟栈的思路分析
1.定义一个top来表示栈顶,初始化为-1
2.入栈的操作,当有数据加入到栈的时候,top++;stack[top]=data
3.出栈的操作,int value=stack[top];top–;return value
基于栈的数据的先进后出和只能在一段进行数据的插入和删除操作,稍加分析,实现起来并不难。
示例代码如下:
public class ArrayStackDemo {
public static void main(String[] args) {
ArrayStack arrayStack = new ArrayStack(4);
String key="";
boolean loop=true;
Scanner scanner = new Scanner(System.in);
while(loop){
System.out.println("show,展示栈");
System.out.println("pop,出栈");
System.out.println("push,进栈");
System.out.println("exit,退出");
key=scanner.next();
switch (key) {
case "push":
System.out.println("输入要入栈的数字");
int i = scanner.nextInt();
arrayStack.push(i);
break;
case "pop":
try {
int pop = arrayStack.pop();
System.out.println("出栈的数据为"+pop);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case "show":
try {
arrayStack.list();
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
default:
scanner.close();
loop=false;
System.out.println("程序结束");
break;
}
}
}
}
class ArrayStack{
private int maxSize;
private int[] stack;
private int top=-1;
public ArrayStack(int maxSize) {
this.maxSize = maxSize;
stack=new int[this.maxSize];//初始化数组
}
//判断栈满
public boolean isFull(){
return top==maxSize-1;
}
//判断栈空
public boolean isEmpty(){
return top==-1;
}
//入栈操作
public void push(int value){
//先对栈进行状态判断
if(isFull()){
return;
}
//在进行添加
top++;
stack[top]=value;
}
/**
* 出栈
* 其实就是将栈顶的数据返回
*/
public int pop(){
if (isEmpty()) {
//抛出异常
throw new RuntimeException("stack is empty");
}
int value=stack[top];
top--;
return value;
}
//遍历栈,要从栈顶开始遍历
public void list(){
if (isEmpty()) {
throw new RuntimeException("stack is empty");
}
for(int i=top;i>=0;i--){
System.out.println(stack[i]);
}
}
}
运行结构需要交互输入,呜呜呜呜太长了,就不放了
单链表模拟栈的实现
相对于数组模拟栈的实现,单链表更加简单明了一点吧,哈哈哈哈嗝 我自己觉得,毕竟数组模拟还是有一个大小的限制(不弄自动扩容的话),单链表模拟的思路也较好理解,基本就是头插法的使用,那这里就不过多比比,代码放这里就好了。
public class LinkedListStackDemo{
public static void main(String[] args) {
LinkedListStack linkedListStack = new LinkedListStack();
linkedListStack.push(1);
linkedListStack.push(2);
linkedListStack.push(3);
linkedListStack.show();
System.out.println("****************************");
linkedListStack.pop();
System.out.println("***************************");
linkedListStack.pop();
System.out.println("***************************");
linkedListStack.pop();
System.out.println("***************************");
linkedListStack.pop();
linkedListStack.show();
}
}
class LinkedListStack{
node head=new node(0,null);
//入栈
public void push(int value){
node newNode=new node(value,null);
if(head.getNext()==null){
head.setNext(newNode);
}else{
newNode.setNext(head.getNext());
head.setNext(newNode);
}
}
//出栈
public void pop(){
node temp=head.getNext();
if(head.getNext()==null){
System.out.println("为空");
return;
}
System.out.println(head.getNext());
temp=temp.getNext();
head.setNext(temp);
}
//栈的展示
public void show(){
if(head.getNext()==null){
System.out.println("栈为空,可以遍历,但没必要");
} node temp=head;
while(true){
if(temp.getNext()==null){
break;
}
temp=temp.getNext();
System.out.println(temp.getNum());
}
}
}
class node{
private int num;
private node next;
public node(int num, node next) {
this.num = num;
this.next = next;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public node getNext() {
return next;
}
public void setNext(node next) {
this.next = next;
}
@Override
public String toString() {
return "node{" +
"num=" + num +
'}';
}
}
运行结果如下
写的就很粗,好多基本判断也没加,自己也知道,太累了 太累了,还要上班,大体实现就是这个样子,先睡觉了
转行成功啊啊啊啊啊啊 要!