main.c(负责测试)
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "nohead.h"
int main()
{
LNode *list = NULL;
struct score_st data[10],mydata,data1;
int i = 0;
srand((unsigned)time(NULL));
for (i = 0; i < 10; i++)
{
data[i].chinese = rand() % 100;
data[i].english = rand() % 100;
data[i].math = rand() % 100;
data[i].student_id = i;
snprintf(data[i].name, 32, "学生的名字是%d", i);
}
data1.chinese = rand() % 100;
data1.english = rand() % 100;
data1.math = rand() % 100;
data1.student_id = 100;
snprintf(data1.name, 32, "学生的名字是%d", 100);
list_display(list);
for (i = 0; i < 10; i++)
{
list_insert(&list, &data[i]);
}
list_display(list);
list_find(list,3,&mydata);
printf("%4s %20s %4s %4s %4s\n", "学号", "姓名", "数学", "语文", "外语");
printf("%4d %20s %4d %4d %4d\n", mydata.student_id,
mydata.name, mydata.math, mydata.chinese,
mydata.english);
list_insert_at(&list,0,&data1);
list_display(list);
list_destroy(list);
}
nohead.c(负责函数实现)
#include<stdio.h>
#include<stdlib.h>
#include "nohead.h"
int list_insert_at(struct LNode **list,int i, struct score_st *data)
{
struct LNode *node = *list,*newnode = NULL;
int j = 0;
if (i < 0)
{
return -1;
}
if (i == 0)
{
newnode = (LNode*)malloc(sizeof(LNode));
if (newnode == NULL)
{
return -3;
}
newnode->score = *data;
newnode->next = *list;
*list = newnode;
return 0;
}
while ((j < i-1)&&(node!=NULL))
{
node = node->next;
j++;
}
if (node == NULL)
{
return -2;
}
newnode = (LNode*)malloc(sizeof(LNode));
if (newnode == NULL)
{
return -3;
}
newnode->score = *data;
newnode->next = node->next;
node->next = newnode;
return 0;
}
int list_insert(struct LNode **list, struct score_st *data)
{
LNode *newnode = NULL;
newnode = (LNode*)malloc(sizeof(LNode));
if (newnode == NULL)
return -1;
newnode->score = *data;
newnode->next = *list;
*list = newnode;
}
void list_display(LNode *list)
{
LNode *ps = list;
if (ps == NULL)
{
printf("链表为空\n");
return -1;
}
printf("学生的信息如下:\n");
printf("%4s %20s %4s %4s %4s\n", "学号", "姓名", "数学", "语文", "外语");
while (ps)
{
printf("%4d %20s %4d %4d %4d\n", ps->score.student_id,
ps->score.name, ps->score.math, ps->score.chinese,
ps->score.english);
ps = ps->next;
}
}
int list_find(LNode *ps,int id,struct score_st *data)
{
while (ps)
{
if (ps->score.student_id == id)
{
*data = ps->score;
return 0;
}
ps=ps->next;
}
return -1;
}
int list_delete(LNode **ps)
{
LNode* curnode = *ps;
if (*ps == NULL)
{
printf("链表为空删除失败\n");
return -1;
}
(*ps) = curnode->next;
free(curnode);
return 0;
}
void list_destroy(LNode *p)
{
LNode *nextnode = NULL;
while (p)
{
nextnode = p->next;
free(p);
p = nextnode;
}
}
nohead.h(负责函数声明)
#ifndef NOHEAD_H__
#define NOHEAD_H__
#define MAX_SIZE 32
struct score_st
{
char name[MAX_SIZE];
int student_id;
int math;
int chinese;
int english;
};
typedef struct LNode
{
struct score_st score;
struct LNode *next;
}LNode;
int list_insert(struct LNode *list, struct score_st *score);
int list_insert_at(struct LNode **list, int i, struct score_st *data);
void list_display(LNode *list);
int list_delete(LNode **ps);
void list_destroy(LNode *list);
int list_find(LNode *ps, int id,struct score_st *data);
#endif