数据结构---Java编码实现栈、队列、链表的常规功能

public class MyStack {
    //用数组实现数据栈
    int[] elements;
 
    public MyStack() {
        this.elements = new int[0];
    }
 
    /**
     * 入栈,入栈的数据放在数组尾部
     */
    public void push(int element){
        //创建一个新的数组
        int[] newArray = new  int[elements.length+1];
        //将数据放到新数组内
        for (int i = 0; i <elements.length;i++){
            newArray[i]=elements[i];
        }
        //将入栈的数据放数组末尾
        newArray[newArray.length-1]=element;
        //新数组赋给数据栈
        elements=newArray;
    }
    /**
     * 出栈
     */
    public int pop(){
        if(elements.length<=0){
            throw new RuntimeException("this stack is null");
        }
        //出栈的数据
        int index = elements[elements.length-1];
        //创建一个新数组
        int[] newArray = new int[elements.length-1];
        //将剩余的数据放入新数组
        for(int i = 0; i <newArray.length;i++){
            newArray[i] = elements[i];
        }
        //新数组赋给数据栈
        elements = newArray;
 
        return index;
    }
    /**
     * 判断栈是否为空
     *
     */
    public  boolean isEmpty(){
        if(elements.length<=0){
            return true;
        }
        return false;
    }
    /**
     * 查看栈顶元素
     */
    public int peek(){
        if(elements.length<=0){
            throw  new RuntimeException("this stack is empty");
        }
        return elements[elements.length-1];
    }
}

对列

public class MyQueue {
    int[] elements;
 
    public MyQueue() {
        this.elements = new int[0];
    }
 
    /**
     * 入队
     */
    public void add(int element){
        //创建一个新的数组
        int[] newArray = new int[elements.length+1];
        //将旧数组的数据放入新数组中
        for(int i = 0 ; i <elements.length; i++){
            newArray[i] = elements[i];
        }
        //将加入的数据放入队列数组的尾部
        newArray[newArray.length-1] = element;
        //将新数组赋给数据对列
        elements = newArray;
    }
 
    /**
     * 出队
     */
    public int poll(){
        if(elements.length==0){
            throw new RuntimeException("this queue is null");
        }
        //取得要出队的数据
        int element = elements[0];
        //创建新的数组
        int[] newArray = new int[elements.length-1];
        //将出队后的数据放入新的数组中
        for(int i = 0 ; i <newArray.length;i++){
            newArray[i] = elements[i+1];
        }
        //将新数组赋给数据栈
        elements = newArray;
 
        return element;
    }
 
    /**
     * 判断对列是否为空
     *
     */
    public  boolean isEmpty(){
        return elements.length<=0;
    }
}

单向链表

/**
 * 单链表
 */
public class Node {
    int data;
 
    Node next;
 
    public Node(int data){
        this.data = data;
    }
 
    /**
     * 追加节点
     * @param node
     */
    public Node append(Node node){
        //当前节点
        Node currentNode = this;
        while(true){
            //得到下一节点
            Node nextNode = currentNode.next;
            if(nextNode==null){
                //如果下一节点为空,说明当前节点为链表的末尾节点
                break;
            }else{
                //把下一节点赋给当前节点,保证当前节点一直为链表的末尾节点
                currentNode = nextNode;
            }
        }
        currentNode.next = node;
 
        return currentNode;
    }
 
    /**
     * 得到节点的数据
     * @return
     */
    public int getDate(){
        return this.data;
    }
    /**
     * 获取下一节点
     */
    public Node getNext(){
        return this.next;
    }
    /**
     * 判断是否是链表的尾部
     */
    public  boolean isLast(){
        return this.next==null;
    }
 
    /**
     * 展示所有节点数据
     */
    public void show(){
        Node currentNode = this;
        while(true){
            System.out.print(currentNode.data+" ");
            currentNode = currentNode.next;
            //当当前节点的下一节点为空时,表示该节点为链表的末尾节点
            if(currentNode ==null){
                break;
            }
        }
        System.out.println();
    }
    /**
     * 删除下一节点
     */
    public void removeNext(){
        if(next.next==null){
            throw new RuntimeException("the next node is lastNode");
        }
        //找到下下个节点
        Node nextNextNode = next.next;
        //将下下个节点赋给下个节点,就是删除了下个节点
        next = nextNextNode;
    }
 
    /**
     * 插入节点到当前节点的下一个节点
     */
    public void insertNext(Node node){
        //得到下下个节点
        Node nextNextNode = next;
        //插入节点到下个节点
        next = node;
        //将剩余节点填入后面
        next.next = nextNextNode;
    }
 
}

单向循环链表

/**
 * 单链表
 */
public class LoopNode {
    int data;
 
    LoopNode next = this;
 
    public LoopNode(int data) {
        this.data = data;
    }
 
    /**
     * 得到节点的数据
     *
     * @return
     */
    public int getDate() {
        return this.data;
    }
 
    /**
     * 获取下一节点
     */
    public LoopNode getNext() {
        return this.next;
    }
 
 
    /**
     * 展示所有节点数据
     */
    public void show() {
        LoopNode currentNode = this;
        while (true) {
            System.out.print(currentNode.data + " ");
            currentNode = currentNode.next;
            //当当前节点的下一节点为空时,表示该节点为链表的末尾节点
            if (currentNode == null) {
                break;
            }
        }
        System.out.println();
    }
 
    /**
     * 删除下一节点
     */
    public void removeNext() {
 
        //找到下下个节点
        LoopNode nextNextNode = next.next;
        //将下下个节点赋给下个节点,就是删除了下个节点
        next = nextNextNode;
    }
 
    /**
     * 插入节点到当前节点的下一个节点
     */
    public void insertNext(LoopNode node) {
        //得到下下个节点
        LoopNode nextNextNode = next;
        //插入节点到下个节点
        next = node;
        //将剩余节点填入后面
        next.next = nextNextNode;
    }
 
}

双向循环链表

public class DubbleLoopNode {
    //上一个节点
    DubbleLoopNode pre = this;
    //下一个节点
    DubbleLoopNode next = this;
    //节点数据
    int data;
 
    public DubbleLoopNode(int data) {
        this.data = data;
    }
    /**
     * 查看节点数据
     */
    public int getData(){
        return this.data;
    }
    /**
     * 得到下一节点
     */
    public DubbleLoopNode getNext(){
        return this.next;
    }
    /**
     * 得到上一节点
     */
    public DubbleLoopNode getPre(){
        return this.pre;
    }
    /**
     * 插入节点在下一节点
     */
    public DubbleLoopNode insert(DubbleLoopNode node){
        //得到下一节点准备放在下下节点
        DubbleLoopNode nextNextNode = this.next;
        //将节点插入
        this.next = node;
        node.pre=this;
        node.next = nextNextNode;
        nextNextNode.pre=node;
 
        return node;
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值