手写一个简单的栈,队列和单向链表

     博主最近换了家新公司,刚进去很是清闲,IT这行业发展太迅速,没事干就得学习,不断学习,看过了许多源码之后,开始自己写一些简单的java源码,巩固一下自己的记忆。先从一些简单的数据结构开始!

一  数组实现栈结构

public class MyStack {
    private long[] a;
    private int size;//栈数组大小
    private int top;//栈顶指针

    public MyStack(int size) {
        this.a = new long[size];
        this.size = size;
        this.top = -1; //表示空栈
    }

    public void push(long value) {
        if (isFull()) {
            System.out.println("栈满了");
            return;
        }
        this.a[++top] = value;

    }

    //返回栈顶内容 但不删除
    public long peek() {
        if (isEmpty()) {
            System.out.println("栈中没有数据");
            return 0;
        }

        return a[top];
    }

    //返回栈顶内容 并删除
    public long pop() {
        if (isEmpty()) {
            System.out.println("栈中没有数据");
            return 0;
        }

        return a[top--];
    }

    //栈实际大小
    public int size() {
        return top + 1;
    }

    public boolean isFull() {
        return top == size - 1;
    }

    public boolean isEmpty() {
        return top == -1;
    }

    public void display() {
        for (int i = top;i >= 0;i --) {
            System.out.println(a[i] + " ");
        }
    }
}

二 循环数组实现一个队列

public class MyQueue {
    private long[] a; //循环数组实现!
    private int maxSize; //总共大小
    private int size; //实际大小
    private int front; //队头
    private int rear; //队尾

    public MyQueue(int maxSize) {
        this.a = new long[maxSize];
        this.maxSize = maxSize;
        this.size = 0;
        this.front = 0;
        this.rear = 0;
    }

    public void insert(long value) {
        if (isFull()) {
            System.out.println("队列已满");
            return;
        }

        rear = ++ rear % maxSize;
        a[rear] = value;
        size ++;

//        //如果队列已满 则回到队头
//        if (rear == size - 1) {
//            rear = -1;
//        }
//        a[++ rear] = value;
    }

    public long remove() {
        if (isEmpty()) {
            System.out.println("队列为空");
            return 0;
        }
        size --;
        front = front % maxSize;
        return a[front++];
    }

    public long peek() {
        if (isEmpty()) {
            System.out.println("队列为空");
            return 0;
        }
        return a[front];
    }

    public boolean isFull() {
        return size == maxSize;
    }

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

    public int size() {
        return size;
    }

    public void display() {
        if (isEmpty()) {
            System.out.println("队列为空");
            return;
        }
        int item = front;
        for (int i = 0;i < size;i++) {
            System.out.println(a[item ++ % maxSize] + "  ");
        }
    }
}

三 单向链表的简单实现

public class MyLNode { //节点实体
    public int data; //节点值
    public MyLNode next; //节点指向的下一节点

    public MyLNode(int data) {
        this(data, null);
    }

    public MyLNode(int data, MyLNode node) {
        this.data = data;
        this.next = node;
    }
}
public class MyLinkedList {//单向链表
    private int size; //链表大小
    public MyLNode head; // 头结点
    public MyLNode tail; //尾结点

    public MyLinkedList() {
        this.head = null;
        this.tail = null;
    }

    public boolean isEmpty() {
        return head == null;
    }

    public void addToHead(int value) {//向头部添加节点
        MyLNode temp = new MyLNode(value, head);
        head = temp;
        if (tail == null) {
            tail = head;
        }
        size ++;
    }

    public void addToTail(int value) {//向尾部添加节点
        MyLNode temp = new MyLNode(value);
        if (isEmpty()) {
            head = tail = temp;
        } else {
            tail.next = temp;
            tail = tail.next;
        }

        size ++;
    }

    public int deleteToHead() {//删除头结点
        if (isEmpty()) {
            System.out.println("链表为空");
            return -1;
        }
        int data = head.data;
        if (head == tail) { //头尾指向一个节点
            head = tail = null;
        } else {
            head = head.next;
        }
        size --;
        return data;
    }

    public int deleteToTail() {//删除尾结点
        if (isEmpty()) {
            System.out.println("链表为空");
            return -1;
        }
        int data = tail.data;
        if (head == tail) { //头尾指向一个节点
            head = tail = null;
        } else {
            MyLNode temp;
            for (temp = head;temp.next != tail;temp = temp.next); //循环找到尾结点的前一节点
            tail = temp;
            tail.next = null;
        }
        size --;
        return data;
    }

    public void delete(int value) {//删除value节点
        if (isEmpty()) {
            System.out.println("链表为空");
            return;
        }

        if (head.data == value) {//如果是头结点
            deleteToHead();
        } else if (tail.data == value) {//如果是尾结点
            deleteToTail();
        } else {

            MyLNode pre; //记录删除节点的前一节点
            MyLNode temp; //要删除的节点
            for (pre = head,temp = head.next;temp.data != value && temp.next != null;temp = temp.next,pre = pre.next) {
                if (temp == tail) {
                    System.out.println(value + "该节点不存在!");
                } else {
                    pre.next = temp.next;
                }
            }
        }

        size --;

    }

    public void insert(int value,int val) {//插入到value值之后
        if (isEmpty()) {
            System.out.println("链表为空");
            return;
        }

        if (head.data == value) {//如果是头结点
            addToHead(val);
        } else if (tail.data == value) {//如果是尾结点
            addToTail(val);
        } else {

            MyLNode temp; //要添加节点的之前节点
            for (temp = head.next;temp.data != value && temp.next != null;temp = temp.next) {
                if (temp == tail) {
                    System.out.println(value + "该节点不存在!");
                } else {
                    MyLNode node = new MyLNode(val, temp.next);
                    temp.next = node;
                }
            }
        }

        size ++;

    }

    public int size() {
        return size;
    }

    public void show() {
        for(MyLNode temp = head;temp != null;temp = temp.next)
            System.out.print(temp.data + " ");
    }
}

    如果有什么错误,或者更好的方式,欢迎大家指出!

猜你喜欢

转载自blog.csdn.net/qq_31689009/article/details/102475333