C语言 队列例程
队列的数据成员为结构体或数组
#include <stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef struct nodeArr
{
int dat[2];
struct nodeArr* next;
}NodeArr;
NodeArr* initQueueAr();
int is_emeptyAr(NodeArr* Q);
void enQueueAr(NodeArr* Q, int* arr);
void deQueueAr(NodeArr* Q);
void printQueueAr(NodeArr* Q);
#include "queSt.h"
//队列的初始化
NodeArr* initQueueAr()
{
NodeArr* Q = (NodeArr*)malloc(sizeof(NodeArr));
assert(Q);
Q->next = NULL;
*Q->dat = 0; // 用来记录元素个数
return Q;
}
//判断队列是否为空
int is_emeptyAr(NodeArr* Q)
{
if (*Q->dat == 0 || Q->next == NULL)
return 1;
else
return 0;
}
//入队 尾插
void enQueueAr(NodeArr* Q, int *arr)
{
NodeArr* q = Q->next;
NodeArr* node = (NodeArr*)malloc(sizeof(NodeArr));
assert(node);
if (is_emeptyAr(Q))
{
Q->next = node;
//node->data = data;
*node->dat = *arr;
*(node->dat + 1) = *(arr + 1);
node->next = NULL;
}
else
{
while (q->next) //两种找到尾结点的方式
{
q = q->next;
}
q->next = node;
*node->dat = *arr;
*(node->dat + 1) = *(arr + 1);
node->next = NULL;
}
(*Q->dat)++;
}
//出队 头出
void deQueueAr(NodeArr* Q)
{
NodeArr* node = Q->next;
if (is_emeptyAr(Q))
return;
else
Q->next = node->next;
free(node);
node = NULL;
(*Q->dat)--;
}
// 打印函数
void printQueueAr(NodeArr* Q)
{
NodeArr* node = Q->next;
while (node)
{
printf("%d, %d. --\n", *(node->dat), *(node->dat + 1));
//printf();
node = node->next;
}
printf("Arr over!\n");
}
#include <iostream>
#include "queue.h"
#include "que2.h"
#include "queSt.h"
#define N 100
int test_p1();
int main()
{
//std::cout << "Hello World!\n";
char a[N] = {0};
Node* Q;
NodeArr* B;
int tc_ar1[2] = {3, 5};
int tc_ar2[2] = {7, 8};
int tc_ar3[2] = {12, 15};
//scanf_s("%s", a, sizeof(a));
//printf("%s\n", a);//方法二
test_p1();
//Q = initQueue();
//enQueue(Q, 1);
//enQueue(Q, 2);
//enQueue(Q, 3);
//enQueue(Q, 8);
//enQueue(Q, 5);
//deQueue(Q);
//printf("%d\n", Q->data);
//printQueue(Q);
B = initQueueAr();
enQueueAr(B, tc_ar1);
enQueueAr(B, tc_ar2);
enQueueAr(B, tc_ar3);
deQueueAr(B);
enQueueAr(B, tc_ar1);
enQueueAr(B, tc_ar3);
deQueueAr(B);
printQueueAr(B);
printf("hello W!");
}
int test_p1()
{
printf("CCT2 Start****\n");
return 0;
}