Array Implementation of stack using C
#include<stdio.h>
#include<stdlib.h>
#define size 5
#define TRUE 1
#define FALSE 0
typedef struct //STRUCTURE DEFINITION
{
int data[size];
int top;
} stack;
stack *stk; //GLOBAL VARIABLE DECLARATION
int push(int); //prototype declarations
int pop();
int display();
int isfull();
int clear();
int isempty();
int main() {
int n, i, ch;
stk = (stack*) malloc(sizeof (stack)); //DYNAMIC MEMORY ALLOCATION
stk->top = -1;
while (1) {
printf("\n***Menu Selection***\n");
printf("1: Push\n");
printf("2: Pop\n");
printf("3: Clear\n");
printf("4: IsEmpty\n");
printf("5: IsFull\n");
printf("0: exit.\n");
printf("********************\n");
printf("Enter your choice(0-5):");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("Enter n:");
scanf("%d", &n);
printf("\n'PUSH()' OUTPUT:-");
push(n) ? printf("TRUE.\n") : printf("FALSE.\n");
display();
break;
case 2:
printf("\n'POP()' OUTPUT:-");
pop(n) ? printf("TRUE.\n") : printf("FALSE.\n");
display();
break;
case 3:
printf("\n'CLEAR()' OUTPUT:-\n");
clear();
printf("\t STACK HASBEEN CLEARED.\n");
break;
case 4:
printf("\n'ISEMPTY()' OUTPUT:-");
isempty() ? printf("TRUE.\n") : printf("FALSE.\n");
break;
case 5:
printf("\n'ISFULL()' OUTPUT:-");
isfull() ? printf("TRUE.\n") : printf("FALSE.\n");
break;
case 0:
printf("GoodBye.....!!!!!\n");
exit(0);
}
}
return 0;
}
int push(int n) //to push(Insert) data into the stack
{
if (isfull()) //checking whether stack is full.
return FALSE;
else {
stk->data[++(stk->top)] = n;
return TRUE;
}
}
int pop() //pop(Delete) data from stack
{
if (isempty()) return FALSE;
else {
stk->top--;
return TRUE;
}
}
int display() {
int i;
if (isempty()) printf("\tstack : [].\n");
else {
printf("Stack\n");
for (i = stk->top; i >= 0; i--) {
printf("[%d] \n", stk->data[i]);
}
}
return 0;
}
int isempty() // check whether the stack is ().
{
return (stk->top == -1);
}
int isfull() // check whether the stack is full.
{
return (stk->top == size - 1);
}
int clear() //Make the stack ().
{
stk->top = -1;
return 0;
}
#include<stdlib.h>
#define size 5
#define TRUE 1
#define FALSE 0
typedef struct //STRUCTURE DEFINITION
{
int data[size];
int top;
} stack;
stack *stk; //GLOBAL VARIABLE DECLARATION
int push(int); //prototype declarations
int pop();
int display();
int isfull();
int clear();
int isempty();
int main() {
int n, i, ch;
stk = (stack*) malloc(sizeof (stack)); //DYNAMIC MEMORY ALLOCATION
stk->top = -1;
while (1) {
printf("\n***Menu Selection***\n");
printf("1: Push\n");
printf("2: Pop\n");
printf("3: Clear\n");
printf("4: IsEmpty\n");
printf("5: IsFull\n");
printf("0: exit.\n");
printf("********************\n");
printf("Enter your choice(0-5):");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("Enter n:");
scanf("%d", &n);
printf("\n'PUSH()' OUTPUT:-");
push(n) ? printf("TRUE.\n") : printf("FALSE.\n");
display();
break;
case 2:
printf("\n'POP()' OUTPUT:-");
pop(n) ? printf("TRUE.\n") : printf("FALSE.\n");
display();
break;
case 3:
printf("\n'CLEAR()' OUTPUT:-\n");
clear();
printf("\t STACK HASBEEN CLEARED.\n");
break;
case 4:
printf("\n'ISEMPTY()' OUTPUT:-");
isempty() ? printf("TRUE.\n") : printf("FALSE.\n");
break;
case 5:
printf("\n'ISFULL()' OUTPUT:-");
isfull() ? printf("TRUE.\n") : printf("FALSE.\n");
break;
case 0:
printf("GoodBye.....!!!!!\n");
exit(0);
}
}
return 0;
}
int push(int n) //to push(Insert) data into the stack
{
if (isfull()) //checking whether stack is full.
return FALSE;
else {
stk->data[++(stk->top)] = n;
return TRUE;
}
}
int pop() //pop(Delete) data from stack
{
if (isempty()) return FALSE;
else {
stk->top--;
return TRUE;
}
}
int display() {
int i;
if (isempty()) printf("\tstack : [].\n");
else {
printf("Stack\n");
for (i = stk->top; i >= 0; i--) {
printf("[%d] \n", stk->data[i]);
}
}
return 0;
}
int isempty() // check whether the stack is ().
{
return (stk->top == -1);
}
int isfull() // check whether the stack is full.
{
return (stk->top == size - 1);
}
int clear() //Make the stack ().
{
stk->top = -1;
return 0;
}