0% found this document useful (0 votes)
101 views19 pages

Dsa Lab 1,2,3 Tasks

The document contains code for three data structure programs - a stack implementation to store student registration numbers, a queue implementation to model web page navigation, and an infix to postfix notation converter. The stack program allows the user to push and display registration numbers. The queue program uses two stacks to model a browser's forward and back buttons. The infix to postfix converter uses stacks to evaluate infix expressions and convert them to postfix notation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
101 views19 pages

Dsa Lab 1,2,3 Tasks

The document contains code for three data structure programs - a stack implementation to store student registration numbers, a queue implementation to model web page navigation, and an infix to postfix notation converter. The stack program allows the user to push and display registration numbers. The queue program uses two stacks to model a browser's forward and back buttons. The infix to postfix converter uses stacks to evaluate infix expressions and convert them to postfix notation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

DSA ASSIGNMENT

TRISHIT GUPTA
20BIT0374
TASK 1
CODE
#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#define SIZE 15

void push(char data[20]);

void pop();

void display();

char stack[SIZE][20];

int top=-1;

int main()

char value[20];

int choice;

printf("enter 15 registration nos \n");

for (int i=0;i<SIZE;i++)

if(i+1==1)

printf("enter %dst value ",i+1);

scanf("%s",value);

push(value);

else if(i+1==2)
{

printf("enter %dnd value ",i+1);

scanf("%s",value);

push(value);

else if(i+1==3)

printf("enter %drd value ",i+1);

scanf("%s",value);

push(value);

else

printf("enter %dth value ",i+1);

scanf("%s",value);

push(value);

while(1)

printf("\n\n***MENU***\n");

printf("2.display \n3.exit");

printf("\nEnter your choice");

scanf("%d",&choice);

switch(choice)

case 2: display();

break;
case 3: exit(0);

default: printf("\n wrong!! try again");

return 0;

void push(char data[20])

if(top==(SIZE-1))

printf("STACK OVERFLOW \n");

else

top++;

strcpy(stack[top],data);

void pop()

if(top==-1)

printf("STACK UNDERFLOW\n");

else

{
top--;

void display()

printf("\n the LAST SUBMITTED REGISTRATION NUMBER is %s \n",stack[top]);

while(top>=10)

pop();

if(top==-1)

printf("stack empty\n");

else

printf("\n\n**10 RECENTLY SUBMITTED NUMBERS **: \n");

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

using namespace std;

struct front

int a[MAX];

int top;

}f;

struct back

int b[MAX];

int top;

}bc;

void pushfr(int item)

if(f.top==MAX-1)

cout<<"\n Cannot move any further\n";

else

f.top++;

f.a[f.top]=item;

void pushbc(int item)

{
if(bc.top==MAX-1)

cout<<"\n Cannot move any further\n";

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++);

cout<<"You are currently on first page."<<endl;

do

cout<<"\n\nEnter your choice:\n1.To go to next page.\n2.To go to previous page.\n3.Exit."<<endl;

cin>>choice;
switch(choice)

case 1:

cout<<"\nYou were on page\t"<<f.a[f.top];

if(isempty())

pushfr(c++);

cout<<endl<<"\nNow you are on page\t"<<f.a[f.top];

else

p=popbc();

pushfr(p);

cout<<endl<<"\nNow you are on page on\t"<<f.a[f.top];

break;

case 2:

z=popfr();

pushbc(z);

if(z!=1)

cout<<"\nYou were on page\t"<<z<<endl;

cout<<endl<<"Now you are on page\t"<<f.a[f.top];

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;

void push(char a){

top++;

stack[top]=a;

char pop(){

return stack[top--];

int precedence(char p){

if(p=='(' || p==')')

return 4;

else if(p=='^')

return 3;

else if(p=='*' || p=='/')


return 2;

else if(p=='+'||p=='-')

return 1;

else if(isalnum(p))

return 0;

void push2(float p){

top2++;

stack2[top2]=p;

float pop2(){

char a;

a=stack2[top2];

top2--;

return a;

int main(){

while(1){

char infix[100]= "\0" , postfix[100]="\0" , prefix[100]="\0";

int k=0 , choice;

printf("\n\n*** Expression Converter and Evaluator ***\n\n");

printf("1. Infix -> Postfix\n");

printf("2. Infix -> Prefix\n");

printf("3. Postfix Evaluation\n");

printf("4. Prefix Evaluation\n");

printf("5. Exit\n\n");

printf("Enter choice -> ");

scanf("%d",&choice);
switch (choice){

case 1:

printf("Enter Infix Expression: ");

scanf("%s",infix);

for (int i = 0; i < strlen(infix); i++){

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{

while(precedence(stack[top])>=precedence(ch) && stack[top]!='('){

postfix[k++]=pop();

push(ch);

while(stack[top]!='\0'){

postfix[k++]=pop();

printf("Postfix Expression: ");

printf("%s",postfix);

break;

case 2:
printf("Enter Infix Expression: ");

scanf("%s",infix);

strrev(infix);

for (int i = 0; i < strlen(infix); i++){

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{

while(precedence(stack[top])>precedence(ch) && stack[top]!=')'){

prefix[k++]=pop();

push(ch);

while(stack[top]!='\0'){

prefix[k++]=pop();

strrev(prefix);

printf("Prefix Expression: ");

printf("%s",prefix);

break;

case 3:

printf("Enter Postfix Expression: ");


scanf("%s",postfix);

for (int i = 0; i < strlen(postfix); i++){

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);

for (int i = strlen(prefix)-1; i>=0 ; i--){

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

You might also like