Reputation: 81
This is a function that calls to arrays in a stack of chararrays and integers. It is supposed to reverse the values, but first i have to figure out which is a string and which is an integer before i can switch them. but i keep getting errors. Does anything look ridiculosuly off on this?
void reverse(Stack *S)
// NOTE: Called w/ user input 'r'
// PRE: Stack S is initialized
// POST: The first two values of the stack are reversed on the stack
{
int valone;
int valtwo;
char stringone[50];
char stringtwo[50];
if (S->size < 1)
{
printf("Error: There are less than 2 values on the stack \n");
}
else
{
valone = (float)topInt(S);
strcpy(stringone, topString(S));
pop(S);
valtwo = (float)topInt(S);
strcpy(stringone, topString(S));
pop(S);
if(stringone[0] == '\n')
{
pushInt(S, valone);
}
else if(valone == '\n')
{
pushString(S, stringone);
}
else if(stringtwo[0] == '\n')
{
pushInt(S, valtwo);
}
else if(valtwo == '\n')
{
pushString(S, stringtwo);
}
}
}
Upvotes: 0
Views: 149
Reputation: 2597
You are popping two values off the stack, but only pushing one value back onto it. You need to change one of the else if
s to if
.
if(stringone[0] == '\n')
{
pushInt(S, valone);
}
else if(valone == '\n')
{
pushString(S, stringone);
}
if(stringtwo[0] == '\n')
{
pushInt(S, valtwo);
}
else if(valtwo == '\n')
{
pushString(S, stringtwo);
}
I don't know if this will solve your problem. What error are you getting? Please post that.
Also, why are you using \n
as some special value here? You will run into strange issues if valone
or valtwo
end up equivalent to the integral value of \n
.
If I were you, I would change the approach to something like...
void reverse(Stack **S)
{
Stack* newS = allocateEmptyStack();
while (!isEmpty(*S))
{
StackItem* item = top(*S);
pop(*S);
push(newS, item);
}
freeStack(*S);
*S = newS;
}
Some potential definitions...
typedef enum ItemType
{
STACK_STRING,
STACK_INT,
STACK_FLOAT
} ItemType;
typedef struct StackItem
{
ItemType type;
void* data;
StackItem* next;
} StackItem;
typedef struct Stack
{
StackItem* top;
} Stack;
Stack* allocateEmptyStack()
{
Stack* S = malloc(sizeof(Stack));
S->top = NULL;
return S;
}
int isEmpty(Stack* S)
{
if (S->top == NULL)
return 1;
return 0;
}
void freeStack(Stack* S)
{
while (!isEmpty(S))
{
StackItem* item = top(S);
pop(S);
freeStackItem(item);
}
free(S);
}
StackItem* top(Stack* S)
{
return S->top;
}
void pop(Stack* S)
{
StackItem* topItem = top(S);
if (topItem != NULL)
{
s->top = topItem->next;
}
}
void push(Stack* S, StackItem* item)
{
item->next = top(S);
s->top = item;
}
StackItem* allocateStackItem(ItemType type, int dataSize)
{
StackItem* item = malloc(sizeof(StackItem));
item->data = malloc(dataSize);
item->type = type;
item->next = NULL;
return item;
}
void freeStackItem(StackItem* item)
{
if (item->data != NULL)
free(item->data);
free(item);
}
An example of initializing a Stack
...
Stack* S = allocateEmptyStack();
StackItem* item = allocateStackItem(STACK_INT, sizeof(int));
int* int_ptr = (int*)(item->data);
*int_ptr = 1234;
push(S, item);
const char* str = "this is a string";
item = allocateStackItem(STACK_STRING, strlen(str) + 1);
char* char_ptr = (char*)(item->data);
strcpy(char_ptr, str);
push(S, item);
Upvotes: 1
Reputation: 1788
Not to be too harsh, but this code is tantamount to completely incoherent. I can't imagine what valone = (float)topInt(S); is intended to do, since valone is an int. You also seem to be assigning both an integer identity and a string identity to the top element of the stack. You pop two items off the stack and push at most one. You strcpy to a fixed lenngth buffer without checking the size of the string you are copying, and finally, if you do push a string on the stack, you're pushing the local variable address which will be invalid when the function exits.
Upvotes: 1
Reputation: 944
Hard to understand without more detail,but it looks like you are doing two pops and only one push. You at least need to tale out your 2nd else.
Upvotes: 0