下面是我自己用C#写的单链表合并,但是得不到结果,请各位大侠帮帮忙,小弟在此谢过了!第一在这里提问,请大家多多关照
using System;
namespace Exercise5dian47
{
class Node
{
public int key;
public string name;
public Node next;
}
class Program
{
Node START;
public Program()
{
START = null;
}
public void Addnode()//新建链表,这个地方没错可以不要看
{
int x; string Name;
x = Convert.ToInt32(Console.ReadLine());
Name = Console.ReadLine();
Node newnode = new Node();
newnode.key = x;
newnode.name = Name;
if (START == null || START.key > newnode.key)
{
newnode.next = START;
START = newnode;
return;
}
Node PREVIOUS, CURRENT;
PREVIOUS = START;
CURRENT = START;
while ((CURRENT != null) && (x >= CURRENT.key))
{
PREVIOUS = CURRENT;
CURRENT = CURRENT.next;
}
newnode.next = CURRENT;
PREVIOUS.next = newnode;
}
public void mergelist(Node list1,Node list2)//合并链表,错误很可能就在这个里面
{
Node current1 = list1;
Node current2 = list2;
Node current;
current=START;
while (current1 != null && current2 != null)
{
if (current1.key < current2.key)
{
current = current1;
current1 = current1.next;
}
else
{
current = current2;
current2 = current2.next;
}
current = current.next;
}
while (current1 != null)
{
current = current1;
current1 = current1.next;
current = current.next;
}
while (current2 != null)
{
current = current2;
current2 = current2.next;
current = current.next;
}
}
public void Display()//显示链表
{
Node currentnode;
currentnode = START;
Console.WriteLine("The list is : ");
while (currentnode != null)
{
Console.WriteLine(currentnode.key + " " + currentnode.name);
currentnode = currentnode.next;
}
}
static void Main(string[] args)
{
Program obj1 = new Program();
Program obj2 = new Program();
Program obj3 = new Program();
Console.WriteLine("How many students do you want to have in node 1 ?");
int times1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the {0} xh and name", times1);
for (int i = 0; i < times1; i++)
{
obj1.Addnode();
}
Console.WriteLine("How many students do you want to have in node 2 ?");
int times2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the {0} xh and name", times2);
for (int i = 0; i < times2; i++)
{
obj2.Addnode();
}
obj3.mergelist(obj1.START, obj2.START);
obj3.Display();
Console.ReadLine();
}
}
}
using System;
namespace Exercise5dian47
{
class Node
{
public int key;
public string name;
public Node next;
}
class Program
{
Node START;
public Program()
{
START = null;
}
public void Addnode()//新建链表,这个地方没错可以不要看
{
int x; string Name;
x = Convert.ToInt32(Console.ReadLine());
Name = Console.ReadLine();
Node newnode = new Node();
newnode.key = x;
newnode.name = Name;
if (START == null || START.key > newnode.key)
{
newnode.next = START;
START = newnode;
return;
}
Node PREVIOUS, CURRENT;
PREVIOUS = START;
CURRENT = START;
while ((CURRENT != null) && (x >= CURRENT.key))
{
PREVIOUS = CURRENT;
CURRENT = CURRENT.next;
}
newnode.next = CURRENT;
PREVIOUS.next = newnode;
}
public void mergelist(Node list1,Node list2)//合并链表,错误很可能就在这个里面
{
Node current1 = list1;
Node current2 = list2;
Node current;
current=START;
while (current1 != null && current2 != null)
{
if (current1.key < current2.key)
{
current = current1;
current1 = current1.next;
}
else
{
current = current2;
current2 = current2.next;
}
current = current.next;
}
while (current1 != null)
{
current = current1;
current1 = current1.next;
current = current.next;
}
while (current2 != null)
{
current = current2;
current2 = current2.next;
current = current.next;
}
}
public void Display()//显示链表
{
Node currentnode;
currentnode = START;
Console.WriteLine("The list is : ");
while (currentnode != null)
{
Console.WriteLine(currentnode.key + " " + currentnode.name);
currentnode = currentnode.next;
}
}
static void Main(string[] args)
{
Program obj1 = new Program();
Program obj2 = new Program();
Program obj3 = new Program();
Console.WriteLine("How many students do you want to have in node 1 ?");
int times1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the {0} xh and name", times1);
for (int i = 0; i < times1; i++)
{
obj1.Addnode();
}
Console.WriteLine("How many students do you want to have in node 2 ?");
int times2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the {0} xh and name", times2);
for (int i = 0; i < times2; i++)
{
obj2.Addnode();
}
obj3.mergelist(obj1.START, obj2.START);
obj3.Display();
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;using System;
namespace Exercise5dian47
{
class Node
{
public int key;
public string name;
public Node next;
}
class Program
{
Node START;
public Program()
{
START = null;
}
public void Addnode()//新建链表,这个地方没错可以不要看
{
int x; string Name;
x = Convert.ToInt32(Console.ReadLine());
Name = Console.ReadLine();
Node newnode = new Node();
newnode.key = x;
newnode.name = Name;
if (START == null || START.key > newnode.key)
{
newnode.next = START;
START = newnode;
return;
}
Node PREVIOUS, CURRENT;
PREVIOUS = START;
CURRENT = START;
while ((CURRENT != null) && (x >= CURRENT.key))
{
PREVIOUS = CURRENT;
CURRENT = CURRENT.next;
}
newnode.next = CURRENT;
PREVIOUS.next = newnode;
}
public Node mergelist(Node list1, Node list2)//合并链表,错误很可能就在这个里面
{
Node current1 = list1;
Node current2 = list2;
Node current = list1;
while (current1 != null && current2 != null)
{
if (current1.key < current2.key)
{
current = current1;
current1 = current1.next;
}
else
{
current = current2;
current2 = current2.next;
}
// current = current.next;
}
if (current1 != null)
{
current.next = current1;
}
if (current2 != null)
{
current.next = current2;
}
return current;
}
public void Display(Node node)//显示链表
{
Node currentnode = node;
// currentnode = START;
Console.WriteLine("The list is : ");
while (currentnode != null)
{
Console.WriteLine(currentnode.key + " " + currentnode.name);
currentnode = currentnode.next;
}
}
static void Main(string[] args)
{
Program obj1 = new Program();
Program obj2 = new Program();
Program obj3 = new Program();
Console.WriteLine("How many students do you want to have in node 1 ?");
int times1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the {0} xh and name", times1);
for (int i = 0; i < times1; i++)
{
obj1.Addnode();
}
Console.WriteLine("How many students do you want to have in node 2 ?");
int times2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the {0} xh and name", times2);
for (int i = 0; i < times2; i++)
{
obj2.Addnode();
}
Node towNodes = obj3.mergelist(obj1.START, obj2.START);
obj3.Display(towNodes);
Console.ReadLine();
}
}
}
你出的问题太多了,我给你修改了一下
using System;
namespace Exercise5dian47
{
class Node
{
public int key;
public string name;
public Node next;
}
class Program
{
Node START;
public Program()
{
START = null;
}
public void Addnode()//新建链表,这个地方没错可以不要看
{
int x; string Name;
x = Convert.ToInt32(Console.ReadLine());
Name = Console.ReadLine();
Node newnode = new Node();
newnode.key = x;
newnode.name = Name;
if (START == null || START.key > newnode.key)
{
newnode.next = START;
START = newnode;
return;
}
Node PREVIOUS, CURRENT;
PREVIOUS = START;
CURRENT = START;
while ((CURRENT != null) && (x >= CURRENT.key))
{
PREVIOUS = CURRENT;
CURRENT = CURRENT.next;
}
newnode.next = CURRENT;
PREVIOUS.next = newnode;
}
public void mergelist(Node list1,Node list2)//合并链表,错误很可能就在这个里面
{
Node current1 = list1;
Node current2 = list2;
Node current;
current=START;
while (current1 != null && current2 != null)
{
if (current1.key < current2.key)
{
current = current1;
current1 = current1.next;
}
else
{
current = current2;
current2 = current2.next;
}
current = current.next;//这个地方,如果其中一个是空的话,current.next就是空的了,所以,所 //current是空的
}
while (current1 != null)//这个地方完全不用循环的,直接用if判断一下,把剩余链表的第一个节点加上就行了
{
current = current1;
current1 = current1.next;
current = current.next;
}
while (current2 != null)//同上
{
current = current2;
current2 = current2.next;
current = current.next;
}
}
public void Display()//显示链表
{
Node currentnode;
currentnode = START;//这个地方的start是空的,所以就显示不出来了,这个是obj3的start,你没给初始化
Console.WriteLine("The list is : ");
while (currentnode != null)
{
Console.WriteLine(currentnode.key + " " + currentnode.name);
currentnode = currentnode.next;
}
}
static void Main(string[] args)
{
Program obj1 = new Program();
Program obj2 = new Program();
Program obj3 = new Program();
Console.WriteLine("How many students do you want to have in node 1 ?");
int times1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the {0} xh and name", times1);
for (int i = 0; i < times1; i++)
{
obj1.Addnode();
}
Console.WriteLine("How many students do you want to have in node 2 ?");
int times2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the {0} xh and name", times2);
for (int i = 0; i < times2; i++)
{
obj2.Addnode();
}
obj3.mergelist(obj1.START, obj2.START);
obj3.Display();//obj3的START是空的,所以你按照你的,永远也显示不出来,所以我就给它加了一个参数
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;using System;
namespace Exercise5dian47
{
class Node
{
public int key;
public string name;
public Node next;
}
class Program
{
Node START;
public Program()
{
START = null;
}
public void Addnode()//新建链表,这个地方没错可以不要看
{
int x; string Name;
x = Convert.ToInt32(Console.ReadLine());
Name = Console.ReadLine();
Node newnode = new Node();
newnode.key = x;
newnode.name = Name;
if (START == null || START.key > newnode.key)
{
newnode.next = START;
START = newnode;
return;
}
Node PREVIOUS, CURRENT;
PREVIOUS = START;
CURRENT = START;
while ((CURRENT != null) && (x >= CURRENT.key))
{
PREVIOUS = CURRENT;
CURRENT = CURRENT.next;
}
newnode.next = CURRENT;
PREVIOUS.next = newnode;
}
public Node mergelist(Node list1, Node list2)//合并链表,错误很可能就在这个里面
{
Node current1 ;
Node current2; Node start;
Node current ;
if (list1.key > list2.key)
{
start = list2;
current2 = list2.next;
current1 = list1; }
else
{
start = list1;
current1 = list1.next;
current2 = list2;
}
current = start;
while (current1 != null && current2 != null)
{
if (current1.key < current2.key)
{
current.next = current1;
current1 = current1.next;
}
else
{
current.next = current2;
current2 = current2.next;
}
current = current.next;
}
if (current1 != null)
{
current.next = current1;
}
if (current2 != null)
{ current.next = current2;
}
return start;
}
public void Display(Node node)//显示链表
{
Node currentnode = node;
// currentnode = START;
Console.WriteLine("The list is : ");
while (currentnode != null)
{
Console.WriteLine(currentnode.key + " " + currentnode.name);
currentnode = currentnode.next;
}
}
static void Main(string[] args)
{
Program obj1 = new Program();
Program obj2 = new Program();
Program obj3 = new Program();
Console.WriteLine("How many students do you want to have in node 1 ?");
int times1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the {0} xh and name", times1);
for (int i = 0; i < times1; i++)
{
obj1.Addnode();
}
Console.WriteLine("How many students do you want to have in node 2 ?");
int times2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the {0} xh and name", times2);
for (int i = 0; i < times2; i++)
{
obj2.Addnode();
}
Node towNodes = obj3.mergelist(obj1.START, obj2.START);
obj3.Display(towNodes);
Console.ReadLine();
}
}
}
以前那个有点小错误,这个是对的