实验室链表

在这里插入代码片
```#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXCHARS 30
#define DEBUG 0
struct NameRec/*申明一个链表结构*/
{
 char name [MAXCHARS];
 struct NameRec *nextAddr;
};
struct  NameRec *queuein, *queueout;
int main()
{
 void readenque();//函数原型
 void serveshow();
 queuein=NULL;//初始化队列指针
 queueout=NULL;
 readenque();
 serveshow();
}
void readenque()
{
 char name[MAXCHARS];
 void enque (char *);
 printf("Enter as many names as you wish,one per line");
 printf("\n To stop entering names,enter a single x\n");
 while (1)
 {
  printf("Enter a name:");
  gets (name);
  if (strcmp(name,"x")==0)
   break;
  enque (name);
 }
}
void serveshow()
{
 char name [MAXCHARS];
 void serve(char*);
 printf("\nThe names served from the queue are:\n");
 while (queueout!=NULL)//显示直到队列结尾
 {
  serve(name);
  printf("%s\n",name);
 }
}
void enque (char*name)
{
 struct NameRec *newaddr;//指向NameRec类型的结构指针
 if(DEBUG)
 {
  printf("Before the enque the address in queuein is %p",queuein);
  printf("Before the enque the address in queueout is %p",queueout);
 }
 newaddr =(struct NameRec*)malloc(sizeof(struct NameRec));
 if (newaddr==(struct NameRec *)NULL)
 {
  printf("\nFailed to allocate memory for this structure\n");
  exit(1);
 }
 if (queueout==NULL)//用两个if语句处理空队列的初始化
  queueout=newaddr;
 if(queuein!=NULL)
  queuein->nextAddr=newaddr;//填入前面结构的地址字段
 strcpy(newaddr->name,name);//储存这个姓名
 newaddr->nextAddr=NULL;//设置地址字段为NULL
 queuein=newaddr;//更新队列顶部的指针
 if (DEBUG)
 {
  printf("\n After the enque the address in queuein is %p\n",queuein);
  printf(" and the address in queueout is %p\n",queueout);
 }
}
void serve(char*name)
{
 struct NamerEC *nextAddr;
 if(DEBUG)
  printf("Before the serve the address in queueout is %p\n",queueout);
 strcpy(name,queueout->name);//从队列底部取回这个名字
 nextAddr = queueout->nextAddr;
 free(queueout);
 queueout=nextAddr;
 if(DEBUG)
  printf("After the serve the address in queueout is %u\n",queueout);
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20181101220530724.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2hodDExMzM1MzQwNzI2Nw==,size_16,color_FFFFFF,t_70)


猜你喜欢

转载自blog.csdn.net/hht113353407267/article/details/83627863