栈
1、数组实现栈
public class StackTest {
int top;
int arr[];
int maxSize;
public StackTest(int maxSize) {
super();
this.top = -1;
this.maxSize = maxSize;
this.arr = new int[maxSize];
}
public boolean isFull() {
return top == maxSize -1;
}
public boolean isEmpty() {
return top == -1;
}
public void add(int index) {
if (isFull()) {
System.out.println("栈满了");
return;
}
this.top++;
this.arr[top] = index;
}
public int del() {
if (isEmpty()) {
throw new RuntimeException("栈是空的");
}
int value = arr[top];
this.top--;
return value;
}
public void show() {
if (isEmpty()) {
System.out.println("栈是空的");
return;
}
for (int i = this.top;i>=0;i--) {
System.out.println(this.arr[i]);
}
}
public static void main(String[] args) {
StackTest stackTest = new StackTest(4);
stackTest.add(4);
stackTest.add(7);
stackTest.add(5);
stackTest.add(2);
stackTest.show();
System.out.println("一条数据出栈后");
stackTest.del();
stackTest.show();
System.out.println("两条条数据出栈后");
stackTest.del();
stackTest.show();
}
}
2、链表实现栈
public class LinkedStack {
public static void main(String[] args) {
LinkedStackNode linkedStackNode1 = new LinkedStackNode(1);
LinkedStackNode linkedStackNode2 = new LinkedStackNode(2);
LinkedStackNode linkedStackNode3 = new LinkedStackNode(3);
LinkedStackNode linkedStackNode4 = new LinkedStackNode(6);
LinkedStackNodeDome l = new LinkedStackNodeDome();
l.add(linkedStackNode1);
l.add(linkedStackNode2);
l.add(linkedStackNode3);
l.add(linkedStackNode4);
l.show();
System.out.println("Top-->" + l.getTop());
System.out.println("length-->" + l.getLength());
}
}
class LinkedStackNode{
public int value;
public LinkedStackNode next;
public LinkedStackNode(int value) {
this.value = value;
}
@Override
public String toString() {
return "LinkedStackNode{" +
"value-->" + value +
'}';
}
}
class LinkedStackNodeDome{
private int length;
private LinkedStackNode head = new LinkedStackNode(0);
public void add(LinkedStackNode heroNode){
heroNode.next = head.next;
head.next = heroNode;
length++;
}
public int get(){
head.next = head.next.next;
return head.value;
}
public void show(){
LinkedStackNode temp = head;
while (temp.next !=null){
temp = temp.next;
System.out.println(temp);
}
}
public int getLength(){
return length;
}
public int getTop(){
return head.next.value;
}
}
3、用队列实现栈
public class QueueTest {
private int maxSize;
private int rear;
private int front;
private int[] arr;
public QueueTest(int maxSize) {
super();
this.maxSize = maxSize;
rear = -1;
front = -1;
arr = new int[maxSize];
}
public boolean isEmpty() {
return front == rear;
}
public boolean isFull() {
return rear == maxSize-1;
}
public void add(int num) {
if (isFull()) {
throw new RuntimeException("队列已满");
}
rear++;
arr[rear] = num;
}
public int get() {
if (isEmpty()) {
throw new RuntimeException("队列无数据");
}
front++;
return arr[front];
}
public void showQueue() {
if (isEmpty()) {
System.out.println("队列无数据");
}
for (int i = front+1;i<=rear;i++) {
System.out.printf("[%d] = %d\n",i+1,arr[i]);
}
}
public int headQueue() {
if (isEmpty()) {
System.out.println("没有数据~~");
}
return arr[front + 1];
}
public int length(){
return rear-front;
}
}
class QueueStack {
QueueTest queue;
public QueueStack(){
queue = new QueueTest(40);
}
public void push(int x) {
queue.add(x);
for (int i = 0;i<queue.length()-1;i++){
queue.add(queue.get());
}
}
public int pop() {
return queue.get();
}
public int top() {
return queue.headQueue();
}
public boolean empty() {
return queue.isEmpty();
}
}
4、双栈实现基本计算器
public class StackCalculator {
public static void main(String[] args) {
StackCalculatorTest stackCalculatorTest = new StackCalculatorTest();
System.out.println(stackCalculatorTest.calculation("7*2*2-5+1-5+3-4"));
}
}
class StackCalculatorTest {
private Stack<Integer> stack1;
private Stack<Character> stack2;
public StackCalculatorTest() {
stack1 = new Stack<>();
stack2 = new Stack<>();
}
public int priority(char c) {
if (c == '+' || c == '-') {
return 1;
}
if (c == '*' || c == '/') {
return 2;
}
return 0;
}
public int switch_(char c1, int num2, int num1) {
int temp = 0;
switch (c1) {
case '*':
temp = num1 * num2;
break;
case '-':
temp = num1 - num2;
break;
case '+':
temp = num1 + num2;
break;
case '/':
temp = num1 / num2;
break;
}
return temp;
}
public int calculation(String s) {
char[] chars = s.toCharArray();
for (char c : chars) {
if (c == '+' || c == '-' || c == '*' || c == '/') {
if (stack2.size() != 0) {
if (priority(c) <= priority(stack2.peek())) {
int num1 = stack1.pop();
int num2 = stack1.pop();
char c1 = stack2.pop();
stack1.push(switch_(c1, num1, num2));
}
}
stack2.push(c);
}
if (c >= '0' && c <= '9') {
Integer num = Integer.parseInt(String.valueOf(c));
stack1.push(num);
}
}
while (!stack2.isEmpty()){
int num1 = stack1.pop();
int num2 = stack1.pop();
char c1 = stack2.pop();
stack1.push(switch_(c1,num1,num2));
}
return stack1.peek();
}
}