Linux學習筆記(演算法與資料結構)之 佇列程式碼(C語言)
阿新 • • 發佈:2018-12-12
1、程式碼在VS2010的C++編譯器中編譯通過,可能有極少部分語法不符合C89標準;bool型別無法使用,用int代替
2、由於VS配置問題,沒有分.c和.h檔案書寫;如果要分,最好將Create_Node和Destory_Node加上static關鍵字修飾,他們只會在所屬的.c檔案中使用。
3、英文註釋...
// Cpp_Study.cpp : 定義控制檯應用程式的入口點。 #include "stdafx.h" #include <stdio.h> #include <stdlib.h> /* *************************************************************** @name : Queue_Node @func : The struct to define a Queue_Node @varialbe : Data: The number recorded in the queue Next: The pointer to the node in the direction of bottom of the queue *************************************************************** */ typedef struct Queue_Node { int Data; Queue_Node* Next; } QUEUE_NODE; /* *************************************************************** @name : Queue_Top @func : The struct to define a Queue @varialbe : Top: The pointer of the top-node of the queue,which will be poped first Bottom: The pointer to the bottom-node. New data will always be at the bottom Size: Size of queue. We record it so we don't calculate it *************************************************************** */ typedef struct Queue_Top { QUEUE_NODE* Top; QUEUE_NODE* Bottom; int size; } QUEUE; /* *************************************************************** @name : Destory_Node @func : To Destory a node @para : queue_node: The pointer of node to destory @return value : Pointer of next node, which become the top of the queue after Destory. *************************************************************** */ QUEUE_NODE* Destory_Node(QUEUE_NODE* queue_node) { if (queue_node == NULL) { printf("The queue_node is NULL. Cannot free the memory.\n"); return NULL; } QUEUE_NODE* queue_next_node = queue_node->Next; free(queue_node); return queue_next_node; } /* *************************************************************** @name : Create_Node @func : To create a node @para : last_node : Always pointer to node of the top of the queue.After the create, it will be the second node of the queue counting from the top Data : The number to record in the created node @return value : Pointer of created node *************************************************************** */ QUEUE_NODE* Create_Node(QUEUE_NODE* last_node,int Data) { QUEUE_NODE* queue_node = (QUEUE_NODE*)malloc(sizeof(QUEUE_NODE)); queue_node->Data = Data; queue_node->Next = last_node; return queue_node; } /* *************************************************************** @name : Queue_Init @func : Init a queue @para : queue: The pointer of queue to init @return value : NULL *************************************************************** */ void Queue_Init(QUEUE* queue) { queue->Bottom = NULL; queue->Top = NULL; queue->size = 0; } /* *************************************************************** @name : Queue_Deinit @func : Destory a queue and free the memory @para : queue: The pointer of queue to destory @return value : NULL *************************************************************** */ void Queue_Deinit(QUEUE* queue) { QUEUE_NODE* queue_node = queue->Top; while (queue_node != NULL) { queue_node = Destory_Node(queue_node); queue->size --; } queue->Bottom = NULL; queue->size = 0; queue->Top = NULL; } /* *************************************************************** @name : Queue_Push @func : Push a data to the top of the queue @para : queue: The pointer of queue to push a data to Data : The data to push @return value : NULL *************************************************************** */ void Queue_Push(QUEUE* queue,int Data) { QUEUE_NODE* queue_node = Create_Node(queue->Top,Data); queue->Top = queue_node; if (queue->Bottom == NULL) queue->Bottom = queue_node; queue->size++; } /* *************************************************************** @name : Queue_Pop @func : Pop a data to the top of the queue @para : queue: The pointer of queue to pop a data @return value : The Data that is poped *************************************************************** */ int Queue_Pop(QUEUE* queue) { if (queue->Top == NULL) { printf("The queue is empty. Failed to pop."); return -1; } int Data = queue->Top->Data; QUEUE_NODE* queue_node = Destory_Node(queue->Top); queue->Top = queue_node; queue->size --; return Data; } /* *************************************************************** @name : Queue_Is_Empty @func : To check whether the queue is empty @para : queue: The pointer of queue to check @return value : 1 : The queue is empty 0 : The queue is not empty *************************************************************** */ int Queue_Is_Empty(QUEUE* queue) { return queue->Top == NULL ? 1 : 0; } /* *************************************************************** @name : Queue_Print_Node_Info @func : Print a data of a node @para : queue_node: The node to print @return value : The pointer of the next node *************************************************************** */ QUEUE_NODE* Queue_Print_Node_Info(QUEUE_NODE* queue_node) { printf("%d ",queue_node->Data); return queue_node->Next; } /* *************************************************************** @name : Queue_Output_All @func : Print all data in a queue @para : queue: The pointer of queue to print @return value : NULL *************************************************************** */ void Queue_Output_All(QUEUE* queue) { QUEUE_NODE* queue_node = queue->Top; while(queue_node != NULL) queue_node = Queue_Print_Node_Info(queue_node); } int main() { //Test Code QUEUE queue; Queue_Init(&queue); printf("asd"); while(1) { int operationNum = 0,num = 0; printf("Please input the operation num:\n1.push\n2.pop\n3.show queue size\ \n4.show all data\n5.delete the queue\n"); scanf("%d",&operationNum); if (operationNum == 1) { scanf("%d",&num); Queue_Push(&queue,num); printf("\n"); } else if (operationNum == 2) { printf("\nPoped %d\n",Queue_Pop(&queue)); } else if (operationNum == 3) { printf("\nThe size of the queue is %d\n",queue.size); } else if (operationNum == 4) { printf("\n"); Queue_Output_All(&queue); printf("\n"); } else if (operationNum == 5) { Queue_Deinit(&queue); printf("\n"); } } }