day6链式队列

linkedqueue.h

#ifndef __LINKED_QUEUE_H__
#define __LINKED_QUEUE_H__

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

typedef int datatype;
typedef struct Node{
	union{
		int len;
		datatype data;
	};
	struct Node *next;
}*linkedlist;
typedef struct Queue{
	linkedlist front;
	linkedlist rear;
}*linkedqueue;

linkedqueue create();
linkedlist create_node();
int enqueue(linkedqueue,datatype);
int dequeue(linkedqueue);
void output(linkedqueue);
linkedqueue free_space(linkedqueue);
#endif

linkedqueue.c

#include "linkedqueue.h"

linkedqueue create(){
	linkedqueue myQueue = (linkedqueue)malloc(sizeof(struct Queue));
	if(myQueue == NULL){
		return NULL;
	}
	linkedlist head  = (linkedlist)malloc(sizeof(struct Node));
	if(head == NULL){
		return NULL;
	}
	head->len = 0;
	head->next = NULL;
	myQueue->front = head;
	myQueue->rear = head;
	return myQueue;

}
linkedlist create_node(){
	linkedlist node = (linkedlist)malloc(sizeof(struct Node));
	if(node == NULL){
		return NULL;
	}
	node->data = 0;
	node->next = NULL;
	return node;
}
int enqueue(linkedqueue myQueue,datatype e){
	if(myQueue == NULL){
		return -1;
	}
	linkedlist newNode = create_node();
	if(newNode == NULL){
		return -1;
	}
	linkedlist p = myQueue->rear;
	newNode->next = p->next;
	newNode->data = e;
	p->next = newNode;
	myQueue->rear = newNode;
	myQueue->front->len++;
	printf("Element %d is enqueued.\n",myQueue->rear->data);
	//printf("The length is  %d\n",myQueue->front->len);
	return 0;
}
int dequeue(linkedqueue myQueue){
	if(myQueue == NULL || myQueue->front == myQueue->rear){
		return -1;
	}
	linkedlist p = myQueue->front->next;
	if(p == myQueue->rear){
		myQueue->rear = myQueue->front;
	}
	int temp = p->data;
	myQueue->front->next = p->next;
	free(p);
	p = NULL;
	myQueue->front->len--;
	//printf("The length is %d\n",myQueue->front->len);
	return temp;
}
void output(linkedqueue myQueue){
	if(myQueue == NULL || myQueue->front == myQueue->rear){
		printf("The queue is empty or doesn't exist.\n");
		return;
	}
	linkedlist p = myQueue->front;
	while(p->next){
		p = p->next;
		//printf("Element %d in the queue\n",p->data);
	}
}
linkedqueue free_space(linkedqueue myQueue){
	if(myQueue == NULL){
		return NULL;
	}
	while(myQueue->rear != myQueue->front){
		dequeue(myQueue);	
	}
	free(myQueue->front);
	myQueue->front = NULL;
	myQueue->rear = NULL;
	free(myQueue);
	myQueue = NULL;
	return myQueue;
	
}

linkedqueue_main.c

#include "linkedqueue.h"
int main(int argc, const char *argv[])
{
	linkedqueue myQueue = create();
	for(int i = 0; i < 5;i++){
		enqueue(myQueue,i);
	}
	output(myQueue);
	for(int i = 0;i < 2;i++){
		int temp = dequeue(myQueue);
		printf("%d is dequeued.\n",temp);
	}
	output(myQueue);
	myQueue = free_space(myQueue);
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值