#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
};
void linkedListTraversal(struct Node *ptr)
{
while (ptr != NULL)
{
printf("Element: %d\n", ptr->data);
ptr = ptr->next;
}
}
int isEmpty(struct Node *top)
{
if (top == NULL)
{
return 1;
}
else
{
return 0;
}
}
int isFull(struct Node *top)
{
struct Node *p = (struct Node *)malloc(sizeof(struct Node));
if (p == NULL)
{
return 1;
}
else
{
return 0;
}
}
struct Node *push(struct Node *top, int x)
{
if (isFull(top))
{
printf("Stack Overflow\n");
}
else
{
struct Node *n = (struct Node *)malloc(sizeof(struct Node));
n->data = x;
n->next = top;
top = n;
return top;
}
}
int pop(struct Node **top)
{
if (isEmpty(*top))
{
printf("Stack Underflow\n");
}
else
{
struct Node *n = *top;
*top = (*top)->next;
int x = n->data;
free(n);
return x;
}
}
int main()
{
int c, p;
struct Node *top = NULL;
printf("Stack has been created successfully\n");
options:
printf("\n\n1) Check if the stack is empty \n2) Display the contents of stack \n3) Push \n4) Pop\n5) exit\n");
printf("enter your choice : ");
scanf("%d", &c);
switch (c)
{
case 1:
printf("Empty([1/0]-->[yes/no]): %d\n", isEmpty(top));
goto options;
break;
case 2:
linkedListTraversal(top);
goto options;
break;
case 3:
printf("enter the value to push : ");
scanf("%d", &p);
top=push(top, p);
goto options;
break;
case 4:
printf("Popped %d from the stack\n", pop(&top));
goto options;
break;
case 5:
exit(0);
break;
default:
printf("invalid input\n");
goto options;
break;
}
return 0;
}