Skip to content
24 changes: 17 additions & 7 deletions data_structures/linked_list/stack_using_linked_lists.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ int main()
}
}

void push(struct node *p)
/**
* push function will add a new node at the head of the list, time complexity O(1)
* @param p pointer to the top of the stack
* @returns void
*/
void push(struct node *p)
{
int item;
struct node *temp;
Expand All @@ -46,11 +51,17 @@ void push(struct node *p)
temp->info = item;

temp->link = top;
top = temp;
top = temp;

printf("inserted succesfully\n");
}
void pop(struct node *p)

/**
* pop function deletes the first node from the head, time complexity O(1)
* @param p pointer to the top of the stack
* @returns void
*/
void pop(struct node *p)
{
int item;
struct node *temp;
Expand All @@ -61,15 +72,15 @@ void pop(struct node *p)
{
item = top->info;
temp = top;
top = top->link;
free(temp);
top = top->link;
free(temp);
printf("Element popped is%d\n", item);
}
}

void display(struct node *p)
{
if (top == NULL)
if (top == NULL)
printf("stack is empty\n");
else
{
Expand All @@ -79,6 +90,5 @@ void display(struct node *p)
printf("%d\n", p->info);
p = p->link;
}
// printf("%d\n",p->info);
}
}