DSA Lab Program Solutions
DSA Lab Program Solutions
Infix to Postfix
#include <stdlib.h>
#include <ctype.h>
#define SIZE 20
struct stack
{
int top;
char data[SIZE];
};
typedef struct stack STACK;
void push(STACK *s,char item)
{
s->data[++(s->top)]=item;
}
}
}
}
while(s->top!=-1)
postfix[j++]=pop(s);
postfix[j]='\0';
printf("\n The postfix expression is %s\n",postfix);
}
int main()
{
STACK s;
s.top=-1;
char infix[SIZE];
printf("\n Read Infix expression\n");
scanf("%s",infix);
infixtopostfix(&s,infix);
return 0;
}
2. Evaluation of Prefix
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <string.h>
#define SIZE 20
struct stack
{
int top;
float data[SIZE];
};
typedef struct stack STACK;
void push(STACK *s,float item)
{
s->data[++(s->top)]=item;
}
int main()
{
char prefix[SIZE];
STACK s;
float ans;
s.top=-1;
printf("\n Read prefix expr\n");
scanf("%s",prefix);
ans=eval(&s,prefix);
printf("\n The final answer is %f\n",ans);
return 0;
}
3. Message Queueing System
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 5
struct queue
{
int front,rear;
char data[SIZE][20];
};
typedef struct queue QUEUE;
void display(QUEUE q)
{
int i;
if(q.front==-1)
printf("\n Queue Empty");
else
{
printf("\n Queue content are\n");
for(i=q.front;i!=q.rear;i=(i+1)%SIZE)
printf("%s\n",q.data[i]);
printf("%s\n",q.data[i]);
}
int main()
{
int ch;
char *del;
char item[20];
QUEUE q;
q.front=-1;
q.rear=-1;
for(;;)
{
printf("\n1. Send\n2. Receive\n3. Display\n4. Exit");
printf("\nRead Choice :");
scanf("%d",&ch);
getchar();
switch(ch)
{
case 1:printf("\n Read msg to be send :");
gets(item);
send(&q,item);
break;
case 2:del=receive(&q);
if(del!=NULL)
printf("\n Element deleted is %s\n",del);
break;
case 3:display(q);
break;
default:exit(0);
}
}
return 0;
}
4. Multiplication of Polynomials using
Singly Linked List
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5
int count;
struct node
{
int co,po;
struct node *addr;
};
typedef struct node *NODE;
NODE insertend(NODE start,int co,int po)
{
NODE temp,cur;
temp=(NODE)malloc(sizeof(struct node));
temp->co=co;
temp->po=po;
temp->addr=NULL;
if(start==NULL)
return temp;
cur=start;
while(cur->addr!=NULL)
cur=cur->addr;
cur->addr=temp;
return start;
}
}
5. Queue of Integers using Circular List
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5
int count;
struct node
{
int data;
struct node *addr;
};
typedef struct node *NODE;
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,i,a[10],ch;
for(;;)
{
printf("\n 1. Create Heap");
printf("\n 2. Extractmax");
printf("\n 3. Exit");
printf("\n Read Choice :");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\n Read no of elements :");
scanf("%d",&n);
printf("\n Read Elements\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
heapify(a,n);
printf("\n Elements after heap\n");
for(i=1;i<=n;i++)
printf("%d\t",a[i]);
break;
case 2:if(n>=1)
{
printf("\n Element deleted is %d\n",a[1]);
a[1]=a[n];
n=n-1;
heapify(a,n);
if(n!=0)
{
printf("\n Elements after reconstructing
heap\n");
for(i=1;i<=n;i++)
printf("%d\t",a[i]);
}
}
else
printf("\n No element to delete");
break;
default:exit(0);
}
}
return 0;
}
8. Expression Tree
#include <stdio.h>
#include <stdlib.h>
#include<ctype.h>
struct node
{
char data;
struct node *left;
struct node *right;
};
typedef struct node *NODE;
struct stack
{
int top;
NODE data[10];
};
typedef struct stack STACK;
}
while(OS.top!=-1)
{
t=pop(&OS);
t->right=pop(&TS);
t->left=pop(&TS);
push(&TS,t);
}
return pop(&TS);
}
int main()
{
char infix[10];
NODE root=NULL;
printf("\n Read the infix expression :");
scanf("%s",infix);
root=create_expr_tree(root,infix);
printf("\n The preorder traversal is\n");
preorder(root);
printf("\n The inorder traversal is\n");
inorder(root);
printf("\n The postorder traversal is\n");
postorder(root);
return 0;
}
9. Binary Tree
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
typedef struct node *NODE;
}
}
}
while(s->top!=-1)
prefix[j++]=pop(s);
prefix[j]='\0';
str1=strrev(prefix);
printf("\n The prefix expression is %s\n",str1);
}
int main()
{
STACK s;
s.top=-1;
char infix[SIZE];
printf("\n Read Infix expression\n");
scanf("%s",infix);
infixtoprefix(&s,infix);
return 0;
}
2. Evaluation of Postfix
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#define SIZE 20
struct stack
{
int top;
float data[SIZE];
};
typedef struct stack STACK;
void push(STACK *s,float item)
{
s->data[++(s->top)]=item;
}
int main()
{
char postfix[SIZE];
STACK s;
float ans;
s.top=-1;
printf("\n Read postfix expr\n");
scanf("%s",postfix);
ans=eval(&s,postfix);
printf("\n The final answer is %f\n",ans);
return 0;
}
3. Circular Queue of Integers
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5
struct queue
{
int front,rear;
int data[SIZE];
};
typedef struct queue QUEUE;
void display(QUEUE q)
{
int i;
if(q.front==-1)
printf("\n Queue Empty");
else
{
printf("\n Queue content are\n");
for(i=q.front;i!=q.rear;i=(i+1)%SIZE)
printf("%d\t",q.data[i]);
printf("%d\t",q.data[i]);
}
int main()
{
int item,ch,del;
QUEUE q;
q.front=-1;
q.rear=-1;
for(;;)
{
printf("\n1. Enqueue\n2. Dequeue\n3. Display\n4. Exit");
printf("\nRead Choice :");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\n Read element to be inserted :");
scanf("%d",&item);
enqueue(&q,item);
break;
case 2:del=dequeue(&q);
if(del!=-1)
printf("\n Element deleted is %d\n",del);
break;
case 3:display(q);
break;
default:exit(0);
}
}
return 0;
}
4. Stack using Singly Linked List
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5
int count;
struct node
{
int data;
struct node *addr;
};
typedef struct node *NODE;
NODE push(NODE start,int item)
{
NODE temp;
if(count>=SIZE)
{
printf("\n Stack Overflow");
return start;
}
else
{
temp=(NODE)malloc(sizeof(struct node));
count=count+1;
temp->data=item;
temp->addr=NULL;
if(start==NULL)
return temp;
temp->addr=start;
return temp;
}
}
NODE pop(NODE start)
{
NODE temp;
if(start==NULL)
{
printf("\n stack Underflow");
return start;
}
else
{
temp=start;
start=start->addr;
printf("\n Element popped is %d\n",temp->data);
count=count-1;
return start;
}
}
struct node
{
int row,col,data;
struct node *next;
struct node *prev;
};
typedef struct node *NODE;
int main()
{
NODE start = NULL;
int i,j,m,n,item;
printf("\n Read the order of the matrix\n");
scanf("%d%d",&m,&n);
printf("\n Read the matrix\n");
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&item);
if(item!=0)
start=insertend(start,i,j,item);
}
}
display(start);
displaymatrix(start,m,n);
return 0;
}
6. Addition of 2 Long integers using
Header node
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *addr;
};
typedef struct node *NODE;