Hi, I need your help with this one.
I created a stack program but there is a problem.
The program doesn't seem to add data into the push function.
Code:
/*
*Stack
*C
*
*/
#include <stdio.h>
#include <stdlib.h>
typedef struct stack Stack;
struct stack{
int data;
Stack *next;
};
void push(Stack *, int);
int pop(Stack *);
int is_empty(Stack *); //
void print_stack(Stack *);
void instructions(void);
int main(void)
{
Stack *node = NULL;
int choice;
int value;
instructions();
printf("?");
scanf("%d", &choice);
while(choice != 3)
{
switch(choice)
{
case 1:
printf("\nEnter an integer => ");
scanf("%d", &value);
push(node,value);
print_stack(node);
break;
case 2:
if(!is_empty(node))
{
printf("\nThe popped value => %d", pop(node));
}
print_stack(node);
break;
default:
printf("\nInvalid choice\n\n");
instructions();
break;
}
printf("?");
scanf("%d ", &choice);
}
printf("End Of Run!\n");
return 0;
}
void push(Stack *node, int value)
{
Stack *new_node;
new_node = (Stack *)malloc(sizeof(Stack));
if(new_node != NULL)
{
new_node->data = value;
new_node->next = node;
node = new_node;
}
else printf("No available memory fo %d\n", value);
}
int pop(Stack *node)
{
Stack *temp;
int value;
temp = node;
value = node->data;
node = node->next;
free(temp);
return value;
}
int is_empty(Stack *node)
{
return node == NULL;
}
void print_stack(Stack *node)
{
if(node == NULL)
{
printf("The stack is empty.\n\n");
}
else
{
printf("Tha stack is: \n");
while(node != NULL)
{
printf("%d --> ", node->data);
node = node->next;
}
printf("NULL\n\n");
}
}
void instructions(void)
{
printf("Enter choice => \n"
"1 push\n"
"2 pop\n"
"3 end\n");
}
Can somebody tell me what's wrong?