Java棧(基於連結串列與基於陣列實現)
阿新 • • 發佈:2018-11-22
基於陣列實現
package com.ma.stack; /** * @author sunfch *此棧實現基於陣列,初始棧時已經設定棧大小 */ public class MyArrayStack { private int size; private int top = -1; private int[] arr; MyArrayStack(){ arr =new int[3]; //預設棧大小為3 } MyArrayStack(int size) { if(size > 0){ this.size = size; arr = new int[size]; } else throw new RuntimeException("陣列長度值不正確"); } //============================================================================== //入棧 public void push(int data){ if(top >= size-1){ System.out.println("棧已滿"); return; } arr[++top] = data; } //============================================================================== //出棧 public int pop(){ if(top < 0){ throw new RuntimeException("棧為空"); } else{ int temp = arr[top]; arr[top] = 0; top--; return temp; } } //檢視棧頂元素但不出棧 public int peek(){ if(top < 0){ throw new RuntimeException("棧為空"); } else{ return arr[top]; } } //============================================================================== public boolean isEmpty(){ return top == -1; } //============================================================================== public static void main(String[] args) { MyArrayStack stack = new MyArrayStack(3);//初始棧大小為3 stack.push(2); stack.push(5); stack.push(10); //stack.push(11); System.out.println(stack.pop()); System.out.println(stack.pop()); System.out.println(stack.pop()); //System.out.println(stack.pop()); // System.out.println(stack.peek()); // System.out.println(stack.peek()); System.out.println(stack.isEmpty()); } }
基於陣列實現:
package com.ma.stack; /** * @author sunfch * 此棧實現基於連結串列,初始棧時未設定棧大小(棧大小可變) *此棧是將新元素插到連結串列頭部(連結串列頭部是棧頂) */ public class MyLinkStack { private Node top;//記錄棧頂元素 private int size = 0;//記錄棧大小 public MyLinkStack() { top = null; } class Node{ public int data; //記錄資料 public Node next; Node(int data){ this.data = data; } } //============================================================================================= //入棧 public void push(int data){ Node newNode = new Node(data); if(top == null){ top =newNode; } else{ newNode.next = top;//將新元素插到連結串列頭部 top = newNode; } size++; } //============================================================================================= //出棧 public Integer pop(){ if(top == null) throw new RuntimeException("棧為空"); else{ int val = top.data; top = top.next; size--; return val; } } //檢視棧頂元素但不出棧 public Integer peek(){ if(top == null) throw new RuntimeException("棧為空"); else{ return top.data; } } //============================================================================================= public boolean isEmpty(){ return top == null; } public int getSize(){ return size; } //============================================================================================= public static void main(String[] args) { MyLinkStack my = new MyLinkStack(); my.push(4); my.push(5); my.push(6); System.out.println("當前size:"+my.getSize()); System.out.println(my.isEmpty()); System.out.println(my.pop()); System.out.println("當前size:"+my.getSize()); System.out.println(my.pop()); System.out.println(my.pop()); System.out.println(my.isEmpty()); //System.out.println(my.pop()); } }