|

楼主 |
发表于 2014-5-21 13:12:59
|
显示全部楼层
- // test.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <stdlib.h>
- #include <windows.h>
- //建立一个链表存放学生的信息
- struct student
- {
- char name[20];
- char sex;
- int age;
- struct student * next;
- };
- //建立动态链表
- struct student *create()
- {
- struct student *head,*p1,*p2;
- int n = 0;
- p1 = p2 = (struct student*)malloc(sizeof(struct student));
- ZeroMemory(p1,sizeof(struct student));
- head = NULL;
- scanf("%s %c %d",p1->name,&p1->sex,&p1->age);
- while(strcmp(p1->name,"end") != 0)
- {
- n = n +1;
- if(n == 1)
- {
- head = p1;
- printf("access done!\n");
- }
- else p2->next = p1;
- p2 = p1;
- p1 =(struct student*)malloc(sizeof(struct student));
- ZeroMemory(p1,sizeof(struct student));
- scanf("%s %c %d",p1->name,&p1->sex,&p1->age);
- printf("access done!\n");
- }
- p2->next = NULL;
- return head;
- }
- int main(int argc, char* argv[])
- {
- struct student *pt;
- printf("请输入姓名、性别(M\F)、年龄:\n");
- pt = create();
- if(pt != NULL)
- printf("姓名\t性别\t年龄\n");
- while(pt != NULL)
- {
- printf("%s\t%c\t%d\n",pt->name,pt->sex,pt->age);
- pt = pt->next;
- }
- system("pause");
- return 0;
- }
复制代码 附上完整代码 |
|