|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>
#include<malloc.h>
#include<string.h>
typedef struct QNode
{
char *data;
struct QNode *next;
}QNode;
typedef struct
{
QNode *front,*rear;
}LinkQueue ;
void InitQueue(LinkQueue *Q)
{
Q->front=Q->rear=(QNode *)malloc(sizeof(QNode));
if(!Q->front)
printf("内存申请失败!!");
Q->front->next=NULL;
printf("内存申请成功,可以继续操作。");
}
LinkQueue *EnQueue(LinkQueue *Q,char *e)
{
QNode *p;
p=(QNode *)malloc(sizeof(QNode));
p->data=(char *)malloc(sizeof(char));
p->data=e;
p->next=NULL;
Q->rear->next=p;
Q->rear=p;
return Q;
}
int QueueEmpty(LinkQueue *Q)
{
if(Q->front==Q->rear)
return 1;
else
return 0;
}
void DeQueue(LinkQueue *Q,char *e)
{
QNode *p=Q->front;
if(QueueEmpty(Q))
printf("队列为空,操作无法进行!!");
p->next=p->next;
e=p->data;
Q->front->next=p->next;
if(Q->rear==p)
free(p);
printf("出队操作成功!!");
}
int Queuelength(LinkQueue *Q)
{
int i=0;
QNode *p;
p=Q->front;
while(p->next)
{
i++;
p=p->next;
}
if(i==1)
{
printf("此队列为空队列!!");
return 0;
}
else
return i;
}
void Gethead(LinkQueue *Q,char *e)
{
QNode *p=Q->front;
e=p->data;
printf("%c ",e);
}
void DestroyQueue(LinkQueue *Q)
{
while(Q->front)
{
Q->rear=Q->front->next;
free(Q->front);
Q->front=Q->rear;
}
printf("清理成功!!!");
}
void visit(char *c)
{
printf("%c",c);
}
void QueueTraverse(LinkQueue *Q)
{
QNode *p=Q->front;
while(p);
{
visit(p->data);
p=p->next;
};
}
void main()
{
int no;
char *c;
int flag=1;
LinkQueue *W;
InitQueue(W);
while(flag)
{
printf("请输入你要的操作: No1入队操作 No2遍历队列 No3求队列长度 No4显示首节点字符 No5清空栈 No6退出");
scanf("%d",&no);
switch(no)
{
case 1: printf("Please input your character, with the blank space key end ");
scanf("%c",c);
while(strcmp(c,' '))
{
EnQueue(W,c);
scanf("%c",c);
};
break;
case 2: QueueTraverse(W);
break;
case 3: Queuelength(W);
break;
case 4: Gethead(W,c);
break;
case 5: DestroyQueue(W);
break;
case 6: break;
default : break;
};
printf("是否继续? No1continue No2exist");
scanf("%d",flag);
}
}
|
|