链队列简单应用将输入的句子输出

#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;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值