问一道简单的线性链表编程题
编制一个在线性链表中数据域值为a的结点之后,插入一个新结点的程序,若原链表中无数据域为a的结点,则把新结点插入表尾。设新结点的数据域为x。 最好是调试过的. class LNode
{
int num;
LNode next;
LNode(int num)
{
this.num=num;
this.next=null;
}
}
class LinkedList
{
LNode star;
LinkedList(int[] num)
{
star=null;
LNode cur=null;
for(int i=0;i<num.length;i++)
{
if (cur==null)
{
cur=new LNode(num[i]);
star=cur;
}
else
{
cur.next = new LNode(num[i]);
cur=cur.next;
}
}
}
public void insert(int a,int x)
{
LNode cur=star,tmp;
while(!(cur==null))
{
if(cur.num==a)
{
tmp=cur.next;
cur.next=new LNode(x);
cur.next.next=tmp;
return;
}
else
cur=cur.next;
}
}
public void show()
{
LNode cur=star;
while(!(cur==null))
{
System.out.print(cur.num+",");
cur=cur.next;
}
System.out.println("");
}
public static void main(String[] args)
{
int[] num = new int[args.length];
for(int n=0;n<args.length;n++)num[n]=Integer.parseInt(args[n]);
LinkedList l=new LinkedList(num);
l.show();
l.insert(11,13);
l.show();
}
}
- 相关内容
- 最近更新
- ·数组问题
- ·C++中怎样将char转换成int
- ·C++6.0 编译器下载地址
- ·一道C程序设计题关于字符串连接
- ·我要C++编译器的下载地址~~~
- ·C#语言中的,继承里的get,set访问..
- ·请问C/C++有那些存储区,各自功能..
- ·T-flash卡的问题
- ·又是诺基亚QD啊!
- ·K750有什么优.缺点?
添加到百度搜藏