33,311
社区成员
发帖
与我相关
我的任务
分享
// ConsoleApplication4.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct station
{
char name[8];
struct station *nextSta;
};
struct station *creat_sta(station *h);
void print_sta(struct station *h);
int num = 0;//全局变量表示站点数
int main()
{
struct station *head;//定义表头指针
head = NULL;//创建一个空表
printf("请输入站名:");
head = creat_sta(head);//调用函数创建链表 printf("--------------------------");
printf("共有%d个站点:\n",num);
print_sta(head);
}
struct station *creat_sta(struct station *h)
{
struct station *p1, *p2;
p1 = p2 = (struct station*)malloc(sizeof(struct station));//申请新节点
if (p2 != NULL)
{
scanf("%s", &p2->name);
p2->nextSta = NULL;
}
while (p2->name[0] != '#')
{
num++;
if (h == NULL)
h = p2;
else
p1->nextSta = p2;
p1 = p2;
p2 = (struct station*)malloc(sizeof(struct station));
if (p2 != NULL)
{
scanf("%s", &p2->name);
p2->nextSta = NULL;
}
}
return h;
}
void print_sta(struct station *h)
{
struct station *temp;
temp = h;
while (temp != NULL)
{
printf("%-8s", temp->name);
temp = temp->nextSta;
}
return ;
}