Linked queue _Alpha
"linked queue.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 27 28 29 30 31 32 33 34 35 36 | #ifndef _LINKED_QUEUE_ #define _LINKED_QUEUE_ struct Queue_Node_Type { char data; struct Queue_Node_Type* Pointer_Next; }; struct Linked_Queue_Type { int Number_Of_Current_Element; struct Queue_Node_Type Front_Header; struct Queue_Node_Type Rear_Header; }; struct Linked_Queue_Type* Create_Linked_Queue(void); int Enqueue_Linked_Queue(struct Linked_Queue_Type* Pointer_Queue, struct Queue_Node_Type element); struct Queue_Node_Type* Dequeue_Linked_Queue(struct Linked_Queue_Type* Pointer_Queue); struct Queue_Node_Type* Peek_Linked_Queue(struct Linked_Queue_Type* Pointer_Queue); void Delete_Linked_Queue(struct Linked_Queue_Type* Pointer_Queue); int Get_Number_Of_Elements_In_The_Linked_Queue(struct Linked_Queue_Type* Pointer_Queue); int Is_Linked_Queue_Empty(struct Linked_Queue_Type* Pointer_Queue); void Display_Linked_Queue(struct Linked_Queue_Type* Pointer_Queue); #endif // !_LINKED_QUEUE_ #ifndef _COMMON_DEFINITION_ #define _COMMON_DEFINITION_ #define TRUE 1 #define FALSE 0 #endif // !_COMMON_DEFINITION_ | 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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | #include <stdio.h> #include <stdlib.h> #include <string.h> #include "linked queue.h" struct Linked_Queue_Type* Create_Linked_Queue(void) { struct Linked_Queue_Type* Pointer_Return = (struct Linked_Queue_Type*)malloc(1 * sizeof(struct Linked_Queue_Type)); if (!(struct Linked_Queue_Type*)Pointer_Return) { printf("Error! The dynamic memory allocation failed. Create_Linked_Queue()\n"); return NULL; } (void*)memset((struct Linked_Queue_Type*)Pointer_Return, 0, 1 * sizeof(struct Linked_Queue_Type)); Pointer_Return->Front_Header.Pointer_Next = NULL; Pointer_Return->Rear_Header.Pointer_Next = NULL; Pointer_Return->Number_Of_Current_Element = 0; return (struct Linked_Queue_Type*)Pointer_Return; } int Enqueue_Linked_Queue(struct Linked_Queue_Type* Pointer_Queue, struct Queue_Node_Type element) { if (!(struct Linked_Queue_Type*)Pointer_Queue) { printf("Error! The linked queue was null. Push_Linked_Queue()\n"); return FALSE; } struct Queue_Node_Type* Pointer_New_Node = (struct Queue_Node_Type*)malloc(1 * sizeof(struct Queue_Node_Type)); if (!(struct Queue_Node_Type*)Pointer_New_Node) { printf("Error! The dynamic memory allocation falied. Push_Linked_Queue()\n"); return FALSE; } (void*)memset((struct Queue_Node_Type*)Pointer_New_Node, 0, 1 * sizeof(struct Queue_Node_Type)); *Pointer_New_Node = *(struct Queue_Node_Type*) &element; Pointer_New_Node->Pointer_Next = NULL; struct Queue_Node_Type* Pointer_Front_Header = (struct Queue_Node_Type*) &Pointer_Queue->Front_Header; struct Queue_Node_Type* Pointer_Rear_Header = (struct Queue_Node_Type*) &Pointer_Queue->Rear_Header; if (Is_Linked_Queue_Empty((struct Linked_Queue_Type*)Pointer_Queue)) { Pointer_Front_Header->Pointer_Next = (struct Queue_Node_Type*) Pointer_New_Node; Pointer_Rear_Header->Pointer_Next = (struct Queue_Node_Type*) Pointer_New_Node; } else { Pointer_Rear_Header->Pointer_Next->Pointer_Next = (struct Queue_Node_Type*) Pointer_New_Node; Pointer_Rear_Header->Pointer_Next = (struct Queue_Node_Type*) Pointer_New_Node; } Pointer_Queue->Number_Of_Current_Element++; return TRUE; } struct Queue_Node_Type* Dequeue_Linked_Queue(struct Linked_Queue_Type* Pointer_Queue) { if (!(struct Linked_Queue_Type*)Pointer_Queue) { printf("Error! The linked queue was null. Pop_Linked_Queue()\n"); return NULL; } if (Is_Linked_Queue_Empty ((struct Linked_Queue_Type*) Pointer_Queue)) { printf("Error! The linked queue was empty. Pop_Linked_Queue()\n"); return NULL; } struct Queue_Node_Type* Pointer_Front_Header = (struct Queue_Node_Type*) &Pointer_Queue->Front_Header; struct Queue_Node_Type* Pointer_Return = (struct Queue_Node_Type*) Pointer_Front_Header->Pointer_Next; Pointer_Front_Header->Pointer_Next = (struct Queue_Node_Type*) Pointer_Front_Header->Pointer_Next->Pointer_Next; Pointer_Return->Pointer_Next = NULL; Pointer_Queue->Number_Of_Current_Element--; return (struct Queue_Node_Type*)Pointer_Return; } struct Queue_Node_Type* Peek_Linked_Queue(struct Linked_Queue_Type* Pointer_Queue) { if (!(struct Linked_Queue_Type*)Pointer_Queue) { printf("Error! The linked queue was null. Peek_Linked_Queue()\n"); return NULL; } if (Is_Linked_Queue_Empty((struct Linked_Queue_Type*) Pointer_Queue)) { printf("Error! The linked queue was empty. Peek_Linked_Queue()\n"); return NULL; } struct Queue_Node_Type* Pointer_Return = (struct Queue_Node_Type*) Pointer_Queue->Front_Header.Pointer_Next; return (struct Queue_Node_Type*)Pointer_Return; } void Delete_Linked_Queue(struct Linked_Queue_Type* Pointer_Queue) { if ((struct Linked_Queue_Type*)Pointer_Queue == NULL) { printf("Error! The linked queue was null. Delete_Linked_Queue()\n"); return; } struct Queue_Node_Type* Pointer_Front_Header = (struct Queue_Node_Type*) &Pointer_Queue->Front_Header; int loop = 0; for (loop = Get_Number_Of_Elements_In_The_Linked_Queue((struct Linked_Queue_Type*)Pointer_Queue); loop; loop--) { struct Queue_Node_Type* Pointer_Delete_Node = (struct Queue_Node_Type*)Pointer_Queue->Front_Header.Pointer_Next; Pointer_Front_Header->Pointer_Next = (struct Queue_Node_Type*)Pointer_Delete_Node->Pointer_Next; free((struct Queue_Node_Type*)Pointer_Queue); } free((struct Linked_Queue_Type*)Pointer_Queue); return; } int Get_Number_Of_Elements_In_The_Linked_Queue(struct Linked_Queue_Type* Pointer_Queue) { if (!(struct Linked_Queue_Type*)Pointer_Queue) { printf("Error! The linked queue was null. Get_Number_Of_Elements_In_The_Linked_Queue()\n"); return FALSE; } return Pointer_Queue->Number_Of_Current_Element; } int Is_Linked_Queue_Empty(struct Linked_Queue_Type* Pointer_Queue) { if (!(struct Linked_Queue_Type*)Pointer_Queue) { printf("Error! The linked queue was null. Is_Linked_Queue_Empty()\n"); return FALSE; } struct Queue_Node_Type* Pointer_Current_Node = (struct Queue_Node_Type*)Pointer_Queue->Front_Header.Pointer_Next; if ((struct Queue_Node_Type*)Pointer_Current_Node == NULL) return TRUE; else return FALSE; } void Display_Linked_Queue(struct Linked_Queue_Type* Pointer_Queue) { if (!(struct Linked_Queue_Type*)Pointer_Queue) { printf("Error! The linked queue was null. Display_Linked_Queue()\n"); return; } if (Is_Linked_Queue_Empty((struct Linked_Queue_Type*) Pointer_Queue)) { printf("Error! The linked queue was empty. Display_Linked_Queue()\n"); return; } printf("Number of elements : %d\n", Get_Number_Of_Elements_In_The_Linked_Queue((struct Linked_Queue_Type*)Pointer_Queue)); struct Queue_Node_Type* Pointer_Current_Node = (struct Queue_Node_Type*)Pointer_Queue->Front_Header.Pointer_Next; int Node_Index = 0; for (Node_Index = Get_Number_Of_Elements_In_The_Linked_Queue((struct Linked_Queue_Type*)Pointer_Queue); Node_Index; Node_Index--) { printf("[%c] ", Pointer_Current_Node->data); Pointer_Current_Node = (struct Queue_Node_Type*)Pointer_Current_Node->Pointer_Next; } printf("\n\n"); return; } | 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 | #include <stdio.h> #include <stdlib.h> #include "linked queue.h" int main(int argc, char* argv[]) { typedef struct Linked_Queue_Type Linked_Queue; typedef struct Queue_Node_Type Queue_Node; Linked_Queue* Pointer_Queue = (Linked_Queue*)Create_Linked_Queue(); if (!(Linked_Queue*)Pointer_Queue) return -1; int loop = 0; for (loop = 0; loop < 26; loop++) { Queue_Node node = { 0, }; node.data = 'A' + loop; Enqueue_Linked_Queue((Linked_Queue*)Pointer_Queue, node); } Display_Linked_Queue((Linked_Queue*)Pointer_Queue); Queue_Node* Pointer_Node = NULL; for (loop = 0; loop < 10; loop++) { Pointer_Node = (Queue_Node*)Dequeue_Linked_Queue((Linked_Queue*)Pointer_Queue); if ((Queue_Node*)Pointer_Node == NULL) { break; } printf("[%c] ", Pointer_Node->data); free((Queue_Node*)Pointer_Node); Pointer_Node = NULL; } printf("\n"); Display_Linked_Queue((Linked_Queue*)Pointer_Queue); printf("\n"); Pointer_Node = (Queue_Node*)Peek_Linked_Queue((Linked_Queue*)Pointer_Queue); if ((Queue_Node*)Pointer_Node) printf("[%c]\n", Pointer_Node->data); Pointer_Node = NULL; Display_Linked_Queue((Linked_Queue*)Pointer_Queue); printf("\n"); system("pause"); return 0; } | cs |
0 개의 댓글:
댓글 쓰기