第五章:栈

5.4栈的快速入门(使用链表模拟栈)

package com.atguigu04.stack;

import java.util.Scanner;

/**
 * @author peng
 * @date 2021/11/15 - 9:01
 *
 * 使用单链表来实现栈,主要功能有push、pop、peek
 * push:实现push功能主要是,在入栈的时候,new一个头节点,再将新的头节点指向原来的头节点;
 * pop:实现pop功能主要是,在出栈的时候,将头节点变为原本头节点的下一个节点即可;
 * peek:实现peek功能主要是,返回头节点的值即可
 */
public class ArrayLinkedListStackDemo {
    public static void main(String[] args) {
        //测试链表实现的栈
        LinkedListStack linkedListStack = new LinkedListStack();
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        String str = "";
        while (loop) {
            System.out.println("show:显示栈的数据");
            System.out.println("push:将一个数放进栈中:");
            System.out.println("pop:将栈中的数据弹出:");
            System.out.println("peek:返回栈顶的元素:");
            System.out.println("exit:退出程序:");
            System.out.println("请输入指令:");
            str = scanner.nextLine();
            switch (str) {
                case "show" :
                    try {
                        linkedListStack.showLinkedListStack();
                        break;
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                case "push":
                    System.out.println("请输入新节点的值:");
                    Node newNode = new Node(scanner.nextInt());
                    linkedListStack.LinkedListPush(newNode);
                    break;
                case "pop":
                    try {
                        System.out.println(linkedListStack.LinkedListPop());
                        break;
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                case "peek":
                    try {
                        System.out.println(linkedListStack.LinkedListPeek());
                        break;
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                case "exit":
                    loop = false;
                    break;
                default:
                    break;
            }
        }
    }
}

/**
 * 先定义一个节点
 */
class Node{
    public int element;
    public Node next = null;
    public Node(int element){
        this.element = element;
    }

    @Override
    public String toString() {
        return "Node{" +
                "element=" + element +
                '}';
    }
}

/**
 * 实现单链表
 */
class LinkedListStack {
    public Node head = null;
    public void LinkedListPush(Node newNode) {
        /*
        实现栈的push功能
         */
        if (head == null) {
            //如果当前链表为空,那么刚插入的节点就是头节点
            head = newNode;
            return;
        }
        //如果当前链表不为空,那么新的节点就指向头节点,再将新的节点作为头节点
        newNode.next = head;
        head = newNode;
        System.out.println("数据入栈成功!");
        return;
    }

    /**
     * 实现栈的pop功能
     */
    public int LinkedListPop() throws Exception {
        //首先判断当前链表是否为空
        if (head == null) {
            throw new Exception("当前栈为空!");
        }
        Node temp = null;//定义一个临时变量,用于删除头节点
        temp = head.next;
        int value = head.element;
        head.next = null;
        head = temp;
        return value;
    }

    /**
     * 实现栈的peek功能
     */
    public int LinkedListPeek() throws Exception {
        //首先判断当前链表是否为空
        if (head == null) {
            throw new Exception("当前链表为空");
        }
        int value = head.element;
        return value;
    }

    /**
     * 输出栈中的元素
     */
    public void showLinkedListStack() throws Exception {
        if (head == null) {
            throw new Exception("当前栈为空!");
        }
        Node temp = head;
        while(true) {
            if (temp == null) {
                break;
            }
            System.out.println(temp.element);
            temp = temp.next;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值