0% found this document useful (0 votes)
6 views

DSA_Lab_Manual[2]MCA

The document contains multiple C programs demonstrating various data structures and algorithms, including infix to postfix conversion, postfix expression evaluation, queue simulation, singly linked list operations, searching techniques (linear and binary), and sorting algorithms (bubble and selection sort). Each section provides code snippets along with explanations of the operations performed. The programs are designed for educational purposes in a computer science curriculum.

Uploaded by

annapoornaa429
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

DSA_Lab_Manual[2]MCA

The document contains multiple C programs demonstrating various data structures and algorithms, including infix to postfix conversion, postfix expression evaluation, queue simulation, singly linked list operations, searching techniques (linear and binary), and sorting algorithms (bubble and selection sort). Each section provides code snippets along with explanations of the operations performed. The programs are designed for educational purposes in a computer science curriculum.

Uploaded by

annapoornaa429
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

DSA 22MCAL16

1) Implement a program in c for converting an infix expression to postfix


expression.
#include<stdio.h>
#include<string.h>
int inputpre(char sym)
{
switch(sym)
{
case'+':
case'-':return 1;
case'*':
case'/':return 3;
case'^':
case'$':return 6;
case'(':return 9;
case')':return 0;
default:return 7;
}
}
int stackpre(char sym)
{
switch(sym)
{
case'+':
case'-':return 2;
case'*':
case'/':return 4;

VTU,Mysuru Dept of CS&E(MCA)


DSA 22MCAL16

case'^':
case'$':return 5;
case'(':return 0;
case'#':return -1;
default:return 8;
}
}
void push(char item,int*top,char s[])
{
s[++(*top)]=item;
}
char pop(int*top,char s[])
{
return s[(*top)--];
}
void infix_to_postfix(char ifix[],char pfix[])
{
int top=-1,i,j=0;
char s[30],sym;
push('#',&top,s);
for(i=0;i<strlen(ifix);i++)
{
sym=ifix[i];
while(stackpre(s[top])>inputpre(sym))
pfix[j++]=pop(&top,s);
if(stackpre(s[top])!=inputpre(sym))

VTU,Mysuru Dept of CS&E(MCA)


DSA 22MCAL16

push(sym,&top,s);
else
pop(&top,s);
}
while(s[top]!='#')
pfix[j++]=pop(&top,s);
pfix[j]='\0';
}
int main()
{
char ifix[20],pfix[20];
clrscr();
printf("enter the valid infix expression\n");
scanf("%s",&ifix);
infix_to_postfix(ifix,pfix);
printf("the postfix expression is =%s",pfix);
getch();
return 0;
}

VTU,Mysuru Dept of CS&E(MCA)


DSA 22MCAL16

2. Design, develop, and execute a program in C to evaluate a valid postfix


expression using stack. Assume that the postfix expression is read as a single
line consisting of non-negative single digit operands and binary arithmetic
operators. The arithmetic operators are + (add), - (subtract), * (multiply) and /
(divide).

#include<stdio.h>
#include<string.h>
#include<math.h>
#define MAX_SIZE 30
int s[MAX_SIZE];
int top=-1;
int isdig(char);
int pop();
void push(int);
int op(int,int,char);
int main()
{
char symbol,postfix[30];
int a,b,res,i;
printf("enter the postfix exp \n");
scanf("%s",postfix);
for(i=0;i<strlen(postfix);i++)
{
symbol = postfix[i];
if(isdig(symbol))
push(symbol-'0');

VTU,Mysuru Dept of CS&E(MCA)


DSA 22MCAL16

else
{
a=pop();
b=pop();
res=op(b,a,symbol);
push(res);
}
}
printf("the expression after evaluation is = ");
printf("%d\n",pop());
getch();
return 0;
}
int pop()
{
if(top!=-1)
return s[top--];
else
{
printf("stack underflow");
}
}
void push(int item)
{
if(top!=MAX_SIZE-1)
s[++top]=item;

VTU,Mysuru Dept of CS&E(MCA)


DSA 22MCAL16

else
printf("stack overflow \n");
}
int op(int op1,int op2,char symbol)
{
switch(symbol)
{
case '+': return op1+op2;
case '-': return op1-op2;
case '*': return op1*op2;
case '/': return op1/op2;
}
}
int isdig(char symbol)
{
return(symbol>='0'&&symbol<='9');
}

VTU,Mysuru Dept of CS&E(MCA)


DSA 22MCAL16

3).Design, develop, and execute a program in C to simulate the working of a


queue of integers using an array. Provide the following operations: a. Insert b.
Delete c. Display.

#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 5
int i;
int queue[MAX_SIZE];
int front = -1;
int rear = -1;
void isEmpty() {
if (rear == -1 && front == -1) {
printf("Queue is empty\n");
} else {
printf("Queue is not empty\n");
}
}
void isFull() {
if (rear == MAX_SIZE - 1) {
printf("Queue is full\n");
} else {
printf("Queue is not full\n");
}
}
void peek() {
if (front == -1 && rear == -1)

VTU,Mysuru Dept of CS&E(MCA)


DSA 22MCAL16

{
printf("There is no element inside the queue to display\n");
}
else
{
printf("The element at the front node is: %d\n", queue[front]);
}
}
void Enqueue() {
int item;
if (rear == MAX_SIZE - 1)
{
printf("Overflow Error\n");
}
else
{
if (front == -1) {
front = 0;
}
printf("Enter the element for insertion: ");
scanf("%d", &item);
rear++;
queue[rear] = item;
}
}
void Dequeue() {

VTU,Mysuru Dept of CS&E(MCA)


DSA 22MCAL16

if (front == -1 && rear == -1)


{
printf("Queue underflow\n");
} else if (front == rear) {
printf("The deleted element from the queue is %d\n", queue[front]);
front = rear = -1;
} else {
printf("The deleted element from the queue is %d\n", queue[front]);
front++;
}
}
void display() {
if (front == -1) {
printf("Queue is empty\n");
} else {
printf("Queue elements are:\n");
for (i = front; i <= rear; i++)
printf("%d\n", queue[i]);
}
}
int main() {
int ch;
clrscr();
while (1) {
printf("\n1. isEmpty Operation\n");
printf("2. isFull Operation\n");

VTU,Mysuru Dept of CS&E(MCA)


DSA 22MCAL16

printf("3. Peek Operations\n");


printf("4. Enqueue Operation\n");
printf("5. Dequeue Operation\n");
printf("6. Display the Queue\n");
printf("7. Exit\n");
printf("Enter your choice of operations: ");
scanf("%d", &ch);
switch (ch) {
case 1:
isEmpty();
break;
case 2:
isFull();
break;
case 3:
peek();
break;
case 4:
Enqueue();
break;
case 5:
Dequeue();
break;
case 6:
display();
break;

VTU,Mysuru Dept of CS&E(MCA)


DSA 22MCAL16

case 7:
exit(0);
default:
printf("Incorrect choice\n");
}
}
return 0;
}

VTU,Mysuru Dept of CS&E(MCA)


DSA 22MCAL16

4. Write a c program to simulate the working of a singly linked list providing


the following operation: a) display & insert b) delete from beginning / end c)
delete a given element?

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
};
struct node *head = NULL;
void insert();
void begin_delete();
void last_delete();
void delete_pos();
void display();
int main()
{
int choice;
do
{
printf("Choose one option from the following list ...\n");
printf("\n1.Insert \n2.Delete from Beginning\n3.Delete from
last\n4.Delete node at specified location\n5.Display\n6.Exit\n");
printf("\nEnter your choice?\n");

VTU,Mysuru Dept of CS&E(MCA)


DSA 22MCAL16

scanf("%d", &choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
begin_delete();
break;
case 3:
last_delete();
break;
case 4:
delete_pos();
break;
case 5:
display();
break;
case 6:
exit(0);
break;
default:
printf("Please enter valid choice..\n");
}
} while(choice != 6);
return 0;

VTU,Mysuru Dept of CS&E(MCA)


DSA 22MCAL16

}
void insert()
{
int ch;
struct node *newnode, *temp;
do
{
newnode = (struct node *) malloc(sizeof(struct node));
if (newnode == NULL)
{
printf("Memory allocation failed\n");
return;
}
printf("Enter Data: ");
scanf("%d", &newnode->data);
newnode->link = NULL;
if (head == NULL)
{
head = newnode;
}
else
{
temp = head;
while (temp->link != NULL)
{
temp = temp->link;

VTU,Mysuru Dept of CS&E(MCA)


DSA 22MCAL16

}
temp->link = newnode;
}
printf("Do you want to insert again?(0,1):");
scanf("%d", &ch);
} while(ch);
}
void begin_delete()
{
if (head == NULL)
{
printf("\nList is empty\n");
}
else
{
struct node *temp = head;
head = head->link;
free(temp);
printf("\nNode deleted from the beginning\n");
}
}
void last_delete()
{
if (head == NULL)
{
printf("\nList is empty\n");

VTU,Mysuru Dept of CS&E(MCA)


DSA 22MCAL16

}
else if (head->link == NULL)
{
free(head);
head = NULL;
printf("\nNode deleted at Last\n");
}
else
{
struct node *temp = head;
while (temp->link->link != NULL)
{
temp = temp->link;
}
free(temp->link);
temp->link = NULL;
printf("\nNode deleted at Last\n");
}
}
void delete_pos()
{
int pos, i = 1;
struct node *temp = head;
struct node *nextnode = temp->link;
if (head == NULL)
{

VTU,Mysuru Dept of CS&E(MCA)


DSA 22MCAL16

printf("\nList is empty\n");
return;
}
printf("\nEnter the position to delete: ");
scanf("%d", &pos);
if (pos == 1)
{
begin_delete();
return;
}
while (i < pos - 1 && temp != NULL)
{
temp = temp->link;
i++;
}
if (temp == NULL || temp->link == NULL)
{
printf("\nPosition out of range\n");
return;
}
temp->link = nextnode->link;
free(nextnode);
printf("\nNode deleted at position %d\n", pos);
}
void display()
{

VTU,Mysuru Dept of CS&E(MCA)


DSA 22MCAL16

if (head == NULL)
{
printf("\nList is empty\n");
}
else
{
struct node *temp = head;
printf("\n elements in the linked list:\n");
while (temp != NULL)
{
printf("%d\t", temp->data);
temp = temp->link;
}
printf("\n");
}
}

VTU,Mysuru Dept of CS&E(MCA)


DSA 22MCAL16

5)Write a C program to Implement the following searching techniques

a. Linear Search b. Binary Search.


#include<stdio.h>
void linear();
void binary();
int i,n,key,arr[6],ch;
int main()
{
clrscr();
printf("enter the number of elements \n");
scanf("%d",&n);
printf("enter the elements to be inserted one by one \n");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
printf("enter the key element to search \n");
scanf("%d",&key);
printf("enter your choice \n");
printf("1.linear search\n2.binary search\n3.exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:linear();
break;
case 2:binary();
break;
case 3:exit(0);

VTU,Mysuru Dept of CS&E(MCA)


DSA 22MCAL16

default:printf("invalid input");
}
getch();
return 0;
}
void linear()
{
for(i=0;i<n;i++)
{
if(arr[i]==key)
{
printf("the search element %d is found at the location %d",key,i);
return;
}
}
printf("the search element %d not found",key);
}
void binary()
{
int mid,f=0,l=n-1,j,temp;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;

VTU,Mysuru Dept of CS&E(MCA)


DSA 22MCAL16

}
}
}
printf("the sorted array is = ");
for(i=0;i<n;i++){
printf("%d \t",arr[i]);
}
while(f<=l)
{
mid=(f+l)/2;
if(arr[mid]==key)
{
printf("the key %d is found in the position %d",key,mid);
return;
}
else if(arr[mid]<key)
{
f=mid+1;
}
else
{
l=mid-1;
}
}
printf("\nthe element not found");
}

VTU,Mysuru Dept of CS&E(MCA)


DSA 22MCAL16

6)Write a C program to implement the following sorting algorithms using user


defined functions: a. Bubble sort (Ascending order) b. Selection sort
(Descending order).

#include<stdio.h>
#include<conio.h>
void selection(int a[],int n)
{
int i,j,temp;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]<a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
}
void bubble(int a[],int n)
{
int i,j,temp;
for(i=0;i<n;i++)
{

VTU,Mysuru Dept of CS&E(MCA)


DSA 22MCAL16

for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
void main()
{
int a[20],n,i,opt;
clrscr();
printf("enter the size of the array:");
scanf("%d",&n);
for(;;)
{
printf("\n***sort**\n");
printf("\n1.selection sort(descending oredr)\n 2.bubble
sort(ascending oredr)\n 3.exit");
printf("\n enter your choice:");
scanf("%d",&opt);
switch(opt)
{
case 1:

VTU,Mysuru Dept of CS&E(MCA)


DSA 22MCAL16

printf("\n enter array elements:\n");


for(i=0;i<n;i++)
scanf("%d",&a[i]);
selection(a,n);
printf("\nelements in descending oredr:\n");
for(i=0;i<n;i++)
printf("\n%d\t",a[i]);
break;
case 2:
printf("\nenter the array elements:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
bubble(a,n);
printf("\n elements in descending order:\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
break;
case 3: exit(0);
default: printf(“your choice is incorrect ….:”);
}
}
}

VTU,Mysuru Dept of CS&E(MCA)


DSA 22MCAL16

7)Find Minimum Cost Spanning Tree of a given undirected graph using


Kruskal's algorithm ( C programming)

#include <stdio.h>
#include< conio.h>
void main() {
int n, v, u, cost[10][10], parent[10], i, j;
int count = 0, mincost = 0, min, a, b;
printf("Enter number of vertices: ");
scanf("%d", &n);
printf("Enter cost matrix:\n");
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
scanf("%d", &cost[i][j]);
if (cost[i][j] == 0)
cost[i][j] = 999; // Assign a large value for representing infinity
}
}
for (i = 1; i <= n; i++) {
parent[i] = 0; // Initialize parent array
}
while (count < n - 1) {
min = 999;
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
if (cost[i][j] < min) {
min = cost[i][j];

VTU,Mysuru Dept of CS&E(MCA)


DSA 22MCAL16

a = u = i;
b = v = j;
}
}
}
while (parent[u])
u = parent[u];
while (parent[v])
v = parent[v];
if (u != v) {
count++;
printf("\nEdge (%d,%d) = %d", a, b, min);
mincost += min;
parent[v] = u;
}
cost[a][b] = cost[b][a] = 999;
}
printf("\nMinimum cost = %d", mincost);
return 0;
}

VTU,Mysuru Dept of CS&E(MCA)


DSA 22MCAL16

8)From a given vertex in a weighted connected graph, find shortest paths to


other vertices Using Dijkstra's algorithm (C programming)

#include<stdio.h>
#include<conio.h>
void dijkstra(int n,int v,int cost[10][10],int dist[10])
{
int count,u,i,w,visited[10],min;
for(i=0;i<n;i++)
{
visited[i]=0;
dist[i]=cost[v][i];
}
visited[v]=1;
dist[v]=1;
count=2;
while(count<=n)
{
min=999;
for(w=0;w<n;w++)
if((dist[w]<min)&&(visited[w]!=1))
{
min=dist[w];
u=w;
}
visited[u]=1;
count++;

VTU,Mysuru Dept of CS&E(MCA)


DSA 22MCAL16

for(w=0;w<n;w++)
if((dist[u]+cost[u][w]<dist[w])&&(visited[w]!=1))
dist[w]=dist[u]+cost[u][w];
}
}
void main()
{
int n,v,cost[10][10],dist[10],i,j;
clrscr();
printf("enter number of verteces:");
scanf("%d",&n);
printf("\n enter cost matrix(for infinity,enter 999):\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&cost[i][j]);
printf("\n enter source vertex:");
scanf("%d",&v);
dijkstra(n,v,cost,dist);
printf("\n shortest path from\n");
for(i=0;i<n;i++)
if(i!=v)
printf("\n%d->%d=%d",v,i,dist[i]);
getch();
}

VTU,Mysuru Dept of CS&E(MCA)

You might also like