| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 215 人关注过本帖
标题:不知道哪里出了错,没有预期的结果出现
只看楼主 加入收藏
wxzyn123
Rank: 1
来 自:江苏
等 级:新手上路
帖 子:50
专家分:0
注 册:2007-9-15
收藏
 问题点数:0 回复次数:1 
不知道哪里出了错,没有预期的结果出现

用链表实现两个链表的合并,按照升序合并,最后并没有输出,不知道哪里出了错,谢谢帮忙啊


namespace chapter5_e1
{
class Node
{
public int rollnumber ;
public string name;
public Node next;
}
class List
{
Node START;
Node next;
public List()
{
START = null;
}
public void addnode()
{
int rollno; string nm;
Console.WriteLine("enter the rollnumber :");
rollno = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("enter the name :");
nm = Console.ReadLine();
Node newnode = new Node();
newnode.rollnumber = rollno;
newnode.name = nm;
if (START == null || rollno <= START.rollnumber)
{
if ((START != null) && (rollno == START.rollnumber))
{
return;
}
newnode.next = START;
START = newnode;
return;
}
Node previous, current;
previous = START;
current = START;
while ((current != null) && (rollno >= current.rollnumber))
{
previous = current; current = current.next;
}
newnode.next = current;
previous.next = newnode;

}
public void traverse()
{
if (empty())
{
Console.WriteLine("\n list is empty");
return;
}
else
{
Console.WriteLine("the record in list are:\n");
Node currentnode;
for (currentnode = START; currentnode != null; currentnode = currentnode.next)
{
Console.WriteLine(currentnode.rollnumber + " " + currentnode.name + " ");
}
}
}
public Node merge(ref List list1, ref List list2, ref List list3)
{
Node pa;
Node pb;
Node pc; Node r=null;
pa = list1.START; pb = list2.START; pc = list3.START;
// r = pc;
while (pa != null && pb != null)
{
if (pa.rollnumber == pb.rollnumber)
{
pc.rollnumber = pa.rollnumber;
pc.name = pa.name;
pa = pa.next;
// r.next = pc; r = pc;
// pc = pc.next;
//return;
r = pc;
}
else
if (pa.rollnumber > pb.rollnumber)
{
pc.rollnumber = pb.rollnumber;
pc.name = pb.name; pb = pb.next;
//r.next = pc; r = pc;
// pc = pc.next;
r = pc;
}
else
{
pc = pa; pa = pa.next;
//r.next = pc; r = pc;
//pc = pc.next;
r = pc;
}
}
if (pa == null)
pc.next = pb;
if (pb == null)
pc.next = pa;
return r;
}

public bool empty()
{
if (START == null) return true;
else return false;
}
static void Main(string[] args)
{
char ch1='y';
List l1 = new List();
while (ch1 == 'y')
{
l1.addnode();
Console.WriteLine("Do you want to continue?(y/n)");
ch1 = Convert.ToChar(Console.ReadLine());
}
l1.traverse();
char ch2 = 'y';
List l2 = new List();
while (ch2 == 'y')
{
l2.addnode();
Console.WriteLine("Do you want to continue?(y/n)");
ch2 = Convert.ToChar(Console.ReadLine());
}
l2.traverse();
List l3 = new List();
l3.merge(ref l1,ref l2,ref l3);
Console.ReadLine();
}
}
}

搜索更多相关主题的帖子: 结果 
2007-10-20 19:19
wxzyn123
Rank: 1
来 自:江苏
等 级:新手上路
帖 子:50
专家分:0
注 册:2007-9-15
收藏
得分:0 
关键是merge函数没有输出,其它的函数都正确

请高手帮忙看一下merge函数就行了
2007-10-20 19:20
快速回复:不知道哪里出了错,没有预期的结果出现
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016910 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved