#include "stdio.h"
#include "stdlib.h"
#include "io.h"
#include "math.h"
#include "time.h"
#if(1)
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 20 /* 存储空间初始分配*/
typedef int Status;
typedef char ElemType; /* SElemType类型根据实际情况而定,这里假设为int */
typedef struct QNode//队结点
{
ElemType data;
struct QNode *next;
}QNode,*Queueptr;
typedef struct //链队类型
{
Queueptr front;
Queueptr rear;
}LinkQueue;
Status INitQueue(LinkQueue *p)//初始化
{
p->front=p->rear=(Queueptr)malloc(sizeof(QNode));
if(!p->front)
{
exit(0);
}
p->front->next=NULL;
return OK;
}
Status ClearQueue(LinkQueue *p)//清空栈
{
if(p->front!=p->rear)
{
p->front=p->rear;
}
return OK;
}
Status QueueEmpty(LinkQueue *p)//判读是否为空
{
if(p->front==p->rear)
{
return TRUE;
}
else
{
return ERROR;
}
}
Status EnQueue(LinkQueue *p,ElemType *e)//插入元素
{
Queueptr q;
q=(Queueptr)malloc(sizeof(QNode));
if(!p->front)
{
exit(0);
}
q->data=*e;
q->next=NULL;
p->rear->next=q;
p->rear=q;
return OK;
}
Status DeQueue(LinkQueue *p,ElemType *e)//弹出元素
{
Queueptr q;
if(p->front==p->rear)
{
return ERROR;
}
q=p->front->next;
*e=q->data;
p->front->next=q->next;
if(p->rear==q)
{
p->rear=p->front;
}
free(q);
}
Status QueueLength(LinkQueue *p)//队列长度
{
return (p->rear-(p->front->next));
}
Status GetHead(LinkQueue *p,ElemType *e)//队列不为空输出第一个元素
{
if(QueueEmpty(p))
{
*e=p->front->next->data;
return OK;
}
else
{
return ERROR;
}
}
Status StackTraverse(LinkQueue *p)//打印出元素
{
Queueptr q;
q=p->front->next;
while(q)
{
printf("%c ",q->data);
q=q->next;
}
printf("\n");
return OK;
}
Status Destory(LinkQueue *p)//销毁队列
{
while(p->front)
{
p->rear=p->front->next;
free(p->front);
p->front=p->rear;
}
return OK;
}
int main()
{
LinkQueue p;
ElemType c;
ElemType e;
INitQueue(&p);
printf("输入一串字符串(以#结尾):\n");
scanf("%c",&c);
while(c!='#')
{
EnQueue(&p,&c);
scanf("%c",&c);
}
printf("链栈的长度为: %d" ,QueueLength(&p));
printf("\n");
printf("依次弹出元素:");
StackTraverse(&p);
printf("\n");
printf("弹出元素:");
DeQueue(&p,&e);
printf(" %c \n",e);
printf("链队是否为空: %d(1是,0否)\n",QueueEmpty(&p));
printf("再次弹出元素:");
GetHead(&p,&e);
printf(" %c \n",e);
system("pause");
return OK;
}
#endif