实现链表的过程详解

目录

一、知己知彼

0.线性表

1.链表

2.实现链表需要什么类,类里需要什么属性,什么方法

二、开始写代码

1.结点类准备

2.链表类准备

3.链表的插入方法

4.链表的显示方法

5.链表的查找方法

6.链表的删除方法

7.链表的长度

三、实现链表的完整代码


一、知己知彼

0.线性表

线性表是最基本、最简单、也是最常用的一种数据结构。线性表(linear list)是数据结构的一种,一个线性表是n个具有

相同特性的数据元素的有限序列。特点

<1>存在唯一一个称为“第一个”的元素

<2>存在唯一一个称为“最后一个”的元素

<3>除第一个元素之外,序列中的每个元素只有一个直接前驱

<4>除最后一个元素之外,序列中的每个元素只有一个直接后继 

1.链表

线性表中的元素除了数据信息外,还要有一个指针来指向它的后继元素,我们称这种元素为结点,结点中存放数据信息的

部分称为数据域,存放指针信息的部分称为指针域。因为每个结点中只有一个指向后继结点的指针,所以称其为单链表。

单链表结点结构如下:

2.实现链表需要什么类,类里需要什么属性,什么方法

(1)既然链表由结点组成,那么肯定要有结点类(Node),结点里有数据属性(data)、指针属性(Node next),还

         有构造方法(给每个结点初始化data值)和显示结点数据的方法display; 

(2)要实现链表,肯定要有链表类(LinkList),链表类里有头结点,有插入、显示、查找、删除、求表长方法

二、开始写代码

1.结点类准备

public class Node {
	//数据域
	public long data;
	//指针域
	public Node next;
	
	public Node(long value) {
		this.data = value;
	}
	
	/**
	 * 显示方法
	 */
	public void display() {
		System.out.print(data + " ");
	}
}

2.链表类准备

public class LinkList {
//头结点
    private Node first;
//可以不写构造方法
    public LinkList(){
        first=null;
    }
}

3.链表的插入方法

     /**
     * 在头结点后面插入数据
     * @param value,插入的值
     */


    void insertFirst(int value){
        Node node=new Node(value);
        if (first==null){
            first=node;
        }else{
            node.next=first;
            first=node;
        }
//        node.next = first;
//        first = node;
    }

4.链表的显示方法

public void display() {
        Node current = first;
        while(current != null) {
            current.display();
            current = current.next;
        }
        System.out.println();
    }

5.链表的查找方法

        public Node find(long value) {
            Node current = first;
            while(current.data != value) {
                if(current.next == null) {
                    return null;
                }
                current = current.next;
            }
            return current;
        }

6.链表的删除方法

(1)删除一个结点,在头结点后进行删除

             public Node deleteFirst(){
                 Node del_node = first;//
                 first=del_node.next;
                 return del_node;
             }

(2)删除指定值得数据

        public Node delete(long value){
            Node current=first;
            Node previous=first;
            while(current.data!=value){
                if (current.next==null){
                    return null;
                }
                previous=current;
                current=current.next;
           }
            if(current == first) {
                first = first.next;
            } else {
                previous.next = current.next;
            }
               return current;
        }

7.链表的长度

        public int length(){
            int i=0;
            Node flag_node=first;
            while (flag_node != null){
                ++i;
                flag_node=flag_node.next;
            }
            return i;
        }

三、实现链表的完整代码

package myLinkList;

public class LinkList {
    //头结点
    private Node first;

    public LinkList(){
        first=null;
    }

    /**
     * 在头结点后面插入数据
     * @param value,插入的值
     */
    void insertFirst(int value){
        Node node=new Node(value);
        if (first==null){
            first=node;
        }else{
            node.next=first;
            first=node;
        }
//        node.next = first;
//        first = node;
    }
    /**
     * 显示方法
     */
    public void display() {
        Node current = first;
        while(current != null) {
            current.display();
            current = current.next;
        }
        System.out.println();
    }
    /**
     * 删除一个结点,在头结点后进行删除
     */
    public Node deleteFirst(){
        Node del_node = first;
        first=del_node.next;
        return del_node;
    }
    /**
     * 查找方法
     */
    public Node find(long value) {
        Node current = first;
        while(current.data != value) {
            if(current.next == null) {
                return null;
            }
            current = current.next;
        }
        return current;
    }
    /**
     * 删除方法,根据数据域来进行删除
     */
    public Node delete(long value){
        Node current=first;
        Node previous=first;
        while(current.data!=value){
            if (current.next==null){
                return null;
            }
            previous=current;
            current=current.next;
        }
        if(current == first) {
            first = first.next;
        } else {
            previous.next = current.next;
        }
        return current;
    }
//链表长度
    public int length(){
        int i=0;
        Node flag_node=first;
        while (flag_node != null){
            ++i;
            flag_node=flag_node.next;
        }
        return i;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值