java语言实现单链表

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) { // 不为0则进行循环
            System.out.println("请输入两个数字:");
            int x = scanner.nextInt();
            int y = scanner.nextInt();
            Point point3 = new Point(x, y);
            myLink2 = new MyLink(myLink2, point3);
        }
        myLink2 = myLink2.getLink();
        return myLink2;
    }

    /**
     * 打印链表
     */
    void printLink(MyLink link) {

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

    /**
     * 插入一个节点到链表中
     *
     * @param link
     *            插入的链表的引用
     */
    MyLink insertLink(MyLink link) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入两个整数:");
        int x = scanner.nextInt();
        int y = scanner.nextInt();
        Point point2 = new Point(x, y);
        link = new MyLink(link, point2);
        return link;
    }

    /**
     * 删除链表
     *
     * @param link
     *            链表的头引用
     * @return 删除之后链表的头节点的引用
     */
    MyLink deleteLink(MyLink link) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入需要删除的节点号");
        int i = scanner.nextInt();
        // 将头节点给保存起来
        MyLink linkHead = link;
        // 删除第一个元素和其他的元素的方法不太一致
        if (i == 1) {
            link = link.getLink();
            return link;
        } else {
            // 现在要找到第i-1个元素
            for (int j = 0; j < i - 2; j++) {
                // link为第i-1个元素的引用
                link = link.getLink();
            }
            // 为第i个元素的引用
            MyLink link2 = link.getLink();
            // 为第i+1个元素的引用
            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.创建链表 2.为链表增加节点 3.删除链表的节点 4.打印链表");
        System.out.println("请输入操作指令:");
        int i = scanner.nextInt();
        switch (i) {
        case 1:
            myLink = createLink();
            System.out.println("操作成功\n");
            menu(myLink);
            break;
        case 2:
            myLink = insertLink(myLink);
            System.out.println("操作成功\n");
            menu(myLink);
            break;
        case 3:
            myLink = deleteLink(myLink);
            System.out.println("操作成功\n");
            menu(myLink);
            break;
        case 4:
            printLink(myLink);
            System.out.println("操作成功\n");
            menu(myLink);
            break;
        default:
            System.out.println("输入的参数不合法!\n");
            menu(myLink);
            break;
        }
    }

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java语言实现单链表的示例代码: ```java public class LinkedList { Node head; // 头结点 // Node 类定义 class Node { int data; Node next; // Node 构造函数 Node(int d) { data = d; next = null; } } // 在链表头部插入结点 public void addNode(int data) { Node new_node = new Node(data); new_node.next = head; head = new_node; } // 在链表尾部插入结点 public void appendNode(int data) { Node new_node = new Node(data); if (head == null) { head = new_node; return; } Node last = head; while (last.next != null) { last = last.next; } last.next = new_node; } // 在给定结点后插入新结点 public void insertAfter(Node prev_node, int data) { if (prev_node == null) { System.out.println("The given previous node cannot be null"); return; } Node new_node = new Node(data); new_node.next = prev_node.next; prev_node.next = new_node; } // 删除结点 public void deleteNode(int key) { Node temp = head; Node prev = null; if (temp != null && temp.data == key) { head = temp.next; return; } while (temp != null && temp.data != key) { prev = temp; temp = temp.next; } if (temp == null) { return; } prev.next = temp.next; } // 遍历链表并打印结点值 public void printList() { Node curr_node = head; while (curr_node != null) { System.out.print(curr_node.data + " "); curr_node = curr_node.next; } } // 主函数 public static void main(String[] args) { LinkedList list = new LinkedList(); list.addNode(1); list.addNode(2); list.addNode(3); list.appendNode(4); list.appendNode(5); list.insertAfter(list.head.next, 6); list.deleteNode(3); list.printList(); } } ``` 上述代码中,我们定义了一个`Node`类表示链表中的结点,并在`LinkedList`类中实现单链表的基本操作,包括在链表头部插入结点、在链表尾部插入结点、在给定结点后插入新结点、删除结点等。在`main`函数中,我们使用上述操作构建了一个单链表,并遍历链表并打印结点值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值