有序链式队列

2014-08-04

& 65279;& 65279; & 65279;& 65279; 编写头文件 struct queue { int num; 代表数据 int high; 优先级1111 struct queue *pNext

&#65279;&#65279; &#65279;&#65279;
1. 编写头文件

struct queue

{

int num; //代表数据

int high; //优先级1111

struct queue *pNext;//存储下一个节点的地址

};

typedef struct queue Queue; //简化队列

Queue * EnQueue(Queue *queueHead, int num, int high); //入队

Queue * DeQueue(Queue *queueHead, Queue *pOut); //出队

Queue * insertEnQueue(Queue *queueHead, int num, int high);

1. 编写实现队列的代码

#include "Queue.h"

#include

#include

//初始化

{

return NULL;

}

//入队

Queue * EnQueue(Queue *queueHead, int num, int high)

{

//分配内存

Queue *pnewnode = (Queue *)malloc(sizeof(Queue));

pnewnode->num = num;

pnewnode->high = high;

pnewnode->pNext = NULL;

{

}

else

{

while (p->pNext != NULL)

{

p = p->pNext;

}

//确定要插入的位置

//插入,这里是尾部插入

p->pNext = pnewnode;

}

}

//出队

Queue * DeQueue(Queue *queueHead, Queue *pOut)

{

{

return NULL;

}

else

{

//这里相当于是

pOut->high = pOut->high;

//记录要删除的地址

//释放节点

free(pTemp);

}

}

////优先级排队

//{

// {

// return;

// }

// else

// {

// for (Queue *p1 = queueHead; p1 != NULL;p1 = p1->pNext)

// {

// for (Queue *p2 = queueHead; p2 != NULL;p2 = p2->pNext)

// {

// if (p1->high > p2->high)

// {

// Queue temp;

// temp.num = p1->num;

// p1->num = p2->num;

// p2->num = temp.num;

//

// temp.high = p1->high;

// p1->high = p2->high;

// //交换节点数据

// p2->high = temp.high;

// }

// }

// }

// }

//}

//打印所有数据，递归

{

{

return;

}

else

{

}

}

Queue * insertEnQueue(Queue *queueHead, int num, int high)

{

//分配内存

Queue *pnewnode = (Queue *)malloc(sizeof(Queue));

pnewnode->num = num;

pnewnode->high = high;

//节点为空

{

pnewnode->pNext = NULL;

}

else

{

//头插

{

//头部插入

//指向这个节点

}

else

{

//头节点

while (p->pNext != NULL)

{

p = p->pNext;

}

//循环到尾部

if (pnewnode->high <= p->high)

{

p->pNext = pnewnode;

pnewnode->pNext = NULL;

}

else

{

Queue *p1, *p2;

p1 = p2 = NULL; //避免野指针

while (p1->pNext != NULL)

{

p2 = p1->pNext;

if (p1->high >= pnewnode->high && p2->highhigh)

{

pnewnode->pNext = p2;

p1->pNext = pnewnode;//插入

break;

}

p1 = p1->pNext;

}

}

}

}

}

3.编写主函数

#include "Queue.h"

#include

#include

int main(int argc,char *argv[])

{

//创建头结点

//初始化