|
-
Apr 15th, 2001, 06:12 PM
#1
Thread Starter
Addicted Member
Any idea why this simple word stack doesn't work?
#include <stdio.h>
#include <malloc.h>
#include <string.h>
typedef enum {FALSE, TRUE} BOOL;
BOOL push (char d []);
BOOL pop (char *d []);
typedef struct _STACK {
char data [10];
struct _STACK *next;
} STACK;
STACK *sp = NULL;
int main (void)
{
char x [10];
char y [10];
push ("hi");
push ("hiback");
pop(&x);
pop(&y);
printf("Words in stack are %s and %s", x, y);
}
BOOL push (char d []){
STACK *new;
new = (STACK *) malloc (sizeof(STACK));
if(new == NULL)
return FALSE;
strcpy(new->data, d);
new->next = sp;
sp = new;
//printf("%s\n", d);
return TRUE;
}
BOOL pop(char *d []){
STACK *old;
if(sp ==NULL)
return FALSE;
old =sp;
*d = old->data;
sp = old->next;
free(old);
return TRUE;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|