package com.zzb.stack;
import java.io.Serializable;
import java.util.Scanner;
/**
* @Auther: Administrator
* @Date: 2020/1/21 12:50
* @Description: 使用 帶頭結點的單向鏈表 模擬 棧 數據結構
* (1)棧是一個先入後出(FILO-First In Last Out)的有序列表
* (2)棧(stack)是限制線性表中元素的插入和刪除只能在線性表的同一端進行的一種特殊線性表。允許插入和刪除的
* 一端,爲變化的一端,稱爲棧頂(Top)另一端爲固定的一端,稱爲棧底(Bottom)
* (3)根據棧的定義可知,最先放入棧中元素在棧底,最後放入的元素在棧頂,而刪除元素剛好相反,最後放入的元
* 素最先刪除,最先放入的元素最後刪除
*
* 技巧:
* 壓棧時採用頭插法
*/
public class SingleLinkedListStackTest {
public static void main(String[] args) {
// 創建棧
SingleLinkedListStack stack = new SingleLinkedListStack();
// 接受用戶輸入
String order = "";
Scanner scanner = new Scanner(System.in);
boolean loop = true;
while(loop) {
System.out.println("---------------push:添加數據到棧----------------");
System.out.println("---------------pop:從棧中獲取數據---------------");
System.out.println("---------------show:遍歷棧---------------------");
System.out.println("---------------top:查看棧頂數據-------------");
System.out.println("---------------exit:退出程序----------------------");
// 接收一個字符
order = scanner.next();
switch(order) {
case "push":
System.out.println("輸入一個數據");
int data = scanner.nextInt();
stack.push(new Node(data));
break;
case "pop":
Node pop = stack.pop();
System.out.println("獲取的數據爲 " + pop);
break;
case "show":
stack.show();
break;
case "top":
Node top = stack.top();
System.out.println("隊列的頭部數據爲 " + top);
break;
case "exit":
loop = false;
scanner.close();
break;
default:
break;
}
}
System.out.println("程序退出");
}
}
// 帶頭結點的單向鏈表模擬棧
class SingleLinkedListStack{
private SingleLinkedList singleLinkedList = new SingleLinkedList();
// 壓棧 push
// 頭插法
public void push(Node node) {
this.singleLinkedList.add(node);
}
// 彈棧 pop
public Node pop() {
return this.singleLinkedList.get();
}
// 遍歷棧
public void show() {
this.singleLinkedList.show();
}
// 獲取棧頂元素
public Node top() {
return this.singleLinkedList.top();
}
}
// 帶頭結點的單向鏈表類
class SingleLinkedList{
// 頭結點,初始化時不代表真實節點對象
// 彈棧 之後 head屬性變量會指向真實節點對象
// head 屬性變量 總是 指向 要被獲取元素 的 前一個位置
private Node head = new Node(-1);
// 壓棧 push
// 頭插法
public void add(Node node) {
Node temp = this.head.getNext();
this.head.setNext(node);
node.setNext(temp);
}
// 彈棧
public Node get() {
if(this.head.getNext() == null) {
return null;
}
this.head = this.head.getNext();
if(head != null) {
return head;
}
return null;
}
// 遍歷棧
public void show() {
// 判斷棧是否爲空
if(this.head.getNext() == null) {
System.out.println("空棧!");
return;
}
Node temp = this.head.getNext();
while(true) {
if(temp == null) {
break;
}
System.out.println(temp);
temp = temp.getNext();
}
}
// 獲取棧頂元素
public Node top() {
return this.head.getNext();
}
public Node getHead() {
return head;
}
public void setHead(Node head) {
this.head = head;
}
}
// 節點類
class Node implements Serializable{
private static final long serialVersionUID = 6336166902396505533L;
private int id;
private Node next;
public Node(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
@Override
public String toString() {
return "Node{" +
"id=" + id +
", next=" + next +
'}';
}
}