数据结构-Java实现栈
来源:互联网 发布:sql中update的用法 编辑:程序博客网 时间:2024/06/04 19:44
概念
栈(stack)又名堆栈,它是一种运算受限的线性表。其限制是仅允许在表的一端进行插入和删除运算。这一端被称为栈顶,相对地,把另一端称为栈底。向一个栈插入新元素又称作进栈、入栈或压栈,它是把新元素放到栈顶元素的上面,使之成为新的栈顶元素;从一个栈删除元素又称作出栈或退栈,它是把栈顶元素删除掉,使其相邻的元素成为新的栈顶元素。栈是先进后出,出栈与入栈操作均在一端进行。
本文使用数组实现栈、使用容器实现栈、使用链表实现栈、两个队列实现栈
数组实现栈
代码实现
package com.billJiang.stack;import java.util.Arrays;import java.util.EmptyStackException;/** * Created by billJiang on 2016/11/29. * 用数组实现栈顶 */public class StackArray { private Object[] array; private int top; private int size=3; public StackArray(){ array=new Object[size]; top=-1; } public boolean isEmpty(){ return top==-1; } public Object pop(){ if(top==-1){ throw new EmptyStackException(); } return array[top--]; } public Object peek(){ if(top==-1) return null; return array[top]; } public Object push(Object element){ if(top==size-1){ array= Arrays.copyOf(array,size*2); } array[++top]=element; return element; }}
测试
package com.billJiang.stack;/** * Created by billJiang on 2016/11/29. */public class StackArrayTest { public static void main(String[] args) { StackArray stack=new StackArray(); stack.push(new Integer(1)); stack.push(new Integer(2)); stack.push(new Integer(3)); stack.push(new Integer(4)); stack.peek(); stack.pop(); stack.push(new Integer(5)); }}
容器实现栈
代码实现
package com.billJiang.stack;import java.util.ArrayList;import java.util.EmptyStackException;import java.util.List;/** * Created by billJiang on 2016/11/30. * 用ArrayList容器实现栈 */public class StackList<T> { private List<T> list; private int size; public StackList(int size){ this.size=size; list=new ArrayList<T>(size); } public T pop(){ if(this.isEmpty()){ throw new EmptyStackException(); } return list.remove(list.size()-1); } public T peek(){ if(this.isEmpty()){ throw new EmptyStackException(); } return list.get(list.size()-1); } public void push(T element){ list.add(element); } public boolean isEmpty(){ return list.isEmpty(); } public int size(){ return list.size(); } public boolean isFull(){ return list.size()==this.size; }}
链表实现栈
链表节点定义
package com.billJiang.stack;/** * Created by billJiang on 2016/11/30. * 单向链表节点 */public class Node<T> { private T item; private Node<T> next; public Node() { this.item = null; this.next = null; } public Node(T item, Node<T> next) { this.item = item; this.next = next; } public T getItem() { return item; } public void setItem(T item) { this.item = item; } public Node<T> getNext() { return next; } public void setNext(Node<T> next) { this.next = next; }}
代码实现
package com.billJiang.stack;import java.util.EmptyStackException;/** * Created by billJiang on 2016/11/30. * 使用单项链表实现栈,出栈、入栈均在一头进行 */public class StackLinkedList<T> { private Node<T> top; public void push(T element) { if (isEmpty()) { top = new Node(element, null); } Node<T> node = new Node(element, top); top = node; } public T pop() { if (isEmpty()) throw new EmptyStackException(); T element = top.getItem(); top = top.getNext(); return element; } public T peek() { if (isEmpty()) throw new EmptyStackException(); return top.getItem(); } public boolean isEmpty() { return top == null; }}
两个队列实现栈
代码实现
思路:入栈在一个队列中进行,出栈时,将队列中其他元素移动另一个队列,剩下一个元素出队列,即出栈。
package com.billJiang.stack;import com.billJiang.queue.QueueArray;/** * Created by billJiang on 2016/12/1. * 两个队列实现栈 */public class StackQueue<T> { private QueueArray<T> queue1; private QueueArray<T> queue2; private int maxLength; public StackQueue(int initial) { this.maxLength = initial; queue1 = new QueueArray<>(initial); queue2 = new QueueArray<>(initial); } public boolean push(T element) { if (size() == maxLength) { return false; } if (queue2.isEmpty()) queue1.put(element); else queue2.put(element); return true; } public T pop() { if(queue1.isEmpty()){ while(queue2.size()>1){ queue1.put(queue2.poll()); } return queue2.poll(); }else{ while(queue1.size()>1){ queue2.put(queue1.poll()); } return queue1.poll(); } } public T peek() { T obj=null; if(queue1.isEmpty()){ while(queue2.size()>0){ obj=queue2.poll(); queue1.put(obj); } }else{ while(queue1.size()>1){ obj=queue1.poll(); queue2.put(obj); } } return obj; } public int size() { return queue1.size() + queue2.size(); }}
1 0
- java 实现栈数据结构
- java实现栈数据结构
- Java实现栈数据结构
- 数据结构-Java实现栈
- Java实现数据结构--栈
- java数据结构,实现栈/队列
- Java 数据结构----栈的实现
- java实现栈,队列数据结构
- Java数据结构--栈的实现
- Java实现数据结构中的栈
- Java数据结构:栈的实现
- JAVA 数据结构之栈实现
- 【JAVA数据结构】栈(数组实现)
- 数据结构--顺序栈java实现
- 数据结构-栈 JAVA语言实现
- 数据结构实战java实现栈
- Java数据结构实现之栈
- java 利用 LinkedList类实现 数据结构 栈.......
- 树莓派 Raspberry Pi 启用 root 登陆账户
- MySQL日期时间函数大全
- Android Studio各种快捷功能及好用的插件
- Gson解析,多个接口有多个不同的名字
- 混合编译:c++文件调用c文件变量和函数、c文件调用c++文件变量和函数
- 数据结构-Java实现栈
- Ubuntu 14.04 安装配置强大的星际译王(stardict)词典
- PL/SQL 基础---数据交互
- BP神经网络概述
- PL/SQL 基础---触发器
- 汉诺塔游戏
- 2D游戏 NGUI 不同Atlas 同Panel下 ,根据图片的x值 UISprite 前后深度算法
- IT运维存在问题及改进
- 论文笔记之Deep Convolutional Networks on Graph-Structured Data