有序链式队列
編寫頭文件
struct queue
{
??? int num;??????????? //代表數據
??? int high;?????????? //優先級1111
??? struct queue *pNext;//存儲下一個節點的地址
};
typedef? struct queue Queue;?????????????????????????? //簡化隊列
Queue * init(Queue *queueHead);??????????????????????? //初始化
Queue * EnQueue(Queue *queueHead, int num, int high);? //入隊
Queue * DeQueue(Queue *queueHead, Queue *pOut);??????? //出隊
Queue * freeall(Queue *queueHead);?????????????????? //清空
void? sort(Queue *queueHead);????????????????????????? //優先級排隊
void printfall(Queue *queueHead);????????????????????? //打印所有數據,遞歸
Queue * insertEnQueue(Queue *queueHead, int num, int high);
?
編寫實現隊列的代碼
#include "Queue.h"
#include <stdio.h>
#include <stdlib.h>
?
//初始化
Queue * init(Queue *queueHead)
{
??? 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;
?
??? if (queueHead == NULL)
??? {
??????? queueHead = pnewnode;
??? }
??? else
??? {
??????? Queue *p = queueHead;
??????? while (p->pNext != NULL)
??????? {
??????????? p = p->pNext;
??????? }
??????? //確定要插入的位置
??????? //插入,這里是尾部插入
??????? p->pNext = pnewnode;
??? }
??? return queueHead;
}
?
//出隊
Queue * DeQueue(Queue *queueHead, Queue *pOut)
{
??? if (queueHead == NULL)
??? {
??????? return NULL;
??? }
??? else
??? {
??????? //這里相當于是
??????? pOut->num = queueHead->num;
??????? pOut->high = pOut->high;
??????? Queue *pTemp = queueHead;????
??????? //記錄要刪除的地址
??????? queueHead = queueHead->pNext;
??????? //釋放節點
??????? free(pTemp);
?
??????? return queueHead;
??? }
}
?
優先級排隊
//void sort(Queue *queueHead)
//{
//? if (queueHead == NULL || queueHead->pNext == NULL)
//? {
//????? 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;
//????????????? }
//????????? }
//? ??? }
//? }
//}
?
//打印所有數據,遞歸
void printfall(Queue *queueHead)
{
??? if (queueHead == NULL)
??? {
??????? return;
??? }
??? else
??? {
??????? printf("%d,%d,%p,%p\n", queueHead->num, queueHead->high, queueHead, queueHead->pNext);
??????? printfall(queueHead->pNext);
??? }
}
?
Queue * insertEnQueue(Queue *queueHead, int num, int high)
{
??? //分配內存
??? Queue? *pnewnode = (Queue *)malloc(sizeof(Queue));
??? pnewnode->num = num;
??? pnewnode->high = high;
??? //節點為空
??? if (queueHead == NULL)
??? {
??????? pnewnode->pNext = NULL;
??????? queueHead = pnewnode;
??????? return queueHead;
??? }
??? else
??? {
??????? //頭插
??? ??? if (pnewnode->high > queueHead->high)
??? ??? {
??????????? //頭部插入
??????????? pnewnode->pNext = queueHead;
??????????? //指向這個節點
??????????? queueHead = pnewnode;
??????????? return queueHead;
??????? }
??????? else
??????? {
??????????? //頭節點
??????????? Queue *p = queueHead;
??????????? while (p->pNext != NULL)
??????????? {
??????????????? p = p->pNext;
??????????? }
??????????? //循環到尾部
??????????? if (pnewnode->high <= p->high)
??????????? {
??????????????? p->pNext = pnewnode;
??????????????? pnewnode->pNext = NULL;
??????????????? return queueHead;
??????????? }
??????????? else
??????????? {
??????????????? Queue *p1, *p2;
??????????????? p1 = p2 = NULL;? //避免野指針
??????????????? p1 = queueHead;? //頭結點
??????????????? while (p1->pNext != NULL)
??????????????? {
??????????????????? p2 = p1->pNext;
??????????????????? if (p1->high >= pnewnode->high && p2->high<pnewnode->high)
??????????????????? {
??????????????????????? pnewnode->pNext = p2;
??????????????????????? p1->pNext = pnewnode;//插入
??????????????????????? break;
??????????????????? }
??????????????????? p1 = p1->pNext;
??????????????? }
??????????????? return queueHead;
??????????? }
??????? }
??? }
}
?
3.編寫主函數
#include "Queue.h"
#include <stdio.h>
#include <stdlib.h>
?
int main(int argc,char *argv[])
{
??? //創建頭結點
??? Queue *phead = NULL;
??? //初始化
??? phead = init(phead);
??? phead = insertEnQueue(phead, 1, 1);
??? printfall(phead);
??? phead = insertEnQueue(phead, 2, 12);
??? printfall(phead);
??? phead = insertEnQueue(phead, 3, 3);
??? printfall(phead);
??? phead = insertEnQueue(phead, 4, 14);
??? printfall(phead);
??? phead = insertEnQueue(phead, 5, 5);
??? printfall(phead);
??? phead = insertEnQueue(phead, 6, 16);
??? printfall(phead);
??? phead = insertEnQueue(phead, 6, 0);
??? printfall(phead);
??? phead = insertEnQueue(phead, 7, 0);
??? printfall(phead);
??? phead = insertEnQueue(phead, 8, 0);
??? printfall(phead);
??? phead = insertEnQueue(phead, 9, 1);
??? printfall(phead);
??? phead = insertEnQueue(phead, 10, 0);
??? printfall(phead);
??? phead = insertEnQueue(phead, 11, 16);
??? printfall(phead);
??? phead = insertEnQueue(phead, 111, 19);
??? printfall(phead);
?
??? printf("打印排序后的鏈式隊列:\n");
??? printfall(phead);
?
?
??? getchar();
??? return 0;
}
?
?
?
總結
- 上一篇: 东莞银行是国企还是私人 哪一年成立的
- 下一篇: volatile,可变参数,memset