博客
关于我
Java~分别用顺序表和链表实现栈和队列,以及库的栈和队列的使用
阅读量:345 次
发布时间:2019-03-04

本文共 3955 字,大约阅读时间需要 13 分钟。

栈与队列基础知识与实现

栈的概念与实现

栈是一种特殊的线性表,其操作具有严格的先进先出(LIFO,Last In First Out)的特性。栈的数据插入和删除只能在固定的一端进行,称为栈顶,另一端称为栈底。栈的数据存储结构可以用数组或链表来实现,具体选择取决于性能需求和数据规模。

栈的操作

  • 压栈(Push):将数据元素插入栈顶。
  • 出栈(Pop):从栈顶删除并取出数据元素。
  • 查看栈顶元素(Peek):查看当前栈顶的数据元素,不改变栈的状态。

栈的实现

以下是使用数组和链表两种数据结构实现栈的代码示例:

数组实现的栈

public class MyStack {    private int[] array = new int[100];    private int size = 0;    public void push(int value) {        array[size] = value;        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 class MyStack2 {    static class Node {        public int val;        public Node next;        public Node(int val) {            this.val = val;        }    }    private Node head;    private Node tail;    public void push(int value) {        Node newNode = new Node(value);        if (head == null) {            head = newNode;            tail = newNode;        } else {            newNode.next = head;            head = newNode;        }    }    public Integer pop() {        if (head == null) {            return null;        }        Node removedNode = head;        head = head.next;        return removedNode.val;    }    public Integer peek() {        if (head == null) {            return null;        }        return head.val;    }}

队列的概念与实现

队列是一种只允许在一端插入数据、另一端删除数据的特殊线性表,其操作具有先进先出的特性。队列的插入操作称为入队列,删除操作称为出队列。队列的数据存储结构可以用数组或链表实现,链表结构在某些情况下表现更优。

队列的操作

  • 入队列(Enqueue):将数据元素插入队尾。
  • 出队列(Dequeue):从队头删除并取出数据元素。
  • 查看队头元素(Peek):查看当前队头的数据元素,不改变队列的状态。

队列的实现

以下是使用链表和数组两种数据结构实现队列的代码示例:

链表实现的队列

public class MyQueue {    static class Node {        public int val;        public Node next;        public Node(int val) {            this.val = val;        }    }    private Node head;    private Node tail;    public void offer(int value) {        Node newNode = new Node(value);        if (head == null) {            head = newNode;            tail = newNode;        } else {            newNode.next = head;            tail = newNode;        }    }    public Integer poll() {        if (head == null) {            return null;        }        Node removedNode = head;        head = head.next;        return removedNode.val;    }    public Integer peek() {        if (head == null) {            return null;        }        return head.val;    }}

数组实现的队列

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 value) {        if (size >= array.length) {            return false;        }        array[tail] = value;        tail++;        if (tail >= array.length) {            tail = 0;        }        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];    }}

栈与队列的使用示例

以下是使用Java库中的StackQueue实现栈和队列的示例代码:

栈示例

import java.util.Stack;public class StackExample {    public static void main(String[] args) {        Stack
stack = new Stack<>(); stack.push('g'); stack.push('o'); stack.push('o'); stack.push('d'); while (!stack.empty()) { System.out.println(stack.pop()); } }}

队列示例

import java.util.LinkedList;public class QueueExample {    public static void main(String[] args) {        LinkedList
queue = new LinkedList<>(); queue.offer("aaa"); queue.offer("bbb"); queue.offer("ccc"); while (!queue.isEmpty()) { System.out.println(queue.poll()); } }}

通过以上内容,可以清晰地了解栈和队列的概念、实现方式以及实际使用示例。

转载地址:http://zjee.baihongyu.com/

你可能感兴趣的文章
netty 主要组件+黏包半包+rpc框架+源码透析
查看>>
Vue过渡 & 动画---vue工作笔记0014
查看>>
Netty 异步任务调度与异步线程池
查看>>
Netty 的 Handler 链调用机制
查看>>
Netty 编解码器详解
查看>>
Netty 解决TCP粘包/半包使用
查看>>
Netty 调用,效率这么低还用啥?
查看>>
Netty+Protostuff实现单机压测秒级接收35万个对象实践经验分享
查看>>
Netty+SpringBoot+FastDFS+Html5实现聊天App详解(一)
查看>>
netty--helloword程序
查看>>
Netty5.x 和3.x、4.x的区别及注意事项(官方翻译)
查看>>
netty——bytebuf的创建、内存分配与池化、组成、扩容规则、写入读取、内存回收、零拷贝
查看>>
netty——Channl的常用方法、ChannelFuture、CloseFuture
查看>>
netty——Future和Promise的使用 线程间的通信
查看>>
Vue输出HTML
查看>>
netty——黏包半包的解决方案、滑动窗口的概念
查看>>
Netty中Http客户端、服务端的编解码器
查看>>
Netty中使用WebSocket实现服务端与客户端的长连接通信发送消息
查看>>
Netty中实现多客户端连接与通信-以实现聊天室群聊功能为例(附代码下载)
查看>>
Netty中的组件是怎么交互的?
查看>>