Operations of Stack in C
A stack supports several standard operations that allow adding, removing, and inspecting elements. Here are the primary operations and their implementation in C:
1. Push Operation
The push operation adds an element to the top of the stack.
- Logic: Check if the stack is full. If not, increment the top pointer and store the element.
Code
#include <stdio.h>
#define MAX 5 // Maximum size of the stack
typedef struct {
int arr[MAX];
int top;
} Stack;
void initStack(Stack *s) {
s->top = -1;
}
int isFull(Stack *s) {
return s->top == MAX – 1;
}
void push(Stack *s, int value) {
if (isFull(s)) {
printf(“Stack Overflow\n”);
} else {
s->arr[++(s->top)] = value;
printf(“Pushed: %d\n”, value);
}
}
int main() {
Stack s;
initStack(&s);
push(&s, 10);
push(&s, 20);
return 0;
}
2. Pop Operation
The pop operation removes the top element from the stack.
- Logic: Check if the stack is empty. If not, retrieve the element at the top pointer, then decrement the pointer.
Code
int isEmpty(Stack *s) {
return s->top == -1;
}
int pop(Stack *s) {
if (isEmpty(s)) {
printf(“Stack Underflow\n”);
return -1;
} else {
return s->arr[(s->top)–];
}
}
int main() {
Stack s;
initStack(&s);
push(&s, 10);
push(&s, 20);
printf(“Popped: %d\n”, pop(&s));
return 0;
}
3. Peek Operation
The peek operation retrieves the top element of the stack without removing it.
- Logic: Check if the stack is empty. If not, return the value at the top pointer.
Code
int peek(Stack *s) {
if (isEmpty(s)) {
printf(“Stack is empty\n”);
return -1;
} else {
return s->arr[s->top];
}
}
int main() {
Stack s;
initStack(&s);
push(&s, 10);
push(&s, 20);
printf(“Top element: %d\n”, peek(&s));
return 0;
}
4. isEmpty Operation
The isEmpty operation checks whether the stack is empty.
- Logic: If top == -1, the stack is empty.
Code
int isEmpty(Stack *s) {
return s->top == -1;
}
int main() {
Stack s;
initStack(&s);
if (isEmpty(&s)) {
printf(“Stack is empty\n”);
}
push(&s, 10);
if (!isEmpty(&s)) {
printf(“Stack is not empty\n”);
}
return 0;
}
5. isFull Operation
The isFull operation checks whether the stack is full.
- Logic: If top == MAX – 1, the stack is full.
Code
int isFull(Stack *s) {
return s->top == MAX – 1;
}
int main() {
Stack s;
initStack(&s);
for (int i = 0; i < MAX; i++) {
push(&s, i + 1);
}
if (isFull(&s)) {
printf(“Stack is full\n”);
}
return 0;
}
6. Display Stack Elements
The display operation prints all elements of the stack from top to bottom.
- Logic: Traverse the array from top to 0.
Code
void display(Stack *s) {
if (isEmpty(s)) {
printf(“Stack is empty\n”);
} else {
printf(“Stack elements: “);
for (int i = s->top; i >= 0; i–) {
printf(“%d “, s->arr[i]);
}
printf(“\n”);
}
}
int main() {
Stack s;
initStack(&s);
push(&s, 10);
push(&s, 20);
push(&s, 30);
display(&s);
return 0;
}
Combining All Operations
Below is a complete program demonstrating all stack operations:
#include <stdio.h>
#define MAX 5
typedef struct {
int arr[MAX];
int top;
} Stack;
void initStack(Stack *s) {
s->top = -1;
}
int isFull(Stack *s) {
return s->top == MAX – 1;
}
int isEmpty(Stack *s) {
return s->top == -1;
}
void push(Stack *s, int value) {
if (isFull(s)) {
printf(“Stack Overflow\n”);
} else {
s->arr[++(s->top)] = value;
printf(“Pushed: %d\n”, value);
}
}
int pop(Stack *s) {
if (isEmpty(s)) {
printf(“Stack Underflow\n”);
return -1;
} else {
return s->arr[(s->top)–];
}
}
int peek(Stack *s) {
if (isEmpty(s)) {
printf(“Stack is empty\n”);
return -1;
} else {
return s->arr[s->top];
}
}
void display(Stack *s) {
if (isEmpty(s)) {
printf(“Stack is empty\n”);
} else {
printf(“Stack elements: “);
for (int i = s->top; i >= 0; i–) {
printf(“%d “, s->arr[i]);
}
printf(“\n”);
}
}
int main() {
Stack s;
initStack(&s);
push(&s, 10);
push(&s, 20);
push(&s, 30);
display(&s);
printf(“Top element: %d\n”, peek(&s));
printf(“Popped: %d\n”, pop(&s));
display(&s);
return 0;
}
Output Example
plaintext
Copy code
Pushed: 10
Pushed: 20
Pushed: 30
Stack elements: 30 20 10
Top element: 30
Popped: 30
Stack elements: 20 10
This covers all essential stack operations implemented in C.