用链表实现两个链表的合并,按照升序合并,最后并没有输出,不知道哪里出了错,谢谢帮忙啊
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();
}
}
}
不知道哪里出了错,没有预期的结果出现