Implement stack using Linked List - C Program

Write a C program to implement stack using linked list, each node should have the following information about a Student: S_Name (string), S_address (string), S_Marks. (Use local pointer i.e. with return statement)

Solution:

Stack:
A stack is basically a container of object that store the data in order of Last in First Out(LIFO). A stack basically perform three operation

Push: for adding the element
Pop: For removing the element
Peek: For finding top most element.

In this example, we implement the stack using linked list and structure is used for storing the information of student.

Program

#include<stdio.h>
#include<malloc.h>
typedef struct node
{
    char s_name[20],s_address[50];
    int s_marks;
    struct node *next;
}s;
s *push(s*);
s *pop(s *);
void display(s *);
int main()
{
    s *top=NULL;
    int ch,x,c=0;
    printf("Enter 1 for push\n");
    printf("Enter 2 for pop\n");
    printf("Enter 3 for display\n");
    do
    {
         printf("Enter your choice: ");
         scanf("%d",&ch);
         switch(ch)
         {
             case 1:
             top=push(top);
             break;
             case 2:
             top=pop(top);
             break;
             case 3:
             display(top);
             break;
         }
         printf("do you want to continuoe press 1: ");
         scanf("%d",&c);
     }while(c==1);
}
s *push(s *top)
{
    s *p;
    p=(s *)malloc(sizeof(s));
    if(p==NULL)
    {
        printf("no memory allocated");
    }
    else
    {
        printf("\nEnter the student name: ");
        scanf("%s",&p->s_name);
        printf("Enter student address: ");
        scanf("%s",&p->s_address);
        printf("Enter the marks of students: ");
        scanf("%d",&p->s_marks);
        p->next=top;
        top=p;
    }
    return(top);
}
s *pop(s *top)
{
   s *p;
   if(top==NULL)
   {
       printf("nothing to pop");
   }
   else
   {
       printf("\nThe student name is: %s",top->s_name);
       printf("\nThe student address is: %s",top->s_address);
       printf("\nThe marks of the student is: %d",top->s_marks);
       top=top->next;
   }
   return(top);
}
void display(s *top)
{
    if(top==NULL)
    {
        printf("nothing to display");
    }
    else
    {
        while(top!=NULL)
        {
             printf("\nThe student name is: %s",top->s_name);
             printf("\nThe student address is: %s",top->s_address);
             printf("\nThe marks of the student is: %d",top->s_marks);
             top=top->next;
         }
     }
}


Output:

stack implementation