Java教程

JAVA 数据结构 单向链表

本文主要是介绍JAVA 数据结构 单向链表,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

JAVA 单向链表

链表和数组都是顺序存储的数据结构,链表对数据的增删要比数组更加方便,因为不用去担心链表长度溢出的情况。数组更适合搜索,只要有了数组下标目标元素通过数组下标直接索引就可以获得。

package 链表;

import java.util.Scanner;

public class ListLink {
    static ListLink L = new ListLink();
    static Link link = L.new Link();

    public void addListNode() {
        int i;
        //增加
        Scanner input = new Scanner(System.in);
        System.out.println("please input a number");
        int number = input.nextInt();
        ListNode[] listNodes = new ListNode[number];
        for (i = 0; i < number; i++) {
            listNodes[i] = L.new ListNode();
            System.out.println("请输入数据:");
            int data = input.nextInt();
            System.out.println("请输入姓名:");
            String name = input.next();
            listNodes[i].setData(data);
            listNodes[i].setName(name);
        }
        //设置初始节点,不是头结点
        link.setHead(listNodes[0]);
        //用for循环给链表赋值
        for (i = 1; i < number; i++) {
            link.addNode(listNodes[i]);
        }
        //获取初始节点
        ListNode head = link.getHead();
        ListNode temp = head;
        while (temp != null) {
            System.out.println(temp.getData() + "\t" + temp.getName());
            temp = temp.next;
        }
    }

    public void deleteListNode() {
        link.deleteNode();
        //获取删除后的结果
        ListNode head = link.getHead();
        ListNode temp = head;
        while (temp != null) {
            System.out.println(temp.getData() + "\t" + temp.getName());
            temp = temp.next;
        }
    }

    public static void main(String[] args) {
        ListLink L = new ListLink();
        L.addListNode();//增加
        L.deleteListNode();//删除
        //对于顺序结构的修改和查询,我认为使用数组更加方便
    }

    class ListNode {
        private int data;
        private String name;
        ListNode next = null;

        public ListNode() {

        }

        public ListNode(int data, String name) {
            this.data = data;
            this.name = name;
        }

        public int getData() {
            return data;
        }

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

        public String getName() {
            return name;
        }

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

    class Link {

        ListNode head;

        //获取头结点
        public void setHead(ListNode head) {
            this.head = head;
        }

        public ListNode getHead() {
            return head;
        }

        //增
        public void addNode(ListNode k) {
            ListNode temp = head;
            while (temp.next != null) {
                temp = temp.next;
            }
            temp.next = k;
        }

        //删
        public void deleteNode() {
            //删除
            Scanner input = new Scanner(System.in);
            System.out.println("根据编号删除,还是根据内容删除?");
            System.out.println("1:编号" + "\t" + "2:内容");
            int number = input.nextInt();
            if (number == 1) {
                System.out.println("请输入删除元素的编号");
                int new_data = input.nextInt();
                ListNode head = link.getHead();
                ListNode temp = head;
                if (head.getData() == new_data) {
                    link.setHead(head.next);
                } else {
                    while (temp != null) {
                        if (temp.getData() == new_data) {
                            break;
                        } else {
                            temp = temp.next;
                        }
                    }
                    if (temp == null) {
                        System.out.println("没有找到该元素");
                        return;
                    } else {
                        ListNode new_temp = head;
                        while (new_temp.next != temp) {
                            new_temp = new_temp.next;
                        }
                        new_temp.next = temp.next;
                    }
                }
            } else if (number == 2) {
                System.out.println("请输入删除元素的内容");
                String new_name = input.next();
                ListNode head = link.getHead();
                ListNode temp = head;
                if (head.getName().equals(new_name)) {
                    link.setHead(head.next);
                } else {
                    while (temp != null) {
                        if (temp.getName().equals(new_name)) {
                            break;
                        } else {
                            temp = temp.next;
                        }
                    }
                    if (temp == null) {
                        System.out.println("没有找到该元素");
                        return;
                    } else {
                        ListNode new_temp = head;
                        while (new_temp.next != temp) {
                            new_temp = new_temp.next;
                        }
                        new_temp.next = temp.next;
                    }
                }
            }
        }
    }

}

 

这篇关于JAVA 数据结构 单向链表的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!