Stack implementation in C using Arrays.
As in one of my previous blog, I showed the Stack implementation in Java using arrays, this time I will demonstrate the same features using C as a programming language. This is a very basic implementation and nicely shows the use of pointers in the easiest way.
#include <stdio.h>
#include <stdlib.h>
struct ArrayStack {
int top;
int capacity;
int *array;
};
struct ArrayStack *createStack(int capacity) {
struct ArrayStack *stack = malloc(sizeof(struct ArrayStack));
stack -> top = -1;
stack -> capacity = capacity;
stack -> array = malloc(stack -> capacity * sizeof(int));
return stack;
}
int isStackEmpty(struct ArrayStack *stack) {
return (stack -> top == -1);
}
int isStackFull(struct ArrayStack *stack) {
return (stack -> top == stack -> capacity - 1);
}
void push(struct ArrayStack *stack, int data) {
if (!isStackFull(stack)) {
stack -> array[++stack -> top] = data;
} else {
printf("The stack is Full!\n");
}
}
int pop(struct ArrayStack *stack) {
if (!isStackEmpty(stack)) {
return stack -> array[stack -> top--];
} else {
printf("The stack is Empty!\n");
return 0;
}
}
void deleteStack(struct ArrayStack *stack) {
if(stack) {
if (stack -> array) {
free(stack -> array);
}
free(stack);
}
}
void display(struct ArrayStack *stack) {
int i = 0;
if(!isStackEmpty(stack)) {
printf("The Stack contents are: ");
for(i = 0; i <= stack -> top; ++i) {
printf("%d ", stack -> array[i]);
}
printf("\n");
} else {
printf("The Stack is empty!\n");
}
}
int main(int argc, char const *argv[])
{
int capacity, operation, num;
char choice;
printf("Enter the stack capacity: ");
scanf("%d", &capacity);
struct ArrayStack *stack = createStack(capacity);
do {
printf("Stack Operations: \n");
printf("\t1. Push\n");
printf("\t2. Pop\n");
printf("\t3. Stack Overflow\n");
printf("\t4. Stack Unverflow\n");
printf("\t5. Display\n");
printf("Enter the operation to be performed: ");
scanf("%d", &operation);
switch(operation) {
case 1: printf("Enter the number to be pushed to the stack: ");
scanf("%d", &num);
push(stack, num);
break;
case 2: printf("The element popped is: %d\n", pop(stack));
break;
case 3: if (isStackFull(stack)) {
printf("Stack Overflow: True\n");
break;
} else {
printf("Stack Overflow: False\n");
break;
}
case 4: if (isStackEmpty(stack)) {
printf("Stack Underflow: True\n");
break;
} else {
printf("Stack Overflow: False\n");
break;
}
case 5: display(stack);
break;
default: printf("InCorrect Input, please try again!\n");
break;
}
printf("Do you want to continue, 'y' / 'n'?: ");
scanf(" %c", &choice);
} while(choice == 'y');
return 0;
}
#include <stdlib.h>
struct ArrayStack {
int top;
int capacity;
int *array;
};
struct ArrayStack *createStack(int capacity) {
struct ArrayStack *stack = malloc(sizeof(struct ArrayStack));
stack -> top = -1;
stack -> capacity = capacity;
stack -> array = malloc(stack -> capacity * sizeof(int));
return stack;
}
int isStackEmpty(struct ArrayStack *stack) {
return (stack -> top == -1);
}
int isStackFull(struct ArrayStack *stack) {
return (stack -> top == stack -> capacity - 1);
}
void push(struct ArrayStack *stack, int data) {
if (!isStackFull(stack)) {
stack -> array[++stack -> top] = data;
} else {
printf("The stack is Full!\n");
}
}
int pop(struct ArrayStack *stack) {
if (!isStackEmpty(stack)) {
return stack -> array[stack -> top--];
} else {
printf("The stack is Empty!\n");
return 0;
}
}
void deleteStack(struct ArrayStack *stack) {
if(stack) {
if (stack -> array) {
free(stack -> array);
}
free(stack);
}
}
void display(struct ArrayStack *stack) {
int i = 0;
if(!isStackEmpty(stack)) {
printf("The Stack contents are: ");
for(i = 0; i <= stack -> top; ++i) {
printf("%d ", stack -> array[i]);
}
printf("\n");
} else {
printf("The Stack is empty!\n");
}
}
int main(int argc, char const *argv[])
{
int capacity, operation, num;
char choice;
printf("Enter the stack capacity: ");
scanf("%d", &capacity);
struct ArrayStack *stack = createStack(capacity);
do {
printf("Stack Operations: \n");
printf("\t1. Push\n");
printf("\t2. Pop\n");
printf("\t3. Stack Overflow\n");
printf("\t4. Stack Unverflow\n");
printf("\t5. Display\n");
printf("Enter the operation to be performed: ");
scanf("%d", &operation);
switch(operation) {
case 1: printf("Enter the number to be pushed to the stack: ");
scanf("%d", &num);
push(stack, num);
break;
case 2: printf("The element popped is: %d\n", pop(stack));
break;
case 3: if (isStackFull(stack)) {
printf("Stack Overflow: True\n");
break;
} else {
printf("Stack Overflow: False\n");
break;
}
case 4: if (isStackEmpty(stack)) {
printf("Stack Underflow: True\n");
break;
} else {
printf("Stack Overflow: False\n");
break;
}
case 5: display(stack);
break;
default: printf("InCorrect Input, please try again!\n");
break;
}
printf("Do you want to continue, 'y' / 'n'?: ");
scanf(" %c", &choice);
} while(choice == 'y');
return 0;
}
Please feel free to comment and let us know if this could be simplified any further!
Please tell me how do we make stack of arrays in C?
ReplyDeletePlease explain how would your struct be!