array stack
"arraystack.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 | #ifndef _ARRAY_STACK_ #define _ARRAY_STACK_ typedef struct Array_Stack_Node_Type { char data; int _Number; } Array_Stack_Node; typedef struct Array_Stack_Type { int Maximum_Element_Count; int Current_Element_Count; Array_Stack_Node* Pointer_Element; } Array_Stack; Array_Stack* Create_Array_Stack(int Maximum_Element_Count); int Push_Array_Stack(Array_Stack* Pointer_Stack, Array_Stack_Node element); Array_Stack_Node* Pop_Array_Stack(Array_Stack* Pointer_Stack); Array_Stack_Node* Peek_Array_Stack(Array_Stack* Pointer_Stack); void Delete_Array_Stack(Array_Stack* Pointer_Stack); int Is_Array_Stack_Full(Array_Stack* Pointer_Stack); int Is_Array_Stack_Empty(Array_Stack* Pointer_Stack); #endif // !_ARRAY_STACK_ #ifndef _COMMON_STACK_DEFAULT #define _COMMON_STACK_DEFAULT #define TRUE 1 #define FALSE 0 #endif // !_COMMON_STACK_DEFAULT | cs |
1 2 3 4 5 6 7 8 | #ifndef _DISPLAY_ #define _DISPLAY_ #include "arraystack.h" void Display_Array_Stack(Array_Stack* Pointer_Stack); #endif // !_DISPLAY_ | 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 | #include <stdio.h> #include <stdlib.h> #include <string.h> #include "arraystack.h" Array_Stack* Create_Array_Stack(int size) { if (size < 0) { printf("Error! Underflow. Create_Array_Stack()\n"); return NULL; } Array_Stack* Pointer_Return = (Array_Stack*)malloc(1 * sizeof(Array_Stack)); if ((Array_Stack*) Pointer_Return == NULL) { printf("Error! Dynamic memory allocation (1), Create_Array_Stack()\n"); return NULL; } (void*)memset((Array_Stack*)Pointer_Return, 0, sizeof(Array_Stack)); (Array_Stack*)Pointer_Return->Maximum_Element_Count = size; (Array_Stack*)Pointer_Return->Pointer_Element = (Array_Stack_Node*)malloc(size * sizeof(Array_Stack_Node)); if ((Array_Stack*)Pointer_Return->Pointer_Element == NULL) { printf("Error! Dynamic memory allocation (2), Create_Array_Stack()\n"); free((Array_Stack*)Pointer_Return); return NULL; } (void*)memset((Array_Stack*)Pointer_Return->Pointer_Element, 0, size * sizeof(Array_Stack_Node)); return (Array_Stack*)Pointer_Return; } int Push_Array_Stack(Array_Stack* Pointer_Stack, Array_Stack_Node element) { if ((Array_Stack*)Pointer_Stack == NULL) { printf("Error! The array is null. Push_Array_Stack()\n"); return FALSE; } else if (Is_Array_Stack_Full((Array_Stack*)Pointer_Stack) != FALSE) { printf("Error! Overflow, Push_Array_Stack()\n"); return FALSE; } *(Pointer_Stack->Pointer_Element + (int)Pointer_Stack->Current_Element_Count) = element; (Array_Stack*)Pointer_Stack->Current_Element_Count++; return TRUE; } Array_Stack_Node* Pop_Array_Stack(Array_Stack* Pointer_Stack) { if ((Array_Stack*)Pointer_Stack == NULL) { printf("Error! The array is null. Pop_Array_Stack()\n"); return FALSE; } else if (Is_Array_Stack_Empty((Array_Stack*)Pointer_Stack) != FALSE) { printf("Error! Underflow, Pop_Array_Stack()\n"); return FALSE; } Array_Stack_Node* Pointer_Return = (Array_Stack_Node*)malloc(1 * sizeof(Array_Stack_Node)); // Dynamic memory allocation if (Pointer_Return == NULL) { printf("Error! Dynamic memory allocation, Pop_Array_Stack()\n"); return FALSE; } *(Array_Stack_Node*)Pointer_Return = *(Array_Stack_Node*)(Pointer_Stack->Pointer_Element + (int)Pointer_Stack->Current_Element_Count - 1); (Array_Stack*)Pointer_Stack->Current_Element_Count--; return (Array_Stack_Node*)Pointer_Return; } Array_Stack_Node* Peek_Array_Stack(Array_Stack* Pointer_Stack) { if ((Array_Stack*)Pointer_Stack == NULL) { printf("Error! The array is null. Peek_Array_Stack()"); return NULL; } else if (Is_Array_Stack_Empty((Array_Stack*)Pointer_Stack) != FALSE) { printf("Error! Underflow, Peek_Array_Stack()\n"); return NULL; } Array_Stack_Node* Pointer_Return = NULL; // non dynamic memory allocation Pointer_Return = &(*(Array_Stack*)(Pointer_Stack->Pointer_Element + (int)Pointer_Stack->Current_Element_Count - 1)); //*(Array_Stack_Node*)Pointer_Return = *(Array_Stack_Node*)(Pointer_Stack->Pointer_Element + (int)Pointer_Stack->Current_Element_Count - 1); // run time error return (Array_Stack_Node*)Pointer_Return; } void Delete_Array_Stack(Array_Stack* Pointer_Stack) { if ((Array_Stack*)Pointer_Stack == NULL) { printf("Error! The array is null. Delete_Array_Stack()\n"); return; } if ((Array_Stack*)Pointer_Stack->Pointer_Element != NULL) { free((Array_Stack*)Pointer_Stack->Pointer_Element); } free((Array_Stack*)Pointer_Stack); return; } int Is_Array_Stack_Full(Array_Stack* Pointer_Stack) { if ((Array_Stack*)Pointer_Stack == NULL) { printf("Error! The array is null. Is_Array_Stack_Full()\n"); return FALSE; } if ((Array_Stack*)Pointer_Stack->Current_Element_Count == (Array_Stack*)Pointer_Stack->Maximum_Element_Count) { return TRUE; } return FALSE; } int Is_Array_Stack_Empty(Array_Stack* Pointer_Stack) { if ((Array_Stack*)Pointer_Stack == NULL) { printf("Error! The array is null. Is_Array_Stack_Empty()\n"); return FALSE; } if ((Array_Stack*)Pointer_Stack->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 | #include <stdio.h> #include "display.h" void Display_Array_Stack(Array_Stack* Pointer_Stack) { if ((Array_Stack*)Pointer_Stack == NULL) { printf("Error! The array is null. Display_Array_Stack()\n"); return; } printf("Stack size : %d, Number of current node : %d\n", (Array_Stack*)Pointer_Stack->Maximum_Element_Count, (Array_Stack*)Pointer_Stack->Current_Element_Count); int size = (Array_Stack*)Pointer_Stack->Maximum_Element_Count, top = (Array_Stack*)Pointer_Stack->Current_Element_Count; int loop = 0; for (loop = size - 1; loop > top - 1; loop--) { printf("[%d]-[Empty]\n", loop); } int index = 0; for (index = top - 1; index > 0 - 1; index--) { printf("[%d]-[%c][%d]\n", index, (Array_Stack*)Pointer_Stack->Pointer_Element[index].data, (Array_Stack*)(*(Pointer_Stack->Pointer_Element + index))._Number); } 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 48 49 50 51 52 53 | #include <stdio.h> #include <stdlib.h> #include "arraystack.h" #include "display.h" int main(int argc, char* argv[]) { Array_Stack* Pointer_Stack = NULL; Array_Stack_Node* Pointer_Node = NULL; (Array_Stack*)Pointer_Stack = (Array_Stack*)Create_Array_Stack(6); if ((Array_Stack*)Pointer_Stack == NULL) { return -1; } Array_Stack_Node node[4] = { 'A', 10, 'B', 20, 'C', 30, 'D', 40 }; (int)Push_Array_Stack((Array_Stack*)Pointer_Stack, node[0]); (int)Push_Array_Stack((Array_Stack*)Pointer_Stack, node[1]); (int)Push_Array_Stack((Array_Stack*)Pointer_Stack, node[2]); (int)Push_Array_Stack((Array_Stack*)Pointer_Stack, node[3]); Display_Array_Stack((Array_Stack*)Pointer_Stack); (Array_Stack_Node*)Pointer_Node = Pop_Array_Stack((Array_Stack*)Pointer_Stack); if ((Array_Stack_Node*)Pointer_Node != NULL) { printf("Pop Value-[%c][%d]\n", (Array_Stack_Node*)Pointer_Node->data, (Array_Stack_Node*) Pointer_Node->_Number); free((Array_Stack_Node*)Pointer_Node); } else { printf("Empty stack\n"); } Display_Array_Stack((Array_Stack*)Pointer_Stack); (Array_Stack_Node*)Pointer_Node = Peek_Array_Stack((Array_Stack*)Pointer_Stack); if ((Array_Stack_Node*)Pointer_Node != NULL) { printf("Peek Value-[%c][%d]\n", (Array_Stack_Node*)Pointer_Node->data, (Array_Stack_Node*)Pointer_Node->_Number); } else { printf("Empty stack\n"); } Display_Array_Stack((Array_Stack*)Pointer_Stack); Delete_Array_Stack((Array_Stack*)Pointer_Stack); return 0; } | cs |
0 개의 댓글:
댓글 쓰기