DSA Assignment
DSA Assignment
Sol:-
#include <stdio.h>
#define size 10
struct stack
int s[size];
int top;
} st;
void insert()
int ele;
scanf("%d", &ele);
if (st.top == size - 1)
}
else
st.top++;
st.s[st.top] = ele;
void delete()
if (st.top == -1)
else
st.top--;
void display()
if (st.top == -1)
else
temp--;
printf("\n");
void main()
st.top = -1;
int choice = 0;
while (choice != 4)
scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
break;
case 2:
delete ();
break;
case 3:
display();
break;
default:
if (choice != 4)
Output:-
Q2. Infix to Postfix Conversion
Sol:-
#include <stdio.h>
struct Stack
int arr[20];
int top;
} st;
if (st.top == 19)
printf("Stack Overflow!");
else
st.top++;
st.arr[st.top] = ele;
char pop()
if (st.top == -1)
printf("Stack Underflow!");
return '0';
int ele;
ele = st.arr[st.top];
st.top--;
if (ele == 1)
return '+';
else if (ele == 2)
return '-';
else if (ele == 3)
return '*';
else if (ele == 4)
return '/';
else if (ele == 5)
return '^';
return '0';
void toPostfix()
int i, j = 0;
switch (infix[i])
case '+':
if (st.top == -1)
{
push(1);
break;
postfix[j++] = pop();
push(1);
break;
case '-':
if (st.top == -1)
push(2);
break;
postfix[j++] = pop();
push(2);
break;
case '*':
if (st.top == -1)
push(3);
break;
postfix[j++] = pop();
push(3);
break;
case '/':
if (st.top == -1)
push(4);
break;
postfix[j++] = pop();
}
push(4);
break;
case '^':
if (st.top == -1)
push(5);
break;
postfix[j++] = pop();
push(5);
break;
case '(':
push(0);
break;
case ')':
if (st.top == -1)
{
printf("Invalid expression.");
break;
while (st.arr[st.top] != 0)
postfix[j++] = pop();
st.top--;
break;
default:
postfix[j++] = infix[i];
break;
postfix[j++] = pop();
i = 0;
while (postfix[i] != '\0')
printf("%c", postfix[i++]);
void main()
st.top = -1;
scanf("%s", &infix);
toPostfix();
Output:-
Sol:-
#include <stdio.h>
#define size 20
struct stack
int arr[size];
int top;
} st;
int len, i;
len = 0;
i = 0;
len++;
i++;
return len;
{
if (st.top == 19)
printf("Stack Overflow!");
else
st.top++;
st.arr[st.top] = ele;
char pop()
if (st.top == -1)
printf("Stack Underflow!");
return '0';
int ele;
ele = st.arr[st.top];
st.top--;
if (ele == 1)
return '+';
else if (ele == 2)
return '-';
else if (ele == 3)
return '*';
else if (ele == 4)
return '/';
else if (ele == 5)
return '^';
return '0';
void reverseInfix()
int i, j, len;
j = 0;
len = length(infix);
infix_reversed[j++] = infix[i];
void reversePrefix()
{
int i, j, len;
j = 0;
len = length(prefix_temp);
prefix[j++] = prefix_temp[i];
void toPrefix()
int i, j;
j = 0;
reverseInfix();
switch (infix_reversed[i])
case '+':
if (st.top == -1)
push(1);
break;
push(1);
break;
else
prefix_temp[j++] = pop();
push(1);
break;
case '-':
if (st.top == -1)
push(2);
break;
}
else if (st.arr[st.top] <= 2)
push(2);
break;
else
prefix_temp[j++] = pop();
push(2);
break;
case '*':
if (st.top == -1)
push(3);
break;
{
push(3);
break;
else
prefix_temp[j++] = pop();
push(3);
break;
case '/':
if (st.top == -1)
push(4);
break;
push(4);
break;
}
else
prefix_temp[j++] = pop();
push(4);
break;
case '^':
if (st.top == -1)
push(5);
break;
push(5);
break;
else
{
prefix_temp[j++] = pop();
push(5);
break;
case ')':
push(0);
break;
case '(':
if (st.top == -1)
printf("Invalid expression.");
break;
while (st.arr[st.top] != 0)
if (st.top == 0)
{
printf("Invalid expression.");
return;
prefix_temp[j++] = pop();
pop();
break;
default:
prefix_temp[j++] = infix_reversed[i];
break;
prefix_temp[j++] = pop();
reversePrefix();
}
void main()
st.top = -1;
scanf("%s", &infix);
toPrefix();
Output:-
Q4.Postfix Evaluation-
Sol:-
#include<stdio.h>
#include<math.h>
#define size 20
struct stack{
int arr[size];
int top;
}st;
char postfix[size];
if (st.top==size-1){
printf("Stack overflow.");
else{
st.top++;
st.arr[st.top]=data;
int pop(){
if (st.top==-1){
printf("Stack Underflow!");
return '0';
int ele;
ele = st.arr[st.top];
st.top--;
return ele;
}
int evaluate(int data1, int data2, char opr){
if (opr=='+'){
else if (opr=='-'){
else if (opr=='*'){
else if (opr=='/'){
else if (opr=='^'){
return pow(data2,data1);
else{
return 0;
int postfix_evaluation(){
switch (postfix[i]){
case '+':
data1 = pop();
data2 = pop();
push(res);
break;
case '-':
data1 = pop();
data2 = pop();
push(res);
break;
case '*':
data1 = pop();
data2 = pop();
push(res);
break;
case '/':
data1 = pop();
data2 = pop();
res = evaluate(data1, data2, '/');
push(res);
break;
case '^':
data1 = pop();
data2 = pop();
push(res);
break;
default:
push(postfix[i]-48);
break;
return pop();
void main(){
st.top = -1;
scanf("%s", &postfix);
int res;
res = postfix_evaluation();
printf("%d\n", res);
Output:-
Sol:-
#include<stdio.h>
#include<math.h>
#define size 20
struct stack{
int arr[size];
int top;
}st;
int len, i;
len = 0;
i = 0;
while (arr[i]!='\0'){
len++;
i++;
return len;
void reverse(){
int i, j, len;
j = 0;
len = length(prefix);
prefixReversed[j++] = prefix[i];
if (st.top==size-1){
printf("Stack overflow.");
else{
st.top++;
st.arr[st.top]=data;
int pop(){
if (st.top==-1){
printf("Stack Underflow!");
return '0';
int ele;
ele = st.arr[st.top];
st.top--;
return ele;
int prefix_evaluation(){
reverse();
switch (prefixReversed[i]){
case '+':
data1 = pop();
data2 = pop();
push(data2 + data1);
break;
case '-':
data1 = pop();
data2 = pop();
push(data2 - data1);
break;
case '*':
data1 = pop();
data2 = pop();
push(data2 * data1);
break;
case '/':
data1 = pop();
data2 = pop();
push(data2 / data1);
break;
case '^':
data1 = pop();
data2 = pop();
push(pow(data2, data1));
break;
default:
push(prefixReversed[i]-48);
break;
return pop();
void main(){
st.top = -1;
scanf("%s", &prefix);
int res;
res = prefix_evaluation();
printf("%d\n", res);
Output:-
Sol:-
#include <stdio.h>
#include <stdlib.h>
#define size 20
struct Queue
int q[size];
} lq;
void insert()
int data;
if (lq.rear == size - 1)
printf("Queue is FUll");
else
scanf("%d", &data);
if (lq.rear == -1)
{
lq.front++;
lq.q[++lq.rear] = data;
else
lq.q[++lq.rear] = data;
void delete()
if (lq.rear == -1)
printf("Queue is Empty");
lq.rear = -1;
lq.front = -1;
else
lq.front++;
}
void display()
int temp;
if (lq.rear == -1)
printf("Queue is Empty");
else
temp = lq.front;
temp++;
void main()
while (choice != 4)
scanf("%d", &choice);
switch (choice)
case 1:
insert();
break;
case 2:
delete ();
break;
case 3:
display();
break;
default:
printf("Wrong choice!");
Output:-
Q7. Circular Queue Implementation-
Sol:-
#include <stdio.h>
#include <stdlib.h>
#define size 20
struct Queue
int q[size];
void insert()
int data;
printf("Queue is FUll");
else
scanf("%d", &data);
if (cq.front == -1)
cq.q[cq.rear] = data;
else
cq.q[cq.rear] = data;
}
void delete()
if (cq.rear == -1)
printf("Queue is Empty");
cq.rear = -1;
cq.front = -1;
else
void display()
int temp;
if (cq.rear == -1)
{
printf("Queue is Empty");
else
temp = cq.front;
if (cq.front == cq.rear)
else
void main()
{
int choice;
while (choice != 4)
scanf("%d", &choice);
switch (choice)
case 1:
insert();
break;
case 2:
delete ();
break;
case 3:
display();
break;
default:
printf("Wrong choice!");
}
}
Output:-
Sol-
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define size 20
struct Queue
{
int q[size];
} pq;
int isEmpty()
if (pq.rear == -1)
return 1;
else
return 0;
int isFull()
if (pq.rear == size - 1)
return 1;
else
{
return 0;
void priority()
int i, j, k, temp;
temp = pq.q[j];
pq.q[j] = pq.q[i];
pq.q[k + 1] = temp;
}
}
void main()
while (choice != 4)
scanf("%d", &choice);
switch (choice)
case 1:
if (isFull() == 1)
printf("Queue is FUll");
else
scanf("%d", &item);
if (pq.rear == -1)
{
pq.front++;
pq.rear++;
pq.q[pq.rear] = item;
else
pq.q[++pq.rear] = item;
priority();
break;
case 2:
if (isEmpty() == 1)
printf("Queue is Empty");
else
pq.rear = -1;
pq.front = -1;
}
else
pq.front++;
break;
case 3:
if (isEmpty() == 1)
printf("Queue is Empty");
else
priority();
temp = pq.front;
temp++;
break;
default:
printf("Wrong choice!");
Sol:-
#include <stdio.h>
#include <stdlib.h>
struct node
int data;
};
void display()
if (head == NULL)
else
temp = head;
temp = temp->next;
printf("\n");
}
void create()
int item;
scanf("%d", &item);
if (head == NULL)
nw->data = item;
nw->next = NULL;
else
nw->data = item;
nw->next = NULL;
tail->next = nw;
tail = nw;
}
void insert_at_begin()
int item;
scanf("%d", &item);
if (head == NULL)
nw->data = item;
nw->next = NULL;
else
nw->data = item;
nw->next = head;
head = nw;
void insert_at_mid()
scanf("%d", &pos);
scanf("%d", &item);
temp1 = temp1->next;
temp2 = temp1->next;
nw->data = item;
nw->next = temp2;
temp1->next = nw;
void insert_at_end()
int item;
scanf("%d", &item);
nw->data = item;
nw->next = NULL;
tail->next = nw;
tail = nw;
void delete_from_start()
if (head == NULL)
else
temp = head;
head = head->next;
temp->next = NULL;
free(temp);
void delete_from_mid()
int pos;
scanf("%d", &pos);
temp1 = temp1->next;
temp2 = temp1->next;
temp1->next = temp2->next;
temp2->next = NULL;
free(temp2);
void delete_from_end()
if (head == tail)
temp = head;
temp->next = NULL;
free(temp);
else
temp = head;
{
temp = temp->next;
temp->next = NULL;
free(tail);
tail = temp;
void main()
int choice;
while (choice != 9)
scanf("%d", &choice);
switch (choice)
case 1:
create();
break;
case 2:
display();
break;
case 3:
insert_at_begin();
break;
case 4:
insert_at_mid();
break;
case 5:
insert_at_end();
break;
case 6:
delete_from_start();
break;
case 7:
delete_from_mid();
break;
case 8:
delete_from_end();
break;
default:
Output:-
Sol:-
#include <stdio.h>
#include <stdlib.h>
struct node
int data;
};
void create()
int data, n, i;
scanf("%d", &n);
scanf("%d", &data);
if (head == NULL)
nw->data = data;
nw->prev = NULL;
nw->next = NULL;
head = tail = nw;
else
nw->data = data;
nw->prev = tail;
nw->next = NULL;
tail->next = nw;
tail = nw;
void display()
if (head == NULL)
else
temp = head;
printf("Elements are: ");
temp = temp->next;
void main()
int choice;
choice = 0;
while (choice != 3)
scanf("%d", &choice);
if (choice == 1)
create();
}
else if (choice == 2)
display();
Output:-