方正国际-C#的单项链表及倒置

没想到连续两天搞了两场方正集团的笔试,今天给过来通知的是方正国际。

笔完的结果和阿帕比截然不同,题量比较大,2个小时。

没来得及弄出点题来,只能把还有印象的三个程序题记录一下。

最后一题是C#编一个单向链表,并倒序。

考场上写的很繁琐,还写了插入,删除等的方法。回来看到了完美主义的一个小练习,觉得简练标准。我稍微加以修改,可以自己设链表长度

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace Project2
{
class Class1
{
[STAThread]
///程序集引用了 System.Windows.Forms 命名空间,但没有使用 System.STAThreadAttribute 属性标记该程序集的入口点。
///Satisfies rule: MarkWindowsFormsEntryPointsWithStaThread
///规则说明:标记了windows窗体的入口点;
static void Main(string[] args)
{
Console.WriteLine(
"请输入链表长度:");
//初始化链表
int l = Console.Read();
char ch = (char)l;
l
= Convert.ToInt32(ch.ToString());
Console.WriteLine(
"请依次输入节点:");
string node = Console.ReadLine();
LineNote startnote
= new LineNote(node);
LineNote note
= startnote;
for (int i = 2; i <= l+1; i++)
{
node
= Console.ReadLine();
note.Next
= new LineNote(node);
note
= note.Next;
}
note
= startnote;
//输出
Console.WriteLine("原链表");
while (note != null)
{
System.Console.WriteLine(note.Note);
note
= note.Next;
}
Console.ReadLine();
//开始反转
note = startnote;
LineNote temp
= note;
while (temp != null)
{
note
= startnote;
if (temp != startnote)
{
startnote
= temp;
temp
= temp.Next;
startnote.Next
= note;
}
else
{
temp
= temp.Next;
note.Next
= null;
}
}
//再次输出
note = startnote;
Console.WriteLine(
"转置后链表");
while (note != null)
{
System.Console.WriteLine(note.Note);
note
= note.Next;
}
Console.ReadLine();
}
}

class LineNote
{
public string Note;
public LineNote Next;
public LineNote(string note)
{
Note
= note;
}

}
}


posted on 2008-10-22 18:30  杨斐  阅读(402)  评论(0编辑  收藏  举报

导航