Java语言如何实现数据结构栈
短信预约 -IT技能 免费直播动态提醒
这篇文章主要介绍了Java语言如何实现数据结构栈,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
首先了解下栈的概念:
栈是限定仅在表头进行插入和删除操作的线性表。有时又叫LIFO(后进先出表)。要搞清楚这个概念,首先要明白”栈“原来的意思,如此才能把握本质。
"栈“者,存储货物或供旅客住宿的地方,可引申为仓库、中转站,所以引入到计算机领域里,就是指数据暂时存储的地方,所以才有进栈、出栈的说法。
实现方式是这样的:首先定义了一个接口,然后通过这个接口实现了线性栈和链式栈,代码比较简单,如下:
package com.peter.java.dsa.interfaces;public interface Stack<T> {Boolean isEmpty();void clear();T pop();Boolean push(T data);int length();T peek();int search(T data);}
线性栈:以数组的方式实现。
package com.peter.java.dsa.common;import com.peter.java.dsa.interfaces.Stack;public class LinearStack<T> implements Stack<T> {@SuppressWarnings("unchecked") private T[] t = (T[]) new Object[16];private int size = 0;@Override public Boolean isEmpty() {// TODO Auto-generated method stubreturn size == 0;}@Override public void clear() {// TODO Auto-generated method stubfor (int i = 0; i < t.length; i++) {t[i] = null;}size = 0;}@Override public T pop() {// TODO Auto-generated method stubif (size == 0) {return null;}T tmp = t[size - 1];t[size - 1] = null;size--;return tmp;}@Override public Boolean push(T data) {// TODO Auto-generated method stubif (size >= t.length) {resize();}t[size++] = data;return true;}@Override public int length() {// TODO Auto-generated method stubreturn size;}@Override public T peek() {// TODO Auto-generated method stubif (size == 0) {return null;} else {return t[size - 1];}}@Override public int search(T data) {// TODO Auto-generated method stubint index = -1;for (int i = 0; i < t.length; i++) {if (t[i].equals(data)) {index = i;break;}}return index;}@SuppressWarnings("unchecked") private void resize() {T[] tmp = (T[]) new Object[t.length * 2];for (int i = 0; i < t.length; i++) {tmp[i] = t[i];t[i] = null;}t = tmp;tmp = null;}@Override public String toString() {// TODO Auto-generated method stubStringBuffer buffer = new StringBuffer();buffer.append("Linear Stack Content:[");for (int i = t.length - 1; i > -1; i--) {buffer.append(t[i].toString() + ",");}buffer.append("]");buffer.replace(buffer.lastIndexOf(","), buffer.lastIndexOf(",") + 1, "");return buffer.toString();}}
链式栈:通过单链表进行实现。
package com.peter.java.dsa.common;import com.peter.java.dsa.interfaces.Stack;public class LinkedStack<T> implements Stack<T> {private Node top;private int size;@Override public Boolean isEmpty() {// TODO Auto-generated method stubreturn size == 0;}@Override public void clear() {// TODO Auto-generated method stubtop = null;size = 0;}@Override public T pop() {// TODO Auto-generated method stubT topValue = null;if (top != null) {topValue = top.data;Node oldTop = top;top = top.prev;oldTop.prev = null;size--;}return topValue;}@Override public Boolean push(T data) {// TODO Auto-generated method stubNode oldTop = top;top = new Node(data);top.prev = oldTop;size++;return true;}@Override public int length() {// TODO Auto-generated method stubreturn size;}@Override public T peek() {// TODO Auto-generated method stubT topValue = null;if (top != null) {topValue = top.data;}return topValue;}@Override public int search(T data) {// TODO Auto-generated method stubint index = -1;Node tmp = top;for (int i = size - 1; i > -1; i--) {if (tmp.data.equals(data)) {index = i;break;} else {tmp = tmp.prev;}}tmp = null;return index;}@Override public String toString() {// TODO Auto-generated method stubStringBuffer buffer = new StringBuffer();buffer.append("Linked Stack Content:[");Node tmp = top;for (int i = 0; i < size - 1; i++) {buffer.append(tmp.toString() + ",");tmp = tmp.prev;}tmp = null;buffer.append("]");buffer.replace(buffer.lastIndexOf(","), buffer.lastIndexOf(",") + 1, "");return super.toString();}private class Node {T data;Node prev;public Node(T data) {// TODO Auto-generated constructor stubthis.data = data;}}}
感谢你能够认真阅读完这篇文章,希望小编分享的“Java语言如何实现数据结构栈”这篇文章对大家有帮助,同时也希望大家多多支持编程网,关注编程网行业资讯频道,更多相关知识等着你来学习!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341