C语言实现链式队列

C语言实现,只是简单实现了初始化、入队、出队和打印四个功能比较简单。

#include<stdio.h>
#include<stdlib.h>

typedef int elemType;

typedef struct qNode{
	elemType info;
	struct qNode *next;
}qNode;

typedef struct{
	qNode *front;
	qNode *rear;
}LinkQueue;

void initQueue(LinkQueue *q){
//	q->front = q->rear = (qNode *)malloc(sizeof(qNode));
//	q->front->next = NULL;
	q->front = q->rear = NULL;
}


void enQueue(LinkQueue *q, elemType value){
	qNode *ptr;
	ptr = (qNode *)malloc(sizeof(qNode));
	ptr->info = value;
	ptr->next = NULL;
	if(q->front == NULL ){
		q->front = ptr;
		q->rear = ptr;
	}
	else{
		q->rear->next = ptr;
		q->rear = ptr;
	}
}

int deQueue(LinkQueue *q){
	qNode *ptr = q->front;
	if(q->front != NULL){
		if(ptr != q->rear)
			q->front = ptr->next;
        else
            q->front = q->rear = NULL;
		free(ptr);
		return 0;
	}
    printf("empty queue !\n");
	return -1;
}

void printQueue(LinkQueue *q){
	qNode *ptr = q->front;
	if(ptr == q->rear && ptr == NULL)
		return ;
	if(ptr != NULL && q->front == q->rear){
		printf("%d\n",ptr->info);
		return ;
	}
	while(ptr != q->rear){
		printf("%d -> ",ptr->info);
		ptr = ptr->next;
	}
	if(q->front != NULL)
		printf("%d\n",ptr->info);
		return ;
}

int main(){
	LinkQueue queue;
	int i;
	initQueue(&queue);
    deQueue(&queue);
    deQueue(&queue);
	for(i=0;i<10;i++){
		enQueue(&queue,i);
		printQueue(&queue);
	}
	for(i=0;i<12;i++){
		if(0 == deQueue(&queue));
			printQueue(&queue);
	}
	return 0;
}




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值