I found some code to make a C implementation of stacks, and decided to use it. However, there were several typedefs, and I am having difficulty printing the values in a stackT (really a char array). Below is the code. What am I doing wrong?
#include <stdio.h>
#include <stdlib.h>
typedef char stackElementT;
typedef struct {
stackElementT *contents;
int maxSize;
int top;
} stackT;
void StackInit(stackT *stackP, int maxSize) {
stackElementT *newContents;
newContents = (stackElementT *)malloc(sizeof(stackElementT)*maxSize);
if (newContents == NULL) {
fprintf(stderr, "Not enough memory.\n");
exit(1);
}
stackP->contents = newContents;
stackP->maxSize = maxSize;
stackP->top = -1; //empty...
}
void StackDestroy(stackT *stackP) {
free(stackP->contents);
stackP->contents = NULL;
stackP->maxSize = 0;
stackP->top = -1; //empty
}
int StackIsEmpty(stackT *stackP) {
return stackP->top < 0;
}
int StackIsFull(stackT *stackP) {
return stackP->top >= stackP->maxSize-1;
}
void StackPush(stackT *stackP, stackElementT element) {
if(StackIsFull(stackP)) {
fprintf(stderr, "Can't push element: stack is full.\n");
exit(1);
}
stackP->contents[++stackP->top] = element;
}
stackElementT StackPop(stackT *stackP) {
if(StackIsEmpty(stackP)) {
fprintf(stderr, "Can't pop element: stack is empty.\n");
exit(1);
}
return stackP->contents[stackP->top--];
}
void StackDisplay(stackT *stackP) {
if(StackIsEmpty(stackP)) {
fprintf(stderr, "Can't display: stack is empty.\n");
exit(1);
}
int i;
printf("[ ");
for (i = 0; i < stackP->top; i++) {
printf("%c, ", stackP[i]); //the problem occurs HERE
}
printf("%c ]", stackP[stackP->top]);
}
int postfix(char* expr, int length) {
int i;
stackT stack;
StackInit(&stack, 1000);
int temp;
for (i = 0; i < length; i++) {
if ((expr[i] >= 48) && (expr[i] <= 57)) {
printf("Is a number! Pushed %d\n", expr[i]);
StackPush(&stack, expr[i]);
}
else {
switch (expr[i]) {
case 43: {
temp = StackPop(&stack);
StackPush(&stack, StackPop(&stack)+temp);
}
break;
case 45: {
temp = StackPop(&stack);
StackPush(&stack, StackPop(&stack)-temp);
}
break;
case 47: {
temp = StackPop(&stack);
StackPush(&stack, StackPop(&stack)/temp);
}
break;
case 42: {
temp = StackPop(&stack);
StackPush(&stack, StackPop(&stack)*temp);
}
break;
default:
break;
}
}
}
return StackPop(&stack);
}
int main() {
int i;
char* expr = "1 2 3 + * 3 2 1 - + *";
for(i = 0; expr[i] != '\0'; i++) ;
printf("%d\n", postfix(expr, i));
}
The compiler (GCC 4.2.1 on MacOS X 10.6.7) tells me:
In my version of the code, these two lines are the
printf()statements inStackDisplay(),right where you state you have problems.
You probably want
stackP->contents[i]. With that fix, the program ‘runs’ but produces:That is your problem to resolve, now.
(Oh, I also fixed the stray semi-colon after the
forloop inmain()as diagnosed in the comments.)The loop should be written as
strlen(expr)(and then you need to#include <string.h>). Indeed, the body of the main program simplifies to:You should normally keep
topindexed to the next location to use, so the initial value would normally be0rather than-1.Don’t learn the ASCII codes for the digits – forget you ever did.
You should write:
or, better (but you have to
#include <ctype.h>too):Similar comments apply to the switch:
I’m not sure of the logic behind the indentation, but that 43 should be written as
'+', 45 as'-', 47 as'/', and 42 as'*'.This generates:
If you fix the number pushing code as shown:
Then you get:
And with a little more instrumentation, along the lines of:
after each operation, the result is:
You were close.