您的位置 电脑知识爱好者 >> 编程知识 >> 问一道简单的线性链表编程题

问一道简单的线性链表编程题

电脑知识爱好者互联网本站整理2007-8-26 14:06:14
知识重点:编制一个在线性链表中数据域值为a的结点之后,插入一个新结点的程序,若原链表中无数据域为a的结点,则把新结点插入表尾。设新结点的数据域为x。 最好是调试过的. class LNode { int num; ..

编制一个在线性链表中数据域值为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();

}

}

  • 在VC++ 6.0下调试通过。

    参与评论
    相关内容
  • 关于我们 | 隐私政策 | 站点地图 | 站长博客|京ICP备07025396号
    添加到百度搜藏 添加到百度搜藏 电脑知识爱好者Copyright ?2006-2008版权所有 我要啦免费统计