java language implementation of singly linked list

package com.irisian;

public class Point {

    private int x;
    private int y;

    public Point() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Point(int x, int y) {
        super();
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    @Override
    public String toString() {
        return "Point [x=" + x + ", y=" + y + "]";
    }
}


package com.irisian;

import java.util.Scanner;

public class MyLink {
    private MyLink link; // 指向自己的引用
    private Point point;

    public MyLink getLink() {
        return link;
    }

    public void setLink(MyLink link) {
        this.link = link;
    }

    public Point getPoint() {
        return point;
    }

    public void setPoint(Point point) {
        this.point = point;
    }

    public MyLink() {
        super();
    }

    public MyLink(MyLink link, Point point) {
        super();
        this.link = link;
        this.point = point;
    }

    /**
     * 创建链表
     *
     * @return
     */
    MyLink createLink() {
        Scanner scanner = new Scanner(System.in);
        Point point2 = new Point(1, 2);
        MyLink myLink2 = null;
        myLink2 = new MyLink(myLink2, point2);
        // do{
        // System.out.println("请输入两个数字:");
        // int x=scanner.nextInt();
        // int y=scanner.nextInt();
        // Point point3 = new Point(x, y);
        // myLink2 = new MyLink(myLink2, point3);
        // }while(myLink2.getPoint().getX()!=0);
        while (myLink2.getPoint().getX() != 0) { // loop if not 0
            System.out.println("Please enter two numbers:");
            int x = scanner.nextInt();
            int y = scanner.nextInt();
            Point point3 = new Point(x, y);
            myLink2 = new MyLink(myLink2, point3);
        }
        myLink2 = myLink2.getLink();
        return myLink2;
    }

    /**
     * print linked list
     */
    void printLink(MyLink link) {

        while (link.getLink() != null) {
            System.out.println(link.getPoint());
            link = link.getLink();
        }
    }

    /**
     * Insert a node into linked list
     *
     * @param link
     * The reference to the inserted linked list
     */
    MyLink insertLink(MyLink link) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter two integers:");
        int x = scanner.nextInt();
        int y = scanner.nextInt();
        Point point2 = new Point(x, y);
        link = new MyLink(link, point2);
        return link;
    }

    /**
     * delete linked list
     *
     * @param link
     * The head reference of the
     linked list* @return the reference to the head node of the linked list after deletion
     */
    MyLink deleteLink(MyLink link) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter the node number to delete");
        int i = scanner.nextInt();
        // save the head node
        MyLink linkHead = link;
        // delete the first element and other elements The method is not consistent
        if (i == 1) {
            link = link.getLink();
            return link;
        } else {
            // Now to find the i-1th element
            for (int j = 0; j < i - 2 ; j++) {
                // link is the reference of the i-1th element
                link = link.getLink();
            }
            // is the reference of the i-th element
            MyLink link2 = link.getLink();
            // is the i+ 1th Element references
            MyLink link3 = link2.getLink();
            link.setLink(link3);
        }
        return linkHead;
    }

    public static void main(String[] args) {
        MyLink myLink = new MyLink();
        myLink.menu(myLink);
    }

    void menu(MyLink myLink) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("1. Create a linked list 2. Add a node to the linked list 3. Delete the node of the linked list 4. Print the linked list");
        System.out.println("Please enter the operation command:");
        int i = scanner. nextInt();
        switch (i) {
        case 1:
            myLink = createLink();
            System.out.println("Operation succeeded\n");
            menu(myLink);
            break;
        case 2:
            myLink = insertLink(myLink);
            System.out.println("The operation succeeded\n");
            menu(myLink);
            break;
        case 3:
            myLink = deleteLink(myLink);
            System.out.println("The operation succeeded\n");
            menu(myLink);
            break;
        case 4:
            printLink(myLink);
            System.out.println("The operation succeeded\n");
            menu(myLink);
            break;
        default:
            System.out.println("The input parameter is invalid!\n") ;
            menu(myLink);
            break;
        }
    }

}



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325597827&siteId=291194637