首页 > 其他分享 >使用链表模拟队列和栈

使用链表模拟队列和栈

时间:2023-09-29 16:33:46浏览次数:38  
标签:Node zan head 队列 next 链表 public 节点 模拟

使用链表模拟队列

  • 案例1
// 创建节点类
public class Node {
    int n;
    Node next;
}

// 编写方法
public class Queue {

    Node head = new Node();

    Node last = new Node();

    private int len=0;

    public int size(){
        return this.len;
    }

    // 添加
    public void add(int n){
        // 新建元素节点
        Node node=new Node();
        node.n=n;
        // 当第一次添加时
        if (head.next==null){
            head.next=node;
            last.next=node; // 让新节点放到last节点后
            //
            len++;
        }else {
            // 已经有数据时
            // [head|][10|][|]
            Node node1= last.next;  // Node node1= head.next; 这里取出10这个节点
            node1.next=node;   // 让10这个节点指向新节点
            last.next=node;    // 让新节点作为last节点
            //
            len++;
        }
    }

    // 获取
    public int get(){
        // 取出头节点后的第一个节点中的值
        Node node= head.next;
        int n= node.n;
        // 头节点指向下下一个节点
        head.next=node.next;
        //
        len--;
        return n;
    }

}
  • 测试
public class test {
    public static void main(String[] args) {
        Queue queue=new Queue();
        queue.add(10);
        queue.add(20);
        queue.add(30);
        queue.add(40);
        queue.add(50);

        // 取出
        while (queue.size()>0){
            System.out.println(queue.get());
        }

    }
}

使用链表模拟栈

  • 案例1
// 创建节点类
public class Nod {
    int n;
    Nod top;
    Nod next;
}

// 编写方法
public class Zan {

    Nod head = new Nod();

    Nod last = new Nod();

    private int len=0;

    public int size(){
        return this.len;
    }

    // 添加
    // [head|][|][|]
    public void add(int n){
        // 新建元素节点
        Nod nod=new Nod();
        nod.n=n;
        // 当第一次添加时
        if (head.next==null){
            head.next=nod; // 新节点放在头节点后
            last.next=nod; // 让新节点放到last节点后
            nod.top=last;  // 新节点的top中存放last节点的地址
            //
            len++;
        }else {
            // 已经有数据时
            // [head|][10|][|]
            Nod nod1= last.next;  // Node node1= head.next; 这里取出10这个节点
            nod1.next=nod;   // 让10这个节点指向新节点
            nod.top=nod1;    // 让新节点的top指向10这个节点
            // 让last节点的next指向新节点,top指向新节点的前一个节点
            last.next=nod;
            last.top=nod1;
            len++;
        }
    }

    // 获取
    public int get(){
        // 取出last节点后面节点中的值
        Nod nod= last.next;
        int n= nod.n;
        // [head|][10|][20|]
        // 让last节点的next指向取出节点的前一个节点
        last.next=last.top;
        // 让last节点的top指向last节点的前一个节点的前一个节点
        last.top=last.top.top;
        //
        len--;
        return n;
    }

}
  • 测试
public class test {
    public static void main(String[] args) {
        Zan zan= new Zan();
        zan.add(10);
        zan.add(20);
        zan.add(30);
        zan.add(40);
        zan.add(50);

        // 取出
        while (zan.size()>0){
            System.out.println(zan.get());
        }

    }
}
  • 案例2
// 创建节点类
public class Node {
    int n;
    Node next;
}

// 编写方法
public class Zhan {

    Node head = new Node();

    private int len=0;

    public int size(){
        return this.len;
    }

    // 添加
    // [head|][|][|]
    public void add(int n){
        // 新建元素节点
        Node node=new Node();
        node.n=n;
        // 当第一次添加时
        if (head.next==null){
            head.next=node; // 新节点放在头节点后
            len++;
        }else {
            // 已经有数据时
            // [head|][10|][|]
            Node cur=head;
            for(int j=0; j<len; j++){
                cur=cur.next;
            }
            cur.next=node;
            len++;
        }
    }

    // 获取
    public int get(){
        Node cur=head;
        for(int j=0; j<len; j++){
            cur=cur.next;
        }
        len--;
        return cur.n;
    }

}
  • 测试
public class test {
    public static void main(String[] args) {
        Zhan zan= new Zhan();
        zan.add(10);
        zan.add(20);
        zan.add(30);
        zan.add(40);
        zan.add(50);

        // 取出
        while (zan.size()>0){
            System.out.println(zan.get());
        }

    }
}
  • 案例3
// 创建节点类
public class Node {
    int n;
    Node next;
}

// 编写方法
public class Zn {

    Node head = new Node();

    // 添加
    // [head|][|][|]
    public void add(int n){
        // 新建元素节点
        Node node=new Node();
        node.n=n;
        // 当第一次添加时
        if (head.next==null){
            head.next=node; // 新节点放在头节点后
        }else {
            // 已经有数据时
            // [head|][10|][|]
            Node cur=head;
            while (cur.next != null) {
                cur=cur.next;
            }
            cur.next=node;
        }
    }

    // 获取
    public int get(){
        Node cur=head;
        Node top = null; // 指向当前节点的上一个节点
        while (cur.next != null) {
            top=cur; // 指向下一个节点前,先存入top
            cur=cur.next;
        }
        top.next=null;
        return cur.n;
    }

}
  • 测试
public class test {
    public static void main(String[] args) {
        Zn zan= new Zn();
        zan.add(10);
        zan.add(20);
        zan.add(30);
        zan.add(40);
        zan.add(50);

        // 后进先出,只要头节点后面的节点不为空,则一直取
        Node cur = zan.head;
        while (cur.next != null) {
            System.out.println(zan.get());
        }

    }
}

标签:Node,zan,head,队列,next,链表,public,节点,模拟
From: https://www.cnblogs.com/dogleftover/p/17737083.html

相关文章

  • 使用数组模拟队列和栈
    使用数组模拟队列案例1publicclassQueue{privateint[]num=newint[5];privateintlen=0;publicintsize(){returnthis.len;}//添加publicintadd(intn){if(len<num.length){num[len]=n;......
  • 模拟链表
    创建节点类publicclassNode{intn;Nodenext;}编写方法publicclassMyLinkList{Nodehead=newNode();privateintlen=0;//获取长度publicintsize(){returnlen;}//添加元素到最后publicvoid......
  • 使用数组模拟集合
    编写方法publicclassMyArrayList{privateint[]n=newint[10];//动态数组privateintsize=0;//长度publicintsize(){returnthis.size;}//添加一个元素publicvoidadd(intelement){n[size]=element;......
  • 链表插入排序
    创建节点类publicclassNode{intn;Nodenext;}第1次推导publicclasstest{publicstaticvoidmain(String[]args){//新建节点Nodenode1=newNode();node1.n=2;Nodenode2=newNode();node......
  • 链表冒泡排序
    创建节点类publicclassNode{intn;Nodenext;}第1次推导publicclasstest{publicstaticvoidmain(String[]args){//新建节点Nodenode1=newNode();node1.n=9;Nodenode2=newNode();no......
  • 浅谈数据结构栈与队列
    栈1.栈的基本概念栈(Stack):是只允许在一端进行插入或删除的线性表。首先栈是一种线性表,但限定这种线性表只能在某一端进行插入和删除操作。不能插入和删除的一端为栈底(Bottom)栈顶(top):线性表允许进行插入删除的那一端栈底(bottom):固定的,不允许进行插入和删除的那一端空栈:不含任何元素的......
  • 恩尼格玛机模拟动图
    打开恩尼格玛机模拟动图网站,可直接查看对应动图效果,具体操作如下:再次点击Encrypt后Output栏会和上次的不同..其他实际项参考下图,具体各项释义可查Enigma运行原理......
  • 模拟集成电路设计系列博客——2.2.1 折叠Cascode放大器的基本结构
    2.2.1折叠Cascode放大器的基本结构许多现代CMOS集成电路放大器设计仅用于驱动容性负载。由于驱动的是容性负载,放大器并不需要通过一个电压缓冲器来获得较低的输出阻抗。因此相比那些必须要驱动阻性负载的放大器,更可能获得更快的速度和更大的信号摆幅。而这些增长仅仅需要通过在......
  • Linux-----单链表
    Linux中实现链表//定义链表节点结构体structNode{intdata;//数据区structNode*next;//指向下一个地址域};//初始化链表为空格structNode*head=NULL;//插入元素到链表的末尾voidinsert(intdata){sturctNode*newNode=(structNode*)malloc(sizeof(struct......
  • FX3U-3A-ADP模拟量和数字量之间转换
    简单的例子:0-10V对应0-8,4-20mA对应0-30 以下是对上面例子的详解:电压:  电压(0-10V)0-10V对应着数字量0-4000数字量与变频器HZ量之间的关系是(4000-0)/(50-0)=80故如果你想转多少HZ数,就需要在后面(乘上80),这个才是你HZ数对应的数字量 注意:如果设置了M8262ON是电流输出......