java下的一个简单链表

程序运行输入描述:
第一行输入一个整数n,代表操作次数
之后的n行输入每次操作的内容,操作内容为
insert x y或delete x,其中x,y均为整数,insert x y 代表在第一个值为x的前面插入y,delete x 代表删除第一个值为x的数。


import java.util.Scanner;

import static java.lang.StrictMath.pow;

public class Main {
    public static void main(String[] args) {
        Linklist linklist = new Linklist();
        Scanner scanner = new Scanner(System.in);
        int operas = 0;

        operas = Integer.parseInt(scanner.nextLine().trim());

        for (int i = 1; i <= operas; i++) {
            String str = scanner.nextLine();
            String[] strs = str.split(" ");
            if ("insert".equals(strs[0])) {
                int x = Integer.parseInt(strs[1]);
                int y = Integer.parseInt(strs[2]);
                linklist.insert(x, y);
            }
            if ("delete".equals(strs[0])) {
                int x = Integer.parseInt(strs[1]);
                linklist.delete(x);
            }
        }
        scanner.close();


        Node temp = linklist.getHead();
        Node temp2;
        while (temp != null) {
            if (linklist.getHead() == linklist.getRear()) {
                System.out.print("NULL");
                break;
            }
            temp2 = temp;
            temp = temp.getNext();//tem points next;
            if (temp2 == linklist.getHead())
                continue;
            System.out.print(temp2.getData() + " ");


        }
    }
}

class Linklist {
    private Node head;//has no previous Node.
    private Node rear;//has no next Node.

    Linklist() {//initialize linkList,the only node is both head and rear;
        this.head = new Node();
        this.rear = this.head;
    }

    public Node getHead() {
        return head;
    }

    public Node getRear() {
        return rear;
    }

    public void setHead(Node head) {
        this.head = head;
    }

    public void setRear(Node rear) {
        this.rear = rear;
    }

    public void delete (int x) { //delete the node whose value equals x.

        Node pointer = this.head;
        while (pointer != null) {
            if (pointer.getData() == x) {
                /*the target node is rear*/
                if (pointer == this.rear) {
                    Node pre = pointer.getPrevious();
                    pre.setNext(null);
                    this.setRear(pre);
                } else {
                    /*the target node is in internal */
                    Node pre = pointer.getPrevious();
                    Node next = pointer.getNext();
                    pre.setNext(next);
                    next.setPrevious(pre);
                }

                break;
            }
            pointer = pointer.getNext();
        }

    }

    public void insert(int x,
                       int y) {//insert y value before the first x,note that the maximum of x and y is (2^31)-2.
        Node yNode = new Node();
        yNode.setData(y);

        /*
         * //to find the first node whose value equating x named temp ;
         * */
        boolean isFound = false;
        Node temp = this.getHead();
        while (temp != null) {
            if (temp.getData() == x) {//found
                isFound = true;
                Node pre = temp.getPrevious();
                pre.setNext(yNode);
                yNode.setPrevious(pre);
                yNode.setNext(temp);
                temp.setPrevious(yNode);

                break;
            }
            temp = temp.getNext();//tem points next;
        }
        if (isFound == false) {
            this.rear.setNext(yNode);
            yNode.setPrevious(this.rear);
            this.setRear(yNode);
        }
    }


}


class Node {
    private Node next;
    private Node previous;
    private int data;

    /*
    /this is the maximum number of int.
    to avoid the default value conflicting to the input value.
    * */
    Node() {
        this.setData((int) (pow(2, 31) - 1));
    }

    public void setData(int data) {
        this.data = data;
    }

    public int getData() {
        return data;
    }

    public Node getNext() {
        return next;
    }

    public Node getPrevious() {
        return previous;
    }

    public void setNext(Node next) {
        this.next = next;
    }

    public void setPrevious(Node previous) {
        this.previous = previous;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值