C++构建简单的链表,求debug
答案:1 悬赏:80 手机版
解决时间 2021-11-25 22:14
- 提问者网友:杀生予夺
- 2021-11-25 16:48
C++构建简单的链表,求debug
最佳答案
- 五星知识达人网友:不想翻身的咸鱼
- 2021-11-25 17:38
#include
#include
#include
typedef struct node {
unsigned id; // 学号
char name[20];
double score;
struct node *next;
}*LinkList, *pNode;
LinkList GetEmptyList() {
LinkList head = (pNode)malloc(sizeof(struct node));
head->next = NULL;
return head;
}
void AddData(LinkList head,pNode newnode) {
newnode->next = head->next;
head->next = newnode;
}
void Show(LinkList head) {
pNode p = head->next;
while(p) {
printf("%u\t%s\t%.2lf\n",p->id,p->name,p->score);
p = p->next;
}
printf("\n");
}
int main() {
unsigned id;
char name[20];
double score;
pNode newnode;
LinkList head = GetEmptyList();
printf("学号 姓名 分数('q' to quit):");
while(scanf("%u%s%lf",&id,name,&score) == 3) {
newnode = (pNode)malloc(sizeof(struct node));
newnode->id = id;
newnode->score = score;
strcpy(newnode->name,name);
AddData(head,newnode);
printf("学号 姓名 分数('q' to quit):");
}
printf("链表数据为:\n");
Show(head);
return 0;
}
#include
#include
typedef struct node {
unsigned id; // 学号
char name[20];
double score;
struct node *next;
}*LinkList, *pNode;
LinkList GetEmptyList() {
LinkList head = (pNode)malloc(sizeof(struct node));
head->next = NULL;
return head;
}
void AddData(LinkList head,pNode newnode) {
newnode->next = head->next;
head->next = newnode;
}
void Show(LinkList head) {
pNode p = head->next;
while(p) {
printf("%u\t%s\t%.2lf\n",p->id,p->name,p->score);
p = p->next;
}
printf("\n");
}
int main() {
unsigned id;
char name[20];
double score;
pNode newnode;
LinkList head = GetEmptyList();
printf("学号 姓名 分数('q' to quit):");
while(scanf("%u%s%lf",&id,name,&score) == 3) {
newnode = (pNode)malloc(sizeof(struct node));
newnode->id = id;
newnode->score = score;
strcpy(newnode->name,name);
AddData(head,newnode);
printf("学号 姓名 分数('q' to quit):");
}
printf("链表数据为:\n");
Show(head);
return 0;
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯