0% found this document useful (0 votes)
25 views

Stack

stack using c
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Stack

stack using c
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

#include <stdio.h>
#include "stack.h"

int main(void) {
struct Stack* stack = createEmptyStack();
char input[MAX_SIZE];

printf("Enter a string: ");


fgets(input, sizeof(input), stdin);

for (int i = 0; input[i] != '\0'; i++) {


if (input[i] != '\n') {
push(stack, input[i]);
}
}

while (!isEmpty(stack)) {
printf("%c", pop(stack));
}
printf("\n");

return 0;
}

2.
#include <stdio.h>
#include "stack.h"

int main(void) {
struct Stack* stack = createEmptyStack();
char choice;
int count = 1;
int ent, min;

do {
printf("Enter operation %d: ", count);
scanf(" %c", &choice);

switch (choice) {
case '+':
scanf(" %d", &ent);
push(stack, ent);
count++;
break;

case '-':
if (!isEmpty(stack)) {
pop(stack);
}
count++;
break;

case '#':
if(!isEmpty(stack)) {
min = pop(stack);
while (!isEmpty(stack)) {
int val = pop(stack);
if (min > val) {
min = val;
}
}
printf("Minimum element: %d\n", min);
}
break;

default:
printf("Invalid operator!");
break;
}
} while(choice != '#');

return 0;
}

3.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "stack.h"

int main(void) {
char exp[MAX_SIZE];
printf("Enter string: ");
scanf("%s", exp);

struct Stack* stack = createEmptyStack();


int i = 0;
while (exp[i]) {
if (exp[i] == '{' || exp[i] == '(' || exp[i] == '['){
push(stack, exp[i]);
}
if (exp[i] == '}' || exp[i] == ')' || exp[i] == ']') {
if (isEmpty(stack)) {
printf("False\n");
return 0;
}
char popped = pop(stack);
if (!((popped == '{' && exp[i] == '}') ||
(popped == '(' && exp[i] == ')') ||
(popped == '[' && exp[i] == ']'))) {
printf("False\n");
return 0;
}
}
i++;
}

if (isEmpty(stack))
printf("True\n");
else
printf("False\n");

return 0;
}

4.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stack.h"

int isOperator(char c){


return c == '+' || c == '-' || c == '*' || c == '/' || c == '%';
}

void prefixToInfix(char prefix[], char infix[]){


struct Stack* stack = createEmptyStack();
int len = strlen(prefix);
for(int i = len - 1; i >= 0; i--){
if(isOperator(prefix[i])){
char op1[MAX_SIZE],op2[MAX_SIZE];
strcpy(op1, pop(stack));
strcpy(op2, pop(stack));
sprintf(infix, "(%s %c %s)",op1 ,prefix[i], op2);
push(stack,infix);
}else{
char operand[2]={prefix[i], '\0'};
push(stack, operand);
}
}
}

int main(void) {

char prefix[MAX_SIZE];
printf("Enter expression: ");
fgets(prefix, sizeof(prefix),stdin);
int len = strlen(prefix);
if(prefix[len -1] == '\n'){
prefix[len - 1] = '\0';
}

char infix[MAX_SIZE];
prefixToInfix(prefix, infix);
printf("%s\n",infix);

return 0;
}

5.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stack.h"

int isOperator(char c){


return c == '+' || c == '-' || c == '*' || c == '/' || c == '%';
}

void postfixToPrefix(char postfix[], char prefix[]){


struct Stack* stack = createEmptyStack();
int len = strlen(postfix);
for(int i = 0; i < len; i++){
if(isOperator(postfix[i])){
char op2[MAX_SIZE],op1[MAX_SIZE];
strcpy(op2, pop(stack));
strcpy(op1, pop(stack));
sprintf(prefix, "%c%s%s", postfix[i], op1, op2);
push(stack,prefix);
}else{
char operand[2]={postfix[i], '\0'};
push(stack, operand);
}
}
strcpy(prefix, pop(stack));
}

int main(void) {

char postfix[MAX_SIZE];
printf("Enter expression: ");
fgets(postfix, sizeof(postfix),stdin);
int len = strlen(postfix);
if(postfix[len -1] == '\n'){
postfix[len - 1] = '\0';
}

char prefix[MAX_SIZE];
postfixToPrefix(postfix, prefix);
printf("%s\n",prefix);

return 0;
}

6.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stack.h"

int isOperator(char c){


return c == '+' || c == '-' || c == '*' || c == '/' || c == '%';
}

int evaluatePostfix(char postfix[]){


struct Stack* stack = createEmptyStack();
int len = strlen(postfix);
for(int i = 0; i < len; i++){
if(postfix[i] == ' ' || postfix[i] == 't'){
continue;
}else if(isOperator(postfix[i])){
int op2 = pop(stack);
int op1 = pop(stack);
switch(postfix[i]){
case '+':
push(stack, op1 + op2);
break;
case '-':
push(stack, op1 - op2);
break;
case '*':
push(stack, op1 * op2);
break;
case '/':
push(stack, op1 / op2);
break;
}
}else{
int operand = 0;
while(isdigit(postfix[i])){
operand = operand * 10 + (postfix[i] - '0');
i++;
}
i--;
push(stack, operand);
}
}
int result = pop(stack);
free(stack);
return result;
}

int main(void) {
char postfix[MAX_SIZE];
printf("Enter expression: ");
fgets(postfix, sizeof(postfix),stdin);
int len = strlen(postfix);
if(postfix[len -1] == '\n'){
postfix[len - 1] = '\0';
}

int result = evaluatePostfix(postfix);


printf("%d\n",result);

return 0;
}

You might also like