栈和队列分别用顺序表组和链表表示

栈的两种表示方法

public class MyStack {
    public int[] arrays = new int[100];
    public int userSize = 0;

    public MyStack() {
    }

    public MyStack(int[] arrays) {
        this.arrays = arrays;
    }

    //进栈
    public void push(int data) {
        this.arrays[this.userSize] = data;
        this.userSize++;
    }

    public Integer pop() {
        if(this.userSize <= 0) {
            return null;
        }
        int ret = this.arrays[this.userSize - 1];
        this.userSize--;
        return ret;
    }

    public Integer peek() {
        if(this.userSize <= 0) {
            return null;
        }
        int ret = arrays[this.userSize - 1];
        return ret;
    }

    public boolean empty() {
        if(this.userSize == 0) {
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        MyStack stack = new MyStack();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);

       while(true) {
           Integer cur = stack.pop() ;
           if(cur == null) {
               break;
           }
           System.out.println(cur);
       }

    }
}
public class MyStackTwo {
    static class Node{
        public int val;
        public Node next;

        public Node(int val) {
            this.val = val;
        }
        public Node() {

        }
    }

    Node head = new Node();

    public void push(int data) {

        Node cur = new Node(data);
        if(this.head == null) {
            this.head = cur;
            return;
        }
        cur.next = this.head;
        this.head = cur;
    }

    public Integer pop() {

        Node node = new Node();
        node = this.head;
        if (node == null) {
            return null;
        }
        this.head = this.head.next;
        return node.val;
    }

    public Integer peek() {
        Node node = new Node();
        if (this.head == null) {
            return null;
        }
        node = this.head;
        return node.val;
    }

    public boolean empty() {
        if(this.head == null) {
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        MyStackTwo stackTwo = new MyStackTwo();
        stackTwo.push(1);
        stackTwo.push(2);
        stackTwo.push(3);
        stackTwo.push(4);
        stackTwo.push(5);

        while(true) {
            Integer cur = stackTwo.pop();
            if(cur == null){
                break;
            }
            System.out.println(cur);
        }
    }

}

队列的两种表达方式

public class MyQueue {
    static class Node {
        public int val;
        public Node next;

        public Node(int val) {
            this.val = val;
        }
        public Node() {

        }
    }

    Node head = new Node(-1);
    Node tail = head;
    public void offer(int val) {
        Node node = new Node(val);
        this.tail.next = node;
        tail = tail.next;
    }

    public Integer poll() {

        if(this.head.next == null) {
            return null;
        }

        Node node = new Node();
        node = this.head.next;
        this.head.next = node.next;
        if(this.head.next == null) {
            this.tail = this.head;
        }
        return node.val;
    }

    public Integer peek() {
        if(this.head.next == null) {
            return null;
        }
        return this.head.next.val;
    }

    public static void main(String[] args) {
        MyQueue queue = new MyQueue();
        queue.offer(1);
        queue.offer(2);
        queue.offer(3);
        queue.offer(4);

        while(true) {
            Integer cur = queue.poll();
            if(cur == null) {
                break;
            }
            System.out.println(cur);
        }

    }
}
public class MyQueueTwo {
    int[] arrays = new int[100];
    int userSize = 0;
    int head = 0;
    int tail = 0;

    public boolean offer(int data) {
        if(this.userSize == this.arrays.length) {
            return false;
        }
        this.arrays[this.tail] = data;
        this.tail++;
        this.tail = this.tail % this.arrays.length;
        this.userSize++;
        return true;
    }

    public Integer poll() {
        if (this.userSize == 0) {
            return null;
        }
        Integer num = this.arrays[this.head];
        this.head++;
        this.head = this.head % this.arrays.length;
        this.userSize--;
        return num;
    }

    public Integer peek() {
        if (this.userSize == 0) {
            return null;
        }
        return this.arrays[this.head];
    }

    public boolean empty() {
        if(this.userSize == 0) {
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        MyQueueTwo queueTwo = new MyQueueTwo();
        queueTwo.offer(1);
        queueTwo.offer(2);
        queueTwo.offer(3);
        queueTwo.offer(4);
        queueTwo.offer(5);

        while (true) {
            Integer num = queueTwo.poll();
            if(num == null) {
                break;
            }
            System.out.println(num);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值