#include "stdio.h"
#include "stdlib.h"
struct node_type {
int data;
struct node_type *next;
};
struct stack_type{
node_type *top;
int length;
};
void push(node_type *head,stack_type stack);
int main()
{
struct stack_type stack;
node_type *list;
list = (node_type *)malloc(sizeof(node_type));
list->next = NULL;
node_type *head;
head = list;
stack.length = 0; //set stack empty
push(head, stack);
list = head->next;
printf("The entegers are:");
do {
printf("%d", stack.top->data);
list = list->next;
stack.top = list;
} while (list->next != 0);
system("pause");
return 0;
}
void push(node_type *head,stack_type stack)
{
int i, n;
node_type *p;
p = (node_type*)malloc(sizeof(node_type));
p = head;
printf("Enter the integers(0 to end):");
for (;;) {
scanf("%d", &n);
if (n == 0)
break;
stack.top->data = n;
p->next = stack.top;
stack.length++;
}
}
when I debug, it said that p->next=NULL and stop,I'm not clear about this.
why I wrong? How to fix it, I'm not clear about the use of NULL and top finger Thanks for the answers in advance.
pinpushand then immediately overwrite the handle to the allocated data; you pass the stack struct by value, thus not reflecting any changes that will be made bypushand ´pop`; you use external variables for the nodes whereas the whole stack should be defined by the members of the stack struct. There's no simple fix for this program, I'm afraid.