java实现数据结构单链表示例(java单链表)
/**
*单向链表
*
*/
publicclassNodeList<E>{
privatestaticclassNode<E>{//节点类
Edata;//节点上的数据
Node<E>next;//指向下一个节点
Node(Ee){
this.data=e;
this.next=null;
}
}
privateNode<E>head;//链表的头节点
privateNode<E>last;//链表的尾节点
privateNode<E>other=null;
privateintlength=0;//节点数量
/**
*无参构造方法
*/
publicNodeList(){
//默认节点为空
this.head=newNode<E>(null);
}
/**
*初始化时创建一个节点
*
*@paramdata
* 数据
*/
publicNodeList(Edata){
this.head=newNode<E>(data);
this.last=head;
length++;
}
/**
*添加一个节点(尾插法)
*
*@paramdata
* 数据
*/
publicvoidadd(Edata){
if(isEmpty()){
head=newNode<E>(data);
last=head;
length++;
}else{
Node<E>newNode=newNode<E>(data);
last.next=newNode;
last=newNode;
}
}
/**
*获得索引处的数据(索引输入错误抛出越界异常)
*@paramindex索引
*@return索引处数据
*/
publicEget(intindex){
if(index<0||index>length){
thrownewIndexOutOfBoundsException("索引越界:"+index);
}
other=head;
for(inti=0;i<index;i++){
other=other.next;
}
returnother.data;
}
/**
*新值替换旧值
*@return成功为true,未找到为false
*/
publicbooleanset(EoldValue,EnewValue){
other=head;
while(other!=null){
if(other.data.equals(oldValue)){
other.data=newValue;
returntrue;
}
other=other.next;
}
returnfalse;
}
/**
*在指定元素后插入一个元素
*
*@paramdata
* 指定的元素
*@paraminsertData
* 需要插入的元素
*@returnfalse为未找到元素,true为插入成功
*/
publicbooleanadd(Edata,EinsertData){
other=head;
while(other!=null){
if(other.data.equals(data)){
Node<E>newNode=newNode<E>(insertData);
Node<E>temp=other.next;
newNode.next=temp;
other.next=newNode;
length++;
returntrue;
}
other=other.next;
}
returnfalse;
}
/**
*链表中是否包含此元素
*@return包含为true,不包含为false
*/
publicbooleancontains(Edata){
other=head;
while(other!=null){
if(other.data.equals(data)){
returntrue;
}
other=other.next;
}
returnfalse;
}
/**
*移除指定的元素
*@paramdata需要移除的元素
*@return不存在为false,成功为true
*/
publicbooleanremove(Edata){
other=head;
Node<E>temp=head; //临时变量,用于保存前一个节点
while(other!=null){
if(other.data.equals(data)){
temp.next=other.next;
length--;
returntrue;
}
temp=other;
other=other.next;
}
returnfalse;
}
/**
*判断链表是否为空
*
*@return空为true,非空为false
*/
publicbooleanisEmpty(){
returnlength==0;
}
/**
*清空链表
*/
publicvoidclear(){
this.head=null;
this.length=0;
}
/**
*输出所有节点
*/
publicvoidprintLink(){
if(isEmpty()){
System.out.println("空链表");
}else{
other=head;
while(other!=null){
System.out.print(other.data);
other=other.next;
}
System.out.println();
}
}
}
相关文章
- java分层打印二叉树_基于Java的二叉树层序遍历打印实现
- java对象组声明_如何在Java中声明对象数组?
- java list去重_JAVA基础-List去重的6种方式[通俗易懂]
- java calendar 设置小时_Java Calendar.set 方法设置时间的问题
- java二维数组试题_Java二维数组及习题总结
- Java详解:淘宝秒杀脚本java
- JAVA常用数据结构及原理分析(面试总结)「建议收藏」
- java 登录 qq_Java实现QQ登录
- java ee简介_Java EE 简介
- java extends runtimeexception_Java中RuntimeException和Exception的区别
- java如何输入字符串_JAVA中怎样输入字符串「建议收藏」
- java axis_Java 使用Axis实现WebService实例
- java oracle数据备份_Java实现Oracle数据库备份
- 用java实现笛卡尔积_Java实现笛卡尔积
- Java多态理解_什么是java多态
- rtsp 获取视频流 java_Java获取rtsp视频流,实现rtsp流预览功能,并将视频流每帧保存成图片…
- java栈数据结构的LinkedList实现详解编程语言
- 【数据结构】之链栈的java实现详解编程语言
- 实现使用Java代码实现MySQL数据库连接(java连接mysql数据库代码)
- Java监控Oracle性能的最佳实践(java监控oracle)
- eeOracle放弃Java EE:梦想的终结(oracle放弃java)
- 写入数据Java实现MySQL数据写入(java向mysql)
- 之间的交互Redis与Java实现交互的探索(redis和java)
- Java下使用Redis进行高效缓存优化(Redis缓存java)
- MySQL与Java的结合:实现强大的数据持久化功能(mysql与java)
- 程序Oracle调用Java程序的实现方法(oracle调用java)
- 使用Java程序执行Linux指令:实现自动化操作(java执行linux命令)
- Oracle中实现Java程序设计的极限可能性(oracle中的java)
- Redis实现Java语言下的遍历处理(redis遍历java)
- 缓存使用Redis让Java代码更加迅速缓存设置(redis设置java)
- Java实现Redis计数器功能(redis计数 java)