Java語言實現數據結構棧代碼詳解
近來復習數據結構,自己動手實現了棧。棧是一種限制插入和刪除只能在一個位置上的表。最基本的操作是進棧和出棧,因此,又被叫作“先進后出”表。
首先了解下棧的概念:
棧是限定僅在表頭進行插入和刪除操作的線性表。有時又叫LIFO(后進先出表)。要搞清楚這個概念,首先要明白”棧“原來的意思,如此才能把握本質。
"棧“者,存儲貨物或供旅客住宿的地方,可引申為倉庫、中轉站,所以引入到計算機領域里,就是指數據暫時存儲的地方,所以才有進棧、出棧的說法。
實現方式是這樣的:首先定義了一個接口,然后通過這個接口實現了線性棧和鏈式棧,代碼比較簡單,如下:
package com.peter.java.dsa.interfaces; /** * 棧操作定義 * * @author Peter Pan */ 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; /** * 線性棧 * * @author Peter Pan */ 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 stub return size == 0; } @Override public void clear() { // TODO Auto-generated method stub for (int i = 0; i < t.length; i++) { t[i] = null; } size = 0; } @Override public T pop() { // TODO Auto-generated method stub if (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 stub if (size >= t.length) { resize(); } t[size++] = data; return true; } @Override public int length() { // TODO Auto-generated method stub return size; } @Override public T peek() { // TODO Auto-generated method stub if (size == 0) { return null; } else { return t[size - 1]; } } /* return index of data, return -1 if no data */ @Override public int search(T data) { // TODO Auto-generated method stub int 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; } /* from the left to the right is from the top to the bottom of the stack */ @Override public String toString() { // TODO Auto-generated method stub StringBuffer 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 stub return size == 0; } @Override public void clear() { // TODO Auto-generated method stub top = null; size = 0; } @Override public T pop() { // TODO Auto-generated method stub T 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 stub Node oldTop = top; top = new Node(data); top.prev = oldTop; size++; return true; } @Override public int length() { // TODO Auto-generated method stub return size; } @Override public T peek() { // TODO Auto-generated method stub T topValue = null; if (top != null) { topValue = top.data; } return topValue; } @Override public int search(T data) { // TODO Auto-generated method stub int 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 stub StringBuffer 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 stub this.data = data; } } }
學習還在進行中,以后會繼續(xù)更新代碼。
就是本文關于Java語言實現數據結構棧代碼詳解的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
相關文章
Spring?Boot?整合?FreeMarker?實例分享
這篇文章主要分享了Spring?Boot整合FreeMarker?實例FreeMarker是一款模板引擎,即一種基于模板和要改變的數據,并用來生成輸出文本,更多相關介紹需要的小伙伴可以參考下面文章內容2022-05-05Springboot開發(fā)OAuth2認證授權與資源服務器操作
這篇文章主要介紹了Springboot開發(fā)OAuth2認證授權與資源服務器操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06