#include <stdio.h>
#define MaxLen 100
typedef struct
{
int no;
int score;
} DataType;
typedef struct
{
DataType data[MaxLen];
int length;
}SeqList;
void InitList(SeqList *p){
p->length=0;
}
void CreateList(SeqList *p){
int i = 0;
DataType x;
printf("请输入学号和成绩,以学号-1为结束:\n");
scanf("%d", &x.no);
while(x.no != -1){
scanf("%d", &x.score);
p->data[i] = x;
scanf("%d", &x.no);
i++;
}
p->length = i;
}
void InsertList(SeqList *p, int i, DataType x) {
for(int j = p->length-1; j > i-1; j--) {
p->data[j+1] = p->data[j];
}
p->data[i-1] = x;
p->length++;
}
DataType GetNode(SeqList *L, int i) {
if(i < 1 || i > L->length) printf("position error\n");
return L->data[i-1];
}
int LocateList(SeqList *p, int no) {
int i = 0;
while(i < p->length && p->data[i].no != no) i++;
if(i < p->length) return i+1;
else return -1;
}
void EditList(SeqList *p, int i, DataType e) {
if(i < 1 || i > p->length) printf("position error\n");
p->data[i-1] = e;
}
void DeleteList(SeqList *L, int i) {
if(i < 1 || i > L->length) printf("position error\n");
for(int j = i; j <= L->length-1; j++)
L->data[j-1] = L->data[j];
L->length--;
}
void PrintList(SeqList *L) {
int i;
for(i = 0; i < L->length; i++) {
printf("[%d,%d]\n",L->data[i].no, L->data[i].score);
}
printf("\n");
}
int main () {
SeqList L;
DataType e;
InitList(&L);
CreateList(&L);
PrintList(&L);
e.no = 4;
e.score = 250;
InsertList(&L, 4, e);
printf("修改后:\n");
PrintList(&L);
int i = LocateList(&L, 3);
if(i > 0) printf("学号为3的成绩:%d \n", GetNode(&L, i).score);
else printf("找不到这个元素呀!!!");
e.no = 3;
e.score = 65;
int m = LocateList(&L, 3);
EditList(&L, m, e);
printf("修改后:\n");
PrintList(&L);
int k = LocateList(&L, 2);
DeleteList(&L, k);
printf("删除学号为2的记录后:\n");
PrintList(&L);
}