栈和队列(基本概念理解+实现代码)

栈:Stack

一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈 顶,另一端称为栈底。栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。
压栈:栈的插入操作叫做进栈/压栈/入栈,入数据在栈顶。
出栈:栈的删除操作叫做出栈,出数据在栈顶。

栈的顺序结构和链式结构,进栈/出栈时间复杂度均为O(1)。

队列:Queue

只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出FIFO(First In First Out)

入队列:进行插入操作的一端称为队尾(Tail/Rear)  时间复杂度:O(1)。

出队列:进行删除操作的一端称为队头 (Head/Front)时间复杂度:O(n)。

队列的链式结构出队列和入队列时间复杂度均为O(n)。

 

栈和队列的实现代码练习:

1.顺序表实现栈

public class MyList {
    private int[] array = new int[100];
    private int size = 0;

    public void push(int val){
        array[size] = val;
        size++;
    }

    public Integer pop(){
        if (size <= 0){
            return null;
        }
        int ret = array[size-1];
        size--;
        return ret;
    }
    public Integer peek(){
        if (size <= 0){
            return null;
        }
        return array[size - 1];
    }

    public static void main(String[] args) {
        MyList myList = new MyList();
        myList.push(8);
        myList.push(45);
        myList.push(32);
        myList.push(56);
        myList.push(7);
        System.out.println(myList.peek());
        System.out.println("================");
        while (myList.size != 0){
            System.out.print(myList.pop() + " ");
        }
    }
}

2.链表实现栈

public class MyList2 {
    class Node{
        int val;
        Node next;

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

    Node head = new Node(-1);
    public void push(int val){
        Node newNode = new Node(val);
        newNode.next = head.next;
        head.next = newNode;
    }

    public Integer pop(){
        if (head.next == null){
            return null;
        }
//        Node del = head.next;
//        head.next = del.next;
//        return del.val;
        int ret = head.next.val;
        head.next = head.next.next;
        return ret;
    }

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

    public static void main(String[] args) {
        MyList2 myList2 = new MyList2();
        myList2.push(5);
        myList2.push(2);
        myList2.push(3);
        myList2.push(4);
        myList2.push(1);
        System.out.println(myList2.peek());
        System.out.println("=================");
        while (myList2.head.next != null){
            System.out.print(myList2.pop() + " ");
        }

    }
}

3.链表实现队列

//链表实现队列
//链表头部作为队首,链表尾部作为队尾,带傀儡节点
public class MyQueue {
    static class Node{
        int val;
        Node next;

        public Node(int val) {
            this.val = val;
        }
    }
    Node head = new Node(-1);
    Node tail = head;
    public void offer(int val){
        Node newNode = new Node(val);
        tail.next = newNode;
        tail = tail.next;
    }
    public Integer poll(){
        if (head.next == null){
            return null;
        }
        Node toDelete = head.next;
        head.next = toDelete.next;
        if (head.next == null){
            tail = head;
        }
        return toDelete.val;
    }
    public Integer peek(){
        if (head.next == null){
            return null;
        }
        return head.next.val;
    }


    public static void main(String[] args) {
        MyQueue myQueue = new MyQueue();
        myQueue.offer(1);
        myQueue.offer(2);
        myQueue.offer(3);
        myQueue.offer(4);
        myQueue.offer(5);
        Node cur = myQueue.head.next;
        while (cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
//        System.out.println(myQueue.poll());
//        System.out.println(myQueue.tail.val);
//        System.out.println("======================");
//        System.out.println(myQueue.head.next.val);
    }
}

4.顺序表实现队列

//顺序表实现队列
public class MyQueue2 {
    private int[] array = new int[100];
    private int head = 0;
    private int tail = 0;
    private int size = 0;

    public boolean offer(int val){
        if (size == array.length){
            return false;
        }
        array[tail] = val;
        tail++;
        tail = tail % array.length;
        size++;
        return true;
    }

    public Integer poll(){
        if (size == 0){
            return null;
        }
        int ret = array[head];
        head++;
        if (head >= array.length){
            head = 0;
        }
        size--;
        return ret;
    }
    public Integer peek(){
        if (size == 0){
            return null;
        }
        return array[head];
    }
    public static void main(String[] args) {
        MyQueue2 myQueue2 = new MyQueue2();
        myQueue2.offer(12);
        myQueue2.offer(45);
        myQueue2.offer(22);
        myQueue2.offer(1);
        System.out.println(myQueue2.peek());
        System.out.println("==============");
        int ret = 0;
        while (myQueue2.size != 0){
            ret = myQueue2.poll();
            System.out.print(ret + " ");
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值