首页 > 其他分享 >单链表(SingleLinkedList)

单链表(SingleLinkedList)

时间:2023-11-22 15:02:49浏览次数:21  
标签:Node head 单链 temp next SingleLinkedList newNode data

单链表

1.创建一个Node类

//    head不能动,头节点作用是表示链表的头
    private Node head;
// 在linkedList类写一个Node的成员内部类
private class Node {
    private int data;
    private Node next;

    public Node(int data) {
        this.data = data;
        this.next = null;
    }

    @Override
    public String toString() {
        return "Node{" +
                "data=" + data +
                '}';
    }
}

2.头插法

//    头插法
    public void addFirst(int data) {
//        首先创建一个节点,要头插的结点
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
            return;
        }
        newNode.next = head;
        head = newNode;
    }

3.尾插法

//    尾插法
public void addLast(int data) {
    //        首先创建一个节点
    Node newNode = new Node(data);
    if (head == null) {
        head = newNode;
        return;
    }
    //        head不能动,需要一个辅助指针temp,遍历完链表
    Node temp = head;
    while (temp.next != null) {
        temp = temp.next;//遍历链表
    }
    temp.next = newNode;
}

4.指定位置插入

//    指定位置插入
    public void addByOrder(int data, int position) {
        //        首先创建一个节点
        Node newNode = new Node(data);
        if (position == 0) {
//            头插法
            newNode.next = head;
            head = newNode;
            return;
        }
//        position是size的话就是尾插法
        if (position==size()){
            addLast(data);
            return;
        }
//        head不能动,需要一个辅助指针temp找的要插入位置的亲一个结点即temp
        Node temp = head;
        int curPosition = 0;
        while (temp.next != null && curPosition < position - 1) {
            temp = temp.next;
            curPosition++;
        }
        newNode.next = temp.next;
        temp.next = newNode;
    }

5.查找key是否在单链表中

//    查找key是否在单链表中
public boolean getKey(int key) {
    if (head.next == null) {
        System.out.println("链表为空~~~");
        return false;
    }
    Node temp = head.next;
    while (temp != null) {
        if (temp.data == key) {
            return true;
        }
        temp = temp.next;
    }
    return false;
}

6.链表长度

//    链表长度
public int size() {
    Node temp = head;
    int count = 0;
    while (temp != null) {
        count++;
        temp = temp.next;
    }
    return count;
}

7.删除数据

标签:Node,head,单链,temp,next,SingleLinkedList,newNode,data
From: https://www.cnblogs.com/mglblog/p/17849016.html

相关文章

  • C语言数据结构_查找并删除单链表中最大值结点并返回值
    代码实现1#include<stdio.h>2#include<stdlib.h>34typedefstructNode//定义一个结构体5{6floatdata;7structNode*next;8}Node;910Node*Chuangzao_LinkedList()//创建一个链表11{12Node*head=NULL;//......
  • 单链表建表,倒置,遍历(不使用Class,简洁易懂)
    在C++中通过递归方法实现单链表倒置初始化列表structListNode{ intval; LiseNode*next; ListNode(intx):val(x),next(NULL){}};遍历voidquery_node(){ node*p=head; while(p!=NULL){ cout<<p->data<<''; p=p->nxt; } cout<<endl;}建表(......
  • (链表)12-单链表的排序
    1importjava.util.*;23/*4*publicclassListNode{5*intval;6*ListNodenext=null;7*publicListNode(intval){8*this.val=val;9*}10*}11*/12publicclassSolution{13/**14*@paramhead......
  • 线性表-单链表
    首先定义一个元素typedefint LlElemtype;然后元素定义单链表,第一个结构体存放数据成员,第二个结构体存放下个节点的地址(可以用指针表示)typedefstruct __LNode{LlElemtypedata;__LNode*next;//用的是前面的名字}LNode,*LinkList  ......
  • Linux多路径IO流量负载和单链路负载压测
     LinuxMultipath的IO流量多链路负载和单链路负载压测 再linux下,对于udev和multipath均能做到自定义并持久化设备名,其中udev还能做到更改设备权限。而multipath也能做到持久化设备名,但无法更改设备权限,但是multipath能够实现更多的功能,比如IO流量负载功能。 测试情况1......
  • 2008秋-计算机软件基础-单链表练习(1)
    /*--------------------------------------------------------设有一个单链表,头结点为head,为递增有序,写一个完整程序,将其改为递减有序。----------------------------------------------------------*/#include<stdio.h>#include<stdlib.h>//定义结点structnodetype......
  • 2008秋-计算机软件基础-单链表完整示例
    /*---------------------------------------------------------Title:CompletedSimpleLinkedListAuthor:EmanLeeDate:Oct22,2008Fuction:OperationonLinkedStoredLinearList.Thisisacompletedsimplesample.Itisrelatedto......
  • 2008秋季-线性表的链式存储(仅单链表)
    /*---------------------------------------------------------Title:单链表Date:September1,2008Fuction:单链表的初始化,创建,插入,删除,查找结点。参考PPT讲稿或者教材2.2.4节.(p56-63)----------------------------------------------------------*/#inclu......
  • 面试必刷TOP101:12、单链表的排序
    一、题目publicclassSolution{/***代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可***@paramheadListNode类theheadnode*@returnListNode类*/publicListNodesortInList(ListNodehead){......
  • 基础数据结构:数组实现的单链表(静态链表)、双链表
    1、单链表(静态链表)以AcWing.826为例,题目要求如下:实现一个单链表,链表初始为空,支持三种操作:向链表头插入一个数;删除第k个插入的数后面的数;在第k个插入的数后插入一个数。现在要对该链表进行M次操作,进行完所有操作后,从头到尾输出整个链表。注意:题目中第k个插入的数并不是指当......