I am implementing a stack using Linked_list in C lang. I have successfully created first Node. And when I am trying to add further nodes dynamically in the first position as stack follows LIFO principle, I believe I am not correctly linking pointers so that the order of Linked list remains intact. The code is pretty simple as shown below, I just need to understand, when I am trying to print the entire linkedlist, it is only printing the data of very first node. Looks like the entire chain is breaking after the first node. Where am I going wrong ?
// Implementation of stack using a linked-list
#include<stdio.h>
#include<stdlib.h>
typedef struct sllnode
{
int data;
struct sllnode *next;
}
stack;
void create(stack *head, int elem)
{
head -> data = elem;
head -> next = NULL;
}
void insert(stack *head, int elem)
{
stack *newNode = malloc( sizeof(stack) );
newNode -> data = elem;
newNode -> next = head;
head = newNode;
}
void display(stack *head)
{
if( head == NULL )
{
printf(" NULL value returned \n");
}
stack *ptr = head;
while(ptr != NULL)
{
printf("%d \n", ptr -> data);
ptr = ptr -> next;
}
}
int main(void)
{
stack *head = malloc( sizeof(stack) );
create(head, 10);
insert(head, 15);
insert(head, 25);
insert(head, 26);
insert(head, 27);
insert(head, 28);
display(head);
}