public StudentLinkList(int count) { Head = new StudentNode(new Student(20120000 + count), null); //头插法建表 for (int i = count - 1; i > 0; i--) { //创建新结点,将原头结点作为新结点下一结点,将新节点作为头结点 Head = new StudentNode(new Student(20120000 + i), Head); } }
static void Main(string[] args) { Console.Title = "按学生成绩构造哈希表"; StudentList students = new StudentList(20); Console.WriteLine("学生成绩单如下:"); for (int i = 0; i < 20; i++) { Console.Write(students[i].Result + "\t"); if ((i + 1) % 5 == 0) Console.WriteLine(); } //创建哈希表 StudentNode[] hashTable = new StudentNode[5]; for (int i = 0; i < 20; i++) { int index = students[i].Result % 5; //计算哈希地址 if (hashTable[index] == null) hashTable[index] = new StudentNode(students[i], null); else hashTable[index] = new StudentNode(students[i], hashTable[index]); } int result; do { Console.WriteLine("请输入要查询的学生的成绩:"); if (!int.TryParse(Console.ReadLine(), out result)) break; bool successful = false; //标记查找是否成功 StudentNode node = hashTable[result % 5]; //计算哈希地址 while (node != null) { if (node.Student.Result == result) //查找成功 { Console.WriteLine("学号:{0}\t姓名:{1}\t年级:{2}", node.Student.Number, node.Student.Name, node.Student.Grade); successful = true; } node = node.Next; } if (!successful) Console.WriteLine("无此学生信息!"); //查找失败 } while (result != -1); Console.ReadLine(); }
//出队 public Student? Out() { if (Front == null) return null; Student student = Front.Student; Front = Front.Next; if (Front == null) Rear = null; return student; }
//进队 public void In(Student student) { if (Rear == null) { Rear = new StudentNode(student, null); Front = Rear; } else { Rear.Next = new StudentNode(student, null); Rear = Rear.Next; } }
public StudentNode(Student student, StudentNode next) { this.Student = student; this.Next = next; }