数据结构--单向链表
来源:互联网 发布:七日杀服务器网络 编辑:程序博客网 时间:2024/04/28 09:11
单向链表
此案例的插入是每次从头部插入新节点,所以遍历时,数据从新到旧
package cn.itcast.entity;
public class Node
{
public int i ;
public Node next ;
}
-------------------------------------
package cn.itcast.entity;
public class NodeList
{
private Node first ;//要插入的新节点
private Node front ;//指向当前节点的前面一个变量
private Node current ;//指向当前节点
//添加新节点
public void insert ( int i )
{
//如果first 为空,代表第一次插入数据,直接向first里放入数据
if ( first == null )
{
first = new Node () ;
first.i = i ;
}
//不为空,说明有数据了,这里我们用从头部插入的方式,即新node为first,老first指向新node.next
else
{
Node newNode = new Node ();
newNode.i = i ;
newNode.next = first ;
first = newNode ;
}
}
//按内容删除
public void delete ( int i )
{
//使用current来遍历链表以查找元素
current = first ;
front = first ;
while ( current != null )
{
//如果不等于,代表没找到要删除的节点
if ( current.i != i )
{
front = current ;
current = current.next ;
}
//否则,代表找到要删除的节点
else
{
//将要删除的节点的前一项的next指向要删除节点的next
front.next = current.next ;
System.out.println ( "已删除" );
return ;
}
}
//跳出循环,代表遍历到底了,没有匹配的数据
System.out.println ( "没找到" );
return ;
}
//按序号修改
public void update ( int i , int data )
{
//序号
int count = 1 ;
current = first ;
//遍历寻找序号符合的
while ( current != null )
{
//找到了
if ( count == i )
{
current.i = data ;
return ;
}
//没找到
current = current.next ;
count ++ ;
}
//跳出循环,代表输入的序号超出当前最大值
System.out.println ( "超出链表长度" );
}
//按内容查找
public Node findByData ( int i )
{
current = first ;
//循环遍历
while ( current != null )
{
//找到就将当前节点返回
if ( current.i == i )
{
return current ;
}
//没找到,就继续下一个
current = current.next ;
}
//循环结束,说明到底了
System.out.println ( "没找到" );
return null ;
}
//链表长度
public int length ()
{
//统计个数
int count = 0 ;
current = first ;
//遍历中统计个数
while ( current != null )
{
++ count ;
current = current.next ;
}
return count ;
}
//遍历
public void display ()
{
current = first ;
while ( current != null )
{
System.out.print ( current.i + "," );
current = current.next ;
}
System.out.println ( );
}
}
-------------------------------------------
package cn.itcast.entity;
import org.junit.Test;
public class test
{
@Test
public void t ()
{
NodeList nl = new NodeList () ;
nl.insert ( 1 );
nl.insert ( 2 );
nl.insert ( 3 );
nl.insert ( 4 );
nl.insert ( 5 );
nl.insert ( 6 );
nl.display ();
System.out.println ( nl.length () );
nl.delete ( 1 );
nl.display ();
System.out.println ( nl.length () );
nl.update ( 6 , 1 );
nl.display ();
System.out.println ( nl.length () );
System.out.println ( nl.findByData ( 6 ).i );
}
}
-------------------------------运行结果--------------------
6,5,4,3,2,1,
6
已删除
6,5,4,3,2,
5
超出链表长度
6,5,4,3,2,
5
6
阅读全文
1 0
- 数据结构-单向链表
- 【数据结构】单向链表
- [数据结构] 单向链表
- 数据结构~~单向链表
- 数据结构单向链表
- 数据结构-单向链表
- 数据结构 单向链表
- 数据结构--单向链表
- 【数据结构】单向链表实例
- 数据结构之单向链表
- 数据结构之单向链表
- 数据结构之单向链表
- 数据结构之单向链表
- c++单向链表-数据结构
- 数据结构(单向链表)
- 数据结构之单向链表
- 数据结构--单向链表1
- 数据结构--单向链表2
- 阿里云大学考试认证
- 11月27日云栖精选夜读:阿里毕玄:智能时代,运维工程师在谈什么?
- 基于Java的矩阵归零问题研究与实现
- MyBatis 延迟加载,一级缓存(sqlsession级别)、二级缓存(mapper级别)设置
- Django post 报错
- 数据结构--单向链表
- logstash启动文件conf之filter模块中字段转换问题
- 《Linux内核完全剖析》读书笔记——第二章 微机组成结构
- log4j SocketAppender 使用及与向logstash端口推送日志
- 高级网络控制
- Json
- IDAPython脚本分享
- 网页设计中的三大类配色
- 安卓 关于list数据操作的设计思路