Professional Documents
Culture Documents
DS Practical Programs
DS Practical Programs
#include<stdio.h>
#include<conio.h>
void main()
int mat[10][10],mat_trans[10][10],row,col,nonZeroValue,i=0,j=0,p=1;
clrscr();
scanf("%d %d",&row,&col);
scanf("%d",&nonZeroValue);
mat[0][0]=row;
mat[1][0]=col;
mat[2][0]=nonZeroValue;
for(i=1;i<=nonZeroValue;i++)
scanf("%d %d %d",&mat[0][i],&mat[1][i],&mat[2][i]);
printf("\n[%d]\t\t[%d]\t\t[%d] ",mat[0][0],mat[1][0],mat[2][0]);
for(i=1;i<=nonZeroValue;i++)
//Finding transpose
mat_trans[0][0] = mat[1][0];
mat_trans[1][0] = mat[0][0];
mat_trans[2][0] = mat[2][0];
//triples and copy those triples first. Then for column 1 and so on..
p = 1;
for(i=0;i<mat_trans[0][0];i++)
for(j=1;j<=nonZeroValue;j++)
if(mat[1][j] == i)
mat_trans[0][p] = i;
mat_trans[1][p] = mat[0][j];
mat_trans[2][p] = mat[2][j];
p++;
/*for(i=1;i<=nonZeroValue;i++)
mat_trans[i][0] = mat[i][1];
mat_trans[i][1] = mat[i][0];
mat_trans[i][2] = mat[i][2];
} */
printf("\n[%d]\t\t[%d]\t\t[%d] ",mat_trans[0][0],mat_trans[1][0],mat_trans[2][0]);
for(i=1;i<=nonZeroValue;i++)
Output:
For value 1 -
For value 2 -
For value 3 -
For value 4 -
For value 5 -
For value 6 -
2 0 3
4 0 3
2 1 4
3 1 5
1 3 7
2 3 6
**** TRANSPOSE OF SPARSE MATRIX IS *****
0 2 3
0 4 3
1 2 4
1 3 5
3 1 7
3 2 6
2] Write a c program to implement stack using static array.
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
clrscr();
top=-1;
scanf("%d",&n);
printf("\n\t--------------------------------");
do
scanf("%d",&choice);
switch(choice)
case 1:
push();
break;
case 2:
pop();
break;
}
case 3:
display();
break;
case 4:
break;
default:
while(choice!=4);
return 0;
void push()
if(top>=n-1)
else
top++;
stack[top]=x;
void pop()
if(top<=-1)
else
top--;
void display()
if(top>=0)
printf("\n%d",stack[i]);
else
}
}
Output:
--------------------------------
1.PUSH
2.POP
3.DISPLAY
4.EXIT
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#define SIZE 40
int pop();
void push(int);
char postfix[SIZE];
void main()
char ch;
clrscr();
stack[i] = -1;
scanf("%s",postfix);
ch = postfix[i];
if(isdigit(ch))
{
push(ch-'0');
b = pop();
a = pop();
switch(ch)
break;
break;
break;
break;
push(result);
pEval = pop();
getch();
}
void push(int n)
stack[++top] = n;
else
printf("Stack is full!\n");
exit(-1);
int pop()
int n;
n = stack[top];
stack[top--] = -1;
return n;
else
printf("Stack is empty!\n");
exit(-1);
}
Output:
Poped element is 6
Poped element is 5
Poped element is 30
Poped element is 4
#include<stdio.h>
#define n 5
void main()
int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
clrscr();
while(ch)
scanf("%d",&ch);
switch(ch)
case 1:
if(rear==x)
else
scanf("%d",&queue[rear++]);
break;
case 2:
if(front==rear)
}
else
x++;
break;
case 3:
if(front==rear)
else
printf("%d",queue[i]);
printf("\n");
break;
case 4:
exit(0);
default:
getch();
}
Output:
1.Insertion
2.Deletion
3.Display
4.Exit
Enter no 1:15
Enter no 2:16
15
16
Deleted Element is 15
16
5] Design, develop and execute a C program to demonstrate how jobs are executed in CPU using
queue. Assume all jobs which are submitted to CPU for execution are stored in a queue and each
job will be executed in first come first serve manner. In this process also calculate waiting time of
a job before being executed by the CPU. An example is mentioned below.
#include<stdio.h>
#define n 5
void main()
int f, wt[n];
clrscr();
while(ch)
scanf("%d",&ch);
switch(ch)
case 1:
if(rear==x)
else
printf("\n Enter process id, Burst time and Arrival time \n");
r=rear++;
break;
case 2:
if(front==rear)
{
printf("\n Queue is empty");
else
f=front++;
x++;
if(f==0)
wt[0]=0;
else
wt[f]=wt[f-1]+queue[f-1][1]-queue[f][2];
break;
case 3:
if(front==rear)
else
printf(" P_Id\tBT\tAT\n");
for(j=0;j<3;j++)
printf(" %d\t",queue[i][j]);
printf("\n");
break;
case 4:
exit(0);
default:
getch();
Output: