数组、单向链表实现栈的功能(Java实现)
栈是一种线性存储结构,存入的数据先进后出。如先后存入1,2,3,取出的数据依次是3,2,1。
1、数组实现
1.1思路
(1)定义一个top代表栈顶 初始化-1(因为数组默认0开始); stack 数组
(2)入栈操作:top++ ;stack[top]=data;
(3)出栈操作:int value=stack[top];top–;return value;
(4)查看:因为先是栈顶 ,所以逆向输出。
1.2代码实现
package test.stack;
import java.util.Scanner;
/**
* @author shkstart
* @create 2021-01-07 11:17
*/
public class stackArrayDemo {
public static void main(String[] args) {
ArrayStack stack = new ArrayStack(4);//最大栈容量
String key = "";
boolean loop1 = true; //控制是否退出菜单
Scanner scanner = new Scanner(System.in);
while (loop1) {
System.out.println("show: 表示显示栈");
System.out.println("exit: 退出程序");
System.out.println("push: 表示添加数据到栈(入栈)");
System.out.println("pop: 表示从栈取出数据(出栈)");
System.out.println("请输入你的选择");
key = scanner.next();
switch (key) {
case "show":
stack.list();
break;
case "push":
System.out.println("请输 入一个数");
int value = scanner.nextInt();
stack.push(value);
break;
case "pop":
try {
int res = stack.pop();
System.out.printf("出栈的数据是 %d\n", res);
} catch (Exception e) {
e.printStackTrace();
}
break;
case "exit":
scanner.close();
loop1 = false;
break;
default:
break;
}
}
System.out.println("程序退出~~~");
}
}
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()) {
System.out.println("满了");
return;
}
top++;
stack[top] = value;
}
public int pop() {
if (isempty()) {
throw new RuntimeException("空");
}
int value = stack[top];
top--;
return value;
}
public void list() {//从栈顶显示
if (isempty()) {
System.out.println("空");
return;
}
for (int i = top; i >= 0; i--) {
System.out.println(stack[i]);
}
}
}
2、链表实现
2.1思路
(1)链表是节点的形式,包含了数据和next域 。以类的形式体现
(2)入栈:定位到最后一个节点(其.nextnull),添加
(3)出栈:将链表最后一个节点出栈(node.next.nextnull,也就是将node.next 节点删除。)
(4)显示:逆序遍历
2.2代码实现
package test.stack;
import com.sun.javafx.collections.ListListenerHelper;
import java.util.List;
import java.util.Stack;
/**
* @author shkstart
* @create 2021-01-07 13:29
*/
public class LinklistStackdemo {
public static void main(String[] args) {
HeroNode hero1 = new HeroNode(1, "宋江", "及时雨");
HeroNode hero2 = new HeroNode(2, "俊义", "玉麒麟");
HeroNode hero3 = new HeroNode(111, "吴用", "智多星");
HeroNode hero4 = new HeroNode(116, "林冲", "豹子头");
LinklistStack linklistStack = new LinklistStack();
linklistStack.push(hero1);
linklistStack.push(hero2);
linklistStack.push(hero3);
// linklistStack.list();
linklistStack.reprint();
System.out.println("**************");
linklistStack.pop();
// linklistStack.list();
linklistStack.reprint();
System.out.println("**************");
linklistStack.pop();
// linklistStack.list();
linklistStack.reprint();
}
}
class LinklistStack {
private HeroNode head = new HeroNode();
public boolean isempty() {
return head.next == null;
}
//逆序打印输出
public void reprint() {
/*if (head.next==null){
System.out.println("没有数据");
}*/
Stack<HeroNode> stack = new Stack<>();
HeroNode cur = head.next;
while (cur != null) {
stack.push(cur);
cur = cur.next;
}
while (stack.size() > 0) {
System.out.println(stack.pop());//pop就是将栈顶的数据取出
}
}
//正序输出
public void list() {
if (isempty()) {
System.out.println("kong");
return;
} else {
HeroNode temp = head.next;
while (true) {
System.out.println(temp);
if (temp.next == null) {
break;
}
temp = temp.next;
}
}
}
//添加
public void push(HeroNode node) {
HeroNode temp = head;
while (true) {
if (temp.next == null) {
temp.next = node;
break;
}
temp = temp.next;
}
}
//取出数据
public void pop() {
/* if (isempty()) {
throw new RuntimeException("kong");
}else {
HeroNode temp = head;
while (true){
temp=temp.next;
if (temp.next == null) {
temp.next=temp.next.next;
break;
}*/
HeroNode temp = head;
if (temp.next == null) {
System.out.println("空");
} else {
while (true) {
if (temp.next.next == null) {
temp.next = temp.next.next;
break;
}
temp = temp.next;
}
}
}
}
class HeroNode {
public int no;
public String name;
public String nickname;
public HeroNode next; //指向下一个节点
//构造器
public HeroNode() {
}
public HeroNode(int no, String name, String nickname) {
this.no = no;
this.name = name;
this.nickname = nickname;
}
//为了显示,重写toString
@Override
public String toString() {
return "HeroNode [no=" + no + ", name=" + name + ", nickname=" + nickname + "]";
}
}
输出结果:
将 1 2 11编号的加入,显示111 2 1。pop(),将111取出,显示 2 1。继续取出,剩下1。
PS:学自尚硅谷的自我总结