Linked List Implementation of a stack
#include<stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
#define defCELL(ETYPE,CELL,STACK) typedef struct slist{ ETYPE data;struct slist *next;}CELL;
defCELL(int, CELL, slist)
CELL *top = NULL, *temp; //Global declaration
int push(int n); /*prototype declaration*/
int pop();
int display();
int clear();
int isempty();
int main() {
int ch, n; //Local variable declaration
while (1) {
printf("\n***Menu Selection***\n");
printf("1: PUSH\n");
printf("2: POP\n");
printf("3: CLEAR\n");
printf("4: ISEMPTY\n");
printf("0: EXIT\n");
printf("********************\n");
printf("Enter your choice(0-4):");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("Enter n:");
scanf("%d", &n);
printf("\n'PUSH()' OUTPUT:-\n");
push(n);
display();
break;
case 2:
printf("\n'pop()' OUTPUT:-");
pop() ? printf("TRUE.\n") : printf("FALSE.\n");
display();
break;
case 3:
printf("\n'CLEAR()' OUTPUT:-\n");
clear();
break;
case 4:
printf("\n'ISEMPTY()' OUTPUT:-");
isempty() ? printf("TRUE.\n") : printf("FALSE.\n");
break;
case 0:
printf("Goodbye....!!!!!!\n");
exit(0);
}
}
return 0;
}
int push(int n) //For pushing data
{
temp = (CELL*) malloc(sizeof (CELL));
temp->data = n;
temp->next = top;
top = temp;
printf("\t%d HASBEEN PUSHED INTO STACK.\n", n);
return 0;
}
int pop()//For poping data
{
if (isempty()) return FALSE;
else {
temp = top;
top = top->next;
free(temp);
return TRUE;
}
}
int display()//For displaying data
{
temp = top;
if (isempty()) printf("\tStack : [].\n");
else {
printf("Stack \n");
for (temp = top; temp != NULL; temp = temp->next) {
printf("[%d]\n ", temp->data);
}
}
return 0;
}
int isempty() {
return (top == NULL);
}
int clear() {
if (isempty()) printf("\tStack : [].\n");
else {
top = NULL;
printf("\t STACK HASBEEN CLEARED.\n");
}
return 0;
}
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
#define defCELL(ETYPE,CELL,STACK) typedef struct slist{ ETYPE data;struct slist *next;}CELL;
defCELL(int, CELL, slist)
CELL *top = NULL, *temp; //Global declaration
int push(int n); /*prototype declaration*/
int pop();
int display();
int clear();
int isempty();
int main() {
int ch, n; //Local variable declaration
while (1) {
printf("\n***Menu Selection***\n");
printf("1: PUSH\n");
printf("2: POP\n");
printf("3: CLEAR\n");
printf("4: ISEMPTY\n");
printf("0: EXIT\n");
printf("********************\n");
printf("Enter your choice(0-4):");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("Enter n:");
scanf("%d", &n);
printf("\n'PUSH()' OUTPUT:-\n");
push(n);
display();
break;
case 2:
printf("\n'pop()' OUTPUT:-");
pop() ? printf("TRUE.\n") : printf("FALSE.\n");
display();
break;
case 3:
printf("\n'CLEAR()' OUTPUT:-\n");
clear();
break;
case 4:
printf("\n'ISEMPTY()' OUTPUT:-");
isempty() ? printf("TRUE.\n") : printf("FALSE.\n");
break;
case 0:
printf("Goodbye....!!!!!!\n");
exit(0);
}
}
return 0;
}
int push(int n) //For pushing data
{
temp = (CELL*) malloc(sizeof (CELL));
temp->data = n;
temp->next = top;
top = temp;
printf("\t%d HASBEEN PUSHED INTO STACK.\n", n);
return 0;
}
int pop()//For poping data
{
if (isempty()) return FALSE;
else {
temp = top;
top = top->next;
free(temp);
return TRUE;
}
}
int display()//For displaying data
{
temp = top;
if (isempty()) printf("\tStack : [].\n");
else {
printf("Stack \n");
for (temp = top; temp != NULL; temp = temp->next) {
printf("[%d]\n ", temp->data);
}
}
return 0;
}
int isempty() {
return (top == NULL);
}
int clear() {
if (isempty()) printf("\tStack : [].\n");
else {
top = NULL;
printf("\t STACK HASBEEN CLEARED.\n");
}
return 0;
}