キューとその基本操作

19571 ワード

〹チェーン式ヘッドファイル
#pragma once
#include
#include
#include
#define _CRT_SECURE_NO_WARNINGS 1
typedef int QDataType;
typedef struct LinkList
{
	struct LinkLis *next;
	QDataType data;
}QLinkList;

typedef struct Queue
{
	QLinkList *front;
	QLinkList *rear;
	int sz;
}Queue;

void QueueInit(Queue* Qlist);
void QueuePush(Queue* Qlist, QDataType data);
void QueuePop(Queue* Qlist);
QDataType QueueFront(Queue* Qlist);
QDataType QueueBack(Queue*  Qlist);
int QueueSize(Queue* Qlist);
int QueueEmpty(Queue* Qlist);

〹チェーン操作
#include "Queue.h"
QLinkList* BuyNode( QDataType Data)
{
	QLinkList *cur = (QLinkList*)malloc(sizeof(QLinkList));
	if (cur == NULL)
	{
		printf("QueueInit :: malloc failed
"
); exit(0); } cur->data = Data; cur->next = NULL; return cur; } void QueueInit(Queue* Qlist) { Qlist->front = NULL; Qlist->rear = NULL; Qlist->sz = 0; } void QueuePush(Queue*Qlist, QDataType data) { assert(Qlist); QLinkList *cur = BuyNode(data); QLinkList *pcur = Qlist->front; if (pcur != NULL) { while (pcur->next != NULL) { pcur = pcur->next; } pcur->next = cur; Qlist->rear = pcur->next; } else { Qlist->front = cur; Qlist->rear = cur; } Qlist->sz++; } void QueuePop(Queue* Qlist) { assert(Qlist); if (Qlist->sz == 0) { printf(" , !
"
); system("pause"); return; } QLinkList *cur = Qlist->front; Qlist->front = Qlist->front->next; free(cur); Qlist->sz--; } QDataType QueueFront(Queue* Qlist) { assert(Qlist); if (Qlist->sz == 0) { printf(" ,
"
); return 0; } return Qlist->front->data; } QDataType QueueBack(Queue* Qlist) { assert(Qlist); if (Qlist->sz == 0) { printf(" ,
"
); } return Qlist->rear->data; } int QueueSize(Queue* Qlist) { assert(Qlist); return Qlist->sz; } int QueueEmpty(Queue* Qlist) { assert(Qlist); if (Qlist->sz = 0) { return 0; } else { return 1; } }
ヽoo!ツ
#include "Queue.h"
int main()
{
	Queue q;
	QueueInit(&q);
	QueuePush(&q, 1);
	QueuePush(&q, 2);
	QueuePush(&q, 3);
	QueuePush(&q, 4);
	QueuePush(&q, 5);
	QueuePop(&q);
    printf("QFront = %d 
"
,QueueFront(&q)); printf("QRear = %d
"
, QueueBack(&q)); printf("QSize = %d
"
, QueueSize(&q)); printf("Queue is Empty ? == %d
"
, QueueEmpty(&q)); system("pause"); return 0; }