Java数据结构与算法之栈和队列
来源:互联网 发布:在领军教育上班 知乎 编辑:程序博客网 时间:2024/05/14 21:36
此类数据结构和算法更多是作为程序员的工具来运用。他们主要作为构思算法的辅助工具,而不是完全的数据存储工具。这些数据结构的生命周期比那些数据库类型的结构要短得多。在程序操作执行期间它们才被创建,通常用它们去执行某项特殊任务,当完成任务之后,它们就被销毁。
两个特点:
1、访问受限
访问是受限制的,即在特定时刻只有一个数据项可以被读取或者被删除。
2、更加抽象
栈、队列和优先级队列是比数组和其他数据存储结构更为抽象的数据结构,主要通过接口对栈、队列和优先级队列进行定义,这些接口表明他们可以完成的操作,而它们的主要实现机制对用户来说是不可见的。
一、栈
特点:
1、栈只允许访问一个数据项,即最后插入的数据项。移除这个数据项后才能访问倒数第二个插入的数据项,依此类推。
2、大部分微处理器运用的是基于栈的体系结构。当调用一个方法时,把它的返回地址和参数压入栈,当方法结束返回时,那些数据出栈。栈操作就嵌入在微处理器中。
3、栈可以由不同的数据结构来实现,比如数组,比如链表。
4、Stack LIFO 后进先出。
注意:不要把栈的概念和实现它的内部数据结构混为一谈。
以下为用数组实现栈:
/** * 栈的实例 * @author wuweizi * */public class StackX {private int maxSize;private long [] stackArray;private int top;//....................................................public StackX(int s) {super();this.maxSize = s;this.stackArray = new long[maxSize];this.top = -1;}//....................................................public void push(long j){//attention! first point addself,and then insert data//写英语注释真憋屈,直接一点吧!此行代码的意思是,先开辟一片空间,然后再往里添加数据stackArray[++top] = j;}//....................................................public long pop(){//attention! first remove data,postfix decrement//此行代码的意思是,先把数据项移走,然后把此片空闲空间回收!你住着我的房子不走,我怎么把房子收回来啊!return stackArray[top--];}public long peek(){return stackArray[top];}//....................................................public boolean isEmpty(){return top == -1;}//....................................................public boolean isFull(){return top == maxSize - 1;} public static void main(String[] args) {StackX theStack = new StackX(10);//make new stacktheStack.push(20);theStack.push(40);theStack.push(60);theStack.push(80);while(!theStack.isEmpty())//until its empty{ //delete item from stack,is not really deletelong value = theStack.pop(); System.out.println(value);//display itSystem.out.println("");}System.out.println("");}}
以下为用链表实现栈:
/** * 节点类 * @author Administrator * */class Link {public long dData;//声明了long数据类型变量public Link next;//声明了Link数据类型对象//构造方法,初始化dDatapublic Link(long dd) {this.dData = dd;}//链表展示方法,将dData显示出来public void displayLink(){System.out.println(dData+"");}}/** * 链表类 * @author Administrator * */class LinkList{private Link first;//声明first节点/** * 初始化first节点 * @param first */public LinkList(Link first) {this.first = null;}/** * 判断first节点是否为空 * @return */public boolean isEmpty(){return first==null;}/** * 添加节点,然后first节点与新节点互换位置 * @param dd */public void insertFirst(long dd){Link newLink = new Link(dd);newLink.next=first;first = newLink;}/** * 删除节点,将first节点归还于链表类 * @return */public long deleteFirst(){Link temp = first;first = first.next;return temp.dData;}public void displayList(){Link current = first;while(current!=null){current.displayLink();current = current.next;}System.out.println("");}}/** * 链表栈类 * @author Administrator * */class LinkStack{private LinkList theList;public LinkStack(LinkList theList) {this.theList = new LinkList(null);}public void push(long j){theList.insertFirst(j);}public long pop(){return theList.deleteFirst();}public boolean isEmpty(){return theList.isEmpty();}public void displayStack(){System.out.println("Stack(top-->bottom):");theList.displayList();}}
/** * 栈应用程序类 * @author Administrator * */public class LinkStackApp {public static void main(String[] args) {LinkStack theStack = new LinkStack(null);theStack.push(20);theStack.push(40);theStack.displayStack();theStack.push(60);theStack.push(80);theStack.displayStack();theStack.pop();theStack.pop();theStack.displayStack();}}
二、队列
队列是一种数据结构,有点类似栈,只是在队列中第一个插入的数据项也会最先被移除(先进先出FIFO),而在栈中,最后插入的数据项最先移除(LIFO)。
以下为用数组实现队列:
<pre name="code" class="java">/** * 队列的实例 * @author wuweizi * */public class Queue {private int maxSize;private long [] queArray;private int front;private int rear;private int nItems;public Queue(int s){maxSize = s;//数组大小queArray = new long[maxSize];//创建一个数组front = 0;//队头下标rear = -1;//队尾下标nItems = 0; //数据项个数}public void insert(long j){//当队尾下标值达到最大值的时候,我们设置队尾下标为-1if (rear == maxSize-1) {rear = -1;}queArray[++rear] = j;nItems++;}public long remove(){long temp = queArray[front++];if (front==maxSize) {front = 0;}nItems--;return temp;} public long peekFront(){return queArray[front];} public long peekRear(){return queArray[rear];}public boolean isEmpty(){return nItems == 0;}public boolean isFull(){return nItems == maxSize;}public int size(){return nItems;}}public class QueueApp {public static void main(String[] args) {Queue theQueue = new Queue(5);theQueue.insert(10);theQueue.insert(20);theQueue.insert(30);theQueue.insert(40);System.out.println(theQueue.remove());System.out.println(theQueue.remove());System.out.println(theQueue.remove());theQueue.insert(50);theQueue.insert(60);theQueue.insert(70);theQueue.insert(80);while(!theQueue.isEmpty()){long n = theQueue.remove();System.out.println(n);System.out.println(" ");}System.out.println(" ");}}
以下为用链表实现队列:
/** * 节点类 * @author Administrator * */class Link {public long dData;//声明了long数据类型变量public Link next;//声明了Link数据类型对象//构造方法,初始化dDatapublic Link(long dd) {this.dData = dd;}//链表展示方法,将dData显示出来public void displayLink(){System.out.println(dData+"");}}/** * 队列两头类 * @author Administrator * */class FirstLastList{private Link first;private Link last;public FirstLastList(Link first, Link last) {this.first = null;this.last = null;}public boolean isEmpty(){return first==null;}public void isnertLast(long dd){Link newLink = new Link(dd);if (isEmpty()) {first = newLink;}else {last.next = newLink;}last = newLink;}public long deleteFirst(){long temp = first.dData;if (first.next==null) {last = null;}first = first.next;return temp;}public void displayList(){Link current = first;while(current!=null){current.displayLink();current = current.next;}System.out.println("");}}/** * 链表实现的队列类 * @author Administrator * */class LinkQueue{private FirstLastList theList;public LinkQueue(FirstLastList theList) {this.theList = new FirstLastList(null, null);} public boolean isEmpty(){return theList==null;}public void insert(long j){theList.isnertLast(j);}public long remove(){return theList.deleteFirst();}public void displayQueue(){System.out.println("Queue(front-->rear)");theList.displayList();}}
/** * 栈应用程序类 * @author Administrator * */public class LinkQueueApp {public static void main(String[] args) {LinkQueue theQueue = new LinkQueue(null);theQueue.insert(20);theQueue.insert(40);theQueue.displayQueue();theQueue.insert(60);theQueue.insert(80);theQueue.displayQueue();theQueue.remove();theQueue.remove();theQueue.displayQueue();}}
三、栈和队列的区别
1、队列先进先出,栈先进后出
2、栈是限定只能在表的一端进行插入和删除操作的线性表;队列限定只能在表的一端进行插入和在另一端进行删除操作的线性表
3、数据元素之间的关系相同,但数据类型却完全不同
4、遍历速度不同,栈只能从头部取数据 也就最先放入的需要遍历整个栈最后才能取出来,而且在遍历数据的时候还得为数据开辟临时空间,保持数据在遍历前的一致性列怎不同,他基于地址指针进行遍历,而且可以从头或尾部开始遍历,但不能同时遍历,无需开辟临时空间,因为在遍历的过程中不影像数据结构,速度要快的多
- Java数据结构与算法之栈和队列
- 数据结构与算法之栈和队列
- 数据结构与算法之栈和队列
- Java数据结构和算法--栈与队列
- Java数据结构和算法--栈与队列
- Java数据结构和算法--栈与队列
- Java数据结构与算法之队列
- Java数据结构与算法之优先级队列
- Java数据结构与算法之Queue队列
- 数据结构与算法分析之04栈和队列
- 数据结构与算法之三(栈和队列的java实现)
- java数据结构与算法 第4章 栈和队列
- 数据结构与算法(java)——栈和队列
- 数据结构与算法(Java语言描述)--栈和队列
- 数据结构与算法之----栈与队列
- 数据结构与算法之栈与队列
- 数据结构与算法之栈与队列
- 数据结构与算法之栈与队列
- Canvas清屏的实现
- Mybatis分页实现流程
- C#到exe程序到PHP获得返回值基础测试
- 汇编学习笔记第11~13章:标志寄存器-内中断-int指令
- storm 简介
- Java数据结构与算法之栈和队列
- adb shell 是什么,如何进入?
- 胸肝魂牵梦萦
- ruby on rails 国际化
- 三种常见的过滤器
- Mysql 左右连接举例说明
- java 操作mongo 一个字段传递两个条件
- Ubuntu12.04 安装vim
- 《Python3程序开发指南(第二版)》第1章 过程型程序设计快速入门 练习