基于java语言的单链表
来源:互联网 发布:淘宝账号在哪看到 编辑:程序博客网 时间:2024/06/04 04:16
链表的基本概念
先来看看百度百科对链表的描述
链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成。每个结点包括两个部分:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域。 相比于线性表顺序结构,操作复杂。由于不必须按顺序存储,链表在插入的时候可以达到O(1)的复杂度,比另一种线性表顺序表快得多,但是查找一个节点或者访问特定编号的节点则需要O(n)的时间,而线性表和顺序表相应的时间复杂度分别是O(logn)和O(1)。
简单链表的逻辑表示如下图所示,其中Head是链表中第一个元素的引用
java代码描述
//学生信息类,存放节点的内容
class Student{ private String name; private int age; private int num; public String getName() { return name; } public int getNum() { return num; } public int getAge() { return age; } public Student(String name, int age, int num) { this.name = name; this.age = age; this.num = num; }}
//节点类,包括学生对象和指向节点的引用
class Node{ Student stu; Node reference; public Node(Student stu) { this.stu = stu; } public Student getStu() { return stu; } public Node getReference() { return reference; }}
//链表,主要执行了插入、删除和查找三种操作class LinkList{ //声明头结点 Node head = null; //插入节点 public void insert(Student stu){ Node node = new Node(stu); node.reference = head; head = node; } //遍历链表 public void Display(){ if(head == null){ System.out.println("链表为空"); } else{ Node currentRef; currentRef = head; while (currentRef != null){ System.out.println(currentRef.getStu().getName()+" "+ currentRef.getStu().getNum()+" "+ currentRef.getStu().getAge()); currentRef = currentRef.reference; } } } //按照学号查找 public Node FindByNum(int num){ if(head == null){ System.out.println("链表为空"); return null; } else{ Node currentRef; currentRef = head; while (currentRef != null){ if(currentRef.getStu().getNum() == num) { return currentRef; } currentRef = currentRef.reference; } return null; } } //根据学号删除 public boolean DelteStu(int num){ if(head == null){ System.out.println("链表为空"); return false; } else if(head.getStu().getNum() == num){ head = head.reference; System.out.println("删除成功"); return true; } else { Node before; Node currentRef; before = head; currentRef = head.reference; while (currentRef !=null){ if(currentRef.getStu().getNum() == num){ before.reference = currentRef.reference; System.out.println("删除成功"); return true; } before = currentRef; currentRef = currentRef.reference; } System.out.println("不存在该学生"); return false; } }
主函数(测试类public class BasicLinkList { public static void main(String[] arg){ LinkList list = new LinkList(); list.insert(new Student("lucy",12,1011)); list.insert(new Student("peter",12,1021)); list.insert(new Student("smith",12,1061)); System.out.println("姓名 学号 年龄"); list.Display(); Node node = list.FindByNum(1061); if(node == null){ System.out.println("不存在这个学生"); } else { System.out.println(node.getStu()); } list.DelteStu(1061); list.Display(); }}第一次贴这么多代码,请大家多多指教!
0 0
- 基于java语言的单链表
- 基于JAVA语言的网络爬虫
- 一种基于java语言的模板引擎velocity的使用
- 基于C语言单链表的成绩管理程序
- Java语言深入--基于JNDI的应用开发
- 基于JAVA XML语言的信息发布系统.
- 基于Android的数独游戏-----用Java语言编写
- Idea基于maven,java语言的spark环境搭建
- 基于Java语言框架SSM写的一个小博客
- 单例模式(Singleton)的分析----基于java语言
- 基于语言的操作系统
- 基于对象的JavaScript语言
- 基于对象的JavaScript语言
- 基于对象的Js语言
- 实现基于数据结构的语言
- 基于对象的JavaScript语言
- 基于C语言的万年历
- 基于R语言的神经网络
- 蓝桥杯 基础练习 数列排序(4)之快排
- C# 抽象类
- OpenCV的安装与配置
- 个人项目——音乐播放器(一)
- 【Unity&Atlas】NGUI与UGUI打包图集的步骤以及比较
- 基于java语言的单链表
- HDU—校赛—1004
- 如何避免 OOM 异常
- Arcgis Javascript那些事儿(八)--图层获取与图层顺序
- dsoframer.cpp
- android ---imageloader 加本地图片时 java.lang.UnsupportedOperationException: UIL doesn't support scheme,
- 手势滑动日历
- 深入理解SELinux SEAndroid(第一部分) .
- 程序猿头发保护协会