05_堆栈

 01_概念


 02_应用场景


03_Java代码实现


04_数组模拟栈的使用

package DataStructureReview;

import java.util.Arrays;
import java.util.Stack;

public class ArrayStackDemo {
    public static void main(String[] args) {
        Stack<String> stack = new Stack();
        stack.push("x");
        stack.push("y");
        stack.push("z");
        System.out.println("栈顶元素为:" + stack.peek());
        System.out.println("获取栈顶元素:" + stack.pop());
        System.out.println("测试堆栈是否为空:" + stack.empty());
        System.out.println("查看对象:x,在堆栈中的位置为:" + stack.search("z"));
        System.out.println("==================数组模拟堆栈======================================");
        ArrayStack stackArr = new ArrayStack(3);
        stackArr.push("x");
        stackArr.push("y");
        stackArr.push("z");
        System.out.println("栈顶元素为:" + stackArr.peek());
        System.out.println("获取栈顶元素:" + stackArr.pop());
        System.out.println("测试堆栈是否为空:" + stackArr.empty());
        System.out.println("查看对象:x,在堆栈中的位置为:" + stackArr.search("z"));
    }
}

class ArrayStack {
    //初始化数组
    private String[] arr;
    //top指针
    private int top = -1;

    public ArrayStack(int size) {
        this.arr = new String[size];
    }

    //empty方法
    public boolean empty() {
        return top == -1;
    }

    //push方法
    public void push(String e) {
        //满堆栈判断
        if (top == arr.length) {
            throw new RuntimeException("此堆栈已满,无法添加元素");
        }
        arr[++top] = e;
    }

    //pop方法
    public String pop() {
        //空栈判断
        if (empty()) {
            throw new RuntimeException("此堆栈为空,无法添加弹出元素");
        }
        return arr[top--];
    }

    //peek方法
    public String peek() {
        //空栈判断
        if (empty()) {
            throw new RuntimeException("此堆栈为空,无法添加查看元素");
        }
        return arr[top];
    }

    //search方法,返回值为-1表示此对象不在此堆栈
    public int search(String e) {
        System.out.println(Arrays.toString(arr));       //空栈判断
        if (empty()) {
            return -1;
        }
        int i = top;
        int cur = 1;
        boolean flag = false;
        for (; i >= 0; i--) {
            if (arr[i] == e) {
                flag = true;
                break;
            }
            cur++;
        }
        if(flag != true){
            cur = -1;
        }
        return cur;
    }

}

05_链表模拟堆栈的使用

package DataStructureReview;

import java.util.Stack;

public class SingleLinkedListStackDemo {
    public static void main(String[] args) {
        Stack<String> stack = new Stack();
        stack.push("x");
        stack.push("y");
        stack.push("z");
        stack.push("m");
        stack.push("k");
        System.out.println("栈顶元素为:" + stack.peek());
        System.out.println("获取栈顶元素:" + stack.pop());
        System.out.println("测试堆栈是否为空:" + stack.empty());
        System.out.printf("查看对象:%s,在堆栈中的位置为:%d \n","m" , stack.search("m"));
        System.out.println("==================链表模拟堆栈======================================");
        SingleLinkedListStack liststack = new SingleLinkedListStack(new SingleLinkedList1());
        liststack.push(new Node("x"));
        liststack.push(new Node("y"));
        liststack.push(new Node("z"));
        liststack.push(new Node("m"));
        liststack.push(new Node("k"));
        System.out.println("栈顶元素为:" + liststack.peek());
        System.out.println("获取栈顶元素:" + liststack.pop());
        System.out.println("测试堆栈是否为空:" + liststack.empty());
        System.out.printf("查看对象:%s,在堆栈中的位置为:%d \n","m" , liststack.search("m"));


    }
}

class SingleLinkedListStack {
    //初始化链表对象
    private SingleLinkedList1 list;

    public SingleLinkedListStack(SingleLinkedList1 list) {
        this.list = list;
    }

    //empty方法
    public boolean empty() {
        return list.head.next == null;
    }

    //peek方法
    public Node peek() {
        if (empty()) {
            throw new RuntimeException("当前堆栈为空,无法查看数据!");
        }
        return list.getLastOne();
    }

    //pop方法
    public Node pop() {
        if (empty()) {
            throw new RuntimeException("当前堆栈为空,无法弹出数据!");
        }
        return list.delete();
    }

    //push方法
    public void push(Node e) {
        list.add(e);
    }

    //search方法
    public int search(String e) {
        //判断是否为空链表
        if (empty()) {
            return -1;
        }
        Node temp = list.head.next;
        boolean flag = false;
        int sum = 1;
        while (true) {
            if (temp.value == e) {
                flag = true;
                break;
            }
            if (temp.next == null) {
                break;
            }
            sum++;
            temp = temp.next;
        }
        if (flag) {
            sum = list.size() + 1 - sum;
        } else {
            sum = -1;
        }
        return sum;

    }

}

class SingleLinkedList1 {
    //初始化head节点
    public Node head = new Node("");

    //add方法,在链表尾部添加
    public void add(Node node) {
        //判断是否为空链表
        if (head.next == null) {
            head.next = node;
            return;
        }
        //遍历链表
        Node temp = head.next;//辅助指针
        while (true) {
            if (temp.next == null) {
                temp.next = node;
                break;
            }
            temp = temp.next;
        }

    }

    //delete方法
    public Node delete() {
        //判断是否为空链表
        if (head.next == null) {
            System.out.println("当前链表为空!");
            return null;
        }
        //遍历链表
        Node temp = head;//辅助指针
        while (true) {
            if (temp.next.next == null) {
                break;
            }
            temp = temp.next;
        }
        Node cur = temp.next;
        temp.next = null;
        return cur;

    }

    //getLastOne方法,返回倒数第一个节点
    public Node getLastOne() {
        //判断是否为空链表
        if (head.next == null) {
            System.out.println("当前链表为空!");
            return null;
        }
        //遍历链表
        Node temp = head.next;//辅助指针
        while (true) {
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }
        return temp;
    }

    //printList方法
    public int size() {
        //判断是否为空链表
        if (head.next == null) {
            return 0;
        }
        //遍历链表
        Node temp = head.next;//辅助指针
        int sum = 0;
        while (true) {
            sum++;
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }
        return sum;
    }

}


class Node {
    public String value;
    public Node next;

    public Node(String value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return value;
    }
}

06_表达式的分类

 

猜你喜欢

转载自www.cnblogs.com/bajiaotai/p/12800605.html
今日推荐