基础数据结构之栈和队列
来源:互联网 发布:域名错误691是什么原因 编辑:程序博客网 时间:2024/06/18 08:03
前言
我们生活中会遇见很多的看起来很棘手的问题,比如一堆尚未处理的邮件,正要去电影院排队购买一张电影票(两张也可以,你值得拥有...),这些我们用自己熟悉的方式来处理他们,而我们自己熟悉的这些方式,很可能就和栈和队列有关.今天,我们就来谈谈栈和队列.
栈的定义
栈是限定仅在表尾进行插入和删除操作的线性表,也称为LIFO(后入先出)结构.
我们把允许插入和删除的地方称之为栈顶,另一端称之为栈底,,不包含任何元素称之为空栈
栈可以用顺序结构和链式结构表示,本例为了方便描述问题,用顺序结构.
栈的插入操作我们称之为入栈(push),栈的删除操作我们称之为出栈(pop)
顺序栈的Java代码实现
5.public class Statck<E extends Object> { 6. private List<E> pool = new ArrayList<E>(); 7. 8. public Statck() { 9. } 10. 11. public void clear() { 12. pool.clear(); 13. } 14. 15. public boolean isEmpty() { 16. return pool.isEmpty(); 17. } 18. 19. /** 20. * 获取栈顶元素 21. * */ 22. public E getTopObjcet() { 23. if (isEmpty()) {return null;} 24. return pool.get(0); 25. } 26. 27. /** 28. * 弹出栈操作 29. * */ 30. public E pop() { 31. if (isEmpty()) {throw new EmptyStackException();} 32. return pool.remove(pool.size() - 1); 33. } 34. 35. /** 36. * 压入栈 37. * */ 38. public void push(E e) { 39. if (isEmpty()) {throw new EmptyStackException();} 40. pool.add(e); 41. } 42. 43. /** 44. * 获取当前栈大小 45. * */ 46. public int getStatckSize() { 47. if (isEmpty()) {throw new EmptyStackException();} 48. return pool.size(); 49. } 50. 51.}
队列的定义
队列是只允许在一端插入,在另一端删除的线性表
允许插入的那一端称之为队尾,允许删除的那一端称之为队头,是FIFO(先入先出)结构
队列有顺序结构和链式结构,一般来说,顺序结构删除操作是o(N)的性能,而链式结构只有o(1);
顺序队列的Java代码实现
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) // 进队列 { if(rear == maxSize-1) // 处理循环 rear = -1; queArray[++rear] = j; // 队尾指针加1,把值j加入队尾 nItems++; } public long remove() // 取得队列的队头元素。 { long temp = queArray[front++]; // 取值和修改队头指针 if(front == maxSize) // 处理循环 front = 0; nItems--; return temp; } public long peekFront() // 取得队列的队头元素。该运算与 remove()不同,后者要修改队头元素指针。 { return queArray[front]; } public boolean isEmpty() // 判队列是否为空。若为空返回一个真值,否则返回一个假值。 { return (nItems==0); } public boolean isFull() // 判队列是否已满。若已满返回一个真值,否则返回一个假值。 { return (nItems==maxSize); }
public int size() // 返回队列的长度 { return nItems; } } public class IntegerQueue{public static void main(String[] args) { Queue theQueue = new Queue(5); // 队列有5个元素 theQueue.insert(10); // 添加4个元素 theQueue.insert(20); theQueue.insert(30); theQueue.remove(); // 移除3个元素 theQueue.insert(60); theQueue.insert(70); theQueue.insert(80); while( !theQueue.isEmpty() ) // 遍历队列并移除所有元素 { long n = theQueue.remove(); // (40, 50, 60, 70, 80) System.out.print(n); System.out.print(" "); } System.out.println(""); } }
0 0
- 基础数据结构之栈和队列
- 数据结构基础:栈和队列
- 基础数据结构之队列
- 数据结构基础之队列
- 数据结构基础之队列
- 数据结构之栈和队列
- 数据结构之栈和队列
- 数据结构之栈和队列
- 数据结构之栈和队列
- 数据结构之栈和队列
- 数据结构之栈和队列
- 数据结构之栈和队列
- 数据结构之栈和队列
- 数据结构之栈和队列
- 数据结构之栈和队列
- 数据结构之栈和队列
- 数据结构之栈和队列
- 数据结构之栈和队列
- [opencv]6.添加滑动条调整图片
- XCode7.3 模拟器键盘不显示解决方案
- Highway Project 浙江省第十三届省赛K题 ZOJ 3946(dijkstra+邻接表+优先队列)
- 2016/5/3
- 第4周项目3小学生算数能力测试
- 基础数据结构之栈和队列
- 普通工厂方法
- redis make test tcl 8.5 or newer in order to run the Redis test
- android get方法访问本地服务器显示refuse原因之一
- TCP拥塞控制图解(不包括RTO,因为它太简单了) 【勘误1】
- 给Android封装的一个简单网络请求框架
- 用python爬虫抓站的一些技巧总结
- 学习Redis的一本好书: Redis Essentials
- Java基础之基本运算