Dsa Lab 1,2,3 Tasks
Dsa Lab 1,2,3 Tasks
TRISHIT GUPTA
20BIT0374
TASK 1
CODE
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define SIZE 15
void pop();
void display();
char stack[SIZE][20];
int top=-1;
int main()
char value[20];
int choice;
if(i+1==1)
scanf("%s",value);
push(value);
else if(i+1==2)
{
scanf("%s",value);
push(value);
else if(i+1==3)
scanf("%s",value);
push(value);
else
scanf("%s",value);
push(value);
while(1)
printf("\n\n***MENU***\n");
printf("2.display \n3.exit");
scanf("%d",&choice);
switch(choice)
case 2: display();
break;
case 3: exit(0);
return 0;
if(top==(SIZE-1))
else
top++;
strcpy(stack[top],data);
void pop()
if(top==-1)
printf("STACK UNDERFLOW\n");
else
{
top--;
void display()
while(top>=10)
pop();
if(top==-1)
printf("stack empty\n");
else
for(int i=top;i>=0;i--)
printf("%s \n",stack[i]);
}
OUTPUT
TASK 2
Web surfing
#include<iostream>
#include<string.h>
#include<cstdlib>
#define MAX 30
struct front
int a[MAX];
int top;
}f;
struct back
int b[MAX];
int top;
}bc;
if(f.top==MAX-1)
else
f.top++;
f.a[f.top]=item;
{
if(bc.top==MAX-1)
else
bc.top++;
bc.b[bc.top]=item;
int popfr()
if(f.top==1)
cout<<"\nCannot go back\n";
int k=f.a[f.top];
f.top--;
return k;
int popbc()
if(bc.top==-1)
cout<<"\nCannot go back\n";
int k=bc.b[bc.top];
bc.top--;
return k;
void displayfr()
int i;
for(i=1;i<f.top;i++)
cout<<f.a[i]<<"\t";
void displaybc()
int i;
for(i=1;i<bc.top;i++)
cout<<bc.b[i]<<"\t";
void initialise()
f.top=0;
bc.top=-1;
bool (isempty())
if(bc.top==-1)
return 1;
else
return 0;
int main()
initialise();
int c=1,choice,z,p;
pushfr(c++);
do
cin>>choice;
switch(choice)
case 1:
if(isempty())
pushfr(c++);
else
p=popbc();
pushfr(p);
break;
case 2:
z=popfr();
pushbc(z);
if(z!=1)
break;
}
else
break;
case 3:
exit(0);
}while(choice!=4);
OUTPUT
TASK 3
INFIX POSTFIX
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define N 1000
char stack[N];
int top=-1;
float stack2[N];
int top2=-1;
top++;
stack[top]=a;
char pop(){
return stack[top--];
if(p=='(' || p==')')
return 4;
else if(p=='^')
return 3;
else if(p=='+'||p=='-')
return 1;
else if(isalnum(p))
return 0;
top2++;
stack2[top2]=p;
float pop2(){
char a;
a=stack2[top2];
top2--;
return a;
int main(){
while(1){
printf("5. Exit\n\n");
scanf("%d",&choice);
switch (choice){
case 1:
scanf("%s",infix);
char ch=infix[i];
if(ch=='(')
push(ch);
else if(isalnum((ch)))
postfix[k++]=ch;
else if(ch==')'){
while(stack[top]!='('){
postfix[k++]=pop();
char elem=pop();
else{
postfix[k++]=pop();
push(ch);
while(stack[top]!='\0'){
postfix[k++]=pop();
printf("%s",postfix);
break;
case 2:
printf("Enter Infix Expression: ");
scanf("%s",infix);
strrev(infix);
char ch=infix[i];
if(ch==')')
push(ch);
else if(isalnum((ch)))
prefix[k++]=ch;
else if(ch=='('){
while(stack[top]!=')'){
prefix[k++]=pop();
char elem=pop();
else{
prefix[k++]=pop();
push(ch);
while(stack[top]!='\0'){
prefix[k++]=pop();
strrev(prefix);
printf("%s",prefix);
break;
case 3:
char ch=postfix[i];
if (isdigit(ch))
push2(ch-'0');
else if (isalpha(ch)){
printf("%c = ",ch);
float temp;
scanf("%f",&temp);
push2(temp);
else{
char a=pop2();
float c= (float)a;
char b=pop2();
float d=(float)b;
if(ch=='+')
push2(d+c);
if(ch=='-')
push2(d-c);
if(ch=='*')
push2(d*c);
if(ch=='/')
push2(d/c);
if(ch=='^')
push2(powf(d,c));
printf("Result = %.2f",stack2[top2]);
break;
case 4:
printf("Enter Prefix Expression: ");
scanf("%s",prefix);
char ch=prefix[i];
if (isdigit(ch))
push2(ch-'0');
else if (isalpha(ch)){
printf("%c = ",ch);
float temp;
scanf("%f",&temp);
push2(temp);
else{
char a=pop2();
float c= (float)a;
char b=pop2();
float d=(float)b;
if(ch=='+')
push2(c+d);
if(ch=='-')
push2(c-d);
if(ch=='*')
push2(c*d);
if(ch=='/')
push2(c/d);
if(ch=='^')
push2(powf(c,d));
printf("Result = %.2f",stack2[top2]);
break;
case 5:
exit(1);
default:
printf("Invalid choice!!");
break;
return 0;
}
OUTPUT