DAY1

1.数组实现栈

public class Stack {
    int size;
    int top;
    int []data;
    public Stack(int size){
        this.size=size;
        data=new int[size];
        top=-1;
    }
    public int getSize(){
        return size;
    }
    public int getTop(){
        return top;
    }
    public boolean isEmpty(){
        return top==-1;
    }
    public boolean isFull(){
        return (top+1)==size;
    }
    public void push(int num) {
        if(isFull()){
            System.out.println("栈已满");
        }else{
            this.data[++top] = num;
        }
    }
    public int pop() throws Exception {
        if(isEmpty()){
            throw new Exception("空栈没有数据可以弹出");
        }else{
            return data[top--];
        }
    }
    public int peek() {
        return this.data[getTop()];
    }
    public static void main(String[] args){
        Stack stack = new Stack(4);
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        System.out.println(stack.getSize());

        while(!stack.isEmpty()) {
            try {
                System.out.println(stack.pop());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}

2.链表实现栈

public class Stack2 {
    int size;
    Node top;

    public Stack2(){
        top=new Node(0);
        top.next=null;
        size=0;
    }
    public int getSize(){
        return size;
    }
    public int getTop(){
        return top.val;
    }
    public boolean isEmpty(){
        return size==0;
    }

    public void push(int num) {
        Node node=new Node(num);
        if(top.next==null){
            top.next=node;
        }else{
            node.next=top.next;
            top.next=node;
        }
        size++;
    }
    public int pop()  {
        int res = -1;
        if(top.next==null){
            System.out.println("空栈");
        }else{
            res=top.next.val;
            top.next=top.next.next;
            size--;
        }
        return res;
    }
    public int peek() {
        int res = -1;
        if(top.next==null){
            System.out.println("空栈");
        }else{
            res=top.next.val;
        }
        return res;
    }
    public static void main(String[] args){
        Stack2 stack = new Stack2();
        stack.push(1);
        stack.push(2);
        stack.pop();
        stack.push(4);
        System.out.println("size:"+stack.getSize());
        while(!stack.isEmpty()) {
            try {
                System.out.println(stack.pop());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.println("size:"+stack.getSize());
    }

}
class Node{
    int val;
    Node next;
    public Node(int x){
        this.val=x;
    }
}

3.栈模拟浏览器前进后退

public class Stack3 {
    public static void main(String[] args){
        Stack X = new Stack();
        Stack Y = new Stack();
        //举例从前到后浏览A B C D四个页面
        X.push(A); X.push(B); X.push(C); X.push(D);

        //回看B
        Y.push(X.pop());//D->Y
        Y.push(X.pop());//C->Y

        //跳回D
        X.push(Y.pop());//C->X
        X.push(Y.pop());//D->X


    }
}

4.数组实现队列

public class Queue {
    int size;
    int head;
    int tail;
    int []queue;
    int count;
    public Queue(int size){
        this.size=size;
        queue=new int[size];

        head=0;
        tail=0;
        count=0;
    }
    public int getSize(){
        return size;
    }

    public boolean isEmpty(){
        return count==0;
    }

    public void offer(int num) {
        if(count == size){
            System.out.println("队列已满");
        }else{
            queue[tail % size] = num;
            tail++;
            count++;
        }
    }
    public int poll() {
        if (count == 0) {
            return -1;
        } else {
            count--;
            return queue[head--];
        }
    }

    public int peek() {
        if (count == 0) {
            return -1;
        } else {
            count--;
            return queue[head];
        }
    }

}

5.链表实现队列

package Day1;

public class Queue2 {
    Node head;
    Node tail;
    public Queue2() {
        Node head = new Node(0);
        Node tail = head;
        head.next = null;
        head.val = -1;
    }

    public void offer(int input){
        Node node = new Node(input);
        if(tail == null){
            head = tail = node;
        }else {
            tail.next = node;
            tail = tail.next;
        }

    }

    public int poll(){
        if(tail == head){
            int out = head.val;
            head = tail =null;
            return out;
        }else if (head == null){
            System.out.println("Empty ");
            return -1;
        }else {
            int out = head.val;
            head = head.next;
            return out;
        }
    }

    public int peek(){
        if(head == null){
            System.out.println("Empty ");
            return -1;
        }
        return head.val;
    }

}

6.单链表

public class SingleLinkedList {
    public Node first; // 头结点
    private int pos = 0;// 节点位置

    public SingleLinkedList () {
        this.first = null;
    }

    public void addFirstNode(int data) {
        Node node = new Node(data);
        node.next = first;
        first = node;
    }
    public Node deleteFirstNode() {
        Node tempNode = first;
        first = tempNode.next;
        return tempNode;
    }
    // 插入
    public void add(int index, int data) {
        Node node = new Node(data);
        Node current = first;
        Node previous = first;
        while (pos != index) {
            previous = current;
            current = current.next;
            pos++;
        }
        node.next = current;
        previous.next = node;
        pos = 0;
    }

    // 删除
    public Node deleteByPos(int index) {
        Node current = first;
        Node previous = first;
        while (pos != index) {
            pos++;
            previous = current;
            current = current.next;
        }
        if (current == first) {
            first = first.next;
        } else {
            pos = 0;
            previous.next = current.next;
        }
        return current;
    }

}

7.翻转链表

LeetCode 206 Reverse Linked List

本人链接:https://blog.csdn.net/susuxuezhang/article/details/88994460

8.合并链表

LeetCode 21 Merge Two Sorted Lists

本人链接:https://blog.csdn.net/susuxuezhang/article/details/89099944

9.找链表中点

LeetCode 876 Middle of the Linked List

本人链接:https://blog.csdn.net/susuxuezhang/article/details/89079179

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值