定义一个学生类Student,包含学号、姓名、性别、专业等属性,然后使用ArrayList类实现一个学生线性表,按学号进行排序,并尝试插入删除操作。
using System;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Student student1 = new Student
{
Id = 3,
Name = "张三",
Gender = "男",
Major = "计算机专业"
};
Student student2 = new Student
{
Id = 2,
Name = "李四",
Gender = "女",
Major = "计算机专业"
};
Student student3 = new Student
{
Id = 1,
Name = "王五",
Gender = "男",
Major = "计算机专业"
};
Student student4 = new Student
{
Id = 4,
Name = "赵六",
Gender = "女",
Major = "计算机专业"
};
// 插入数据
ArrayList list = new ArrayList();
list.Add(student1);
list.Add(student2);
list.Add(student3);
list.Add(student4);
// 根据Id排序
StudentComparer comparer = new StudentComparer();
list.Sort(comparer);
foreach (Student item in list)
{
Console.WriteLine(item.Id + "," + item.Name);
}
// 删除性别为女的数据
for (int i = list.Count - 1; i >= 0; i--)
{
Student student = list[i] as Student;
if (student.Gender == "女")
{
list.RemoveAt(i);
}
}
Console.ReadKey();
}
}
public class StudentComparer : IComparer
{
public int Compare(object x, object y)
{
Student a = x as Student;
Student b = y as Student;
if (a.Id < b.Id)
{
return -1;
}
else if (a.Id > b.Id)
{
return 1;
}
else
{
return 0;
}
}
}
public class Student
{
/// <summary>
/// 学号
/// </summary>
public int Id { get; set; }
/// <summary>
/// 姓名
/// </summary>
public string Name { get; set; }
/// <summary>
/// 性别
/// </summary>
public string Gender { get; set; }
/// <summary>
/// 专业
/// </summary>
public string Major { get; set; }
}
}
你要用啥架构啊
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;
const int n=5;
/*
* nodeEntry : 节点数据类型
* nodeADT : 节点结构
* linkADT : 链表结构
*/
typedef struct Student
{
int num;
char name[30];
char sex;
float score1;//语文
float score2;//数学
float score3;//英语
//struct Student *next;
}Student;
typedef struct linkCDT {
nodeADT head;
}*linkADT;
/*
* InitLink : 初始化链表
* CreateNode : 创建节点
* AppendLink : 添加数据
*/
nodeADT CreateNode(Student entry) {
nodeADT p=(nodeADT)malloc(sizeof*p);
p->entry=entry,p->next=0;
return p;
}
/*
SortLink : 排序链表
//按学号排序
void SortLinkID(linkADT link) {
nodeADT pHead,pRear,p,tp;
if (!link) return;
for (pHead=link->head,pRear=0;pHead;pHead=pHead->next) {
for (tp=pHead,p=pHead->next;p;tp=p,p=p->next)
if (pHead->entry.num>=p->entry.num)
tp->next=p->next,p->next=pHead,pHead=p,p=tp;
if (!pRear) link->head=pHead;
else pRear->next=pHead;
pRear=pHead;
}
//按英语成绩排序
void SortLinkEnglish(linkADT link) {
nodeADT pHead,pRear,p,tp;
if (!link) return;
for (pHead=link->head,pRear=0;pHead;pHead=pHead->next) {
for (tp=pHead,p=pHead->next;p;tp=p,p=p->next)
if (pHead->entry.score3>=p->entry.score3)
tp->next=p->next,p->next=pHead,pHead=p,p=tp;
if (!pRear) link->head=pHead;
else pRear->next=pHead;
pRear=pHead;
}
}
//按姓名的字典序进行排序
void SortLinkName(linkADT link) {
nodeADT pHead,pRear,p,tp;
if (!link) return;
for (pHead=link->head,pRear=0;pHead;pHead=pHead->next) {
for (tp=pHead,p=pHead->next;p;tp=p,p=p->next)
if (pHead->entry.name[0]>=p->entry.name[0])
tp->next=p->next,p->next=pHead,pHead=p,p=tp;
if (!pRear) link->head=pHead;
else pRear->next=pHead;
pRear=pHead;
}
}
//按姓名的长度进行排序
void SortLinkNameLength(linkADT link) {
nodeADT pHead,pRear,p,tp;
if (!link) return;
for (pHead=link->head,pRear=0;pHead;pHead=pHead->next) {
for (tp=pHead,p=pHead->next;p;tp=p,p=p->next)
if (strlen(pHead->entry.name)>=strlen(p->entry.name))
tp->next=p->next,p->next=pHead,pHead=p,p=tp;
if (!pRear) link->head=pHead;
else pRear->next=pHead;
pRear=pHead;
}
参考这个,将部分java代码修改下 https://blog.csdn.net/m0_51383338/article/details/110201051