用链表实现队列和栈

1.链表实现队列

1.定义队列接口

public interface MyQueue {
    int size();
    boolean isEmpty();
    void offer(Integer value);
    int poll();
    int peek();
}

2.设计接口的实现类
首先队列是一个容器,要设置容器的各种方法,我们需要一些原料,这里我选择两个节点和一个表示容器大小的N,头节点first用于获取队列头部元素,last指向队列尾部,同时也作为一个游标,来循环访问队列各个元素。
关于方法,主要是offer方法和poll方法的实现,对于offer方法,队列为空时,直接将头和尾指向新节点,不为空时,我们将当前节点指向新节点,并且游标后移,每次判断完后,容量N加一。对于poll方法,也要先进行判空操作,如果为空,返回-1,如果不为空,我们从头部获取要弹出元素,first后移,并且容量减一,完成出队操作。

public class ListQueue implements MyQueue{
    class Node{
        int val;
        Node next=null;
        Node(int x){
            this.val=x;
        }
    }
    private Node first;
    private Node last;
    private int N=0;

    @Override
    public int size() {
        return N;
    }

    @Override
    public boolean isEmpty() {
        return N==0;
    }

    @Override
    public void offer(Integer value) {
        Node newNode=new Node(value);
        if(isEmpty()){
            first=newNode;
            last=newNode;
        }
        else{
            last.next=newNode;
            last=newNode;
        }
        N++;
    }

    @Override
    public int poll(){
        if(isEmpty()){
            return -1;
        }
        int value=first.val;
        first=first.next;
        N--;
        return value;
    }

    @Override
    public int peek() {
        if(isEmpty()){
            return -1;
        }
        return first.val;
    }
}

3.验证设计的正确性

public static void main(String[] args){
        ListQueue queue=new ListQueue();
        queue.offer(1);
        queue.offer(2);
        queue.offer(4);
        queue.offer(3);
        queue.offer(5);
        System.out.println(queue.isEmpty());
        System.out.println(queue.peek());
        queue.poll();
        System.out.println(queue.poll());
        System.out.println(queue.peek());

    }

最终打印的结果:
在这里插入图片描述

2.链表实现栈

1.定义栈的接口

public interface MyStack {
    int size();
    boolean isEmpty();
    void push(Integer value);
    int pop();
    int peek();
}

2.设计接口的实现类
同样先准备一些原料,包括一个节点top,和一个表示栈容量大小的N,top节点总是指向栈顶元素。
然后设计push和pop方法,对于push方法,我们利用链表的头插法,每一个新进来的节点都指向当前的top,然后再把top以到新节点的位置,同时容量N加一。对于pop方法,同样是先判空,如果为空,就返回-1,不为空,就记录当前栈顶元素值,弹出,并且将top后移,容量减一。

public class ListStack implements MyStack{
    class Node{
        int val;
        Node next=null;
        Node(int x){
            this.val=x;
        }
    }
    private Node top;
    private int N=0;
    @Override
    public int size() {
        return N;
    }

    @Override
    public boolean isEmpty() {
        return N==0;
    }

    @Override
    public void push(Integer value) {
        Node newTop=new Node(value);
        newTop.next=top;
        top=newTop;
        N++;
    }

    @Override
    public int pop() {
        if(isEmpty()){
            return -1;
        }
        int value=top.val;
        top=top.next;
        N--;
        return value;
    }

    @Override
    public int peek() {
        if(isEmpty()){
            return -1;
        }
        return top.val;
    }
}

3.验证设计的正确性

public class Main {
    public static void main(String[] args){
        ListStack stack=new ListStack();
        stack.push(1);
        stack.push(3);
        stack.push(2);
        stack.push(5);
        stack.push(4);
        System.out.println(stack.isEmpty());
        System.out.println(stack.peek());
        stack.pop();
        System.out.println(stack.pop());
        System.out.println(stack.peek());
        }
}

最终打印的结果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值