Java栈结构的实现(数组+链表)--数据结构的学习
package algorithm.stack;
import java.util.Scanner;
import java.util.Stack;
public class StackDemo {
public static void main(String[] args) {
arrayStackTest();
linkedStackTest();
}
private static void arrayStackTest() {
System.out.println("数组栈----------------");
AarrayStack stack = new AarrayStack(5);
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("push : 压栈");
System.out.println("pop : 出栈");
System.out.println("show : 显示");
System.out.println("exit : 退出");
String info = scanner.next();
switch (info) {
case "push":
System.out.println("input a vlaue..");
int value = scanner.nextInt();
stack.push(value);
break;
case "pop":
try {
int val = stack.pop();
System.out.println("value = " + val);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case "show":
stack.show();
break;
case "exit":
System.out.println("bye-bye");
scanner.close();
return;
default:
System.out.println("input error");
break;
}
}
}
private static void linkedStackTest() {
System.out.println("链表栈----------------");
SinglelinkedStack stack = new SinglelinkedStack(5);
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("push : 压栈");
System.out.println("pop : 出栈");
System.out.println("show : 显示");
System.out.println("exit : 退出");
String info = scanner.next();
switch (info) {
case "push":
System.out.println("input a vlaue..");
int value = scanner.nextInt();
String name = "push";
StackNode node = new StackNode(value, name);
stack.push(node);
break;
case "pop":
try {
StackNode node1 = stack.pop();
System.out.println("value = " + node1);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case "show":
stack.show();
break;
case "exit":
System.out.println("bye-bye");
scanner.close();
return;
default:
System.out.println("input error");
break;
}
}
}
}
class SinglelinkedStack {
StackNode top;
Integer maxSize;
SingleLinkedList<StackNode> list;
int count;
public SinglelinkedStack(int maxSize) {
this.maxSize = maxSize;
count=0;
list = new SingleLinkedList<StackNode>();
top = list.getHead();
}
public boolean isEmpty() {
return count == 0;
}
public boolean isFull() {
return count == maxSize;
}
public void push(StackNode value) {
if (isFull()) {
System.out.println("栈满");
return;
}
count++;
top = list.add(value);
}
public StackNode pop() {
if (isEmpty()) {
throw new RuntimeException("栈空");
}
count--;
StackNode value = list.del();
return value;
}
public void show() {
if (isEmpty()) {
System.out.println("栈空");
return;
}
System.out.println("--------");
list.show();
System.out.println("--------");
list.show1();
System.out.println("--------");
list.show2(list.getHead().getNext());
}
}
class SingleLinkedList<Node> {
private StackNode head;
public SingleLinkedList() {
head = new StackNode(-1, null);
}
public StackNode getHead() {
return head;
}
public int length() {
StackNode temp = head.getNext();
int len = 0;
while (temp != null) {
len++;
temp = temp.getNext();
}
return len;
}
public StackNode add(StackNode node) {
StackNode temp = head;
while (true) {
if (temp.getNext() == null) {
break;
}
temp = temp.getNext();
}
temp.setNext(node);
return temp.getNext();
}
public StackNode del() {
StackNode temp = head;
while (true) {
if (temp.getNext().getNext() == null) {
break;
}
temp = temp.getNext();
}
StackNode value = temp.getNext();
temp.setNext(null);
return value;
}
public void createLinkedList(int num) {
if (num <= 0) {
System.out.println("参数错误");
return;
}
StackNode temp = head;
for (int i = 0; i < num; i++) {
StackNode node = new StackNode(i, "" + ("" + i).hashCode());
temp.setNext(node);
temp = temp.getNext();
}
}
public void show() {
if (head.getNext() == null) {
return;
}
StackNode temp = head.getNext();
while (temp != null) {
System.out.println(temp);
temp = temp.getNext();
}
}
public void show1() {
Stack<StackNode> s = new Stack<StackNode>();
StackNode temp = head;
while(temp.getNext() != null) {
s.push(temp.getNext());
temp = temp.getNext();
}
while(!s.empty()) {
System.out.println(s.pop());
}
}
public void show2(StackNode head) {
if (head.getNext() != null) {
show2(head.getNext());
}
System.out.println(head);
}
}
class StackNode {
private int val;
private String data;
private StackNode next;
public StackNode(int val, String data) {
super();
this.val = val;
this.data = data;
}
public StackNode getNext() {
return next;
}
public void setNext(StackNode next) {
this.next = next;
}
public int getVal() {
return val;
}
public void setVal(int val) {
this.val = val;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
@Override
public String toString() {
return "Node [val=" + val + ", data=" + data + "]";
}
}
class AarrayStack {
int top;
int maxSize;
int[] arr;
public AarrayStack(int maxSize) {
top = -1;
this.maxSize = maxSize;
arr = new int[maxSize];
}
public boolean isFull() {
return top == maxSize - 1;
}
public boolean isEmpty() {
return top == -1;
}
public void push(int value) {
if (isFull()) {
System.out.println("栈满");
return;
}
arr[++top] = value;
}
public int pop() {
if (isEmpty()) {
throw new RuntimeException("栈空");
}
return arr[top--];
}
public void show() {
if (isEmpty()) {
System.out.println("栈空");
return;
}
for (int i = top; i >= 0; i--) {
System.out.printf("arr[%d] = %d\n", i, arr[i]);
}
}
}