顺序表实现
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX_SIZE 1024
typedef struct {
char name[MAX_SIZE];
int score, total;
double fina;
}Data;
typedef struct {
Data *data;
int cur, size;
}Sqlist;
void sqlistInit(Sqlist *sq)
{
sq->data = (Data *)malloc(MAX_SIZE * sizeof(Data));
sq->cur = 0;
sq->size = MAX_SIZE;
}
void sqlistAdd(Sqlist *sq)
{
const int coefficient = 2;
sq->size *= coefficient;
sq->data = (Data *)realloc(sq->data, sizeof(Data) * sq->size);
}
void sqlistInput(Sqlist *sq)
{
Data temp;
int cur = 0;
printf("please input the information of students:");
printf("name 'null' to exit.\n");
printf("please input the student information-name,score,final_grade,total_grade:\n");
while (scanf("%s", temp.name))
{
if (!strcmp(temp.name, "null")) break;
if (sq->cur >= sq->size)
sqlistAdd(sq);
cur = sq->cur;
scanf("%d%lf%d", &temp.score, &temp.fina, &temp.total);
strcpy(sq->data[cur].name, temp.name);
sq->data[cur].score = temp.score;
sq->data[cur].fina = temp.fina;
sq->data[sq->cur++].total = temp.total;
temp.name[0] = '\0';
}
}
void sqlistOutput(Sqlist *sq)
{
int i;
for (i = 0;i < sq->cur;i++)
printf("%s %d %.2f %d\n", sq->data[i].name, sq->data[i].score, \
sq->data[i].fina, sq->data[i].total);
}
int main()
{
Sqlist stu_info;
sqlistInit(&stu_info);
sqlistInput(&stu_info);
sqlistOutput(&stu_info);
system("pause");
return 0;
}
带头结点的单链表实现
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX_SIZE 1024
typedef struct Node Linklist;
typedef struct {
char name[MAX_SIZE];
int score, total;
double fina;
}Data;
struct Node {
Data data;
Linklist *next;
};
Linklist *createLink()
{
Linklist *head = (Linklist *)malloc(sizeof(Linklist));
Linklist *pnew, *pend;
Data temp;
head->next = NULL;
pend = head;
printf("please enter the information, 'null' to exit.\n");
while (scanf("%s", &temp.name))
{
pnew = (Linklist *)malloc(sizeof(Linklist));
if (!strcmp(temp.name, "null")) break;
scanf("%d%lf%d", &temp.score, &temp.fina, &temp.total);
strcpy(pnew->data.name, temp.name);
pnew->data.score = temp.score;
pnew->data.total = temp.total;
pnew->data.fina = temp.fina;
pnew->next = NULL;
pend->next = pnew;
pend = pnew;
}
return head;
}
void outputLink(Linklist *head)
{
Linklist *st;
for (st = head->next;st != NULL;st = st->next)
printf("%s %d %.2f %d\n", st->data.name, st->data.score,\
st->data.fina, st->data.total);
}
int main()
{
Linklist *head;
head = createLink();
outputLink(head);
system("pause");
return 0;
}