bosthhe1 / -

数据结构与算法(初阶)
0 stars 0 forks source link

队列 #8

Open bosthhe1 opened 2 years ago

bosthhe1 commented 2 years ago

主函数

define _CRT_SECURE_NO_WARNINGS 1

include"head.h"

void TestQueue1() { QueueQ Queue; QueueInit(&Queue); QueuePlus(&Queue,1); QueuePlus(&Queue, 2); Queuefront(&Queue); QueuePop(&Queue);//出队列,先进先出,这里先出1 Queuefront(&Queue); QueuePop(&Queue);//这里出队列,出2 QueuePlus(&Queue, 3);//入列 Queuefront(&Queue);//只有一个3 } int main() { TestQueue1(); return 0; }

bosthhe1 commented 2 years ago

函数定义

define _CRT_SECURE_NO_WARNINGS 1

include"head.h"

void QueueInit(QueueQ ps) { ps->Head = NULL; ps->Tail = NULL; } void QueuePlus(QueueQ ps,int x) { Queuequeue p = (Queuequeue )malloc(sizeof(Queuequeue)); p->next = NULL; p->x = x; if (p == NULL) { printf("FILE OPEN FAIL"); exit(-1); } if (ps->Head == NULL){ ps->Tail = ps->Head = p; } else { ps->Tail->next = p; ps->Tail = ps->Tail->next; } } void QueuePop(QueueQ ps) { assert(ps); assert(ps->Head); Queuequeue newHead = ps->Head->next; free(ps->Head); ps->Head = newHead; if (ps->Head == NULL) ps->Tail = NULL; } bool QueueEmpty(QueueQ ps) { if (ps->Head != NULL) return false; else return true; } void QueueBack(QueueQ ps) { assert(ps); printf("%d ", ps->Tail->x); } void Queuefront(QueueQ *ps) { assert(ps); printf("%d ", ps->Head->x); }

bosthhe1 commented 2 years ago

函数声明

pragma once

include

include

include

include

typedef struct Queuequeue { Queuequeue next; int x; }Queuequeue; typedef struct Queueq { Queuequeue Head; Queuequeue Tail; }QueueQ; void QueueInit(QueueQ ps); void QueuePlus(QueueQ ps,int x); void QueuePop(QueueQ ps); void QueueBack(QueueQ ps); void Queuefront(QueueQ ps); bool QueueEmpty(QueueQ *ps);