初始化队列
Status InitQueue(LinkQueue *p)
{
p->front=p->rear=(Node *)malloc(sizeof(Node));
if(p->front==NULL)
return NO;
p->front->next=NULL;
p->length=0;
return YES;
}
求队列中元素的个数
int SizeQueue(LinkQueue *p)
{
return p->length;
}
进队
Status EnQueue(LinkQueue *p,ElemType value)
{
Node *q;
q=(Node *)malloc(sizeof(Node));
if(!q)
return NO;
q->date=value;
q->next=NULL;
p->rear->next=q;
p->rear=q;
p->length++;
return YES;
}
出队
Status DeQueue(LinkQueue *p,ElemType *value)
{
Node *q;
if(SizeQueue(p)==0)
return NO;
q=p->front->next;
*value=q->date;
p->front->next=q->next;
p->length--;
return YES;
}
获取队头元素
Status GetHead(LinkQueue *p,ElemType *value)
{
Node *q;
if(SizeQueue(p)==0)
return NO;
q=p->front->next;
*value=q->date;
return YES;
}
遍历
void PutQueue(LinkQueue *p)
{
Node *q=p->front;
for(;q->next!=NULL;q=q->next)
printf("%d ",q->next->date);
综上,写成工程
status.h
#ifndef STATUS_H
#define STATUS_H
#define YES 1
#define NO 0
typedef int Status;
typedef int ElemType;
#endif
list.h
#ifndef LIST_H
#define LIST_H
#include <stdio.h>
#include "status.h"
typedef struct Node
{
ElemType date;
struct Node *next;
}Node;
typedef struct
{
int length;
Node *front;//队头指针
Node *rear;//队尾指针
}LinkQueue;
Status InitQueue(LinkQueue *p);
Status EnQueue(LinkQueue *p,ElemType value);
void PutQueue(LinkQueue *p);
int SizeQueue(LinkQueue *p);
Status GetHead(LinkQueue *p,ElemType *value);
#endif
list.c
#include <stdio.h>
#include "list.h"
//初始化队列
Status InitQueue(LinkQueue *p)
{
p->front=p->rear=(Node *)malloc(sizeof(Node));
if(p->front==NULL)
return NO;
p->front->next=NULL;
p->length=0;
return YES;
}
//进队(队尾)
Status EnQueue(LinkQueue *p,ElemType value)
{
Node *q;
q=(Node *)malloc(sizeof(Node));
if(!q)
return NO;
q->date=value;
q->next=NULL;
p->rear->next=q;
p->rear=q;
p->length++;
return YES;
}
//遍历
void PutQueue(LinkQueue *p)
{
Node *q=p->front;
for(;q->next!=NULL;q=q->next)
printf("%d ",q->next->date);
}
//求元素的个数
int SizeQueue(LinkQueue *p)
{
return p->length;
}
//出队(队头)
Status DeQueue(LinkQueue *p,ElemType *value)
{
Node *q;
if(SizeQueue(p)==0)
return NO;
q=p->front->next;
*value=q->date;
p->front->next=q->next;
p->length--;
return YES;
}
//获取队头元素
Status GetHead(LinkQueue *p,ElemType *value)
{
Node *q;
if(SizeQueue(p)==0)
return NO;
q=p->front->next;
*value=q->date;
return YES;
}
main.c
#include <stdio.h>
#include "list.h"
int main()
{
LinkQueue que;
LinkQueue *p=&que;
ElemType value;
int n,t,i;
printf("================初始化队列=====================\n");
t=InitQueue(p);
if(t=YES)
printf("初始化成功");
else
printf("初始化失败");
printf("\n===========================进队=========================\n");
printf("请输入进队元素的个数:");
scanf("%d",&n);
printf("请依次输入%d个元素:",n);
for(i=0;i<n;i++)
{
scanf("%d",&value);
EnQueue(p,value);
}
PutQueue(p);
printf("\n====================队的长度====================\n");
printf("此时元素个数为:%d",SizeQueue(p));
printf("\n===================出队=====================\n");
DeQueue(p,&value);
printf("出队的元素为:%d\n",value);
PutQueue(p);
printf("\n===================获取队头元素=====================\n");
GetHead(p,&value);
printf("%d\n",value);
}
结果: