DSA_Lab_Manual[2]MCA
DSA_Lab_Manual[2]MCA
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))
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;
}
#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');
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;
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');
}
#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)
{
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() {
case 7:
exit(0);
default:
printf("Incorrect choice\n");
}
}
return 0;
}
#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");
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;
}
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;
}
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");
}
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)
{
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()
{
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");
}
}
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;
}
}
}
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");
}
#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++)
{
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:
#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];
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;
}
#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++;
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();
}