DS课设图书管理系统(链表)代码




class Book {
    private String name;
    private double price;


    public Book(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public String getInfo() {
        return "书名:《" + this.name + "》,"+"价格:" + this.price + "元.";

    }
//+"书本号为:"+this.id

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    
    public boolean compare(Book book) {
        if (this == book) {
            return true;
        }
        if (book == null) {
            return false;
        }
        if (this.name.equals(book.name) && this.price == book.price) {
            return true;
        }
        return false;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}



class Link {
    class Node {// 定义节点类
        private Node next;// 下一个节点
        private Book data;// 保存书本数据

        // 构造方法
        public Node(Book data) {
            this.data = data;
        }

        // 1.添加节点
        public void addNode(Node newNode) {
            if (this.next == null) {// 当前节点的下一个节点为空
                this.next = newNode;
            } else {// 向后继续保存
                this.next.addNode(newNode);
            }
        }

        // 2.查询数据
        public boolean containsNode(Book data) {
            if (data.compare(this.data)) {// 当前节点数据为要查询的数据
                return true;// 后面不在需要查询
            } else {// 当前节点不能满足查询
                if (this.next != null) {// 判断后续是否还有节点
                    return this.next.containsNode(data);    //递归
                } else {// 没后后续节点
                    return false;
                }
            }
        }

        // 3.数据修改
        public void setNode(int index, Book data) {
            // 使用自定义foot内容与 index比较
            if (Link.this.foot++ == index) {            //链表的指针等于下标
                this.data = data;// 进行内容修改
            } else {// 继续
                this.next.setNode(index, data);
            }
        }

        // 4.获取数据
        public Book getNode(int index) {
            // 使用自定义foot内容与 index比较
            // foot自增,目的为下一次查询方便
            if (Link.this.foot++ == index) {// 要查询的索引
                return this.data;// 返回当前节点数据
            } else {// 继续向后查
                return this.next.getNode(index);
            }
        }

        // 5.删除数据
        public void removeNode(Node previous, Book data) {
            if (data.compare(this.data)) {// 数据对应
                previous.next = this.next;// 空出当前节点             //?????
            } else {// 继续向后查询
                this.next.removeNode(this, data);
            }
        }

        // 6.对象数组
        public void toArrayNode() {
            Link.this.retArray[Link.this.foot++] = this.data;
            if (this.next != null) {// 后续还有元素
                this.next.toArrayNode();
            }
        }

    }


    // ================以上为内部类================
    private Node root;// 根节点  头节点
    private int count = 0;// 保存元素个数
    private int foot = 0;    //相当于指针
    private Book[] retArray;// 返回数组

    //所有的链表的方法调用均通过root头节点开始

    // 1.添加数据
    public void add(Book data) {
        if (data == null) {// 数据不允许为null
            return;
        }
        Node newNode = new Node(data);// 保存数据,数据打包
        if (this.root == null) {// 当前没有根节点
            this.root = newNode;// 新节点为根节点
        } else {// 根节点存在,其它节点给Node处理
            this.root.addNode(newNode);
        }
        this.count++;// 每次保存成功后自增一次
    }


    // 2.查询数据
    public boolean contains(Book data) {
        if (data == null || this.root == null) {// 没有数据,根节点也没有数据
            return false;
        }
        return this.root.containsNode(data);
    }


    // 3.修改数据
    public void set(int index, Book data) {
        if (index > this.count) {
            return;
        }
        this.foot = 0;// 归零,从头向后开始
        this.root.setNode(index, data);// 修改过程交给Node
    }

    // 4.获取数据
    public Book get(int index) {
        if (index > this.count) {// 超出查询范围
            return null;
        }
        this.foot = 0;// 归零,从头向后开始
        return this.root.getNode(index);// 查询过程交给Node
    }

    // 5.删除数据
    public void remove(Book data) {
        if (this.contains(data)) {// 1.先判断是否存在此数据
            // 2.判断删除的数据是否根节点数据
            // 3.root是Node的对象,此处直接访问内部类的私有操作
            if (data.compare(this.root.data)) {// 根节点是否满足删除
                this.root = this.root.next;// 空出当前节点
            } else {// 不是根节点
                // 根节点的下一个节点开始判断
                this.root.next.removeNode(this.root, data);
            }
            this.count--;// 删除成功后自减
        }
    }

    // 6.链表长度
    public int length() {
        return this.count;
    }

    // 7.链表是否为空
    public boolean isEmpty() {
        return this.count == 0;
    }

    // 8.对象数组
    public Book[] toArray() {
        if (this.root == null) {
            return null;
        }
        this.foot = 0;
        this.retArray = new Book[this.count];// 开辟数组
        this.root.toArrayNode();
        return this.retArray;
    }

    // 9.添加数据到指定位置
    public boolean insertAdd(Book data, int index) {
        if (data == null) {// 数据不允许为null
            return false;
        }

        if (length() < index) {
            return false;
        }
        this.count++;// 每次保存成功后自增一次
        Node newNode = new Node(data);// 保存数据,数据打包
        int i = 1;
        Node t = this.root;
        for (; i < length(); i++) {
            if (i + 1 == index) {
                Node next = t.next;
                t.next = newNode;
                newNode.next = next;
            }
            t = t.next;
        }
        return true;
    }

    // 10. 对数据进行插入排序
    public void insertSort() {
        if (this.length() == 0 || this.length() == 1) {             //用不了
            return;
        }
        this.root = insertSort(this.root);
    }

    private static Node insertSort(Node head) {
        Node sortedL, sortedR;                  //左节点,右节点
        sortedL = head;
        sortedR = head;
        Node current;                           //当前节点
        while (sortedR.next != null) {                  //右节点下一个不为空
            current = sortedR.next;                     //当前节点等于右节点的下一个
            if (current.data.getPrice() > sortedL.data.getPrice()) {                                //价格判断判断
                sortedR.next = current.next;                                        //交换
                current.next = sortedL;
                sortedL = current;
            } else if (sortedL.data.getPrice() >= current.data.getPrice() && current.data.getPrice() > sortedR.data.getPrice()) {           //判断
                Node p = sortedL;                               //临时节点p,初始化为左节点
                while (p != sortedR) {
                    if (p.data.getPrice() >= current.data.getPrice() && current.data.getPrice() > p.next.data.getPrice()) {     //确定此临时节点大于当前节点的价格,并且当前节点价格大于临时节点的下一个
                        sortedR.next = current.next;                                    //交换
                        current.next = p.next;
                        p.next = current;
                        break;
                    }
                    p = p.next;
                }
            } else {
                sortedR = current;
            }

        }
        return sortedL;
    }

    // 10. 对数据进行快速排序
    public void quickSort(Book[] books) {
        int length;
        if (books == null || (length = books.length) == 0 || length == 1) {
            return;
        }
        quickSort(books, 0, length - 1);
    }

    private static void quickSort(Book[] books, int low, int high) {
        int i, j;
        if (low > high) {
            return;
        }
        i = low;
        j = high;
        //temp就是基准位
        Book temp = books[low];

        while (i < j) {
            while (temp.getPrice() >= books[j].getPrice() && i < j) {
                j--;
            }
            while (temp.getPrice() <= books[i].getPrice() && i < j) {
                i++;
            }
            //如果满足条件则交换
            if (i < j) {
                Book z = books[i];
                Book y = books[j];
                books[i] = y;
                books[j] = z;
            }
        }

        books[low] = books[i];
        books[i] = temp;

        //递归调用左半数组
        quickSort(books, low, j - 1);
        //递归调用右半数组
        quickSort(books, j + 1, high);
    }

//
//    //9.价格降序排序 插入排序
//    public Book[] insertSort(Book[] data) {
//        for (int i = 1; i < data.length; i++) {
//            double mostPrice = data[i].getPrice();
//            int index = i;
//            while (index > 0) {
//                if (mostPrice > data[index - 1].getPrice()) {
//                    data[index] = data[index - 1];
//                } else {
//                    break;
//                }
//                index--;
//            }
//        }
//        return data;
//
//    }
//
//    //9.插入排序
//    public void InSert(Node head){
//        if (head == null || head.next ==null)  return;
//
//        Node pre = head;
//        Node cur = head.next;
//
//    }
//

    // 打印方法
    public static void print(Book[] data) {
        for (Book x : data) {
            System.out.println(x.getInfo() + " ");
        }
        System.out.println();
    }

}



class BookMain {
    public static void main(String[] args) {


        Link link = new Link();


        Scanner input = new Scanner(System.in);
        made();
        int num;
        do {
            System.out.println("请输入操作代码(0-退出)");
            num = input.nextInt();


            switch (num) {
                case 1:
                    System.out.println("请输入书本数量:");
                    int inputNum = input.nextInt();
                    for (int i = 1; i <= inputNum; i++) {
                        System.out.println("请输入第" + i + "本书的书名:");
                        String bookName = input.next();
                        System.out.println("请输入第" + i + "本书的价格:");
                        int inputPrice = input.nextInt();
                        Book book = new Book(bookName, inputPrice);
//                        System.out.println("请输入书本号:");
//                        int inputId = input.nextInt();
//                        book.setId(inputId);
                        link.add(book);
                    }
                    made();
                    break;

                case 2:
                    System.out.println("请输入查询的书本名:");
                    String bookName2 = input.next();
                    System.out.println("请输入该书的价格:");
                    int inputPrice = input.nextInt();
                    Book book2 = new Book(bookName2,inputPrice);
                    System.out.println(link.contains(book2));
                    made();
                    break;

                case 3:
                    System.out.println("请输入要修改书目的位置:");
                    int index3 = input.nextInt();
                    System.out.println("请输入要修改书目的名称");
                    String bookName3 = input.next();
                    System.out.println("请输入修改数目的价格:");
                    int inputPrice3 = input.nextInt();
                    Book book3 = new Book(bookName3, inputPrice3);
                    link.set(index3,book3);
                    made();
                    break;

                case 4:
                    System.out.println("请输入需要查询的书目位置:");
                    int index4 = input.nextInt();
                    System.out.println(link.get(index4-1));
                    made();
                    break;

                case 5:
                    System.out.println("请输入需要删除的书名:");
                    String bookName5 = input.next();
                    System.out.println("请输入其准确的价格:");
                    int inputPrice5 = input.nextInt();
                    Book book1 = new Book(bookName5, inputPrice5);
                    link.remove(book1);
                    made();
                    break;

                case 6:
                    System.out.print("当前书本数量为:");
                    System.out.print(link.length());
                    made();
                    break;

                case 7:
                    System.out.println("请输入需要插入的位置");
                    int index7 = input.nextInt();
                    System.out.println("请输入书目名称");
                    String bookName7 = input.next();
                    System.out.println("请输入该书的价格:");
                    int inputPrice7 = input.nextInt();
                    System.out.println("请输入该书的书号:");
//                    int inputId = input.nextInt();
                    Book book7 = new Book(bookName7,inputPrice7);
                    link.insertAdd(book7,index7);
//                    book7.setId(inputId);
                    made();
                    break;

                case 8:
                    link.insertSort();
                    System.out.println("已按价格降序完成排序");
                    made();
                    break;

                case 9:
                    Book[] book = link.toArray();
                    link.print(book);
                    made();
                    break;

            }
        }while (num != 0);
            input.close();



    }
    public static void made() {
        System.out.println("-----图书管理系统-----");
        System.out.println("操作选项菜单:"+ "\n1.添加书本" + "\t2.查询书本(未完善)" +"\t3.修改书本信息"
                + "\t4.根据位置查询书本" + "\t5.删除书本"+ "\n6.查询书本数量"+  "\t7.插入书本"+"\t8.按价格排序"+"\t9.显示书本信息"+"\t0.退出");
    }

}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值