• 单向链表


      单向链表(单链表)是链表的一种,其特点是链表的链接方向是单向的,对链表的访问要通过顺序读取从头部开始;链表是使用指针进行构造的列表;又称为结点列表,因为链表是由一个个结点组装起来的;其中每个结点都有指针成员变量指向列表中的下一个结点;

    列表是由结点构成,head指针指向第一个成为表头结点,而终止于最后一个指向nuLL的指针。

    创建链表--增加节点

     1 public class Node {
     2     Node next = null; 
    3
    int data; 4 public void Node(int data){ 5 this.data = data; 6 } 7 8 } 9 10 11 public class LinkedList { 12 Node head = null; 13 public void addNode(int data){ 14 Node newNode = new Node(); 15 if(head == null){ 16 head = newNode; 17 }else{ 18 Node temp = head; 19 while(temp.next != null ){ 20 temp = temp.next; 21 } 22 temp.next = newNode; 23 } 24 25 }

    输出10个数的链表

     1 package com.feimao.algorithm.test;
     2 
     3 public class Node {
     4     int data;
     5     Node next;
     6 
     7     public Node(int data) {
     8         this.data = data;
     9         this.next = null;
    10     }
    11 
    12     public int getData() {
    13         return data;
    14     }
    15 
    16     public void setData(int data) {
    17         this.data = data;
    18     }
    19 }
    20 package com.feimao.algorithm.test;
    21 
    22 public class LinkedListTest {
    23     private static int input = 10;
    24     public static void main(String[] args){
    25         Node head = new Node(0);
    26         Node temp = head;
    27         for(int i = 1 ; i <= input ; i++){
    28             temp.next = new Node(i);
    29             temp = temp.next;
    30             System.out.print("-->" +temp.getData());
    31         }
    32 
    33 
    34     }
    35 }
  • 相关阅读:
    E
    牛客比赛—身体训练
    前缀和例题
    欧拉函数模板
    3.30训练题
    poj1321棋盘问题
    记set学习
    B. K-th Beautiful String
    codeforces1293C
    LightOJ 1370 Bi-shoe and Phi-shoe
  • 原文地址:https://www.cnblogs.com/feimaoyuzhubaobao/p/9808058.html
Copyright © 2020-2023  润新知