NodeQueue1.h
#ifndef _NODE_QUEUE_H
#define _NODE_QUEUE_H
#include <stdio.h>
#include <stdlib.h>
#define DataType int
struct NodeQueue
{
int num;//编号
DataType data[5];//数据
int length;
struct NodeQueue *pNext;
};
typedef struct NodeQueue Queue;
void Init(Queue **phead);
void Enqueue(Queue **phead, int num, DataType data[], int priority,int length);
void Dequeue(Queue **phead, Queue *res);
void Freeall(Queue **phead);
void Showall(Queue *phead);
#endif
NodeQueue1.c
#include "NodeQueue1.h"
void Init(Queue **phead)
{
*phead = NULL;
}
void Enqueue(Queue **phead, int num, DataType data[], int priority,int length)
{
int i;
Queue *newnode = (Queue *)malloc(sizeof(Queue));
newnode->num = num;
for(i=0;i<length;i++){
newnode->data[i] = data[i];
}
newnode->length = length;//length为传入数组的元素的个数
newnode->pNext = NULL;
if (*phead == NULL)
{
*phead = newnode;
}
else
{
Queue *head = *phead;
while (head->pNext != NULL)
{
head = head->pNext;
}
head->pNext = newnode;
}
}
void Dequeue(Queue **phead, Queue *res)
{
int i;
if (*phead == NULL)
{
return;
}
else
{
Queue *p = (*phead)->pNext;
for(i=0;i<(*phead)->length;i++){
res->data[i] = (*phead)->data[i];
}
res->num = (*phead)->num;
res->length = (*phead)->length;
free(*phead);
*phead = p;
}
}
void Freeall(Queue **phead)
{
if (*phead == NULL)
{
return;
}
else
{
Queue *p1 = NULL, *p2 = NULL;
p1 = *phead;
while (p1->pNext != NULL)
{
p2 = p1->pNext;
p1->pNext = p2->pNext;
free(p2);
}
free(p1);
p1 = NULL;
}
}
void Showall(Queue *phead)
{
int i;
if (phead == NULL)
{
puts("there no data in queue");
}
else
{
puts("*************Data Start****************");
while (phead != NULL)
{
printf("num=%d,data=%d %d %d %d %d\n", phead->num, phead->data[0], phead->data[1],phead->data[2],phead->data[3],phead->data[4]);
phead = phead->pNext;
}
puts("*************Data End******************");
}
putchar('\n');
}
main.c
#include "NodeQueue1.h"
void main()
{
Queue *phead = NULL;
Queue res;
int i;
int p1[5]={1,2,3,4,5};
int p2[5]={6,7,8,9,10};
Init(&phead);
Showall(phead);
Enqueue(&phead,1,p1,6,5);
Enqueue(&phead,2,p2,4,5);
Showall(phead);
while (phead != NULL)
{
Dequeue(&phead, &res);
printf("res->length:%d\n",res.length);
printf("Array %d:\n",res.num);
for(i=0;i<res.length;i++){
printf("%d ",res.data[i]);
}
printf("\n");
Showall(phead);
}
}