java 操作双向链表
2014-10-16 21:44
344 查看
java 操作双向链表
最近在复习数据结构,看了链表的操作,但是书上是用C实现的,我本人用java实现了双向
链表的操作,现在贴出代码与大家分享一下:
package com.qgmobile.list;
/**
* 双向链表节点
* @author yangchuxi
*
*/
public class TwoWayNode<Object> {
public Object t;
//后继
public TwoWayNode<Object> next;
//前驱
public TwoWayNode<Object> pre;
public TwoWayNode(Object t){
this.t=t;
}
public String toString(){
return t.toString();
}
}
package com.qgmobile.list;
/**
* 对双向链表的操作
* @author yangchuxi
*
*/
public class TwoWayList {
private TwoWayNode<Object> head;
private int size;
/**
* 链表的初始化
*/
public TwoWayList() {
size=0;
//表头不放数据
head=new TwoWayNode<Object>(null);
head.next=null;
head.pre=null;
}
//插入到链表前段(表头之后)
public boolean insertfirst(TwoWayNode<Object> tn){
if(tn!=null){
//首先要先把插入节点的前驱和后继搞定先,注意第二句和第三局的顺序
if(head.next!=null){
tn.pre=head;
tn.next=head.next;
head.next.pre=tn;
head.next=tn;
}else{
head.next=tn;
tn.pre=head;
}
size++;
return true;
}
return false;
}
//插入到链表的末尾
public boolean insertlast(TwoWayNode<Object> tn){
if(tn!=null){
TwoWayNode<Object> current=head;
while(current.next!=null){
current=current.next;
}
current.next=tn;
tn.pre=current;
size++;
return true;
}
return false;
}
// 在指定节点后添加节点
public boolean insertinto(TwoWayNode<Object> tn1,TwoWayNode<Object> tn2){
if(tn1!=null && tn2!=null){
//首先遍历这个链表,看看有没有等于tn1
TwoWayNode<Object> current=head;
while(current!=tn1){
current=current.next;
if(current==null){
return false;
}
}
if(tn1==head){
insertfirst(tn2);
size++;
return true;
}
if(current.next==null){
insertlast(tn2);
size++;
return true;
}
tn2.pre=current;
tn2.next=current.next;
current.next.pre=tn2;
current.next=tn2;
size++;
return true;
}
return false;
}
//删除链表前端节点
public boolean deletehead(){
if(head.next!=null){
TwoWayNode<Object> current=head.next;
head.next=current.next;
current.next.pre=head;
current=null;
size--;
return true;
}
return false;
}
// 删除尾节点
public void deltetail(){
TwoWayNode<Object> current=head;
while(current.next!=null){
current=current.next;
}
TwoWayNode<Object> pre=current.pre;
pre.next=null;
current=null;
size--;
}
//删除指定节点
public boolean deletenode(TwoWayNode<Object> tn){
if(tn!=null){
TwoWayNode<Object> current=head;
while(current!=tn){
current=current.next;
if(current==null){
return false;
}
}
if(tn==head.next){
return deletehead();
}
//保存与tn相等的值
TwoWayNode<Object> acurrent=current;
while(current.next!=null){
current=current.next;
}
if(current==tn){
deltetail();
return true;
}
TwoWayNode<Object> pre=acurrent.pre;
TwoWayNode<Object> next=acurrent.next;
pre.next=next;
next.pre=pre;
acurrent=null;
size--;
return true;
}
return false;
}
// 链表长度
public int getSize() {
return size;
}
// 正序和反序遍历遍历链表并打印
public void diplay() {
TwoWayNode<Object> current = head.next;
TwoWayNode<Object> tail=null;
System.out.println("正序输出:");
while (current != null) {
System.out.println(current.toString());
tail=current;
current = current.next;
}
// System.out.println("tail"+tail.toString());
System.out.println("反序输出:");
while(tail!=head){
if(tail!=null){
System.out.println(tail.toString());
}
tail=tail.pre;
}
}
public static void main(String[] args) {
TwoWayList twoWayList=new TwoWayList();
TwoWayNode<Object> node1=new TwoWayNode<Object>("test1");
TwoWayNode<Object> node2=new TwoWayNode<Object>("test2");
TwoWayNode<Object> node3=new TwoWayNode<Object>("test3");
TwoWayNode<Object> node4=new TwoWayNode<Object>("test4");
//twoWayList.insertlast(node4);
twoWayList.insertfirst(node1);
twoWayList.insertlast(node2);
twoWayList.insertfirst(node3);
twoWayList.insertinto(node2, node4);
twoWayList.diplay();
//twoWayList.deletehead();
//twoWayList.deltetail();
twoWayList.deletenode(node2);
twoWayList.diplay();
}
}
最近在复习数据结构,看了链表的操作,但是书上是用C实现的,我本人用java实现了双向
链表的操作,现在贴出代码与大家分享一下:
package com.qgmobile.list;
/**
* 双向链表节点
* @author yangchuxi
*
*/
public class TwoWayNode<Object> {
public Object t;
//后继
public TwoWayNode<Object> next;
//前驱
public TwoWayNode<Object> pre;
public TwoWayNode(Object t){
this.t=t;
}
public String toString(){
return t.toString();
}
}
package com.qgmobile.list;
/**
* 对双向链表的操作
* @author yangchuxi
*
*/
public class TwoWayList {
private TwoWayNode<Object> head;
private int size;
/**
* 链表的初始化
*/
public TwoWayList() {
size=0;
//表头不放数据
head=new TwoWayNode<Object>(null);
head.next=null;
head.pre=null;
}
//插入到链表前段(表头之后)
public boolean insertfirst(TwoWayNode<Object> tn){
if(tn!=null){
//首先要先把插入节点的前驱和后继搞定先,注意第二句和第三局的顺序
if(head.next!=null){
tn.pre=head;
tn.next=head.next;
head.next.pre=tn;
head.next=tn;
}else{
head.next=tn;
tn.pre=head;
}
size++;
return true;
}
return false;
}
//插入到链表的末尾
public boolean insertlast(TwoWayNode<Object> tn){
if(tn!=null){
TwoWayNode<Object> current=head;
while(current.next!=null){
current=current.next;
}
current.next=tn;
tn.pre=current;
size++;
return true;
}
return false;
}
// 在指定节点后添加节点
public boolean insertinto(TwoWayNode<Object> tn1,TwoWayNode<Object> tn2){
if(tn1!=null && tn2!=null){
//首先遍历这个链表,看看有没有等于tn1
TwoWayNode<Object> current=head;
while(current!=tn1){
current=current.next;
if(current==null){
return false;
}
}
if(tn1==head){
insertfirst(tn2);
size++;
return true;
}
if(current.next==null){
insertlast(tn2);
size++;
return true;
}
tn2.pre=current;
tn2.next=current.next;
current.next.pre=tn2;
current.next=tn2;
size++;
return true;
}
return false;
}
//删除链表前端节点
public boolean deletehead(){
if(head.next!=null){
TwoWayNode<Object> current=head.next;
head.next=current.next;
current.next.pre=head;
current=null;
size--;
return true;
}
return false;
}
// 删除尾节点
public void deltetail(){
TwoWayNode<Object> current=head;
while(current.next!=null){
current=current.next;
}
TwoWayNode<Object> pre=current.pre;
pre.next=null;
current=null;
size--;
}
//删除指定节点
public boolean deletenode(TwoWayNode<Object> tn){
if(tn!=null){
TwoWayNode<Object> current=head;
while(current!=tn){
current=current.next;
if(current==null){
return false;
}
}
if(tn==head.next){
return deletehead();
}
//保存与tn相等的值
TwoWayNode<Object> acurrent=current;
while(current.next!=null){
current=current.next;
}
if(current==tn){
deltetail();
return true;
}
TwoWayNode<Object> pre=acurrent.pre;
TwoWayNode<Object> next=acurrent.next;
pre.next=next;
next.pre=pre;
acurrent=null;
size--;
return true;
}
return false;
}
// 链表长度
public int getSize() {
return size;
}
// 正序和反序遍历遍历链表并打印
public void diplay() {
TwoWayNode<Object> current = head.next;
TwoWayNode<Object> tail=null;
System.out.println("正序输出:");
while (current != null) {
System.out.println(current.toString());
tail=current;
current = current.next;
}
// System.out.println("tail"+tail.toString());
System.out.println("反序输出:");
while(tail!=head){
if(tail!=null){
System.out.println(tail.toString());
}
tail=tail.pre;
}
}
public static void main(String[] args) {
TwoWayList twoWayList=new TwoWayList();
TwoWayNode<Object> node1=new TwoWayNode<Object>("test1");
TwoWayNode<Object> node2=new TwoWayNode<Object>("test2");
TwoWayNode<Object> node3=new TwoWayNode<Object>("test3");
TwoWayNode<Object> node4=new TwoWayNode<Object>("test4");
//twoWayList.insertlast(node4);
twoWayList.insertfirst(node1);
twoWayList.insertlast(node2);
twoWayList.insertfirst(node3);
twoWayList.insertinto(node2, node4);
twoWayList.diplay();
//twoWayList.deletehead();
//twoWayList.deltetail();
twoWayList.deletenode(node2);
twoWayList.diplay();
}
}
相关文章推荐
- 用java实现的双向链表增加删除修改操作
- Java实现双向链表的基本操作
- java实现双向链表的操作
- java中双向链表的增、删、查操作
- java 操作双向链表
- 双向链表的操作 模板的特化
- C语言实现双向链表的基本操作
- 2007.08.22双向链表(前插操作,删除操作)
- 双向链表基本操作
- Java语言中链表和双向链表
- java双向链表的实现(部分功能)
- JAVA实现双向链表
- Java语言中链表和双向链表
- 双向链表边的基本操作
- Java语言中链表和双向链表
- C语言实现双向链表的基本操作
- 算法(05):基本链表的操作及双向链表
- java 语言中链表和双向链表的实现!
- Java语言中链表和双向链表的实现-Java基础-Java-编程开发
- 双向链表的基本操作