Can someone help me understand whats happening in the following functions? Specifically the use of s1->top? And what is the movement of s1->top in the functions push &pop & display? Because if in function push, s1->top moves to the right whenever a number is pushed? then why in the display function, it says s1->top is first in the traversing, while in push s1->top is n the right, while in printing the values, we need to firstly be in the left and then traverse..why?
Stack createStack() {
Stack s1;
s1 = (Stack) malloc(sizeof(Stack_Head));
s1 - > count = 0;
s1 - > top = NULL;
return s1;
}
Nodeptr createNode(dataitem item) {
Nodeptr temp;
temp = (Nodeptr) malloc(sizeof(Node));
temp - > data = item;
temp - > next = NULL;
return temp;
}
void push(Stack s1, dataitem item) {
Nodeptr temp = createNode(item);
temp - > next = s1 - > top;
s1 - > top = temp;
s1 - > count++;
}
void display(Stack s1) {
Nodeptr ptr = s1 - > top;
while (ptr1 = NULL) {
printf("%d", ptr - > data);
ptr = ptr - > next;
}
printf("\n");
}
void pop(Stack s1) {
Nodeptr temp;
if (isEmpty(s1))
printf("List is Empty");
else {
temp = s1 - > top;
s1 - > top = temp - > next;
temp - > next = NULL;
free(temp);
s1 - > count;
}
int isEmpty(Stack s1) {
return s1 - > top == NULL;
}