I've recently studied about linked-list and tried to create a push and pop function.
I've successfully created one which look like this.
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
};
struct node* buffer = NULL;
void push(int elem){
struct node *new_element = (struct node*)malloc(sizeof(struct node));
new_element->next = buffer;
new_element->data = elem;
buffer = new_element;
}
int pop(void){
int elem = buffer->data;
struct node *eliminate = buffer;
buffer = eliminate->next;
free(eliminate);
return elem;
}
Afterwards I tried to print them this way:
int main(void) {
push(5);
push(7);
push(8);
pop();
printf("%d %d", buffer->data, buffer->next->data);
}
However, the result was 7 5 instead of 5 7. Is there anything wrong with my push function?