#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;
}