Console.WriteLine(s[i]);
}
publicstaticvoidMain(string[]args)
{
Students1=newStudent(3,18,"Lily");
Students2=newStudent(1,21,"Suan");
Students3=newStudent(33,20,"John");
Students4=newStudent(13,20,"Lucy");
Students5=newStudent(8,17,"Jack");
Student[]s={s1,s2,s3,s4,s5};
Console.WriteLine("班级学生名单如下:
");
Output(s);//第1次调用output方法输出数组
//【代码4】将学生按照学号排序
Console.WriteLine("按学号由小到大排序...");
Output(s);//第2次调用output方法输出数组
//【代码5】将所有学生的年龄加1
Console.WriteLine("所有学生年龄加1后...");
Output(s);//第3次调用output方法输出数组
//【代码6】统计大于20岁的学生个数
Console.WriteLine("大于20岁人数是:
"+count);
}
}
2.已知链表的每个结点定义如下:
Node{
intdata;
Nodenext;
}
请根据【模版二】,将【代码1】到【代码5】替换为C#代码,以实现如下要求:
(1)创建一个初始为10个结点的单向链表,结点数据用随机函数产生。
(2)输出该链表:
(3)在链表的首部增加一个数据为50的结点,并输出链表。
(4)在链表的尾部增加一个数据为50的结点,并输出链表。
(5)输出结果,如图2所示:
图2
usingSystem;
classNode{
publicintdata;//数据域
publicNodenext;//链域,存放下一个结点的引用
}
publicclassLink{
Nodeheader;
/*构建含n个结点的链表链表的头用header指示*/
publicLink(intn){
Nodep,q;//用于链表的推进,p为链表中当前最后结点,q为要新加的结点
header=newNode();//创建首结点
Randomrnd=newRandom();
header.data=rnd.Next(1,10);//使用随机函数,给首结点的data域赋值
p=header;
for(intk=1;k<=n-1;k++)
{
//【代码1】创建除了首节点之外的其他节点
}
}
voidOutputLink()
{
//【代码2】输出链表中所有元素的数据
}
publicstaticvoidMain(String[]a)
{
//【代码3】创建含10个元素的链表
x.OutputLink();//输出链表
//【代码4】创建一个数据为50的新结点,放到链表的头部
x.OutputLink();
//【代码5】至链表的尾部,在尾部增加一个data值为50的新结点
x.OutputLink();
}
}
5.代码
第一题:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespaceConsoleApplication1
{
classStudent
{
privateintnum;
privatestringname;
privateintage;
publicStudent(intnum,intage,stringname)
{
this.num=num;
this.name=name;
this.age=age;
}
publicoverridestringToString()
{
return("学号:
"+this.num+",姓名:
"+this.name+",年龄:
"+this.age);
}
publicstaticvoidoutput(Student[]s)
{
for(inti=0;i{
Console.WriteLine(s[i]);
}
}
staticvoidMain(string[]args)
{
inti,j;
intpeoplesum=0;
Students1=newStudent(3,18,"Lily");
Students2=newStudent(1,21,"Suan");
Students3=newStudent(33,20,"John");
Students4=newStudent(13,20,"Lucy");
Students5=newStudent(8,17,"Jack");
Student[]s={s1,s2,s3,s4,s5};
Studentmin;
Console.WriteLine("该学生名单是:
");
output(s);
for(i=0;i{
for(j=i;j{
if(s[i].num>s[j].num)
{
min=s[i];
s[i]=s[j];
s[j]=min;
}
}
}
Console.WriteLine("学生按学号顺序为...");
output(s);
for(i=0;i{
s[i].age=s[i].age+1;
}
Console.WriteLine("所有学生年龄加1后...");
output(s);//第3次调用output方法输出数组
for(i=0;i{
if(s[i].age>20)
{
peoplesum++;
}
}
Console.WriteLine("年龄大于20岁人数为:
"+peoplesum);
Console.ReadKey();
}
}
}
实验截图:
第二题:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespaceConsoleApplication1
{
classProgram
{
staticvoidMain(string[]args)
{
Linkx=newLink(10);
x.OutputLink();
Nodeno=newNode();
no.data=50;
no.next=x.header;
x.header=no;
x.OutputLink();
Nodeno2=newNode();
no2.data=50;
x.last.next=no2;
x.OutputLink();
Console.ReadKey();
}
}
publicclassNode
{
publicintdata;
publicNodenext;
}
publicclassLink
{
publicNodeheader;
publicNodep,q;
publicNodelast;
publicLink(intn)
{
header=newNode();
Randomrnd=newRandom();
header.data=rnd.Next(1,10);
p=header;
for(intk=1;k<=n-1;k++)
{
Nodenode=newNode();
node.data=rnd.Next(1,10);
p.next=node;
p=p.next;
}
last=p;
}
publicvoidOutputLink()
{
p=header;
q=p.next;
while(q!
=null)
{
Console.Write(p.data);
Console.Write("-->");
p=q;
q=p.next;
}
Console.WriteLine();
}
}
}
实验截图
参考文献
《sualC#2008大学教程第三版》………P.J.DeitelH.M.Deitel