码迷,mamicode.com
首页 > 其他好文 > 详细

队列的基本操作

时间:2014-06-11 06:37:33      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:c   数据结构   队列   存储结构   进队出队   

队列的存储结构有两种:一种是线性表存储,一种是链式存储。用线性表存储时,要注意队列的长度有没有超过预先设置的大小,在这个程序中,队列的可以在存满的时候,自动增加队列的长度。用链表存储,则没有长度的限制。下面分别是这两种存储结构的实现。

线性表存储:

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

#define QUEUE_INIT_SIZE 10
#define QUEUE_REALLOCATION 2

typedef int ElemType;

/*顺序队列的存储结构定义*/
typedef struct SqQueue
{
	ElemType *base;
	int front;
	int rear;
	int QueueLength;
}SqQueue;

void Init_SqQueue(SqQueue *S)
{
	S->base = (ElemType*)malloc(QUEUE_INIT_SIZE*sizeof(ElemType));
	if (!S->base)
		exit(1);
	S->rear = S->front = 0;
	S->QueueLength = QUEUE_INIT_SIZE;
}

void En_SqQueue(SqQueue *S, ElemType e)
{
	if ((S->rear + 1) % S->QueueLength == S->front){
		S->base = (ElemType*)malloc(S->base,(S->QueueLength+QUEUE_REALLOCATION)*sizeof(ElemType));
		if (!S->base)
			exit(1);
		if (S->front > S->rear){
			int i;
			for (i = S->front; i < S->QueueLength; ++i){
				S->base[i + QUEUE_REALLOCATION] = S->base[i];
			}
			S->front += QUEUE_REALLOCATION;
		}
		S->QueueLength += QUEUE_REALLOCATION;
	}
	S->base[S->rear] = e;
	S->rear = (S->rear + 1) % S->QueueLength;
}

int De_SqQueue(SqQueue *S, ElemType *e)
{
	if (S->rear == S->front)
		return 0;
	*e = S->base[S->front];
	S->front = (S->front + 1) % S->QueueLength;
	return 1;
}

int SqQueueLength(SqQueue S)
{
	return (S.rear - S.front + S.QueueLength) % S.QueueLength;
}

int is_SqQueueEmpty(SqQueue S)
{
	if (S.front == S.rear)
		return 1;
	else
		return 0;
}

void SqQueueTest()
{
	SqQueue S;
	Init_SqQueue(&S);
	printf("Please input some numbers to enqueue(Ctrl+Z to end):\n");
	int e;
	while (scanf("%d", &e) != EOF)
		En_SqQueue(&S, e);
	printf("The dequeue sequence is:\n");
	while (!is_SqQueueEmpty(S)){
		De_SqQueue(&S,&e);
		printf("%d ",e);
	}
	printf("\n");
}

int main()
{
	SqQueueTest();
	return 0;
}



链表存储:

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

/*链式队列的存储结构定义*/
typedef struct LinkQueue_Node
{
	ElemType data;
	struct LinkQueue_Node *next;
}*Queue_pNode, Queue_Node;

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

void Init_LinkQueue(LinkQueue *L)
{
	L->front = L->rear = (Queue_pNode)malloc(sizeof(Queue_Node));
	if (!L->front)
		exit(1);
	L->front->data = 0;
	L->front->next = NULL;
}

void En_LinkQueue(LinkQueue *L, ElemType e)
{
	Queue_pNode p = (Queue_pNode)malloc(sizeof(Queue_Node));
	if (!p)
		exit(1);
	p->data = e;
	p->next = NULL;
	L->rear->next = p;
	L->rear = p;
}

int De_LinkQueue(LinkQueue *L, ElemType *e)
{
	Queue_pNode p=L->front->next;
	if (L->front == L->rear)
		return 0;
	*e = p->data;
	L->front->next = p->next;
	if (p == L->rear)
		L->rear = L->front;
	free(p);
	return 1;
}

int LinkQueueLength(LinkQueue L)
{
	Queue_pNode p = L.front;
	if (L.front == L.rear)
		return 0;
	int length = 0;
	while (p != L.rear){
		++length;
		p = p->next;
	}
	return length;
}

int is_LinkQueueEmpty(LinkQueue L)
{
	if (L.front == L.rear)
		return 1;
	else
		return 0;
}

void LinkQueueTest()
{
	LinkQueue L;
	Init_LinkQueue(&L);
	printf("Please input some numbers to enqueue(Ctrl+Z to end).\n");
	int e;
	while (scanf("%d", &e) != EOF)
		En_LinkQueue(&L,e);
	printf("The dequeue sequence is : \n");
	while (!is_LinkQueueEmpty(L)){
		De_LinkQueue(&L,&e);
		printf("%d ",e);
	}
	printf("\n");
}

int main()
{
	LinkQueueTest();
	return 0;
}



队列的基本操作,布布扣,bubuko.com

队列的基本操作

标签:c   数据结构   队列   存储结构   进队出队   

原文地址:http://blog.csdn.net/nyist327/article/details/29416277

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!