jelasin 的学生作业:
#include
#include
#include
#include
#define RED "\033[31m"
#define NONE "\033[0m"
// 写出下列类型的创建,判满,插入输出
#define MAX 10
//实际学⽣的存储
struct student
{
char name[20];
int id;
int age;
};
typedef struct student datatype_t;
typedef struct{
datatype_t buf[MAX]; //定义数组记录班级学⽣每个学⽣的信息。
int n; //学⽣实际到来的个数。
}seqlist_t;
seqlist_t *create_empty_seqlist()
{
seqlist_t * l = (seqlist_t *)malloc(sizeof(seqlist_t));
if (NULL == l)
{
perror(RED "[-] malloc error when create_empty_seqlist" NONE);
return NULL;
}
memset(l, '\x00', sizeof(seqlist_t));
l->n = 0;
return l;
}
bool is_full_seqlist(seqlist_t *l)
{
return l->n >= MAX ? true : false;
}
void insert_data_seqlist(seqlist_t *l, datatype_t data)
{
if (is_full_seqlist(l))
{
printf(RED "[*]The list is full, cannot insert data.\n" NONE);
return;
}
l->buf[l->n] = data;
l->n++;
return;
}
void printf_data_seqlist(seqlist_t *l)
{
for (int i = 0; i n; i++)
{
printf("Name: %s, ID: %d, Age: %d\n", l->buf[i].name, l->buf[i].id, l->buf[i].age);
}
return;
}
void destroy_seqlist(seqlist_t *l)
{
free(l);
l = NULL;
return;
}
int main(int argc, char const *argv[])
{
setbuf(stdout, NULL);
seqlist_t *list = create_empty_seqlist();
datatype_t student1 = {"Alice", 1, 20};
insert_data_seqlist(list, student1);
datatype_t student2 = {"Bob", 2, 21};
insert_data_seqlist(list, student2);
datatype_t student3 = {"him", 3, 22};
insert_data_seqlist(list, student3);
insert_data_seqlist(list, student3);
insert_data_seqlist(list, student3);
insert_data_seqlist(list, student3);
insert_data_seqlist(list, student3);
insert_data_seqlist(list, student3);
insert_data_seqlist(list, student3);
insert_data_seqlist(list, student3);
printf_data_seqlist(list);
insert_data_seqlist(list, student3);
destroy_seqlist(list);
return 0;
}
➜ 1 ./main
Name: Alice, ID: 1, Age: 20
Name: Bob, ID: 2, Age: 21
Name: him, ID: 3, Age: 22
Name: him, ID: 3, Age: 22
Name: him, ID: 3, Age: 22
Name: him, ID: 3, Age: 22
Name: him, ID: 3, Age: 22
Name: him, ID: 3, Age: 22
Name: him, ID: 3, Age: 22
Name: him, ID: 3, Age: 22
[*]The list is full, cannot insert data.