实验室链表

在这里插入代码片
```#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/20181101215856760.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2hodDExMzM1MzQwNzI2Nw==,size_16,color_FFFFFF,t_70)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值