Implementation-of-Stack-Using-pointer-In-C-with-dataStructure
admin
#Implementation-of-Stack-Using-pointer-In-C-with-dataStructure
Are you looking to understand how to implement a stack using pointers in C programming? In this article, we dive deep into creating a stack, pushing and popping elements, reversing stacks, and handling specific stack operations with pointers. With examples and detailed explanations, this guide is tailored for both beginners and experienced programmers.
A stack is a Last In, First Out (LIFO) data structure where elements are added (pushed) and removed (popped) from the top. By using pointers, we can dynamically manage memory and implement various stack operations efficiently.
Key features of this implementation include:
Here is the complete code for implementing a stack using pointers in C:
#include<stdio.h>
#define MAX 10
void push(int *stack, int *top, int val) {
if (*top == MAX - 1) {
printf("\nStack is Overflow");
return;
}
stack[++(*top)] = val;
}
int pop(int *stack, int *top) {
if (*top == -1) {
printf("\nStack is Underflow");
return -999;
}
return stack[(*top)--];
}
void show(int *stack, int *top) {
if (*top == -1) {
printf("\nStack is Empty");
return;
}
for (int i = *top; i >= 0; i--) {
printf(" %d", stack[i]);
}
}
void reverse(int *stack, int *top) {
int s2[MAX], t2 = -1, s3[MAX], t3 = -1;
while (*top != -1) {
push(s2, &t2, pop(stack, top));
}
while (t2 != -1) {
push(s3, &t3, pop(s2, &t2));
}
while (t3 != -1) {
push(stack, top, pop(s3, &t3));
}
}
void cnt_occ_ele(int *stack, int *top, int val) {
int s2[MAX], t2 = -1, p_ele, count = 0;
while (*top != -1) {
p_ele = pop(stack, top);
if (val == p_ele) {
count++;
}
push(s2, &t2, p_ele);
}
while (t2 != -1) {
push(stack, top, pop(s2, &t2));
}
printf("\n\nOccurrences of %d = %d", val, count);
}
void del_occ_ele(int *stack, int *top, int val) {
int s2[MAX], t2 = -1, p_ele;
while (*top != -1) {
p_ele = pop(stack, top);
if (val != p_ele) {
push(s2, &t2, p_ele);
}
}
while (t2 != -1) {
push(stack, top, pop(s2, &t2));
}
}
void bottam_ele(int *stack, int *top) {
int s2[MAX], t2 = -1, b_ele;
while (*top != -1) {
push(s2, &t2, pop(stack, top));
}
b_ele = pop(s2, &t2);
while (t2 != -1) {
push(stack, top, pop(s2, &t2));
}
printf("\nBottom Element: %d", b_ele);
}
int main() {
int s1[MAX], t1 = -1;
int no, ch;
do {
printf("\n1. Push\n2. Pop\n3. Show\n4. Reverse\n5. Bottom Element\n6. Count Occurrences of Element\n7. Delete Occurrences of Element\n0. Exit\nEnter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("\nEnter number: ");
scanf("%d", &no);
push(s1, &t1, no);
break;
case 2:
no = pop(s1, &t1);
if (no != -999) {
printf("\n%d popped", no);
}
break;
case 3:
show(s1, &t1);
break;
case 4:
reverse(s1, &t1);
break;
case 5:
bottam_ele(s1, &t1);
break;
case 6:
printf("\nEnter element to count occurrences: ");
scanf("%d", &no);
cnt_occ_ele(s1, &t1, no);
break;
case 7:
printf("\nEnter element to delete all occurrences: ");
scanf("%d", &no);
del_occ_ele(s1, &t1, no);
break;
case 0:
break;
default:
printf("\nInvalid choice");
}
} while (ch != 0);
return 0;
}
In this article, we demonstrated how to implement a stack using pointers in C. By leveraging pointers, the stack's top position is dynamically updated, providing efficient memory management and flexibility. This method allows for operations like reversing, counting, and deleting elements, making it a versatile approach in C programming.
Mastering pointer-based stack implementation is essential for anyone diving into data structures. Continue exploring and applying these concepts to strengthen your coding skills!