zhanming.cpp

来自「采用了链表的形式」· C++ 代码 · 共 58 行

CPP
58
字号
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
struct station
{
char name[8];
struct station *nextSta;
};
struct station *creat_sta(struct station *h);
void print_sta(struct station *h);
int num=0;
main()
{
 struct station *head;
 head=NULL;
 printf("please input station name:\n");
 head=creat_sta(head);
 printf("------------------------\n");
 printf("Number of station = %d\n",num);
 print_sta(head);
 getch();
}
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;
   }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?