-
help again with stacks
Before I start
Thanks Sebastiani your suggestion was extremely helpful..
But the program pushes the characters into the stack but it doesn't pop them:
[code]
/* reversChar.c */
#include <stdio.h>
#include <stdlib.h>
#define N 81
typedef struct stack
{
int *array;
int size;
int top;
}STACK;
STACK *S;
STACK *StackCreate(int nSize);
void StackDestr(STACK *S);
void StackPush(STACK *S, int Value);
void StackPop(STACK *S, int Target);
int StackIsFull(STACK *S);
int StackIsEmpty(STACK *S);
int main (void)
{
char line[80], revline[80];
int i, ch;
StackCreate(N);
printf("Enter a line to be reversed: \n");
for (i = 0; (i <80 ) && ((ch = getchar()) !=EOF) && (ch != '\n'); i++ )
line [i] = (char)ch;
line[i] = '\0';
printf("Line input: %s\n", line);
for (i=0; i<N && line[i] != 0; i++)
{
StackPush(S, line[i]);
}
for (i=0;i<line[i];i++)
{
printf("array[%d]=%c\n",i,S->array[i]);
}
for (i=0; i<N && line[i] != 0; i++)
{
StackPop(S, line[i]);
printf("line[i] is located at %p\n", line[i]);
}
revline[i] = '\0';
printf("Line Output: %s\n", revline);
return 0;
}
STACK *StackCreate(int nSize)
{
int k;
S = (STACK *)malloc(sizeof(STACK));
if (S == NULL) {perror("reversChar"); exit(1);}
S->array = (int *)malloc(nSize*sizeof(int));
if (S->array == NULL) { perror("reversChar"); exit(1);}
S->size = nSize;
S->top = -1;
for (k=0; k<S->size; k++)
S->array[k] = 0;
return S;
}
void StackDestr(STACK *S)
{
free(S->array);
free(S);
return;
}
void StackPush(STACK *S, int Value)
{
if (!StackIsFull(S) )
{
S->top = 1 + S->top;
S->array[S->top] = Value;
}
else puts("No room in Stack");
return ;
}
void StackPop(STACK *S, int Target)
{
if (!StackIsEmpty(S) )
{
printf("S->top - is located: %p\n", S->top);
Target = S->array[S->top];
S->array[S->top] = 0;
S->top--;
}
return;
}
int StackIsFull(STACK *S)
{
int next;
next = 1 + S->top;
if (next > -1 + S->size) return 1;
else return 0;
}
int StackIsEmpty(STACK *S)
{
if (S->top < 0) return 1;
else return 0;
}
[code]
Enter a line to be reversed:
hello
Line input: hello
array[0]=h
array[1]=e
array[2]=l
array[3]=l
array[4]=o
S->top - sim at: 00000004
line[i] is located at 00000068
S->top - sim at: 00000003
line[i] is located at 00000065
S->top - sim at: 00000002
line[i] is located at 0000006C
S->top - sim at: 00000001
line[i] is located at 0000006C
S->top - sim at: 00000000
line[i] is located at 0000006F
Line Output: ¦¦¦¦¦
Press any key to continue
I also printed out the memory addresses maybe i'm wrong but aren't the memory addresses of the line[i] of the pop supposed to be at the same place as the S->top. I apologize for not using code tags I have to read up on them
-
I noticed that too, let me look at it a little closer...
-
Mostly just one-off errors....
Code:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define NAX 81
void Free( void *ptr )
{
if(ptr != NULL) free(ptr);
}
typedef struct stack
{
char *array;
int size;
int top;
}STACK;
STACK *S;
STACK *StackCreate(int nSize);
void StackDestr(STACK *S);
char StackPush(STACK *S, char Value);
char StackPop(STACK *S);
int StackIsFull(STACK *S);
int StackIsEmpty(STACK *S);
int StackCount(STACK *S);
int main (void)
{
char line[NAX-1], revline[NAX-1];
int p, i, ch;
StackCreate(NAX);
printf("Enter a line to be reversed: \n");
fgets(line, NAX-1, stdin);
for (p=0; p < strlen(line); p++)
{
StackPush(S, line[p]);
}
printf("# Of Chars: %i\n", StackCount(S));
for (i=0; i < strlen(line); i++)
{
revline[i] = StackPop(S);
}
revline[i] = '\0';
printf("Line Output: %s\n", revline);
getch();
return 0;
}
STACK *StackCreate(int nSize)
{
int k;
S = (STACK *)malloc(sizeof(STACK));
if (S == NULL) {perror("reversChar"); exit(1);}
S->array = (char *)malloc(nSize*sizeof(char));
if (S->array == NULL) { perror("reversChar"); exit(1);}
S->size = nSize;
S->top = 0;
for (k=0; k<S->size; k++)
S->array[k] = 0;
return S;
}
void StackDestr(STACK *S)
{
Free(S->array);
Free(S);
return;
}
char StackPush(STACK *S, char Value)
{
if (StackIsFull(S))
{
puts("No room in Stack");
return 0;
}
S->array[S->top] = Value;
S->top = 1 + S->top;
return Value;
}
char StackPop(STACK *S)
{
if(StackIsEmpty(S))return 0;
char firstchar;
S->top--;
firstchar = S->array[S->top];
S->array[S->top] = 0;
return firstchar;
}
int StackIsFull(STACK *S)
{
if (S->top >= S->size - 1)
return 1;
else
return 0;
}
int StackIsEmpty(STACK *S)
{
if (S->top <= 0)
return 1;
else
return 0;
}
int StackCount(STACK *S)
{
return S->top-1;
}