class Node { int value; Node prior; Node next; public Node() { value = int.MinValue; prior = null; next = null; } public Node(int value) { this.value = value; this.prior = null; this.next = null; } public int Value { get { return this.value; } set { this.value = value; } } public Node Prior { get { return this.prior; } set { this.prior = value; } } public Node Next { get { return this.next; } set { this.next = value; } } } class LinkList { Node head, last; int length; public LinkList() { head = new Node(); last = head; } public int Length { get { return this.length; } } public void Append(int value) { last.Next = new Node(value); last.Next.Prior = last; last = last.Next; length++; } private void Swap(Node n1, Node n2) { n1.Value = n1.Value + n2.Value; n2.Value = n1.Value - n2.Value; n1.Value = n1.Value - n2.Value; } public void Sort() { if (length < 2) return; for (Node q = last; q != head.Next; q = q.Prior) { bool swapped = false; for (Node p = head.Next; p != q; p = p.Next) { if (p.Value > p.Next.Value) { Swap(p, p.Next); swapped = true; } } if (!swapped) { break; } } } public IEnumerable<int> GetValues() { for (Node p = head.Next; p != null; p = p.Next) { yield return p.Value; } } } class Program { static void Main(string[] args) { LinkList linkList = new LinkList(); linkList.Append(1); linkList.Append(3); linkList.Append(5); linkList.Append(7); linkList.Append(9); linkList.Append(2); linkList.Append(4); linkList.Append(6); linkList.Append(8); linkList.Append(10); linkList.Sort(); foreach (int i in linkList.GetValues()) { Console.WriteLine(i); } } }
汗! 15楼写的程序和我11楼的一模一样,只不过sort写错了,没用泛型。
using System; using System.Collections.Generic; using System.Text;namespace 结构体 { public struct Sp { public string name; public string phoneNum; } class Program { static void Main(string[] args) { Sp[] s1 = new Sp[5]; s1[0].name = "chen"; s1[0].phoneNum = "42533chen"; s1[1].name = "xiang"; s1[1].phoneNum = "42533xiang"; s1[2].name = "li"; s1[2].phoneNum = "42533li"; s1[3].name = "ming"; s1[3].phoneNum = "42533ming"; s1[4].name = "feng"; s1[4].phoneNum = "42533feng"; GetData(s1); Console.ReadKey(); } static void GetData(Sp[] s1) { //冒泡排序 for (int i = 0; i < s1.Length-1; i++) { for (int j = i + 1; j < s1.Length; j++) { int biJiao; biJiao=string .Compare (s1[i].name,s1[j].name); if (biJiao > 0) { string tempName; string tempPhone; tempName = s1[i].name; tempPhone = s1[i].phoneNum; s1[i].name = s1[j].name; s1[i].phoneNum = s1[j].phoneNum; s1[j].name = tempName; s1[j].phoneNum = tempPhone; } } } for (int i = 0; i < s1.Length; i++) { Console.WriteLine("name is {0}, phonenum is {1}",s1[i].name ,s1 [i].phoneNum ); } } } } //还是泛型简单。sort()
if (biJiao > 0) { Sp temp = new Sp(); temp = s1[i]; s1 [i]=s1 [j]; s1[j] = temp; }
public string Name; public string Phone; public SP(string name, string phone) { Name = name; Phone = phone; }这是11楼代码中的几句,有个问题不太明白,用构造函数给类的成员变量赋值,类的成员变量是不是应该为私有的?
for(i=0;i<NUM;i++) printf("%s\t\t\t%s\n",man[i].name,man[i].phone);
}
[/code]
1.
class MainClass
{
static void Main()
{ SP[] sp = new SP[3];
sp[0].name = "c";
sp[0].phone = "003";
sp[1].name = "a";
sp[1].phone = "001";
sp[2].name = "b";
sp[2].phone = "002"; foreach (SP e in sp)
{
Console.WriteLine(e.name);
} Array.Sort<SP>(sp, new SortByName());
foreach (SP e in sp)
{
Console.WriteLine(e.name);
}
Console.ReadLine();
}
} struct SP
{
public string name;
public string phone;
}
class SortByName : IComparer<SP>
{
public int Compare(SP x, SP y)
{
return string.Compare(x.name, y.name);
} }
using System;
using System.Collections.Generic;class MainClass
{
static void Main()
{ SP[] array = new SP[3];
array[0].Name = "A";
array[0].Phone = "11111111";
array[1].Name = "C";
array[1].Phone = "22222222";
array[2].Name = "B";
array[2].Phone = "33333333";
getdata(array);
outdata(array);
Console.Read();
}
static void getdata(SP[] array)
{
Array.Sort<SP>(array);
}
static void outdata(SP[] array)
{
foreach (SP i in array)
Console.WriteLine(i);
}
}struct SP:IComparable<SP>
{
public string Name;
public string Phone;
public SP(string name, string phone)
{
Name = name;
Phone = phone;
} public int CompareTo(SP other)
{
return this.Name.CompareTo( other.Name);
}
public override string ToString()
{
return string.Format("Name is :{0}.\n\tPhone is:{1}\n",Name,Phone);
}
}
// Student Phone-number
struct SP : IComparable<SP>
{
// 为了方便起见,简要说明设计思路,这里不使用属性
public string Name;
public string PhoneNumber;
public SP(string name, string phoneNumber)
{
this.Name = name;
this.PhoneNumber = phoneNumber;
}
public int CompareTo(SP obj)
{
return this.Name.CompareTo(obj.Name);
}
public override string ToString()
{
return string.Format("Name: {0}, PhoneNumber: {1}", this.Name, this.PhoneNumber);
}
} class Program
{
static void Main(string[] args)
{
List<SP> spList = new List<SP>();
spList.Add(new SP("Mary", "67853495"));
spList.Add(new SP("Norton", "67343495"));
spList.Add(new SP("Jackson", "67857595"));
spList.Add(new SP("Stephen", "67854565"));
spList.Add(new SP("John", "67856195"));
spList.Add(new SP("Narow", "77853495"));
spList.Add(new SP("May", "68853495"));
spList.Add(new SP("Sam", "27853495"));
spList.Add(new SP("Sams", "57853395"));
spList.Add(new SP("Rooney", "57853495")); GetData(spList);
OutData(spList);
}
static void GetData(List<SP> spList)
{
spList.Sort();
}
static void OutData(List<SP> spList)
{
foreach (SP sp in spList)
{
Console.WriteLine(sp.ToString());
}
}
}
class Node
{
int value;
Node prior;
Node next;
public Node()
{
value = int.MinValue;
prior = null;
next = null;
}
public Node(int value)
{
this.value = value;
this.prior = null;
this.next = null;
}
public int Value
{
get
{
return this.value;
}
set
{
this.value = value;
}
}
public Node Prior
{
get
{
return this.prior;
}
set
{
this.prior = value;
}
}
public Node Next
{
get
{
return this.next;
}
set
{
this.next = value;
}
}
}
class LinkList
{
Node head, last;
int length;
public LinkList()
{
head = new Node();
last = head;
}
public int Length
{
get
{
return this.length;
}
}
public void Append(int value)
{
last.Next = new Node(value);
last.Next.Prior = last;
last = last.Next;
length++;
}
private void Swap(Node n1, Node n2)
{
n1.Value = n1.Value + n2.Value;
n2.Value = n1.Value - n2.Value;
n1.Value = n1.Value - n2.Value;
}
public void Sort()
{
if (length < 2) return; for (Node q = last; q != head.Next; q = q.Prior)
{
bool swapped = false;
for (Node p = head.Next; p != q; p = p.Next)
{
if (p.Value > p.Next.Value)
{
Swap(p, p.Next);
swapped = true;
}
}
if (!swapped)
{
break;
}
}
}
public IEnumerable<int> GetValues()
{
for (Node p = head.Next; p != null; p = p.Next)
{
yield return p.Value;
}
}
}
class Program
{
static void Main(string[] args)
{
LinkList linkList = new LinkList();
linkList.Append(1);
linkList.Append(3);
linkList.Append(5);
linkList.Append(7);
linkList.Append(9);
linkList.Append(2);
linkList.Append(4);
linkList.Append(6);
linkList.Append(8);
linkList.Append(10);
linkList.Sort();
foreach (int i in linkList.GetValues())
{
Console.WriteLine(i);
}
}
}
15楼写的程序和我11楼的一模一样,只不过sort写错了,没用泛型。
using System.Collections.Generic;
using System.Text;namespace 结构体
{
public struct Sp
{
public string name;
public string phoneNum;
}
class Program
{
static void Main(string[] args)
{
Sp[] s1 = new Sp[5];
s1[0].name = "chen";
s1[0].phoneNum = "42533chen";
s1[1].name = "xiang";
s1[1].phoneNum = "42533xiang";
s1[2].name = "li";
s1[2].phoneNum = "42533li";
s1[3].name = "ming";
s1[3].phoneNum = "42533ming";
s1[4].name = "feng";
s1[4].phoneNum = "42533feng";
GetData(s1);
Console.ReadKey();
}
static void GetData(Sp[] s1)
{
//冒泡排序
for (int i = 0; i < s1.Length-1; i++)
{
for (int j = i + 1; j < s1.Length; j++)
{
int biJiao;
biJiao=string .Compare (s1[i].name,s1[j].name);
if (biJiao > 0)
{
string tempName;
string tempPhone;
tempName = s1[i].name;
tempPhone = s1[i].phoneNum;
s1[i].name = s1[j].name;
s1[i].phoneNum = s1[j].phoneNum;
s1[j].name = tempName;
s1[j].phoneNum = tempPhone;
}
}
}
for (int i = 0; i < s1.Length; i++)
{
Console.WriteLine("name is {0}, phonenum is {1}",s1[i].name ,s1 [i].phoneNum );
}
}
}
}
//还是泛型简单。sort()
{
Sp temp = new Sp();
temp = s1[i];
s1 [i]=s1 [j];
s1[j] = temp;
}
public string Phone;
public SP(string name, string phone)
{
Name = name;
Phone = phone;
}这是11楼代码中的几句,有个问题不太明白,用构造函数给类的成员变量赋值,类的成员变量是不是应该为私有的?