STACK LAB Programs(Code Tantra)
3.3.4. Reversing an array using stack
#include<stdio.h>
#include<conio.h>
#define SIZE 30
int A[SIZE];
int top = -1;
void push(long int element) {
if(top == SIZE - 1) {
printf("Stack is overflow.\n");
} else {
top = top + 1;
A[top] = element;
}
}
int pop() {
int x;
if(top < 0) {
printf("Stack is underflow.\n");
return -1;
} else {
x = A[top];
top = top - 1;
return x;
}
}
void main() {
int size, index;
int arr[30];
printf("Enter the size of the array : ");
scanf("%d", &size);
if(size < 0) {
printf("Invalid input! Size should be greater than 0.\n");
return;
}
for(index = 0; index < size; index++) {
printf("Enter arr[%d] : ",index);
scanf("%d",&arr[index]);
}
printf("Array before reversing : ");
for(index = 0; index < size; index++) {
printf("%d ",arr[index]);
}
printf("\n");
//Write the logic for reversing an array using
//stack and their opertions.
for(index = 0; index < size; index++) {
push(arr[index]);
}
for(index = 0; index < size; index++) {
arr[index]=pop();
}
printf("Array after reversing : ");
for(index = 0; index < size; index++) {
printf("%d ",arr[index]);
}
printf("\n");
}
3.3.5. Reversing a string using a stack.
#include<stdio.h>
#include<string.h>
#define STACK_MAX_SIZE 30
char arr[STACK_MAX_SIZE];
int top = -1;
void push(char element) {
if(top == STACK_MAX_SIZE - 1) {
printf("Stack is overflow.\n");
} else {
top = top + 1;
arr[top] = element;
}
}
char pop() {
long int x;
if(top < 0) {
printf("Stack is underflow.\n");
return -1;
} else {
x = arr[top];
top = top - 1;
return x;
}
}
void reverse(char str[]) {
int index;
for(index = 0; str[index]!='\0'; index++) {
push(str[index]);
}
for(index = 0; str[index]!='\0'; index++) {
str[index]=pop();
}
}
void main() {
char ch[80], temp;
printf("Enter a string : ");
scanf("%s", ch);
reverse(ch);
printf("The reverse of a given string : %s\n", ch);
}
3.3.6. Calculating factorial of a number using a stack.
#include<stdio.h>
#include<conio.h>
#define MAX 30
long int A[MAX];
int top = -1;
void push(long int element) {
if(top == MAX - 1) {
printf("Stack is overflow.\n");
} else {
top = top + 1;
A[top] = element;
}
}
long int pop() {
long int x;
if(top < 0) {
printf("Stack is underflow.\n");
return -1;
} else {
x = A[top];
top = top - 1;
return x;
}
}
// Implement the factorial program using the stack.
long int factorial(long int n) {
long int i=1,j;
push(1);
for(i=1;i<=n;i++)
{
j=i*pop();
push(j);
}
j=pop();
return j;
}
void main() {
long int n;
printf("Enter an integer : ");
scanf("%ld", &n);
if(n < 0) {
printf("Invalid input! Only positive numbers are allowed.\n");
return;
}
printf("Factorial of %ld is : %ld\n", n, factorial(n));
}
3.3.7. Understanding Tail Recursion.
#include <stdio.h>
#include <conio.h>
#include "FactorialFunction.c"
int fact(unsigned int n) {
return factTR(n, 1);
}
void main() {
int n;
printf("Enter a positive number : ");
scanf("%d", &n);
if (n > 0)
printf("The factorial of %d is %d\n",n,fact(n));
else
printf("Invalid Number\n");
}
int factTR(int n, int result)
{
if(n==0 || n==1)
{
return result;
}
return factTR(n-1,n*result);
}
LAB Programs
3.4.1. Equalize Stack Heights.
#include <stdio.h>
#include <stdlib.h>
int totalHeight(int *s, int size) {
int sum = 0;
for (int i = 0; i < size; i++)
sum = sum+s[i];
return sum;
}
int findequalstackHeights(int *s1, int n1, int *s2, int n2, int *s3, int n3)
{
int h1 = totalHeight(s1, n1);
int h2 = totalHeight(s2, n2);
int h3 = totalHeight(s3, n3);
int i = 0, j = 0, k = 0; // indices for each stack
while(h1 && h2 && h3)
{
if (h1 == h2 && h2 == h3)
return h1;
if (h1 >= h2 && h1 >= h3) {
h1 = h1-s1[i++];
}else if (h2 >= h1 && h2 >= h3) {
h2 = h2-s2[j++];
} else{
h3 =h3- s3[k++];
}
}
return 0;
}
int main() {
int n1, n2, n3,i;
scanf("%d", &n1);
int *s1 = (int *)malloc(n1*sizeof(int));
for (i = 0; i < n1; i++)
scanf("%d", &s1[i]);
scanf("%d", &n2);
int *s2 = (int *)malloc(n2*sizeof(int));
for (i = 0; i < n2; i++)
scanf("%d", &s2[i]);
scanf("%d", &n3);
int *s3 = (int *)malloc(n3*sizeof(int));
for (i = 0; i < n3; i++)
scanf("%d", &s3[i]);
int ans = findequalstackHeights(s1, n1, s2, n2, s3, n3);
printf("%d\n", ans);
free(s1);
free(s2);
free(s3);
return 0;
}
3.4.2. Bracket Validation.
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#define MAX_STACK_SIZE 10000
// Stack implementation for storing brackets
char stack[MAX_STACK_SIZE];
int top=-1;
void push(char x) {
if(top == MAX_STACK_SIZE - 1)
{
return;
} else
{
top = top + 1;
stack[top] = x;
}
}
char pop() {
if(top==-1)
return '\0';
else
{
char x;
x=stack[top];
top=top-1;
return x;
}
}
bool isValid(char *s)
{
for(int i=0;s[i]!='\0';s++)
{
if(*s=='(' || *s=='{' || *s=='[')
push(*s);
else if(*s==')' || *s=='}' || *s==']')
{
char l=pop();
if(*s==')' && l=='(')
continue;
else if(*s=='}' && l=='{')
continue;
else if(*s==']' && l=='[')
continue;
else
return false;
}
}
if(top==-1)
return true;
else
return false;
}
int main() {
char s[MAX_STACK_SIZE];
printf("");
scanf("%s", s);
if (isValid(s)) {
printf("true\n");
} else {
printf("false\n");
}
return 0;
}
3.4.5. Stack Operations.
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
typedef struct Stack {
Node* top;
} Stack;
//typedef Stack * stk;
void initializeStack(Stack * stk) {
stk->top=NULL;
}
int isEmpty(Stack *stk) {
if(stk->top==NULL)
return 1;
else
return 0;
}
void push(Stack *stk, int x) {
Node *p;
p=(Node*)malloc(sizeof(Node));
if(p==NULL)
printf("overflow");
else{
p->data=x;
p->next=NULL;
p->next=stk->top;
stk->top=p;
printf("Pushed %d\n", x);
}
}
void pop(Stack *stk) {
if(isEmpty(stk))
printf("Stack is empty\n");
else
{
printf("Popped %d\n",stk->top->data);
Node *s;
s=stk->top;
stk->top=stk->top->next;
free(s);
}
}
void peek(Stack *stk) {
if(isEmpty(stk))
printf("Stack is empty\n");
else
{
printf("Top Element: %d\n",stk->top->data);
}
}
int main() {
Stack stack;
initializeStack(&stack);
int numOperations, choice, value;
printf("No of operations: ");
scanf("%d", &numOperations);
for (int i = 0; i < numOperations; i++) {
printf("Choose an operation:\n1. Push\n2. Pop\n3. Peek\n4. Check if stack is
empty\n5. Exit\nchoice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("value to push: ");
scanf("%d", &value);
push(&stack, value);
break;
case 2:
pop(&stack);
break;
case 3:
peek(&stack);
break;
case 4:
if (isEmpty(&stack)) {
printf("Stack is empty\n");
} else {
printf("Stack is not empty\n");
}
break;
case 5:
printf("Exiting program!");
exit(0);
default:
printf("Invalid choice\n");
}
}
return 0;
}