栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 软件开发 > 后端开发 > Java

java数据结构之单向链表(入门)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java数据结构之单向链表(入门)

 链表是一种常见的数据结构,其中运用到了结构体指针,链表可以实现动态存储分配,换而言之,链表是一个功能强大的数组,可以在某个节点定义多种数据类型,可以实现任意的添加,删除,插入节点等。链表分为两类,一种是有头节点的链表,另一种是无头节点的链表。头节点没有数据域的,其他每个节点都有两部分,一是数据域,二是指针域。
 

 

public class LinkedList {
  private Node head;//头部指针
  private int count;//节点数量

  public void add(Object data) {
    Node node = new Node();
    node.data = data;//传入数据
    if (count < 1) {//节点数量小于1
      this.head = node;
    } else {
      Node tmp = head;
      while (tmp.next != null) {
        tmp = tmp.next;
      }
      tmp.next = node;
    }
    count++;
  }

  public static void main(String[] args) {
    LinkedList list = new LinkedList();
    list.add(1);
    list.add(2);
    System.out.println(list.count);

    Node tmp = list.head;
    while(tmp!=null) {
      System.out.println(tmp);
      tmp=tmp.next;
    }
  }


  class Node {
    private Object data;//存放数据
    private Node next;//下一个位置
    @Override
    public String toString() {
      return "Node [data=" + data + ", next=" + next + "]";
    }
    
  }
}

转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/1040879.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号