本文实例为大家分享了java实现单链表、双向链表的相关代码,供大家参考,具体内容如下
java实现单链表:
package code;
class node
{
node next;
int data;
public node(int data)
{
this.data=data;
}
}
class linklist
{
node first;
//头部
public linklist()
{
this.first=null;
}
public void addnode(node no)
{
no.next=first;
first=no;//在头部添加
}
public void delectnode()
{
node n=first.next;
first=null;
first=n;//在头部删除
}
//删除指定位置
public int number()
{
int count=1;
//查看有多少元素
node nd=first;
while(nd.next!=null)
{
nd=nd.next;
count++;
}
return count;
}
public void delectexact(int n)
{
//删除指定位置
if(n>1)
{
int count=1;
node de=first;
while(count<n-1)
{
de=de.next;
count++;
}
de.next=de.next.next;
}
else
first=first.next;
}
public void addexact(int n,node nd)
{
if(n>1)//添加指定位置
{
int count=1;
node de=first;
while(count<n-1)
{
de=de.next;
count++;
}
nd.next=de.next;
de.next=nd;
}
else
first=first.next;
}
public int findnode(int n)
{
int count=1;//查找一个数对应的位置
node de=first;
while(de.data!=n)
{
de=de.next;
count++;
if(de==null)
{
return -1;
}
}
return count;
}
public void print()
{
node no=first;//打印所有
while(no!=null)
{
system.out.println(no.data);
no=no.next;
}
}
}
public class textnode
{
public static void main(string[] args)
{
linklist ll=new linklist();
ll.addnode(new node(12));
ll.addnode(new node(15));
ll.addnode(new node(18));
ll.addnode(new node(19));
ll.addnode(new node(20));
/*system.out.println(ll.first.data);
ll.delectnode();
system.out.println(ll.first.data);*/
system.out.println(ll.number());
ll.delectexact(3);
ll.addexact(3, new node(100));
system.out.println(ll.number());
// ll.print();
system.out.println(ll.findnode(112));
}
}
java实现双向链表:
public class doublelink
{
public static void main(string[]args)
{
node2 no=new node2(5);
no.addleft(new node2(6));
no.addright(new node2(7));
/*no.print();
no.print2();*/
no.addexact2(1, new node2(8));
no.print();
system.out.println("--------------");
no.print2();
}
}
class node2
{
public node2 first;
public node2 end;
public node2 left;
public node2 right;
int data=0;
public node2(int n)
{
first=this;
end=this;
first.data=n;
}
//从头部添加
public void addleft(node2 before)
{
first.left=before;
before.right=first;
first=before;
}
//从尾部添加
public void addright(node2 after)
{
end.right=after;
after.left=end;
end=after;
}
//插入正数(第三声)的第几个
public void addexact(int n,node2 no)
{
int count=0;
if(n==0)
{
addleft(no);
}
else
{
node2 f=first;
while(true)
{
f=f.right;
count++;
if(count==n)
{
//此处为四个指针的指向的变化
no.left=f.left;
f.left.right=no;
// first.left=no;
no.right=f;
f.left=no;
break;
}
}
}
}
//插入倒数的第几个
public void addexact2(int n,node2 no)
{
int count=0;
if(n==0)
{
addright(no);
}
else
{
node2 f=end;
while(true)
{
f=f.left;
count++;
if(count==n)
{
no.left=f;
no.right=f.right;
f.right.left=no;
f.right=no;
break;
}
}
}
}
//正序遍历
public void print()
{
system.out.println(first.data);
while(first.right!=null)
{
system.out.println(first.right.data);
first=first.right;
}
// system.out.println(end.data);
}
//倒序遍历
public void print2()
{
system.out.println(end.data);
while(end.left!=null)
{
system.out.println(end.left.data);
end=end.left;
}
}
}
/*值得注意的是,每一次插入一个新的对象的时候,需要注意指针指向的改变。
首先是这个新的对象两边的指向(左和右),其次是时左边的对象向右的指向
和右边对象向左的指向。
这四个指针的指向必须正确,否则可能导致正序或者倒序遍历无法实现。
*/
/*对比单链表,单链表只能从一个方向遍历,因为只有一个头,而双向链表,有头和尾,可以从
* 头遍历,也可以从尾遍历,而且其中一个对象因为有两个方向的指针,所以他可以获得左边的
* 对象也可以获得右边的对象。
* 但是单链表的话,因为只有一个方向,所以只能向左或右。添加对象的时候,双向也可以从头添加,也可以从尾添加。
* 如果单链表要实现两个方向添加比较难得,或者说不行,因为他只有向左或向右的一个方向的指针
* 而双向链表每个对象都有两个方向的指针没这样更灵活,但是这同样有缺点,因为这样的话每个对象
* 都会包含两个指针,这同样内存会消耗更多。
*
* */
以上就是本文的全部内容,希望对大家学习java程序设计有所帮助。
更多java实现单链表、双向链表。