stack using linked lists Algorithm

The stack using linked lists algorithm is an efficient and flexible data structure that allows for dynamic management of elements in a stack. It combines the benefits of a stack data structure, which allows for constant-time addition and removal of elements at the top of the stack, with the dynamic resizing abilities of a linked list. This combination makes it an ideal choice for managing collections of elements that follow the Last In, First Out (LIFO) principle, where the most recently added element is the first one to be removed from the stack. In a stack using linked lists, elements are stored in nodes, each of which contains a reference to the next node in the list. The stack itself maintains a reference to the top node. To add elements to the stack (push operation), a new node is created, and its reference to the next node is set to point to the current top node. The stack's reference to the top node is then updated to point to the new node. To remove an element from the stack (pop operation), the stack's reference to the top node is updated to point to the next node in the list, effectively removing the previous top node from the stack. This process allows for constant-time complexity for both push and pop operations, as well as dynamic resizing of the stack as elements are added or removed.
#include<stdio.h>
#include<stdlib.h>
struct node
{
	int info;
	struct node *link;
};
struct node *top=NULL,*temp;
void  push(struct node*);
void pop(struct node*);
void display(struct node*);

int main()
{ 
 int x=0,item;  
 printf("\t****stack using linked list****\n");
	while(x!=4)
	{
		printf("enter your choice");
		printf("\n1.push\n2.pop\n3.display\n4.exit\n");
		scanf("%d",&x);
		switch(x)
		{
			case 1:
			     push(top);
			   break;
			case 2: pop(top);
		  break;
			case 3: display(top);
			      break;
			case 4: return 0;
			      
		}
	}
	
}

void push(struct node *p)
{
	int item;
	struct node *temp;
	temp=(struct node *)malloc(sizeof(struct node));
	printf("enter element to be inserted\n");
			scanf("%d",&item);
			temp->info=item;
			

    
			temp->link=top;
		top=temp;
		
	

	printf("inserted succesfully\n");
}
void pop(struct node *p)
{
	int item;
	struct node *temp;
	
		if(top==NULL)
	printf("stack is empty\n");
	  else{
	  	item=top->info;
	  	temp=top;
	  	top=top->link;
	  	free(temp);
	  	printf("Element popped is%d\n",item);
	  }
	
}


void display(struct node *p)
{
	
	if(top==NULL)
	printf("stack is empty\n");
	else
	{	printf("Elements in the stack are\n");
		while(p!=NULL)
		{
		printf("%d\n",p->info);
		p=p->link;
         }
        // printf("%d\n",p->info);
        
	}

}


LANGUAGE:

DARK MODE: