array queue
"arrayqueue.h"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #ifndef _ARRAY_QUEUE_ #define _ARRAY_QUEUE_ #include "structqueue.h" typedef struct Array_Queue_Node_Type Array_Queue_Node; typedef struct Array_Queue_Type Array_Queue; Array_Queue* Create_Array_Queue(int Maximum_Element_Count); int Enqueue_Array_Queue(Array_Queue* Pointer_Queue, Array_Queue_Node element); Array_Queue_Node* Dequeue_Array_Queue(Array_Queue* Pointer_Queue); Array_Queue_Node* Peek_Array_Queue(Array_Queue* Pointer_Queue); void Delete_Array_Queue(Array_Queue* Pointer_Queue); int Is_Array_Queue_Full(Array_Queue* Pointer_Queue); int Is_Array_Queue_Empty(Array_Queue* Pointer_Queue); #endif // !_ARRAY_QUEUE_ #ifndef _COMMON_QUEUE_DEFAULT_ #define _COMMON_QUEUE_DEFAULT_ #define TRUE 1 #define FALSE 0 #endif // !_COMMON_QUEUE_DEFAULT_ | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #ifndef _STRUCT_QUEUE_ #define _STRUCT_QUEUE_ struct Array_Queue_Node_Type { char data; }; struct Array_Queue_Type { int Maximum_Element_Count; int Current_Element_Count; int front; int rear; struct Array_Queue_Node_Type* Pointer_Element; }; #endif // !_STRUCT_QUEUE_ | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | #include <stdio.h> #include <stdlib.h> #include <string.h> #include "arrayqueue.h" Array_Queue* Create_Array_Queue(int Maximum_Element_Count) { if (Maximum_Element_Count < 1) { printf("Error! The size must be greater then zero. Create_Array_Queue()\n"); return NULL; } Array_Queue* Pointer_Return = (Array_Queue*)malloc(1 * sizeof(Array_Queue)); if ((Array_Queue*)Pointer_Return == NULL) { printf("Error! The dynamic memory allocation.(1) Create_Array_Queue()\n"); return NULL; } (void*)memset((Array_Queue*)Pointer_Return, 0, sizeof(Array_Queue)); Pointer_Return->Maximum_Element_Count = Maximum_Element_Count; Pointer_Return->front = -1; Pointer_Return->rear = -1; (Array_Queue*)Pointer_Return->Pointer_Element = (Array_Queue_Node*)malloc(Maximum_Element_Count * sizeof(Array_Queue_Node)); if ((Array_Queue*)Pointer_Return->Pointer_Element == NULL) { printf("Error! The dynamic memory allocation.(2) Create_Array_Queue()\n"); free((Array_Queue*)Pointer_Return); return NULL; } (void*)memset((Array_Queue_Node*)Pointer_Return->Pointer_Element, 0, Maximum_Element_Count * sizeof(Array_Queue_Node)); return (Array_Queue*)Pointer_Return; } int Enqueue_Array_Queue(Array_Queue* Pointer_Queue, Array_Queue_Node element) { if ((Array_Queue*)Pointer_Queue == NULL) { printf("Error! The dynamic memory allocation. Enqueue_Array_Queue ()\n"); return FALSE; } else if (Is_Array_Queue_Full((Array_Queue*)Pointer_Queue) == TRUE) { printf("Error! The Queue was full. Enqueue_Array_Queue()\n"); return FALSE; } Pointer_Queue->rear++; *(struct Array_Queue_Node_Type*)(Pointer_Queue->Pointer_Element + (int)Pointer_Queue->rear) = *(struct Array_Queue_Node_Type*) &element; Pointer_Queue->Current_Element_Count++; return TRUE; } Array_Queue_Node* Dequeue_Array_Queue(Array_Queue* Pointer_Queue) { if ((Array_Queue*)Pointer_Queue == NULL) { printf("Error! The array queue is null. Dequeue_Array_Queue()\n"); return NULL; } else if (Is_Array_Queue_Empty((Array_Queue*)Pointer_Queue) == TRUE) { printf("Error! The array queue is empty. Dequeue_Array_Queue()\n"); return NULL; } Array_Queue_Node* Pointer_Return = (Array_Queue_Node*)malloc(1 * sizeof(Array_Queue_Node)); if ((Array_Queue_Node*)Pointer_Return == NULL) { printf("Error! The dynamic memory allocation. Dequeue_Array_Queue()\n"); return NULL; } Pointer_Queue->front++; *(Array_Queue_Node*)Pointer_Return = *(Array_Queue_Node*)(Pointer_Queue->Pointer_Element + (int)Pointer_Queue->front); Pointer_Queue->Current_Element_Count--; return (Array_Queue_Node*)Pointer_Return; } Array_Queue_Node* Peek_Array_Queue(Array_Queue* Pointer_Queue) { if ((Array_Queue*)Pointer_Queue == NULL) return NULL; else if (Is_Array_Queue_Empty((Array_Queue*)Pointer_Queue) == TRUE) return NULL; return &(*(Array_Queue*)(Pointer_Queue->Pointer_Element + (int)Pointer_Queue->front + 1)); } void Delete_Array_Queue(Array_Queue* Pointer_Queue) { if ((Array_Queue*)Pointer_Queue == NULL) return; else if ((Array_Queue*)Pointer_Queue->Pointer_Element != NULL) free((Array_Queue*)Pointer_Queue->Pointer_Element); free((Array_Queue*)Pointer_Queue); return; } int Is_Array_Queue_Full(Array_Queue* Pointer_Queue) { if ((Array_Queue*)Pointer_Queue == NULL) return FALSE; else if (Pointer_Queue->Current_Element_Count == Pointer_Queue->Maximum_Element_Count || Pointer_Queue->rear == Pointer_Queue->Maximum_Element_Count - 1) return TRUE; return FALSE; } int Is_Array_Queue_Empty(Array_Queue* Pointer_Queue) { if ((Array_Queue*)Pointer_Queue == NULL) return FALSE; else if (Pointer_Queue->Current_Element_Count == 0) return TRUE; return FALSE; } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | #include <stdio.h> #include <stdlib.h> #include "arrayqueue.h" void Display_Array_Queue(Array_Queue* Pointer_Queue); int Enqueue_Array_Queue_Character(Array_Queue* Pointer_Queue, char data); int main(int argc, char** argv) { Array_Queue* Pointer_Queue = NULL; (Array_Queue*)Pointer_Queue = (Array_Queue*)Create_Array_Queue(4); if ((Array_Queue*)Pointer_Queue == NULL) return -1; Enqueue_Array_Queue_Character((Array_Queue*)Pointer_Queue, 'A'); Enqueue_Array_Queue_Character((Array_Queue*)Pointer_Queue, 'B'); Enqueue_Array_Queue_Character((Array_Queue*)Pointer_Queue, 'C'); Enqueue_Array_Queue_Character((Array_Queue*)Pointer_Queue, 'D'); Display_Array_Queue((Array_Queue*)Pointer_Queue); Array_Queue_Node* Pointer_Node = Dequeue_Array_Queue((Array_Queue*)Pointer_Queue); if ((Array_Queue_Node*)Pointer_Node != NULL) { printf("Dequeue Value-[%c]\n", (char)Pointer_Node->data); free((Array_Queue_Node*)Pointer_Node); } Display_Array_Queue((Array_Queue*)Pointer_Queue); (Array_Queue_Node*)Pointer_Node = (Array_Queue_Node*)Peek_Array_Queue((Array_Queue*)Pointer_Queue); if ((Array_Queue_Node*)Pointer_Node != NULL) { printf("Peek Value-[%c]\n", (char)Pointer_Node->data); } Display_Array_Queue((Array_Queue*)Pointer_Queue); Enqueue_Array_Queue_Character((Array_Queue*)Pointer_Queue, 'E'); Display_Array_Queue((Array_Queue*)Pointer_Queue); Delete_Array_Queue((Array_Queue*)Pointer_Queue); (Array_Queue*)Pointer_Queue = NULL; Display_Array_Queue((Array_Queue*)Pointer_Queue); return 0; } int Enqueue_Array_Queue_Character(Array_Queue* Pointer_Queue, char data) { Array_Queue_Node node = { 0, }; node.data = data; return Enqueue_Array_Queue((Array_Queue*)Pointer_Queue, node); } void Display_Array_Queue(Array_Queue* Pointer_Queue) { if ((Array_Queue*)Pointer_Queue == NULL) return; printf("Size queue : %d, Current node count : %d\n", Pointer_Queue->Maximum_Element_Count, Pointer_Queue->Current_Element_Count); int index = 0; for (index = (int)Pointer_Queue->front + 1; index <= (int)Pointer_Queue->rear; index++) { printf("[%d]-[%c]\n", index, (char)(*(Pointer_Queue->Pointer_Element + index)).data); } return; } | cs |
0 개의 댓글:
댓글 쓰기