栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

单列表singleList模拟栈Stack数据结构

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

单列表singleList模拟栈Stack数据结构

栈的简单介绍

  1. 栈是一种先入后出,后入先出的有序列表
  2. 栈是限制线性表中的元素的插入和删除只能在线性表的同一端进行的一种特殊线性表,允许插入和删除的一端,为变化的一端,称为栈顶(top),另一端为固定的一端,称为栈底(Bottom)。

单列表的节点介绍

使用单列表模拟栈的底层实现

package stack;


import java.util.Scanner;


public class singleListStackDemo {
    public static void main(String[] args) {

        //用单列表->表示栈
        singleListStack stack = new singleListStack();

        singleList list1 = new singleList(1,"stack1");
        singleList list2 = new singleList(2,"stack2");
        singleList list3 = new singleList(3,"stack3");
        singleList list4 = new singleList(4,"stack4");

        stack.push(list1);
        stack.push(list2);
        stack.push(list3);
        stack.push(list4);

        // 出栈
        System.out.println("出栈---后入先出");
        System.out.println(stack.pop());
        System.out.println(stack.pop());

        System.out.println("遍历");
        stack.list();

        System.out.println("程序退出~~~");

    }
}

// 单向列表模拟栈
class singleListStack {

    //先初始化一个头节点, 头节点不要动, 不存放具体的数据
    private singleList head = new singleList(0, "");

    //返回头节点
    public singleList getHead() {
        return head;
    }

    // 入栈
    public void push(singleList singleList) {
        singleList temp = this.head;
        while (true) {
            if (temp.next == null) {//说明找到了尾节点
                break;
            }
            temp = temp.next; // 向后移,直至找到尾节点为null
        }
        temp.next = singleList;
    }

    // 出栈
    // 栈是先进后出,后入先出,所以将挂在最后的节点先pop
    public singleList pop() {
        singleList temp = this.head;
        while (true) {
            if (temp.next.next == null) {//说明找到了尾节点
                break;
            }
            temp = temp.next; // 向后移,直至找到尾节点为null
        }
        singleList val = temp.next;
        temp.next = null;
        return val;
    }

    // 遍历栈 先进后出,后入先出
    // 使用栈的逆序
    public void list() {
        // 创建新的单节点 存放逆序的数据
        singleList reverseSingleList = new singleList(0, "");

        // 创建临时中转节点。
        singleList cur = head.next;
        singleList next = null;
        while (cur != null) {
            next = cur.next; // 存放后面节点的值
            cur.next = reverseSingleList.next; // 用老单列表指向新单列表的头部
            reverseSingleList.next = cur; // 画图分析
            cur = next;
        }
        //head.next = reverseSingleList.next; // 在将原来的头节点next指向逆序后的单列表
        // 此处不能改变原来节点的顺序  逆序后就达到了后进先出 在遍历即可
        singleList temp = reverseSingleList.next;
        if(temp == null){
            throw new RuntimeException("栈空,无法遍历数据!");
        }
        while (true){
            System.out.printf("stack=%sn",temp.toString());
            if(temp.next ==null){
                break;
            }
            temp=temp.next;// 向后移
        }


    }


}


class singleList { //成员内部类
    int no;
    String name;
    singleList next;

    public singleList(int no, String name) {
        this.no = no;
        this.name = name;
    }

    @Override
    public String toString() {
        return "singleList{" +
                "no=" + no +
                ", name='" + name + ''' +
                '}';
    }
}

使用数组模拟栈的底层实现

package stack;


public class ArrayStackDemo {
}

// 栈的数组模拟

class ArrayStack {

    private int top = -1;
    private int[] arr;
    private int maxSize;

    public ArrayStack(int maxSize) {
        this.maxSize = maxSize;
    }

    // 判断栈是否满了
    public boolean isFull() {
        return top == maxSize - 1;
    }

    // 判断栈空
    public boolean isEmpty() {
        return top == -1;
    }

    // 入栈
    public void push(int value) {
        if (isFull()) {
            throw new RuntimeException("栈满,无法加入数据~");
        }
        top++;
        arr[top] = value;
    }

    // 出栈
    public int pop() {
        // 从上往下取
        if (isEmpty()) {
            throw new RuntimeException("栈空,无法取数据~");
        }
        int value = arr[top];
        top--;
        return value;
    }

    // 遍历栈
    public void list(){
        // 从上往下取
        if (isEmpty()) {
            throw new RuntimeException("栈空,无法取数据~");
        }
        for (int i = top; i >=0 ; i--) {
            System.out.printf("stack[%d]=%d",i,arr[i]);
        }

    }


}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1003221.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号