一个简单存储结构的链表与顺序表实现

顺序表实现

//顺序表
#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值