单链表排序问题(C#)

shixiangzb007 2008-10-10 03:45:29
各位大虾,最近遇见一个把我雷倒的问题,关于单链表排序的,但是我不是学计算机的,所以对数据结构不清楚,可是在网上也没有找到合适的范例,还请各位大虾多多指教。
题目:一个单链表里面存储着整形数据,但是数据是无序的。要求写一个方法把这个单链表按升序排列。
public class LinkedList
{
public Node node;
public Element data;
}

先谢谢各位了。
...全文
491 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
Eagle_ice 2008-10-12
  • 打赏
  • 举报
回复
冒泡排序,控制台应用程序,链表自己实现吧...

using System;

class Sort
{
static void Main()
{
int[] a={3,2,5,4,10,6,8,1};
Console.WriteLine("排序前顺序:\n");
for(int i=0;i<a.Length;i++)
{
Console.Write("{0} ",a[i]);
}
Console.WriteLine();
for(int i=0;i<a.Length-1;i++)
{
for(int j=0;j<a.Length-1-i;j++)
{
int temp;
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
Console.WriteLine("排序后的顺序:\n");
for(int i=0;i<a.Length;i++)
{
Console.Write("{0} ",a[i]);
}

}
}
Eagle_ice 2008-10-12
  • 打赏
  • 举报
回复
楼上的不错...
wartim 2008-10-12
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 shixiangzb007 的回复:]

你好,wartim,
我感刚运行了你的代码,可是现实的结果只有一个数字“7”,请再次指教。谢谢。
to:Eagle_ice
谢谢你的回复,可是我们想讨论的是LinkedList的排序,请用LinkedList的模型回复,谢谢
[/Quote]

不会啊,我运行的结果是 6 6 7 10 34 52,一定是你什么地方写错了
先不要在你的项目里去改,新建个项目WindowsFormsApplication34
直接把代码复制过去就可以看到结果了
孤剑 2008-10-12
  • 打赏
  • 举报
回复
shixiangzb007

你用啥运行的?
这个只是一个思路,并不是完整的代码,请自己完善。
shixiangzb007 2008-10-12
  • 打赏
  • 举报
回复
小弟在网上找到一篇文章,请大家一起研究。
我会将分散发给wartim和AloneSword,同时谢谢各位的参与。
生命不熄,代码不止。

namespace testSortLinklist
{
public class Employee : IComparable<Employee>
{
private string name;
public Employee(string name)
{
this.name = name;
}
public override string ToString()
{
return this.name;
}

public int CompareTo(Employee rhs)
{
return this.name.CompareTo(rhs.name);
}
public bool Equals(Employee rhs)
{
return this.name == rhs.name;
}
}

public class Node<T> : IComparable<Node<T>> where T : IComparable<T>
{
private T data;
private Node<T> next = null;
private Node<T> prev = null;

public Node(T data)
{
this.data = data;
}
public T Data
{
get
{
return this.data;
}
}
public Node<T> Next
{
get
{
return this.next;
}
}

public int CompareTo(Node<T> rhs)
{
return data.CompareTo(rhs.data);
}
public bool Equals(Node<T> rhs)
{
return this.data.Equals(rhs.data);
}

public Node<T> Add(Node<T> newNode)
{
if (this.CompareTo(newNode) > 0)
{
newNode.next = this;
if (this.prev != null)
{
this.prev.next = newNode;
newNode.prev = this.prev;
}
this.prev = newNode;
return newNode;
}
else
{
if (this.next != null)
{
this.next.Add(newNode);
}
else
{
this.next = newNode;
newNode.prev = this;
}
return this;
}
}

public override string ToString()
{
string output = data.ToString();
if (next != null)
output += ", " + next.ToString();
return output;
}
}

public class LinkedList<T> where T : IComparable<T>
{
private Node<T> headNode = null;

public T this[int index]
{
get
{
int ctr = 0;
Node<T> node = headNode;
while (node != null && ctr <= index)
{
if (ctr == index)
{
return node.Data;
}
else
{
node = node.Next;
}
++ctr;
}
throw new ArgumentOutOfRangeException();
}
}

public LinkedList()
{
}

public void Add(T data)
{
if (headNode == null)
{
headNode = new Node<T>(data);
}
else
{
headNode =headNode.Add (new Node<T>(data));
}
}

public override string ToString()
{
if (this.headNode != null)
return this.headNode.ToString();
else
return string.Empty;
}
}

class Program
{
static void Main(string[] args)
{
Run();
}
public static void Run()
{
LinkedList<int> myLinkedList = new LinkedList<int>();
Random rand = new Random();
Console.Write("Adding: ");
for (int i = 0; i < 10; i++)
{
int nextInt = rand.Next(30);
Console.Write("{0} ", nextInt);
myLinkedList.Add(nextInt);
}

LinkedList<Employee> employees = new LinkedList<Employee>();
employees.Add(new Employee ("John"));
employees.Add(new Employee("Paul"));
employees.Add(new Employee("George"));
employees.Add(new Employee("Ringo"));

Console.WriteLine(" Retrieving collections...");

Console.WriteLine("Integers: " + myLinkedList);
Console.WriteLine("Employees: " + employees);

Console.ReadKey();
}
}
}
shixiangzb007 2008-10-12
  • 打赏
  • 举报
回复
to:AloneSword
这个代码运行不过去啊,请继续指教
孤剑 2008-10-12
  • 打赏
  • 举报
回复
最简单的实现:
namespace COM.ZTE.RUCL.PTag.Test.Cmd
{
class Program
{
static void Main(string[] args)
{
#region 构建结构
LinkedList LL;

LinkedList[] Temp = new LinkedList[6];
// 给 LL 赋点值。。。
LL = new LinkedList();
LL.data = 10;
Temp[0] = LL;

LL = new LinkedList();
LL.data = 34;
LL.node = Temp[0];
Temp[1] = LL;

LL = new LinkedList();
LL.data = 6;
LL.node = Temp[1];
Temp[2] = LL;

LL = new LinkedList();
LL.data = 52;
LL.node = Temp[2];
Temp[3] = LL;

LL = new LinkedList();
LL.data = 6;
LL.node = Temp[3];
Temp[4] = LL;

LL = new LinkedList();
LL.data = 7;
LL.node = Temp[4];
Temp[5] = LL;
#endregion

ArrayList list = new ArrayList();
// 构建排序二叉树
for (int i = 0; i < 6; i++)
list.Add(Temp[i]);

// 【重点】排序 【一句话搞定】
list.Sort(new LinkedListSort());

Console.WriteLine("over");

Console.Read();

}
}

/// <summary>
/// 实体类
/// </summary>
public class LinkedList
{
public LinkedList node = null;
public int data;
}

/// <summary>
/// 排序类
/// </summary>
public class LinkedListSort : IComparer
{
#region IComparer Members

public int Compare(object x, object y)
{
LinkedList lX = (LinkedList)x;
LinkedList lY = (LinkedList)y;
if (lX.data > lY.data)
{
return 1;
}
else if (lX.data < lY.data)
{
return -1;
}
return 0;
//throw new NotImplementedException();
}

#endregion
}


}


如果你是想学习算法的话,推荐你看一下二叉树的排序,那个比较快。很多地方都是用那个算法的。
shixiangzb007 2008-10-12
  • 打赏
  • 举报
回复

你好,wartim,
我感刚运行了你的代码,可是现实的结果只有一个数字“7”,请再次指教。谢谢。
to:Eagle_ice
谢谢你的回复,可是我们想讨论的是LinkedList的排序,请用LinkedList的模型回复,谢谢
wartim 2008-10-11
  • 打赏
  • 举报
回复

namespace WindowsFormsApplication34
{
public partial class Form1 : Form
{
public class LinkedList
{
public LinkedList node = null;
public int data;
}

class Tree
{
public Tree Left = null;
public Tree Right = null;
public int Value;
}

LinkedList LL, Head;
Tree T = null;

public Form1()
{
InitializeComponent();
LinkedList Temp = null;

// 给 LL 赋点值。。。
LL = new LinkedList();
LL.data = 10;
Temp = LL;
LL = new LinkedList();
LL.data = 34;
LL.node = Temp;
Temp = LL;
LL = new LinkedList();
LL.data = 6;
LL.node = Temp;
Temp = LL;
LL = new LinkedList();
LL.data = 52;
LL.node = Temp;
Temp = LL;
LL = new LinkedList();
LL.data = 6;
LL.node = Temp;
Temp = LL;
LL = new LinkedList();
LL.data = 7;
LL.node = Temp;
Head = LL;

// 构建排序二叉树
for (LL = Head; LL != null; LL = LL.node)
AddToTree(ref T, LL.data);

// 输出排序结果
String S = String.Empty;
GetResult(ref T, ref S);
MessageBox.Show(S);
}

void AddToTree(ref Tree T, int value)
{
if (T == null)
{
T = new Tree();
T.Value = value;
}
else if (value < T.Value) // 比当前值小于的往左
AddToTree(ref T.Left, value);
else if (value >= T.Value) // 比当前值大于等于的往右
AddToTree(ref T.Right, value);
}

void GetResult(ref Tree T, ref String S)
{
if (T == null)
return; // 已经没有节点了
if (T.Left != null)
GetResult(ref T.Left, ref S); // 向左走
S += T.Value.ToString() + " ";
if (T.Right != null)
GetResult(ref T.Right, ref S); // 向右走
}
}
}
shixiangzb007 2008-10-11
  • 打赏
  • 举报
回复
没有人可以帮帮我吗?
shixiangzb007 2008-10-10
  • 打赏
  • 举报
回复
老大,冒泡排序也好,二分法排序也好,我想要的是完整的代码,谢谢
韦大宝110 2008-10-10
  • 打赏
  • 举报
回复
冒泡排序???
shixiangzb007 2008-10-10
  • 打赏
  • 举报
回复
我倒是明白这么做,但是我不会写具体的代码,请把代码附上,谢谢
leez0301 2008-10-10
  • 打赏
  • 举报
回复
从头到尾比较吧,依次把较大的或者较小的向后移动就行了,每次改变他的后继结点就行啊!
shixiangzb007 2008-10-10
  • 打赏
  • 举报
回复
你这样转出来的是ArrayList的输出,而不是LinkedList class的输出。谢谢,请继续
zzyhuian06142 2008-10-10
  • 打赏
  • 举报
回复
虽然不明白你的意思
PS:转成ArrayList,再用Sort可以自动排列

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧