- 上机环境centos7下 Qt5.11 gcc
- 一些简单的函数封装
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> struct coach { char name[60]; int age; }; struct linktbl { struct coach* data; struct linktbl* next; };
- 节点数据录入
void addnodedata(struct coach* nodedata) { char name[60]; int age; printf("name:\n"); scanf("%s",name); strcpy(nodedata->name,name); printf("age:\n"); scanf("%d",&age); nodedata->age=age; //printf("%s\n",temp->data->name); //scanf("%s",temp->data->age); //printf("%s\n",temp->data->name); //scanf("%s",temp->data->age); }
- 初始化---在head节点后添加第一个节点
void initltbl(struct linktbl* head) { struct linktbl* anext; anext = (struct linktbl*)malloc(sizeof(struct linktbl)); struct coach* nodedata; nodedata = (struct coach*)malloc(sizeof(struct coach)); addnodedata(nodedata); strcpy(anext->data.name,nodedata->name); anext->data.age=nodedata->age; head->next=anext; }
- 打印所有节点
void printall(struct linktbl* head) { struct linktbl* temp=NULL; temp = (struct linktbl*)malloc(sizeof(struct linktbl)); temp=head->next; while(temp!=NULL) { printf("name is %s, age is %d\n",temp->data.name,temp->data.age); temp=temp->next; } }
主函数调用
int main() { struct linktbl* head; head = (struct linktbl*)malloc(sizeof(struct linktbl)); initltbl(head); printall(head); printall(head); printall(head); printall(head); printall(head); printall(head); return 0; }
测试结果: