栈和队列的Java实现
一、 栈
1、概念
栈是一种特殊的线性表,它只能在栈顶(top)进行插入(push)和删除(pop)操作。

栈的常用操作:
入栈(push):向栈顶插入元素
出栈(pop):从栈顶删除元素
访问栈顶元素(peek):访问栈顶元素
2、 栈的顺序结构的实现
public class SequenceStack<T> {
private Object[] elementData; //数组用于承装元素
private int DEFAULT_SIZE = 20; //初始数组默认大小
private int capacity;
private int capacityIncrement = 5;
private int size = 0; //栈中当前容量
public SequenceStack(){
capacity = DEFAULT_SIZE;
elementData = new Object[capacity];
}
public SequenceStack(T element){
this();
elementData[0] = element;
size++;
}
//返回栈的长度
public int length(){
return this.size;
}
//入栈操作
public void push(T element){
this.ensureCapacity();
elementData[size] = element;
size++;
}
//出栈操作
public T pop(){
T popElement = (T)elementData[size-1];
size--;
return popElement;
}
//访问栈顶元素
public T peek(){
return (T)elementData[size-1];
}
//判断是否为空
public boolean isEmpty(){
boolean b = false;
if(size == 0){
b = true;
}
return b;
}
//清空栈
public void clear(){
for(Object o:elementData){
o = null;
}
size = 0;
}
//遍历栈
public void view(){
System.out.print("当前栈中元素为:");
for(int i = 0;i < size;i++){
System.out.print(elementData[i] + " ");
}
System.out.println();
}
//栈容量检测与扩充
public void ensureCapacity(){
if(capacityIncrement > 0){
while((size+1) > capacity){
capacity+=capacityIncrement;
}
}
else{
while((size+1) > capacity){
capacity = capacity * 2;
}
}
}
public static void main(String[] args) {
SequenceStack<String> sequenceStack = new SequenceStack<> ();
sequenceStack.push("hello");
sequenceStack.push("world");
sequenceStack.push("perfect");
System.out.print("入栈操作后,");
sequenceStack.view();
sequenceStack.pop();
System.out.print("出栈操作后,");
sequenceStack.view();
System.out.println("当前栈顶元素为:" + sequenceStack.peek());
sequenceStack.clear();
System.out.println("clear之后,栈是否为空:" + sequenceStack.isEmpty());
}
}
3、栈的链式结构的实现:
public class LinkedStack<T> {
private class Node{
private T data;
private Node next;
public Node(){
}
public Node(T element,Node next){
this.data = element;
this.next = next;
}
}
private Node top;
private int size = 0;
public LinkedStack(){
}
public LinkedStack(T element){
top = new Node(element,null);
size++;
}
//获取栈大小
public int length(){
return size;
}
//入栈操作
public void push(T element){
Node newNode;
newNode = new Node(element, top);
top = newNode;
size++;
}
//出栈操作
public T pop(){
Node oldNode = top;
top = top.next;
oldNode.next = null;
size--;
return oldNode.data;
}
//获取栈顶元素
public T peek(){
return top.data;
}
//清空栈
public void clear(){
top = null;
size = 0;
}
public boolean isEmpty(){
if(size == 0){
return true;
}
return false;
}
//遍历栈中元素
public void view(){
Node currentNode = top;
System.out.print("栈中元素为:");
while(currentNode != null){
System.out.print(currentNode.data + " ");
currentNode = currentNode.next;
}
System.out.println();
}
public static void main(String[] args) {
LinkedStack<String> linkedStack = new LinkedStack<>();
linkedStack.push("hello");
linkedStack.push("world");
linkedStack.push("perfect");
System.out.print("入栈操作后,");
linkedStack.view();
linkedStack.pop();
System.out.print("出栈操作后,");
linkedStack.view();
System.out.println("当前栈顶元素为:" + linkedStack.peek());
linkedStack.clear();
System.out.println("clear之后,栈是否为空:" + linkedStack.isEmpty());
}
}
三、 队列
1、概念
队列是一种被限制的线性表,它只允许在表的前端(front,即队尾)进行删除操作,只允许在表的后端(rear,即队头)进行插入操作

常用操作:
加入元素:向队列rear端插入元素
删除元素:从队列的front端删除元素
访问队列front端元素:
2、队列的顺序存储结构及实现
public class SequenceQueue<T> {
private int DEFAULT_SIZE = 20;
private int capacity;
private int front = 0;
private int rear = 0;
private Object[] elementData;
public SequenceQueue(){
capacity = DEFAULT_SIZE;
elementData = new Object[capacity];
}
public SequenceQueue(T element){
this();
elementData[0] = element;
rear++;
}
// 获取队列长度
public int length(){
return rear-front;
}
//向队列尾添加元素
public void add(T element){
if(rear > capacity-1){
throw new IndexOutOfBoundsException("队列已满");
}
elementData[rear++] = element;
}
//删除队列头元素
public T remove(){
if((rear-front) == 0){
throw new IndexOutOfBoundsException("队列为空,不能删除");
}
T remove = (T)elementData[front];
elementData[front++] = null;
return remove;
}
//获取队列头部元素
public T getElement(){
return (T)elementData[front];
}
//判断队列是否为空
public boolean isEmpty(){
return (rear-front) == 0;
}
//清空队列
public void clear(){
Arrays.fill(elementData, null);
front = 0;
rear = 0;
}
//遍历队列
public void view(){
System.out.print("队列中元素为:");
for(int i = front;i < rear; i++){
System.out.print(elementData[i] + " ");
}
System.out.println();
}
public static void main(String[] args) {
SequenceQueue<String> sequenceQueue = new SequenceQueue<> ();
sequenceQueue.add("hello");
sequenceQueue.add("world");
sequenceQueue.add("perfect");
sequenceQueue.view();
System.out.println("执行remove删除的元素为:" + sequenceQueue.remove());
System.out.println("队列头部元素为:" + sequenceQueue.getElement());
sequenceQueue.clear();
System.out.println("clear之后队列长度:" + sequenceQueue.length());
}
}
3、顺序存储结构的循环队列
public class LoopSequenceQueue<T> {
private int DEFAULT_SIZE = 5;
private int capacity;
private int front = 0;
private int rear = 0;
private Object[] elementData;
public LoopSequenceQueue(){
capacity = DEFAULT_SIZE;
elementData = new Object[capacity];
}
public LoopSequenceQueue(T element){
this();
elementData[0] = element;
rear++;
}
//获取队列长度
public int length(){
if(isEmpty()){
return 0;
}
return rear > front?rear-front:(capacity-(front-rear));
}
//向队列中插入元素
public void add(T element){
if(rear == front && elementData[front] != null){
throw new IndexOutOfBoundsException("队列已满");
}
elementData[rear] = element;
rear = (rear+1) > (capacity-1)?0:(++rear);
}
//从队列头删除元素
public T delete(){
if(isEmpty()){
throw new IndexOutOfBoundsException("队列为空");
}
T del = (T)elementData[front];
elementData[front] = null;
front = (front+1) > capacity?0:++front;
return del;
}
//清空队列
public void clear(){
Arrays.fill(elementData, null);
front = 0;
rear = 0;
}
//遍历队列
public void view(){
System.out.print("队列中元素为:");
if(front < rear){
for(int i = front;i < rear;i++){
System.out.print(elementData[i] + " ");
}
}
else{
for(int i = front;i < capacity;i++)
System.out.print(elementData[i] + " ");
for(int i = 0;i < rear;i++)
System.out.print(elementData[i] + " ");
}
}
//判断队列是否为空
public boolean isEmpty(){
if(front == rear && elementData[front] == null){
return true;
}
return false;
}
public static void main(String[] args) {
LoopSequenceQueue<String> loopSequenceQueue = new LoopSequenceQueue<>();
loopSequenceQueue.add("hello");
loopSequenceQueue.add("world");
loopSequenceQueue.add("perfect");
loopSequenceQueue.add("3333333");
loopSequenceQueue.delete();
loopSequenceQueue.add("4444444444");
loopSequenceQueue.add("555555555");
loopSequenceQueue.view();
System.out.println("当前队列长度为:" + loopSequenceQueue.length());
}
}
4、队列的链式存储结构
public class LinkedQueue<T> {
private class Node{
private T data;
private Node next;
public Node(){
}
public Node(T element,Node next){
this.data = element;
this.next = next;
}
}
private Node front;
private Node rear;
private int size = 0;
public LinkedQueue(){
this.front = null;
this.rear = null;
}
public LinkedQueue(T element){
rear = new Node(element,null);
front = rear;
size++;
}
//获取队列长度
public int length(){
return size;
}
//从队尾插入元素
public void add(T element){
Node newNode = new Node(element,null);
if(rear == null){
rear = newNode;
front = rear;
}
else{
rear.next = newNode;
rear = newNode;
}
size++;
}
//删除队头元素
public T remove(){
if(size == 0){
throw new IndexOutOfBoundsException("队列为空,不能删除");
}
Node delNode = front;
front = front.next;
delNode.next = null;
size--;
return delNode.data;
}
//清空队列
public void clear(){
front = null;
rear = null;
size = 0;
}
//遍历队列元素
public void view(){
System.out.print("队列中元素为:");
Node currentNode = front;
while(currentNode != null){
System.out.print(currentNode.data + " ");
currentNode = currentNode.next;
}
System.out.println();
}
public static void main(String[] args) {
LinkedQueue<String> linkedQueue = new LinkedQueue<> ();
linkedQueue.add("hello");
linkedQueue.add("world");
linkedQueue.add("perfect");
linkedQueue.add("3333333");
linkedQueue.remove();
linkedQueue.add("4444444444");
linkedQueue.add("555555555");
linkedQueue.view();
System.out.println("当前队列长度为:" + linkedQueue.length());
}
}
注:本文部分内容参考自《疯狂Java程序员的基本修养》
栈和队列的Java实现的更多相关文章
- 数据结构之栈和队列及其Java实现
栈和队列是数据结构中非常常见和基础的线性表,在某些场合栈和队列使用很多,因此本篇主要介绍栈和队列,并用Java实现基本的栈和队列,同时用栈和队列相互实现. 栈:栈是一种基于“后进先出”策略的线性表.在 ...
- 算法_栈与队列的Java链表实现
链表是一个递归的数据结构,它或者为null,或者是指向一个结点的引用,该结点含有一个泛型的元素和指向另一个链表的引用.可以用一个内部类来定义节点的抽象数据类型: private class Node ...
- 剑指Offer-5.用两个栈实现队列(C++/Java)
题目: 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 分析: 栈的特点是先进后出,队列的特点则是先进先出. 题目要求我们用两个栈来实现一个队列,栈和队列都有入栈 ...
- 数据结构 1 线性表详解 链表、 栈 、 队列 结合JAVA 详解
前言 其实在学习数据结构之前,我也是从来都没了解过这门课,但是随着工作的慢慢深入,之前学习的东西实在是不够用,并且太皮毛了.太浅,只是懂得一些浅层的,我知道这个东西怎么用,但是要优化.或者是解析,就不 ...
- 两个队列实现栈&两个栈实现队列(JAVA)
1,两个栈实现队列 题目描述 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 思路:栈的特点时先进后出,队列的特点是先进先出. 若此时有两个队列stack1,st ...
- 剑指offer第二版面试题8:用两个栈实现队列(JAVA版)
题目:用两个栈实现一个队列.队列的声明如下,请实现它的两个函数appendTail和deletedHead,分别完成在队列尾部插入节点和在队列头部删除节点的功能. 分析: 我们通过一个具体的例子来分析 ...
- 剑指offer 计划1(栈与队列)---java
1.1.题目1 剑指 Offer 09. 用两个栈实现队列 1.2.解法 解法如题目所说.定义两个栈.这里假设第一个栈为a,第二个栈为b. 实现两个函数增加尾和删除头. 增加即直接push入第一个栈. ...
- 栈和队列的java简单实现
今天看了一本书<啊哈 算法>,书的内容不多,一共两章,第一章是常见的排序算法包括桶排序.冒泡排序和快速排序,这些事基础的排序算法网上有很多资料说明,这里主要说第二章栈,对列,链表,书上使用 ...
- 剑指offer-面试题7:俩个栈实现队列(java)
详细分析请参照C语言版,这里仅仅给出实现代码,注释很详细,不得不说java各种api用起来真是爽飞了 1 package com.xsf.SordForOffer; 2 3 import java.u ...
随机推荐
- Outlook2007、2010和Foxmail的簽名設計
由於個人習慣問題公司大部分人採用第三方郵件工具,對與郵件的通訊設置大家完全可以通過嚮導完成,但是郵件的簽名設計往往隐藏了起来,现在就由我来带大家进行个性签名设计. Outlook2007 第一步: 点 ...
- Ubuntu12.04 LTS Add Sources List
1. First Step: sudo gedit /etc/apt/sources.list 2. Add Soures List Content: # deb cdrom:[Ubuntu LTS ...
- JVM调优的几种策略(转)
JVM参数调优是一个很头痛的问题,可能和应用有关系,别人说可以的对自己不一定管用.下面是本人一些JVM调优的实践经验,希望对读者能有帮助,环境LinuxAS4,resin2.1.17,JDK6.0,2 ...
- Unix/Linux环境C编程入门教程(30) 字符串操作那些事儿
函数介绍 rindex(查找字符串中最后一个出现的指定字符) 相关函数 index,memchr,strchr,strrchr 表头文件 #include<string.h> 定义函数 c ...
- 获取ActiveX控件本身所在的路径 和 error PRJ0050
一. CString GetCurPath() { TCHAR exeFullPath[MAX_PATH]; CString strPath; ...
- 经典CSS颜色混合模式
转自:http://www.webhek.com/css-blend-mode/ 注意:只有使用最新版的谷歌浏览器.火狐浏览器,才能正确的显示本文中的演示. Photoshop里最没有用处的一种功能— ...
- 匿名方法和Lambda表达式
匿名方法本质上是一传递给委托的代码块,是使用委托的另一种方法. 规则: 1.匿名方法中不能使用跳转语句跳至次匿名方法的外部,反之亦然:匿名方法外部的跳转语句也不能跳转到匿名方法的内部: 2.在匿名方法 ...
- Hadoop 3、Hadoop 分布式存储系统 HDFS
HDFS是Hadoop Distribute File System 的简称,也就是Hadoop的一个分布式文件系统. 一.HDFS的优缺点 1.HDFS优点: a.高容错性 .数据保存多个副本 .数 ...
- 【Android Training UI】创建自定义Views(Lesson 1 - 创建一个View类)
发布在我的网站 http://kesenhoo.github.io/blog/2013/06/30/android-training-ui-creating-custom-views-lesson-1 ...
- 连不上VSS 【转】
今天打开项目,但是连不上VSS,报错如下: (一)现象: Could not find the Visual SourceSafe Internet Web Service connection in ...