即兴写一个java链栈

java链栈(int型数据)

先实现链表的功能,这里我为了方便使用了双向链表。

通过屏蔽一些链表功能从而实现栈的功能。

直接上菜:::::

//写一个int型的链栈。
//实现了基本的增删改查。
//使用双向链表来够造基本底层逻辑。
//并使用头插法添加新数据
public class Stack {
    private Lnode head;
    private int length;
    int getLength(){
        return this.length;
    }
    //栈的功能区----------------------------------
    //初始化栈
    public void Init_Stack(){
        this.head = new Lnode();
        //初始化头结点上下域全设置为空
        this.head.setNext(null);
        this.head.setSup(null);
        //头结点初始化data设置为-1
        this.head.setData(-1);
        //初始化栈的长度设置为0
        this.length = 0;
    }
    //进栈
    public void pop_Stack(int data){
        add_Data(data);
    }
    //出栈
    public int push_Stack(){
        if (this.length == 0){
            System.out.println("出栈失败哦!栈是空的呀!!");
            System.exit(-1);

        }else{
            Lnode p = this.head.getNext();
            int cache = p.getData();
            Dele_Data(cache);
            return cache;
        }
        return -1;
    }
    //打印栈内元素
    public String Print_Stack(){
        if(this.length == 0){
            return "[]";
        }
        Lnode p = head.getNext();
        String s ="[";
        for (int i=0;i<this.length;i++){
            if(i == this.length - 1){
                s = s + p.getData() + "]";
            }else{
                s = s + p.getData() + " ";
                p = p.getNext();
            }
        }
        return s;
    }
    //链表操作区----------------------------------------
    //增加一个结点
    public void add_Data(int data){
        //创建一个结点
        Lnode add_cache = new Lnode();
        //将数据添加到新节点中
        add_cache.setData(data);
        if(this.length == 0){
            //将头结点后面的第一个结点找到,并设置一个指针指向它
            //空指针异常和c++中的一级指针传参时不能传null一样。
            add_cache.setSup(head);
            this.head.setNext(add_cache);
            add_cache.setNext(null);
        }else{
            //将头结点后面的第一个结点找到,并设置一个指针指向它
            //空指针异常和c++中的一级指针传参时不能传null一样。
            Lnode p = this.head.getNext();
            p.setSup(add_cache);
            add_cache.setNext(p);
            add_cache.setSup(this.head);
            this.head.setNext(add_cache);
        }
        this.length++;
    }
    //查找一个结点
    public Lnode Search_Data(int data){
        if(length == 0){
            System.out.println("链表为空,啥也没有!!!");
            return null;
        }
        Lnode l_cache = this.head.getNext();
        int cache = length;
        while(cache>0){
            if(l_cache.getData() == data){
                System.out.println("已经查找到元素!!!");
                return l_cache;
            }else{
                l_cache = l_cache.getNext();
                cache--;
            }
        }
        System.out.println("链表里面没有该元素!!!");
        return null;
    }
    //删除指定结点
    public boolean Dele_Data(int data){
        //查找指定结点位置
        if(this.length == 0){
            return false;
        }
        if(this.length == 1){
            Lnode l = this.head.getNext();
            l.setSup(null);
            this.head.setNext(null);
            this.length--;
            return true;
        }
        Lnode l_cache = Search_Data(data);
        if(l_cache == null){
            System.out.println("删除失败。");
            return false;
        }else{
            Lnode pre = l_cache.getSup();
            Lnode rear = l_cache.getNext();
            pre.setNext(rear);
            rear.setSup(pre);
            this.length--;
            return true;
        }
    }
    //将旧数据找到并改为新数据
    public boolean Change_Data(int old_data,int new_data){
        Lnode l_cache = Search_Data(old_data);
        if (l_cache == null){
            System.out.println("没又找到符合的数据,无法修改。");
            return false;
        }else{
            l_cache.setData(new_data);
            return true;
        }
    }
}
//链表结点
//链表结构为双向链表
//结点保存上一级和下一级的地址
class Lnode{
    //数据域
    private int data;
    //下一个指针域
    private Lnode next;
    //上一个指针域
    private Lnode sup;
    int getData(){
        return this.data;
    }
    Lnode getNext(){
        return this.next;
    }
    Lnode getSup(){
        return this.sup;
    }
    void setData(int data){
        this.data = data;
    }
    void setNext(Lnode next){
        this.next = next;
    }
    void setSup(Lnode sup){
        this.sup = sup;
    }
}

测试代码

public class Stack_Text {
    public static void main(String[] args) {
        Stack s = new Stack();
        s.Init_Stack();
        int[] num = {1,2,3,5,4,87,5};
        for(int i:num){
            s.pop_Stack(i);
        }
        int cache = s.getLength();
        for (int i = 0; i <cache; i++) {
            System.out.println(s.push_Stack());
            System.out.println(s.Print_Stack());
        }
        System.out.println("查看length:"+s.getLength());
        System.out.println("再出栈一个:"+s.push_Stack());
        System.out.println("查看栈内元素:"+s.Print_Stack());
    }
}

以下为运行效果图

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值