#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
typedef int Status;
typedef char Elemtype;
typedef struct QueueNode{
Elemtype data;
struct QueueNode* next;
}QueueNode,*QueueNodePtr;
typedef struct LinkQueue{
QueueNodePtr front;
QueueNodePtr rear;
}LinkQueue,*LinkQueuePtr;
//创建一个头节点,front和rear都指向头节点。
Status InitQueue(LinkQueue *q){
q->front = (QueueNode*)malloc(sizeof(QueueNode));
if(! q->front )
return ERROR;
q->rear = q->front;
q->rear->next = NULL;
return OK;
}
Status InsertQueue(LinkQueue *q,Elemtype value){
QueueNode* new;
new = (QueueNode*)malloc(sizeof(QueueNode));
if(! new )
return ERROR;
new->data = value;
new->next = NULL;
q->rear->next = new;
q->rear = new;
return OK;
}
Status DeleteQueue(LinkQueue *q,Elemtype *value){
QueueNode* p;
if( q->front == q->rear ){
//printf("\nQueue Is Empty!\n");
return ERROR;
}
p = q->front->next;
*value = p->data;
q->front->next = p->next;
if(p == q->rear)
q->rear = q->front;
free(p);
return OK;
}
Status DestroyQueue(LinkQueue *q){
while(q->front){
q->rear = q->front->next;
free(q->front);
q->front = q->rear;
}
return OK;
}
Status PrintString(){
LinkQueue LQ;
InitQueue(&LQ);
Elemtype c;
printf("Please Enter a Sentence end of '#'\n");
scanf("%c", &c);
while( '#' != c ){
InsertQueue(&LQ, c);
scanf("%c", &c);
}
while(DeleteQueue(&LQ, &c))
printf("%c", c);
printf("\n");
}
Status ShowQueue(LinkQueue q){
q.front = q.front->next;
while( q.rear != q.front ){
printf("%c ", q.front->data);
q.front = q.front->next;
}
printf("%c\n", q.front->data);
}
int main(){
PrintString();
/*
LinkQueue LQ;
InitQueue(&LQ);
InsertQueue(&LQ,'a');
InsertQueue(&LQ,'b');
InsertQueue(&LQ,'c');
InsertQueue(&LQ,'d');
ShowQueue(LQ);
Elemtype r1,r2;
DeleteQueue(&LQ,&r1);
printf("%c\n",r1);
DeleteQueue(&LQ,&r2);
printf("%c\n",r2);
DestroyQueue(&LQ);
*/
return 0;
}
链队列简单应用将输入的句子输出
最新推荐文章于 2022-05-17 20:53:19 发布