/ .c///
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdlib.h>
#include <string>
#include "stackNode.h"
#define N 100
typedef char datatype;
//队列问题
//struct queue
//{
//
// datatype data[N];
// int front;//对头
// int rear;//队尾
//};
Queue *initQueue(Queue * sq)
{
return NULL;
}
//int isempyty(Queue *sq);
//datatype getHead(Queue *sq);
Queue *enQUEUE(Queue *sq, int num, int high)//入队
{
Queue *newNode = (Queue*)malloc(sizeof(Queue));
newNode->num = num;
newNode->high = high;
newNode->pNext = NULL;
if (sq == NULL)
{
sq = newNode;
return sq;
}
else
{
Queue *p = sq;
while (p->pNext != NULL)//找到队尾
{
p = p->pNext;
}
//从而确定要插入的位置!
p->pNext = newNode;
sort(sq);
return sq;
}
}
Queue *deQUEUE(Queue *sq, Queue *pout)//出兑,也可能改变头结点,需要返回queue指针
{
if (sq == NULL)
{
return NULL;
}
else
{
pout->num = sq->num;
pout->high = sq->high;
Queue *p = sq;
sq = p->pNext; //跳过sq指向的结点
free(p);//释放
p = NULL;
return sq;
}
return sq;
}
Queue* freeAll(Queue *sq)//清空
{
if (sq == NULL)
{
return NULL;
}
else
{
Queue *p1 , *p2;
p1 = p2 = NULL;
p1 = sq;
while (p1->pNext !=NULL)
{
p2 = p1->pNext;
p1->pNext = p2->pNext;
free(p2);
p2 = NULL;
}
free(sq);
sq = NULL;
}
return NULL;
}
void sort(Queue *sq)//冒泡排序
{
if (sq == NULL || sq->pNext == NULL)
{
return;
}
for (Queue *p1 = sq; p1->pNext != NULL;p1 = p1->pNext)
{
for (Queue*p2 = p1; p2->pNext != NULL; p2 = p2->pNext)
{
if (p1->high < p2->high)
{
Queue *temp = NULL;//或者定义为Queue temp,如果定义为Queue *temp;则下面必须要对temp分配内存!
temp = (Queue*)malloc(sizeof(Queue));
temp->num = p1->num;
p1->num = p2->num;
p2->num = temp->num;
/
temp->high = p1->high;
p1->high = p2->high;
p2->high = temp->high;//交换节点的数据
}
}
}
}
Queue *insertQUEUE(Queue *sq, int num, int high)//队列插入
{
Queue *temp = NULL;//或者定义为Queue temp,如果定义为Queue *temp;则下面必须要对temp分配内存!
temp = (Queue*)malloc(sizeof(Queue));
temp->num = num;
temp->high = high;
if (sq == NULL)
{
temp->pNext = NULL;
sq= temp;
return sq;
}
else
{
if (temp->high > sq->high)
{
temp->pNext = sq;//头部插入
sq = temp;
return sq;
}
else
{
Queue *p = sq;//头结点
while (p->pNext != NULL)
{
p = p->pNext;
}
//p循环到尾部,尾部插入
if (temp->high <= p->high)
{
p->pNext = temp;
temp->pNext = NULL;
return sq;
}
else
{
Queue *p1, *p2;
p1 =p2 = NULL;
p1 = sq;//头结点
while (p1->pNext !=NULL)
{
p2 = p1->pNext;
if (p1->high >= temp->high && p2->high <= temp->high)
{
temp->pNext = p2;
p1->pNext = temp;
break;
}
p1 = p1->pNext;
}
return sq;
}
}
}
}
void printfAll(Queue *sq)
{
if (sq == NULL)
{
return;
}
else
{
printf("%d, %d, %p, %p \n", sq->high, sq->num, sq, sq->pNext);
printfAll(sq->pNext);//进入下一个结点
}
}
void main()
{
Queue *phead = NULL ;
phead = initQueue(phead);
phead = insertQUEUE(phead, 1, 50);
printfAll(phead);
printf("\n\n");
phead = insertQUEUE(phead, 2, 20);
printfAll(phead);
printf("\n\n");
phead = insertQUEUE(phead, 3, 50);
printfAll(phead);
printf("\n\n");
phead = insertQUEUE(phead, 2, 40);
printfAll(phead);
printf("\n\n");
phead = insertQUEUE(phead, 5, 70);
printfAll(phead);
printf("\n\n");
phead = insertQUEUE(phead, 2, 70);
printfAll(phead);
Queue *p = NULL;
p = initQueue(p);
p = (Queue*)malloc(sizeof(Queue));
phead = freeAll(phead);
printfAll(phead);
/*while (phead != NULL)
{*/
//phead = deQUEUE(phead, p);
//printf("出兑后\n");
//printf("%d, %d \n", p->high, p->num);
//}
system("pause");
}
.h //
#define N 100
typedef char datatype;
//队列问题
struct queue
{
int num;//代表数据
int high;//优先级
struct queue *pNext;//存储下一个节点的地址
};
typedef struct queue Queue;
Queue *initQueue(Queue * sq);
//int isempyty(Queue *sq);
//datatype getHead(Queue *sq);
Queue *enQUEUE(Queue *sq, int num,int high);//入队
Queue *deQUEUE(Queue *sq,Queue *pout);//出兑,也可能改变头结点,需要返回queue指针
Queue* freeAll(Queue *sq);//清空
void sort(Queue *sq);//排序
Queue *insertQUEUE(Queue *sq, int num, int high);//队列插入
void printfAll(Queue *sq);