Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

LAB-02

~S.SOUMYA
QUESTION1:-Program to perform push,pop and peek operations on a stack
using arrays.
CODE:-
#include<stdio.h>
#include<conio.h>
int s[100],x,max,top,choice;
void push(void);
void pop(void);
void peek(void);
int main(){
top=-1;
printf("Enter the size of the stack: ");
scanf("%d",&max);
menu:{
printf("\nStack operations using array\n");
printf("1.PUSH\n2.POP\n3.PEEK\n4.EXIT\n");
do
{
printf("enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
goto menu;
break;
}
case 2:
{
pop();
goto menu;
break;
}
case 3:
{
peek();
goto menu;
break;
}
case 4:
{
break;
}
default:
{
printf("Please enter a valid choice(1/2/3/4)");
}
}
}
while(choice!=4);
return 0;
}
}
void push(){
if(top==max-1)
{
printf("Stack is overflow");
}
else
{
printf("enter a value to be pushed:");
scanf("%d",&x);
top=top+1;
s[top]=x;
}
}
void pop()
{
if(top==-1)
{
printf("stack is underflow");
}
else
{
printf("the popped element is :%d",s[top]);
top=top-1;
}
}
void peek()
{
if(top==-1)
{
printf("stack is underflow");
}
else
{
printf("The peek element is:%d",s[top]);
}
}
OUTPUT:-
QUESTION 2:-Program to implement stack using linked list.
CODE:-
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
}*top,*max,*temp;
int x,choice,count;
void push(void);
int pop();
void peek(void);
void traversal(void);
int main(){
menu:{
printf("\n\nStack operations using Linked list\n");
printf("1.PUSH\n2.POP\n3.PEEK\n4.TRAVERSAL\n5.EXIT\n");
do
{
printf("enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
goto menu;
break;
}
case 2:
{
printf("the popped element is:%d",pop());
goto menu;
break;
}
case 3:
{
peek();
goto menu;
break;
}
case 4:
{
traversal();
goto menu;
break;
}
case 5:
{
exit(0);
break;
}
default:
{
printf("Please enter a valid choice(1/2/3/4/5)\n");
}
}
}
while(choice!=4);
return 0;
}
}
void push()
{
temp=(struct node*)malloc(1*sizeof(struct node));
printf("input the data to be pushed:");
scanf("%d",&x);
temp->data=x;
temp->next=NULL;
if(top==NULL)
{
top=temp;
}
else
{
temp->next=top;
top=temp;
}
count++;
printf("Node is inserted");
}
int pop()
{
if(top==NULL)
{
printf("stack is underflow");
}
else
{
struct node *temp=top;
int temp_data=top->data;
top=top->next;
free(temp);
return temp_data;
}
}
void peek()
{
if(top==NULL)
{
printf("stack is underflow");
}
else
{
printf("The peek element is:%d",top->data);
}
}
void traversal()
{
if(top==NULL)
{
printf("stack is underflow");
}
else
{
printf("The stack is:");
max=top;
while(max!=NULL)
{
printf("%d ",max->data);
max=max->next;
}
}
}
OUTPUT:-
QUESTION 3:-Program to reverse the contents of stack
CODE:-
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 10
int stack[MAX_SIZE];
int top = -1;
//push element
void push(int item) {
if (top == MAX_SIZE - 1) {
printf("Stack Overflow!\n");
return;
}
stack[++top] = item;
}
//pop element
int pop() {
if (top == -1) {
printf("Stack Underflow!\n");
return -1;
}
return stack[top--];
}
//reverse the stack
void reverse() {
int temp[MAX_SIZE];
int i, j;
for (i = 0; i <= top; i++) {
temp[i] = stack[i];
}
for (i = 0, j = top; j >= 0; i++, j--) {
stack[i] = temp[j];
}
}
int main() {
int i, n, item;
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter the elements:\n");
for (i = 0; i < n; i++) {
scanf("%d", &item);
push(item);
}
printf("Stack before reversal:\n");
for (i = top; i >= 0; i--) {
printf("%d ", stack[i]);
}
reverse();
printf("\nStack after reversal:\n");
for (i = top; i >= 0; i--) {
printf("%d ", stack[i]);
}
return 0;
}
OUTPUT:-

QUESTION 4:-Program to check balancing of paranthesis.


CODE:-
#include <stdio.h>
#include <string.h>
#define MAX 100

char stack[MAX];
int top = -1;

//push element
void push(char item) {
if (top == MAX-1) {
printf("Stack Overflow\n");
return;
}
stack[++top] = item;
}

//pop element
char pop() {
if (top == -1) {
printf("Stack Underflow\n");
return ' ';
}
return stack[top--];
}

//checkING if the parentheses are balanced


int isBalanced(char exp[]) {
int i;
for (i = 0; i < strlen(exp); i++) {
if (exp[i] == '(' || exp[i] == '[' || exp[i] == '{') {
push(exp[i]);
} else if (exp[i] == ')' || exp[i] == ']' || exp[i] == '}') {
if (top == -1) {
return 0;
} else if ((exp[i] == ')' && stack[top] == '(') || (exp[i] == ']' && stack[top]
== '[') || (exp[i] == '}' && stack[top] == '{')) {
pop();
} else {
return 0;
}
}
}
if (top == -1) {
return 1;
} else {
return 0;
}
}

int main() {
char exp[MAX];
printf("Input an expression: ");
scanf("%s", exp);
if (isBalanced(exp)) {
printf("Parentheses are balanced.\n");
} else {
printf("Parentheses are not balanced.\n");
}
return 0;
}
OUTPUT:-
QUESTION 5:-Program to convert infix to postfix and prefix
CODE:-
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100
char stack[MAX];
int top = -1;
// Function to push element to stack
void push(char item) {
if (top == MAX-1) {
printf("Stack Overflow!\n");
return;
}
stack[++top] = item;
}
// Function to pop element from stack
char pop() {
if (top == -1) {
printf("Stack Underflow!\n");
return ' ';
}
return stack[top--];
}
// Function to check if the character is an operator
int isOperator(char ch) {
if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '^') {
return 1;
} else {
return 0;
}
}
// Function to get the precedence of the operator
int getPrecedence(char ch) {
if (ch == '+' || ch == '-') {
return 1;
} else if (ch == '*' || ch == '/') {
return 2;
} else if (ch == '^') {
return 3;
} else {
return 0;
}
}
// Function to convert infix to postfix
void infixToPostfix(char infix[], char postfix[]) {
int i, j = 0;
for (i = 0; i < strlen(infix); i++) {
if (infix[i] == '(') {
push(infix[i]);
} else if (infix[i] == ')') {
while (stack[top] != '(') {
postfix[j++] = pop();
}
pop();
} else if (isOperator(infix[i])) {
while (top != -1 && getPrecedence(infix[i]) <=
getPrecedence(stack[top])) {
postfix[j++] = pop();
}
push(infix[i]);
} else {
postfix[j++] = infix[i];
}
}
while (top != -1) {
postfix[j++] = pop();
}
postfix[j] = '\0';
}
// Function to convert infix to prefix
void infixToPrefix(char infix[], char prefix[]) {
int i, j = 0;
strrev(infix);
for (i = 0; i < strlen(infix); i++) {
if (infix[i] == '(') {
infix[i] = ')';
} else if (infix[i] == ')') {
infix[i] = '(';
}
}
infixToPostfix(infix, prefix);
strrev(prefix);
}

int main() {
char infix[MAX], postfix[MAX], prefix[MAX];
printf("Enter an infix expression: ");
scanf("%s", infix);
infixToPostfix(infix, postfix);
printf("Postfix expression: %s\n", postfix);
infixToPrefix(infix, prefix);
printf("Prefix expression: %s\n", prefix);
return 0;
}
OUTPUT:-

QUESTION 6:-Program to evaluate a postfix and prefix expressions.


CODE:-
#include<stdio.h>
#include<string.h>
int s[100];
int top=-1;
int operators(char ch,int x,int y){
switch(ch){
case '+':
return x+y;
case '-':
return x-y;
case '*':
return x*y;
case '/':
return x/y;
}
}
void push(int n){
s[++top]=n;
}
int pop(){
return s[top--];
}
int postfixevaluation(char st[]){
int x,y,sum=0;
for(int i=0;st[i]!='\0';i++){
sum=0;
if(st[i]!='+' && st[i]!='-' && st[i]!='*' && st[i]!='/'){
push(st[i]-'0');
}
else{
y=pop();
x=pop();
sum=operators(st[i],x,y);
push(sum);
}
}
return s[top--];
}
int prefixevaluation(char st[]){
int x,y,sum=0;
strrev(st);
for(int i=0;st[i]!='\0';i++){
sum=0;
if(st[i]!='+' && st[i]!='-' && st[i]!='*' && st[i]!='/'){
push(st[i]-'0');
}
else{
y=pop();
x=pop();
sum=operators(st[i],y,x);
push(sum);
}
}
return s[top--];
}
int main(){
printf("enter the postfix expression: ");
char po[100];
scanf("%s",po);
printf("the value of the postfix expression is: %d\n",postfixevaluation(po));
char pr[100];
printf("enter the prefix expression: ");
scanf("%s",pr);
printf("the value of the prefix expression is: %d",prefixevaluation(pr));
}
OUTPUT:-

You might also like