Establishment and operation of the queues

First, the basic idea

1. the sequence of stack operation is almost the same, except that when deleting elements, delete the first node;

2. The sequence of stack space by the base pointer to the application, using the top pointer insertion, deletion does not affect the base, so the last time can be fully released to release;

3. But different order queue, if the establishment of front and rear_next (next position last element), if the application by the memory front, in the front of the delete operation will change

   Point, will be a memory leak when the final release;

4. Therefore, the establishment of additional specialized memory pointer to generate, modify the front and rear_next this will not affect the release of the last memory;

Second, the code is as follows

/*
项目名称:顺序队列的建立与基本操作

编译环境:VC++ 2008

作者相关:。。。

最后修改:2019.6.23


学习目标:初始化、销毁、清空、判空、求长、返回队头元素、插入元素、删除元素、输出队列中元素

注意事项:1.测试所有功能是否正常

*/

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

#define QUEUE_INIT_SIZE   100
#define QUEUEINCREMENT    10

#define ERROR  0
#define OK     1

typedef int  ElemType;
typedef bool Status;

typedef struct{

	ElemType *front;//头指针
	ElemType *rear_next;//rear指向最后一个元素的下一个位置
	ElemType *room;//用于产生动态内存
	int queuesize;//队列容量

}SqQueue;

Status InitQueue(SqQueue *s);

Status DestroyQueue(SqQueue *s);

Status ClearQueue(SqQueue *s);

Status QueueEmpty(SqQueue s);

int    QueueLength(SqQueue s);

Status GetHead(SqQueue s,ElemType *e);

Status EnQueue(SqQueue *s,ElemType e);

Status DeQueue(SqQueue *s,ElemType *e);

Status QueueTraverse(SqQueue s);

void visit(ElemType c);

int main()
{
	SqQueue s;
	ElemType e;

	InitQueue(&s);

	if(QueueEmpty(s))
		printf("队列为空!\n\n");
	else
		printf("队列非空!\n\n");

	int k = QueueLength(s);
	printf("队列的长度为:%d \n\n",k);

	for(int i=1;i<10;i++)
		EnQueue(&s,i);
	QueueTraverse(s);

	GetHead(s,&e);
	printf("队首元素为:%d \n\n",e);

	DeQueue(&s,&e);
	printf("删除的元素为:%d \n\n",e);

	ClearQueue(&s);

	DestroyQueue(&s);

	return 0;
}

Status InitQueue(SqQueue *s)
{
	s->room = (ElemType *)malloc(QUEUE_INIT_SIZE*sizeof(ElemType));
	if(!s->room)
		return ERROR;

	s->rear_next = s->front = s->room;
	s->queuesize = QUEUE_INIT_SIZE;

	return OK;
}

Status DestroyQueue(SqQueue *s)
{
	free(s->room);

	return OK;
}

Status ClearQueue(SqQueue *s)
{
	s->rear_next = s->front = s->room;

	return OK;
}

Status QueueEmpty(SqQueue s)
{
	if(s.rear_next==s.front)
		return true;
	else
		return false;
}

int    QueueLength(SqQueue s)
{
	return s.rear_next-s.front;
}

Status GetHead(SqQueue s,ElemType *e)
{
	if(!QueueEmpty(s))
		*e = *s.front;

	return OK;
}

Status EnQueue(SqQueue *s,ElemType e)//操作与栈相同
{
	if(s->queuesize<=QueueLength(*s))
	{
		s->room = (ElemType *)realloc(s->room,
			(s->queuesize+QUEUEINCREMENT)*sizeof(ElemType)); 
		if(!s->room)
			return ERROR;

		s->front = s->room;
		s->rear_next=s->room+s->queuesize;
		s->queuesize+=QUEUEINCREMENT;
	}

	*s->rear_next++=e;

	return OK;
}

Status DeQueue(SqQueue *s,ElemType *e)
{

	if(QueueEmpty(*s))
		return ERROR;
	else
		*e = *s->front++;

	return OK;
}

Status QueueTraverse(SqQueue s)
{

	printf("队列内容: ");
	while(s.front != s.rear_next)
	{
		visit(*s.front);
		++s.front;
	}
	printf("\n\n");
	return OK;
}

void visit(ElemType c)
{
	printf("%d ",c);
}

Third, the effect

 

Guess you like

Origin blog.csdn.net/Sruggle/article/details/93378688
Recommended