[代码记录] C语言链表

#开始

·  简单的创建链表

#代码

  

 1 #include<conio.h>
 2 #include<stdio.h>
 3 #include<stdlib.h>
 4 
 5 struct Str
 6 {
 7     char name[100];
 8     int sex;
 9     int id;
10     Str * next;
11 };
12 
13 void printStruct(Str *str)    //输出链表
14 {
15     Str *strTemp = str;
16     while(strTemp->next)
17     {
18         printf("=========print=============\n");
19         printf("id: %d\n",strTemp->id);
20         printf("name: %s\n",strTemp->name);
21         printf("sex: %d\n",strTemp->sex);
22         strTemp = strTemp->next;
23     }
24 }
25 
26 int main()
27 {
28     Str* strFirst = (Str*)malloc(sizeof(Str));
29     Str* strTemp = strFirst;
30     while(strTemp->next)    //创建链表
31     {
32         printf("=========================\n");
33         printf("输入id:");
34         scanf("%d",&strTemp->id);
35         if(strTemp->id == 0)    //如果输入id == 0 就退出输入
36         {
37             strTemp->next = NULL;    //给next赋值NULL 并且返回到循环的最开始的位置
38             continue;
39         }
40         printf("输入name:");
41         scanf("%s",strTemp->name);
42 
43         printf("输入sex:");
44         scanf("%d",&strTemp->sex);
45 
46         strTemp->next=(Str*)malloc(sizeof(Str)); 
47         strTemp = strTemp->next;
48     }
49     printStruct(strFirst);    //输出链表
50 
51     _getch();
52     return 0;
53 }

 

#运行环境

  win7 32位

  VS2010 

posted @ 2018-09-07 09:28  Startu  阅读(158)  评论(0编辑  收藏  举报