#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
typedef struct node {
double dataPtr;
struct node* link;
} STACK_NODE;
typedef struct {
int count;
STACK_NODE* top;
} STACK;
STACK* createStack (void);
bool pushStack (STACK* stack, double dataInPtr);
double popStack (STACK* stack);
double stackTop (STACK* stack);
bool emptyStack (STACK* stack);
bool fullStack (STACK* stack);
int stackCount (STACK* stack);
STACK* destroyStack (STACK* stack);
STACK* createStack (void) {
STACK* stack;
stack = (STACK*) malloc (sizeof(STACK));
if
(stack) {
stack->count = 0;
stack->top = NULL;
}
printf(
"Address of stack: %p on createStack\n"
, &stack);
return
stack;
}
bool pushStack (STACK* stack, double dataInPtr) {
STACK_NODE* newPtr;
newPtr = (STACK_NODE*) malloc (sizeof(STACK_NODE));
if
(!newPtr)
return
false
;
newPtr->dataPtr = dataInPtr;
newPtr->link = stack->top;
stack->top = newPtr;
(stack->count)++;
return
true
;
}
double popStack (STACK* stack) {
double dataOutPtr;
STACK_NODE* temp;
if
(stack->count == 0) {
dataOutPtr = -1;
printf(
"stack empty : POP\n"
);
}
else
{
temp = stack->top;
dataOutPtr = stack->top->dataPtr;
stack->top = stack->top->link;
free(temp);
stack->count--;
}
return
dataOutPtr;
}
double stackTop (STACK* stack) {
if
(stack->count == 0) {
printf(
"stack empty: stackTOP\n"
);
return
-1;
}
else
return
stack->top->dataPtr;
}
bool emptyStack (STACK* stack) {
return
(stack->count == 0);
}
bool fullStack (STACK* stack) {
STACK_NODE* temp;
printf(
"Address of temp (New Node): %p\n"
, &temp);
printf(
"Address of stacktop: %p\n"
, &(stack->top));
if
((temp = (STACK_NODE*) malloc (sizeof(*(stack->top))))) {
free(temp);
return
false
;
}
return
true
;
}
int stackCount (STACK* stack) {
return
stack->count;
}
STACK* destroyStack (STACK* stack) {
STACK_NODE* temp;
if
(stack) {
while
(stack->top != NULL) {
temp = stack->top;
stack->top = stack->top->link;
free(temp);
}
free(stack);
}
return
NULL;
}
int main() {
STACK* stack = createStack();
char string[100];
string[0]=
'h'
;
string[1]=
'e'
;
string[2]=
'l'
;
string[3]=
'l'
;
string[4]=
'o'
;
printf(
"string have %zu chars \n"
,strlen(string));
printf(
"stackCount = %i \n"
,stackCount(stack));
pushStack(stack,string[0]);
printf(
"stackCount = %i \n"
,stackCount(stack));
for
(int i=0; i<strlen(string); i++) pushStack(stack,string[i]);
pushStack(stack,string[0]);
printf(
"stackCount = %i \n"
,stackCount(stack));
printf(
"stackTop = %.0f \n"
,stackTop(stack));
printf(
"%.0f \n"
,popStack(stack));
printf(
"%.0f \n"
,popStack(stack));
printf(
"%.0f \n"
,popStack(stack));
printf(
"%.0f \n"
,popStack(stack));
if
(!emptyStack(stack)) printf(
"emptyStack = false \n"
);
if
(!fullStack(stack)) printf(
"fullStack = false \n"
);
printf(
"%.0f \n"
,popStack(stack));
printf(
"%.0f \n"
,popStack(stack));
printf(
"%.0f \n"
,popStack(stack));
printf(
"%.0f \n"
,popStack(stack));
printf(
"sizeof int = %zu bytes\n"
, sizeof (int));
printf(
"sizeof string[100] = %zu bytes\n"
, sizeof string);
destroyStack(stack);
printf(
"stackCount = %i \n"
,stackCount(stack));
return
0;
}