NM Record

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 87

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DATA STRUCTURES LABORATORY

(JUN 2020 - DEC 2020)

III SEMESTER
STUDENT NAME :

REGISTER NO :

YEAR/SEMESTER/SECTION :

BATCH :
TABLE OF CONTENTS

Page Staff
S.No Date Title of the Exercises Marks
no signature
ROOTS OF NON – LINEAR EQUATION USING BISECTION
1 METHOD
ROOTS OF NON – LINEAR EQUATION USING NEWTON’S
2 METHOD.

TO FIND THE LARGEST EIGEN VALUE OF A MATRIX BY


3 POWER - METHOD.

SOLVE THE SYSTEM OF LINEAR EQUATIONS USING


4 GAUSS - ELIMINATION METHOD.

SOLVE THE SYSTEM OF LINEAR EQUATIONS USING


5 GAUSS - JORDAN METHOD.

SOLVE THE SYSTEM OF LINEAR EQUATIONS USING


6 GAUSS - SEIDAL ITERATION METHOD.

FIND AREA BY USING TRAPEZOIDAL RULE.


7

FIND AREA BY USING SIMPSON’S 1/3 RULE.


8

FIND AREA BY USING SIMPSON’S 3/8 RULE.


9
TO FIND THE NUMERICAL SOLUTION OF HEAT
10 EQUATION.
EX.NO:1
ROOTS OF NON – LINEAR EQUATION USING BISECTION METHOD
DATE:

AIM:
To write a program in “C” Language to find out the root of the Algebraic and Transcendental
equations using Bisection Method.

ALGORITHM:

Step - 1 Start of the program.

Step - 2. Input the variable x1, x2 for the task.

Step - 3. Check f(x1)*f(x2)<0

Step - 4. If yes proceed

Step - 5. If no exit and print error message

Step - 6. Repeat 7-11 if condition not satisfied

Step - 7. X0=(x1+x2)/2

Step - 8. If f(x0)*f(x1)<0

Step - 9. X2=x0

Step - 10. Else

Step - 11. X1=x0

Step - 12. Condition:

Step - 13.if | (x1-x2)/x1) | < maximum possible error or f(x0)=0

Step - 14. Print output

Step - 15. End of program.


PROGRAM:
#include<stdio.h>
#include<math.h>
#include<conio.h>
#include<stdlib.h>

/*Uncomment the function u want to use or else add u r own*/

//#define f(x) ((x*x*x)-(4*x)-9)


//#define f(x) ((x*x*x)-(x)-1)
//#define f(x) ((x)-(cos(x)))
//#define f(x) ((x*sin(x))-1)
//#define f(x) ((exp(x))-(3*x))
//#define f(x) ((x*x*x)-(9*x)+1) //”range to be entered mannually 2,4”

void main()
{
float a=-1,b=-1,c,prv=-1;
int i;
char ch;
system("cls");

printf("Do you want to choose interval (y/n)?: ");


scanf("%c",&ch);
//If interval exists
if (ch=='y'||ch=='Y')
{
printf("Enter value of A and B\n");
scanf("%f%f",&a,&b);
}
//For finding intervals automatically
else
{
for(i=0;a==-1||b==-1;i++)
{
if(f(0)<0)
{
if(f(i)<0)
a=i;
if(f(i)>0)
b=i;
}
else
{
if(f(i)>0)
a=i;
if(f(i)<0)
b=i;
}
}
printf("\nFinding values of A and B...\nA=%.f\nB=%.f\n",a,b);
}

printf(" A\t\tB\t\tRoot\n");
//Iterration Table
while((ceil(10000*prv)/10000)!=(ceil(10000*c)/10000))
{
//Prv checks previous iteration value, loop terminates when root and
//prev are equal
prv=c;
c=((a+b)/2);
printf(" %.4lf\t\t%.4f\t\t%.4f\n",a,b,c);

if(f(a)>f(b)) //f(a)>f(b)
{
if(f(c)>0)
a=c;
else
b=c;
}
else //f(b)>f(a)
{
if(f(c)>0)
b=c;
else
a=c;
}

}
printf("\nThe Aproximate Root is %.4f",c);
getch();
}
INPUT/OUTPUT:

f(x)=((x*x*x)-(x)-1)

CRITERIA MAX.MARKS MARKS OBTAINED


AIM & ALGORITHM 5
EXECUTION & OUTPUT 10
VIVA 5
TOTAL 20

RESULT:
EX.NO:2
ROOTS OF NON – LINEAR EQUATION USING NEWTON’S METHOD.
DATE:

AIM:

To write a program in “C” Language to implement Newton’s Raphson method.

ALGORITHM :

Step 1. Start the program.

Step 2. Define the function f(x), f’(x)

Step 3. Enter the initial guess of the root , say x0

Step 4. Calculate xn+1 = xn – [f(xn)/ f’(xn)], n = 0, 1, 2, …

Step 5. If |xn+1 – xn| < ϵ, ϵ being prescribed accuracy then go to step 7.

Step 6. Set xn = xn+1 and go to step 4.

Step 7. Print the value of xn.

Step 8. Stop the program.

PROGRAM:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define f(x) ((3*x)-cos(x)-1) //Function
#define f1(x) (3+sin(x)) //Derivative of the function

void main()
{
float a=-1,b=-1,c,prv=-1;
int i;
char ch;
system("cls");
printf("Do you want to choose interval (y/n)?: ");
scanf("%c",&ch);
if (ch=='y'||ch=='Y')
{
printf("Enter value of A and B\n");
scanf("%f%f",&a,&b);
}
else
{
for(i=0;a==-1||b==-1;i++)
{
if(f(0)<0)
{
if(f(i)<0)
a=i;
if(f(i)>0)
b=i;
}
else
{
if(f(i)>0)
a=i;
if(f(i)<0)
b=i;
}
}
printf("\nFinding values of A and B...\nA=%.f\nB=%.f\n",a,b);
}

printf(" Itt\t\t\tRoot\n");

c=(abs(f(a))>=abs(f(b)))?a:b;
i=0;
while((ceil(10000*prv)/10000)!=(ceil(10000*c)/10000))
{
prv=c;
c=(c-(f(c)/f1(c)));
printf(" %d\t\t%.4f\n",i,c);
i++;
}
printf("\nThe Aproximate Root is %.4f",c);
}
INPUT/OUTPUT:

CRITERIA MAX.MARKS MARKS OBTAINED


AIM & ALGORITHM 5
EXECUTION & OUTPUT 10
VIVA 5
TOTAL 20

RESULT:
EX.NO:3
TO FIND THE LARGEST EIGEN VALUE OF A MATRIX BY POWER - METHOD.
DATE:

AIM:

To write a C program to implement the largest eigen value of a matrix by power - method.

ALGORITHM:

1. Start

2. Read Order of Matrix (n) and Tolerable Error (e)

3. Read Matrix A of Size n x n

4. Read Initial Guess Vector X of Size n x 1

5. Initialize: Lambda_Old = 1

6. Multiply: X_NEW = A * X

7. Replace X by X_NEW

8. Find Largest Element (Lamda_New) by Magnitude from X_NEW

9. Normalize or Divide X by Lamda_New

10. Display Lamda_New and X

11. If |Lambda_Old - Lamda_New| > e then set Lambda_Old = Lamda_New and goto
step (6) otherwise goto step (12)

12. Stop
PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>

int cp(float z[],float p[],int n)


{
if (n==0)
return 0;
else if(z[n]==p[n])
{
n--;
cp(z,p,n);
return 1;
}
else
return 0;
}

void main()
{

int i,j,n;
float A[40][40],x[40],z[40],p[40],max;
system("cls");
printf("\nEnter the order of matrix:");
scanf("%d",&n);
printf("\nEnter matrix elements row-wise\n");
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
{
printf("A[%d][%d]=", i,j);
scanf("%f",&A[i][j]);
}
}
printf("\nEnter the column vector\n");
for(i=1; i<=n; i++)
{
printf("X[%d]=",i);
scanf("%f",&x[i]);
}
while(1)
{
for(i=1; i<=n; i++)
{
p[i]=z[i];
}
for(i=1; i<=n; i++)
{
z[i]=0;
for(j=1; j<=n; j++)
{
z[i]=z[i]+A[i][j]*x[j];
}
}
max=fabs(z[1]);
for(i=2; i<=n; i++)
{
if((fabs(z[i]))>max)
max=fabs(z[i]);
}
for(i=1; i<=n; i++)
{
z[i]=z[i]/max;
}

for(i=1; i<=n; i++)


{
z[i]=round(10*z[i])/10;
x[i]=z[i];
}
if(cp(z,p,n)==1)
break;
}
printf("\n The required eigen value is %f",round(max));
printf("\n\nThe required eigen vector is :\n");
for(i=1; i<=n; i++)
{
printf("%f\t",z[i]);
}
getch();
}
INPUT/OUTPUT:

CRITERIA MAX.MARKS MARKS OBTAINED


AIM & ALGORITHM 5
EXECUTION & OUTPUT 10
VIVA 5
TOTAL 20
RESULT:
EX.NO:4 SOLVE THE SYSTEM OF LINEAR EQUATIONS USING GAUSS - ELIMINATION
DATE: METHOD.

AIM:

To write a C program to implement the system of linear equations using gauss - elimination
method.

ALGORIHTM:

1. Start

2. Read Number of Unknowns: n

3. Read Augmented Matrix (A) of n by n+1 Size

4. Transform Augmented Matrix (A) to Upper Trainagular Matrix by Row Operations.

5. Obtain Solution by Back Substitution.

6. Display Result.

7. Stop
PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>

int main()
{

int i,j,k,n;
float a[10][10], x[10], r;
system("cls");

printf("Enter the values:\n\n");


n=3;
for(i=1;i<=n;i++)
{
for(j=1;j<=n+1;j++)
{
printf("a[%d][%d] = ",i,j);
scanf("%f", &a[i][j]);
}
}
for(i=1;i<=n-1;i++)
{
if(a[i][i] == 0.0)
{
printf("Error");
exit(0);
}
for(j=i+1;j<=n;j++)
{
r = a[j][i]/a[i][i];

for(k=1;k<=n+1;k++)
{
a[j][k] = a[j][k] - r*a[i][k];
}
}
}
x[n] = a[n][n+1]/a[n][n];

for(i=n-1;i>=1;i--)
{
x[i] = a[i][n+1];
for(j=i+1;j<=n;j++)
{
x[i] = x[i] - a[i][j]*x[j];
}
x[i] = x[i]/a[i][i];
}
printf("\nAnswer:\n");

printf("x = %0.3f\n", x[1]);


printf("y = %0.3f\n", x[2]);
printf("z = %0.3f\n", x[3]);

return(0);
}
INPUT/OUTPUT:

CRITERIA MAX.MARKS MARKS OBTAINED


AIM & ALGORITHM 5
EXECUTION & OUTPUT 10
VIVA 5
TOTAL 20

RESULT:
EX.NO:5
SOLVE THE SYSTEM OF LINEAR EQUATIONS USING GAUSS - JORDAN METHOD
DATE:

AIM:

To write a C program to implement the system of linear equations using gauss - jordan method.

ALGORITHM:

1. Start

2. Read Number of Unknowns: n

3. Read Augmented Matrix (A) of n by n+1 Size

4. Transform Augmented Matrix (A) to Diagonal Matrix by Row Operations.

5. Obtain Solution by Making All Diagonal Elements to 1.

6. Display Result.

7. Stop
PROGRAM:

#include<stdio.h>
#include<math.h>
int main()
{
float a[10][ 10], x[10], ratio;
int i,j,k,n;
printf("Enter number of unknowns: ");
scanf("%d", &n);
printf("Enter coefficients of Augmented Matrix:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n+1;j++)
{
printf("a[%d][%d] = ",i,j);
scanf("%f", &a[i][j]);
}
}
for(i=1;i<=n;i++)
{
if(a[i][i] == 0.0)
{
printf("Mathematical Error!");
exit(0);
}
for(j=1;j<=n;j++)
{
if(i!=j)
{
ratio = a[j][i]/a[i][i];
for(k=1;k<=n+1;k++)
{
a[j][k] = a[j][k] - ratio*a[i][k];
}
}
}
}
for(i=1;i<=n;i++)
{
x[i] = a[i][n+1]/a[i][i];
}
/* Displaying Solution */
printf("\nSolution:\n");
for(i=1;i<=n;i++)
{
printf("x[%d] = %0.3f\n",i, x[i]);
}
getch();
return(0);
}
INPUT/OUTPUT:

CRITERIA MAX.MARKS MARKS OBTAINED


AIM & ALGORITHM 5
EXECUTION & OUTPUT 10
VIVA 5
TOTAL 20

RESULT:
EX.NO:6 SOLVE THE SYSTEM OF LINEAR EQUATIONS USING GAUSS -
DATE: SEIDAL ITERATION METHOD.

AIM:
To write a C program to implement the system of linear equations using gauss - seidal iteration
method.

ALGORITHM:

set x = X0
repeat
maxdiff: = 0
for i := 1 to n
y:=(bi−∑j=1j≠inaijxj)/aii
if | y − xi | < maxdiff then maxdiff: = | y − xi |
xi := y
next i
until maxdiff > ∈
[x is the refined solution.
PROGRAM:

#include<stdio.h>

#include<conio.h>

#include<math.h>

#include<stdlib.h>

#define f1(x,y,z) (85-6*y-z)/27

#define f2(x,y,z) (72-6*x-2*z)/15

#define f3(x,y,z) (110-x+y)/54

int main()

float x0, y0, z0, x1, y1, z1;

int count=1;

system("cls");

printf("\nCount\tx\ty\tz\n");

do

x0 = x1;

y0 = y1;

z0 = z1;

x1 = f1(x1,y1,z1);

y1 = f2(x1,y1,z1);

z1 = f3(x1,y1,z1);

printf("%d\t%0.4f\t%0.4f\t%0.4f\n",count, x1,y1,z1);

count++;

}while(x0!=x1 && y0!=y1 && z0!=z1);

printf("\nSolution: x=%0.3f, y=%0.3f and z = %0.3f\n",x1,y1,z1);

getch();

return 0;

}
NPUT AND OUTPUT:

CRITERIA MAX.MARKS MARKS OBTAINED


AIM & ALGORITHM 5
EXECUTION & OUTPUT 10
VIVA 5
TOTAL 20

RESULT:
EX.NO:7
FIND AREA BY USING TRAPEZOIDAL RULE
DATE:

AIM:
To write a C program to implement find area by using trapezoidal rule.

ALGORITHM:

1. Start

2. Define function f(x)

3. Read lower limit of integration, upper limit of integration and number of sub interval

4. Calcultae: step size = (upper limit - lower limit)/number of sub interval

5. Set: integration value = f(lower limit) + f(upper limit)

6. Set: i = 1

7. If i > number of sub interval then goto

8. Calculate: k = lower limit + i * h

9. Calculate: Integration value = Integration Value + 2* f(k)

10. Increment i by 1 i.e. i = i+1 and go to step 7

11. Calculate: Integration value = Integration value * step size/2

12. Display Integration value as required answer

13. Stop
PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<math.h>
/* Define function here */
#define f(x) 1/(1+pow(x,2))
int main()
{
float lower, upper, integration=0.0, stepSize, k;
int i, subInterval;
clrscr();
/* Input */
printf("Enter lower limit of integration: ");
scanf("%f", &lower);
printf("Enter upper limit of integration: ");
scanf("%f", &upper);
printf("Enter number of sub intervals: ");
scanf("%d", &subInterval);

/* Calculation */
/* Finding step size */
stepSize = (upper - lower)/subInterval;

/* Finding Integration Value */


integration = f(lower) + f(upper);
for(i=1; i<= subInterval-1; i++)
{
k = lower + i*stepSize;
integration = integration + 2 * f(k);
}
integration = integration * stepSize/2;
printf("\nRequired value of integration is: %.3f", integration);
getch();
return 0;
}
INPUT AND OUTPUT:

CRITERIA MAX.MARKS MARKS OBTAINED


AIM & ALGORITHM 5
EXECUTION & OUTPUT 10
VIVA 5
TOTAL 20

RESULT:
EX.NO:8
FIND AREA BY USING SIMPSON’S RULE
DATE:

AIM:

To write a C program to implement find area by using simpson’s 1/3 rule.

ALGORITHM:

1. Start

2. Define function f(x)

3. Read lower limit of integration, upper limit of


integration and number of sub interval

4. Calcultae: step size = (upper limit - lower limit)/number of sub interval

5. Set: integration value = f(lower limit) + f(upper limit)

6. Set: i = 1

7. If i > number of sub interval then goto

8. Calculate: k = lower limit + i * h

9. If i mod 2 =0 then
Integration value = Integration Value + 2* f(k)
Otherwise
Integration Value = Integration Value + 4 * f(k)
End If

10. Increment i by 1 i.e. i = i+1 and go to step 7

11. Calculate: Integration value = Integration value * step size/3

12. Display Integration value as required answer

13. Stop
PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<math.h>

/* Define function here */


#define f(x) 1/(1+x*x)
int main()
{
float lower, upper, integration=0.0, stepSize, k;
int i, subInterval;
clrscr();
/* Input */
printf("Enter lower limit of integration: ");
scanf("%f", &lower);
printf("Enter upper limit of integration: ");
scanf("%f", &upper);
printf("Enter number of sub intervals: ");
scanf("%d", &subInterval);

/* Calculation */
/* Finding step size */
stepSize = (upper - lower)/subInterval;

/* Finding Integration Value */


integration = f(lower) + f(upper);
for(i=1; i<= subInterval-1; i++)
{
k = lower + i*stepSize;
if(i%2==0)
{
integration = integration + 2 * f(k);
}
else
{
integration = integration + 4 * f(k);
}
}
integration = integration * stepSize/3;
printf("\nRequired value of integration is: %.3f", integration);
getch();
return 0;
}
INPUT/OUTPUT:

CRITERIA MAX.MARKS MARKS OBTAINED


AIM & ALGORITHM 5
EXECUTION & OUTPUT 10
VIVA 5
TOTAL 20

RESULT:
EX.NO:9
FIND AREA BY USING SIMPSON’S 3/8 RULE.
DATE:

AIM:
To write a program in C to implement find area by using simpson’s 3/8 rule.

ALGORITHM:

1. Start

2. Define function f(x)

3. Read lower limit of integration, upper limit of integration and number of sub interval

4. Calcultae: step size = (upper limit - lower limit)/number of sub interval

5. Set: integration value = f(lower limit) + f(upper limit)

6. Set: i = 1

7. If i > number of sub interval then goto

8. Calculate: k = lower limit + i * h

9. If i mod 3 =0 then
Integration value = Integration Value + 2* f(k)
Otherwise
Integration Value = Integration Value + 3 * f(k)
End If

10. Increment i by 1 i.e. i = i+1 and go to step 7

11. Calculate: Integration value = Integration value * step size*3/8

12. Display Integration value as required answer

13. Stop
PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<math.h>
/* Define function here */
#define f(x) 1/(1+x*x)
int main()
{
float lower, upper, integration=0.0, stepSize, k;
int i, subInterval;
clrscr();
/* Input */
printf("Enter lower limit of integration: ");
scanf("%f", &lower);
printf("Enter upper limit of integration: ");
scanf("%f", &upper);
printf("Enter number of sub intervals: ");
scanf("%d", &subInterval);

/* Calculation */
/* Finding step size */
stepSize = (upper - lower)/subInterval;

/* Finding Integration Value */


integration = f(lower) + f(upper);
for(i=1; i<= subInterval-1; i++)
{
k = lower + i*stepSize;
if(i%3 == 0)
{
integration = integration + 2 * f(k);
}
else
{
integration = integration + 3 * f(k);
}
}
integration = integration * stepSize*3/8;
printf("\nRequired value of integration is: %.3f", integration);
getch();
return 0;
}
INPUT/OUTPUT:

CRITERIA MAX.MARKS MARKS OBTAINED


AIM & ALGORITHM 5
EXECUTION & OUTPUT 10
VIVA 5
TOTAL 20

RESULT:
EX.NO:10
TO FIND THE NUMERICAL SOLUTION OF HEAT EQUATION.
DATE:

AIM:
To write a program in C to implement to find the numerical solution of heat equation.

ALGORITHM:

PROGRAM:

#include<stdio.h>
#include<math.h>
#define X 8
#define T 5
float fun(int a)
{
return(4*a-(a*a)/2.0);
}
main()
{
float u[X+1][T+1], h=1.0, k=0.0125,c,al,us,ue;
int j,i;
printf("\n Enter the square of 'c' :");
scanf("%f",&c);
al=c*k/pow(h,2);
printf(" Enter the value of u[0, t] :");
scanf("%f",&us);
printf(" Enter the value of u[%d,t]:", X);
scanf("%f",&ue);
for(i=0;i<=T;i++)
u[0][i]=u[X][i]=us;
for(j=1;j<=X-1;j++)
u[j][0]=fun(j);
for(i=0;i<=T-1;i++)
for(j=1;j<=X-1;j++)
u[j][i+1]=al*u[j-1][i]+(1-2*al)*u[j][i]+al*u[j+1][i];
printf(" The value of alpha is %4.2f\n",al);
printf(" The value of u[j,i] are:\n ");
for(i=0;i<T;i++)
{
for(j=0;j<X;j++)
printf("%7.4f\t",u[j][i]);
printf("\n");
}
}
INPUT/OUTPUT:
CRITERIA MAX.MARKS MARKS OBTAINED
AIM & ALGORITHM 5
EXECUTION & OUTPUT 10
VIVA 5
TOTAL 20

RESULT:

EX.NO:11
QUEUE USING LINKED LIST
DATE:

AIM:
To write a program in C to implement Queue using Linked List.

ALGORITHM:

Algorithm for Queue using Linked List:

INSERT()

tmp=(struct node*)malloc(sizeof(struct node)); // allocate memory

tmp->info=added_item; // input the item into queue

tmp->link=NULL; // set node links

if(front==NULL)

// Queue is empty

front=tmp;

else

rear->link=tmp;

rear=tmp;

DELETE()
{

if(front==NULL)

// queue empty condition

else

tmp=front;

front=front->link; // item deleted

free(tmp);

DISPLAY()

struct node*ptr;

ptr=front;

if(front==NULL)

Write("\n Queue is empty.");

else

Write("\n Queue elements. \n");

while(ptr!=NULL)

Write(" %d ",ptr->info);

ptr=ptr->link;

Write("\n");

}// end of else

}// end of display


PROGRAM:
INPUT/OUTPUT:
CRITERIA MAX.MARKS MARKS OBTAINED
AIM & ALGORITHM 5
EXECUTION & OUTPUT 10
VIVA 5
TOTAL 20

RESULT:
EX.NO:12
DOUBLY LINKED LIST
DATE:

AIM:
To write a program in C to implement Doubly Linked List.

ALGORITHM:

Algorithm for Doubly Linked List:

INSERT AT FIRST(X)

struct node *tmp;

tmp=malloc(sizeof(struct node));// memory allocation

tmp->prev=NULL;

tmp->info=num;

tmp->next=start;

start->prev=tmp;//set node links

start=tmp;

INSERT AT MIDDLE(X)

struct node *tmp,*q;

int i;

q=start;

for(i=0;i<(c-1);i++)

q=q->next;

if(q==NULL)

return;

tmp=malloc(sizeof(struct node));
tmp->info=num;

q->next->prev=tmp;

tmp->next=q->next;

tmp->prev=q;

q->next=tmp;

DELETE (X)

struct node *tmp,*q;

/ *if(start==NULL)

printf("\n List is empty! ");

return;

} */

if(start->info==num)

tmp=start;

start=start->next;

start->prev=NULL;

free(tmp);

return;

q=start;

while(q->next->info!=num ) // Element deleted in between

q=q->next;

if((q->next->info==num)&&(q->next->next==NULL)) // Last element deleted

tmp=q->next;

free(tmp);

q->next=NULL;
return;

if(q->next->info==num)

tmp=q->next;

q->next=tmp->next;

tmp->next->prev=q;

free(tmp);

return;

printf("\n Element %d not found. \n",num);

DISPLAY

struct node *q;

if(start==NULL)

printf("\n List is empty.");

return;

q=start;

printf("\n List is: ");

while(q!=NULL)

printf(" %d ",q->info);

q=q->next;

printf("\n");

} // End of display()

COUNT()

struct node *q=start;


int cnt=0;

while(q!=NULL)

q=q->next;

cnt++;

printf("\n Number of elements are %d ",cnt);

} // End of count

REVERSE()

rev()

struct node *p1,*p2;

p1=start;

p2=p1->next;

p1->next=NULL;

p1->prev=p2;

while(p2!=NULL)

p2->prev=p2->next;

p2->next=p1;

p1=p2;

p2=p2->prev; // next of p2 changed to prev

start=p1;

} // end of rev()
PROGRAM:
INPUT/OUTPUT:
CRITERIA MAX.MARKS MARKS OBTAINED
AIM & ALGORITHM 5
EXECUTION & OUTPUT 10
VIVA 5
TOTAL 20
RESULT:
EX.NO:13
TREE TRAVERSALS
DATE:

AIM:
To write a program in C to implement Tree Traversals

ALGORITHM:

Algorithm for Tree Traversals:

PREORDER(STRUCT NODE *R)

if(r!=NULL)

printf("\t %d",r->data);

preorder(r->left);

preorder(r->right);

INORDER (STRUCT NODE *R)

if(r!=NULL)

inorder(r->left);

Write("\t %d",r->data);

inorder(r->right);

POSTORDER(STRUCT NODE *R)

{ if(r!=NULL)

{ postorder(r->left);

postorder(r->right);

printf("\t %d",r->data);

}}
PROGRAM:
INPUT/OUTPUT:
CRITERIA MAX.MARKS MARKS OBTAINED
AIM & ALGORITHM 5
EXECUTION & OUTPUT 10
VIVA 5
TOTAL 20

RESULT:
EX.NO:14
BREATH FIRST SEARCH
DATE:

AIM:
To write a program in C to implement Breath First Search.

ALGORITHM:

Algorithm for Breath First Search:

ALGORITHM BFS(INT V)

for(i=1;i<=n;i++)

vis[i]=0;

vis[v]=1;

u=v;

while(v<=n)

front=0;

rear=0;

for(i=1;i<=n;i++)

q[i]=0;

for(w=1;w<=n;w++)

front++;

q[front]=w;

vis[w]=1;

if((rear==0)&&(front==0))

return;

else
{

while(rear<front)

rear++;

u=q[rear];

printf(" -> %d",u);

v++;

}
PROGRAM:
INPUT/OUTPUT:

CRITERIA MAX.MARKS MARKS OBTAINED


AIM & ALGORITHM 5
EXECUTION & OUTPUT 10
VIVA 5
TOTAL 20

RESULT:
EX.NO:15
DEPTH FIRST SEARCH
DATE:

AIM:
To write a program in C to implement Depth First Search.
ALGORITHM:

ALGORITHM DEPTH FIRST SEARCH (INT V)

int w;

vis[v]=1;

printf(" -> %d",v);

for(w=1;w<=n;w++)

if(vis[w]==0)

if(adj[v][w]==1)

dfs(w);

}
PROGRAM:
INPUT/OUTPUT:

CRITERIA MAX.MARKS MARKS OBTAINED


AIM & ALGORITHM 5
EXECUTION & OUTPUT 10
VIVA 5
TOTAL 20

RESULT:
EX.NO:16
SHELL SORT
DATE:

AIM:
To write a program in C to implement Shell Sort.
ALGORITHM:

Algorithm for Shell Sort:

shellSort()

A : array of items

/* calculate interval*/

while interval < A.length /3 do:

interval = interval * 3 + 1

end while

while interval > 0 do:

for outer = interval; outer < A.length; outer ++ do:

/* select value to be inserted */

valueToInsert = A[outer]

inner = outer;

/*shift element towards right*/

while inner > interval -1 && A[inner - interval] >= valueToInsert do:

A[inner] = A[inner - interval]

inner = inner - interval

end while

/* insert the number at hole position */

A[inner] = valueToInsert
end for

/* calculate interval*/

interval = (interval -1) /3;

end while

end procedure
PROGRAM:
INPUT / OUTPUT:

CRITERIA MAX.MARKS MARKS OBTAINED


AIM & ALGORITHM 5
EXECUTION & OUTPUT 10
VIVA 5
TOTAL 20

RESULT:

You might also like