0% found this document useful (0 votes)
21 views112 pages

CS3361 Lab Record 2

Record for engineering cse

Uploaded by

Arun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views112 pages

CS3361 Lab Record 2

Record for engineering cse

Uploaded by

Arun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 112

EX NO:01

DATE :

PROGRAM TO USING ASSIGNMENT OPERATION

AIM:
To write a c program to using assignment
operation.

ALGORITHM:

SETP 1: Start the program.

STEP 2: Declare the values.

STEP 3: Read the inputs.

STEP 4: Calculate a assignment


operation(+,-,*,/,%)
for the input of three numbers.

STEP 5: Display the result.

STEP 6: Stop the program.


PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,d;
clrscr();
printf(“enter the value of a:”);
scanf(“%d”,&a);
printf(”enter the value of b:”);
scanf(“%d”,&b);
printf(“enter the value of c:”);
scanf(“%d”,&c:);
d=a+b+c;
printf(“addition of a,b and c:%d”,d);
d=a-b-c;
printf(“subtraction of a,b and c:%d”,d);
d=a*b*c;
printf(“multiplication of a,b and c:%d”,d);
d=b/a;
printf(“divition of b and a:%d”,d);
d=b%a;
printf(“modulus of b and a:%d”,d);
getch;
}
EX NO :02
DATE :

PROGRAM TO ODD (OR) EVEN

AIM:

To write a c program odd or even

ALGORITHM:

STEP 1: Start the program.

STEP 2: Declare the values.

STEP 3: Read the input values num.

STEP 4:If (num%2=0)given number is even else


given
number is odd.

STEP 4: Display the result.

STEP 5: Stop the program.


PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int a,;
clrscr();
printf(“enter the value of a,”);
scanf(“%d”,&a);
{
if(a%2==0)
printf(“the given number is even”);
else
printf(“the given numbber is odd”);
}
getch();
}
EX NO :03
DATE :

PROGRAM TO GREATER OF THREE NUMBER

AIM:

To write a c program greater of three number.

ALGORITHM:

STEP 1: Start the program.

STEP 2: Declare the variabels.

STEP 3: Read the inputs valus a,b,c.

STEP 4: If (a>b&&a>c)a is big else(b>a&&b>c)b is


a big else(c>a&&c>b) print to c is big.

STEP 5: Diplay the result.

STEP 6: Stop the program.


PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf(“enter the value of a,b,c”);
scanf(“%d%d%d”,&a,&b,&c);
{
if(a>b&&a>c)
printf(“a is greatest number”);
else if(b>a&&b>c)
printf(“b is greatest number”);
else
printf(“c is greatest number”);
}
getch();
}
EX NO:04
DATE :

PROGRAM TO MULTIPLICATION TABLE

AIM:

To write a c program to multiplication table.

ALGORITHM:

STEP 1: Start the program.

STEP 2: Declare the variables.

STEP 3: Read the input values int a,i=1.

STEP 4: Print the multiplication table.

STEP 5: Display the result.

STEP 6: Stop the program.


PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
clrscr();
printf(“enter the value of multiplication table:”);
scanf(“%d”,&n);
for(i=1;i<=10;i++)
{
printf(“%d*%d=%d\n”,n,i,n*i);
}
getch();
}
EX NO:05
DATE :

PROGRAM TO CALCLUATE ARITHMETIC OPERATION


DEPENDING ON OPERATION

AIM:

To write a c program to calculate the arthmetic


operation depending on operation.

ALGORITHM:

STEP 1: Start the program.

STEP 2: Declare the variables.

STEP 3: Read the inputs.

STEP 4: Calculate arithmetic operation(+,-,*,m,%)


for the inputs of two numbers.

STEP 5: Display the results.

STEP 6: Stop the program.


PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;char_ch;
clrscr();
printf(“enter any arithmetic opreation:”);
scanf(“%c”,&ch);
printf(“enter two numbers:”);
scanf(“%d%d”,&a,&b);
switch(ch)
{
case‘+’:printf(“\n the sum is %d”,a+b);
break;
case‘-’:printf(“\n the diffrence is %d”,a-b);
break;
case‘*’:printf(“\n the product is %d”,a*b);
break;
case‘/’:printf(“\n the quotient is %d”,a/b);
break;
case‘%’:printf(“\n the remainder is %d”,a%b);
break;
defult:printf(“not valid opreater”);
}
getch();
}
EX NO:06
DATE :

PROGRAM TO ARMSTRONG NUMBER

AIM:

To write a c program to check wether the


given number is armstorng or not using while
loop.

ALGORITHM:

STEP 1: Start the program.

STEP 2: Read the variable N.

STEP 3: Assign N1=N,

STEP 4: create set a loop using the condition


while
(N1!=0) it the condition is true
REM=N1%10;
num=num+REM*REM;N1=N1/10.

STEP 5: Else check the condition it (num=N)if the


condition true.

STEP 6: Print the result.

STEP 7: Stop the program.


PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int n,n1,rem,num=0;
clrscr();
printf(“\n check whether a given number is armstrong
number or not”)
printf(” enter a passitive integer:”);
scanf(“%d”,&n);
n1=n;
while(n1!=0)
{
rem=n1%10;
num=num+(rem*rem*rem);
n1=n1/10;
}
if(num==n)
printf(“%d is an armstrong number”,n);
else
printf(“%d is not a armstrong number”,n);
getch();
}
EX NO:07
DATE :

PROGRAM TO FACTORIAL OF A NUMBER

AIM:

To write a c program to calculate the factorial


of the given number using for loop.

ALGORITHM:

STEP 1: Start the program.

STEP 2: Enter a number.

STEP 3: Set a loop to find the factorial of the


given no using fact=fact*.

STEP 4: Print the factorial of the given number.

STEP 5: Stop the program.


PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int fact=1,i,num;
printf(” enter the number”);
scanf(“%d”,&num);
for(i=1,i<=num;i++)
fact=fact*i;
}
printf(” the factorial of %d is %d”,num,fact);
getch();
}
}
EX NO:08
DATE :

PROGRAM TO USE FIBONACCI SERIES

AIM:

To write a c program to find the fibonacci


series of the given number using do while loop.

ALGORITHM:

STEP 1: Start the program.

STEP 2: Set f=0,f1=0,f2=1,i=1;

STEP 3: Read N

STEP 4: Printf

STEP 5: f=f1+f2

STEP 6: Printf

STEP 7: f2=f1;f1;i=i+1

STEP 8: If (i<n) go to step 5,otherwise go to


step 9

STEP 9: Print the result

STEP 10: Stop the program.


PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int i=1,n,f,f1,f2;
printf(“enter the number”);
scenf(“%d”,&n);
f=0;
f1=0;
f2=1;
printf(“fibonacci series”);
do
{
i++;
printf(“%d\n”,f);
f=f1+f2;
f2=f1;
f1=f;
}
while(i<=n);
}
EX NO:09
DATE :

PROGRAM TO SEARCH AN ELEMENT FROM AN ARRAY

AIM:

To write a c program to search an element from


an array using one dimensional array.

ALGORITHM:

STEP 1: Start the program

STEP 2: Declare the variables in a[0],i,n,m,c=o;

STEP 3: Check wether the given key present in


the array is arr[i]=key.

STEP 4: If yes print search found

STEP 5: Else print not search found

STEP 6: Display the result

STEP 7: Stop the program.


PROGRAM:

#include<stdio.h>
#include<conio.h>
int main()
{
int a[10],i,n,m,c=0;
printf(“enter the size of an array:”);
scanf(“%d”,&n);
printf(“enter the element of the array:”);
for(i=0;i<=n-1;i++)
{
scanf(“%d”,&a[i]);
}
printf(“enter the number to be search:”);
scanf(“%d”,&m);
for(i=0;i<n-1;i++)
{
if(a[i]==m)
{
c=1;
break;
}
}
if(c==0)
printf(“the number is not in the list”);
else
printf(“the number is found”);
return(0);
getch();
}
EX NO:10
DATE :

PROGRAM TO TWO DIMENSIONAL ARRAY MATRIX


MULTIPLICATION

AIM:

To write a c program for matrix multiplication.

ALGORITHM:

STEP 1: Start the program

STEP 2: Declare the variabels

STEP 3: Enter the element of matrix by row wise


using loops.

STEP 4: Check the number of rows and columns of


first and second matrix.

STEP 5: Mulitiply the matrix using nested loops

STEP 6: Print the results

STEP 7: Stop the program.


PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],i,j,k,m,n,o,p;
clrscr();
printf(“\n matrix multiplication\n”);
printf(\n \n”);
printf(“\n enter the rows & column of first matrix:”);
scanf(“%d%d”,&m,&n);
printf(“\n enter the rows & column of second matrix:”);
scanf(“%d%d”,&o,&p);
if(n!=0)
{
printf(“matrix multiplication is not possible”);
printf(“\n cloumn of first matrix must be same as row of
second matrix”);
}
else
{
printf(“\n enter the first matrix-->”);
for(i=0;i<m;i++)
{
for(j=0;i<n;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“\n enter the second matrix-->”);
for(i=0;i<0;i++)
{
for(j=0;j<p;j++)
{
scanf(“%d”,&b[i][j]);
}
}
printf(“\n\n first matrix is\n”);
for(i=0;i<m;i++)
{
printf(“\n”);
for(j=0;j<n;j++)
{
printf(“%d\t”,a[i][j]);
}
}
printf(“\n\n the second matrix is \n”);
for(i=0;i<o;i++)
{
printf(“\n”);
for(j=0;j<p;j++)
{
printf(“%d\t”,b[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j,p;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
}
printf(“\n\n the multiplication of two matrix is \n”);
for(i=0;i<m;i++);
{
printf(“\n”);
for(j=0;j<p;j++)
{
printf(“%d\t”,c[i][j]);
}
}
getch();
}
EX NO:11
DATE :

PROGRAM TO ILLUSTRATE THE CONCEPT OF


SWAPPING TWO NUMBERS USING FUNCTION

AIM:

To write a c program to illustrate the concept


of
swapping two numbers using function.

ALGORITHM:

STEP 1: Start the program

STEP 2: Declare the variables a,b

STEP 3: Compute,swap(a,b)

STEP 4: Compute temp=a;


a=b;
b=temp;

STEP 5: Print the result

STEP 6: Stop the program.


PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
printf(“\n enter value of a&b:”);
scanf(“%d%d”,&a,&);
printf(“\n before swapping:\n”);
printf(“\n a=%d\n\nb=%d”,a,b);
swap(a,b);
getch();
}
swap(int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
printf(“\n after swapping:\n”);
printf(“\n a=%d\n\nb=%d”,a,b);
return(0);
}
EX NO:12
DATE :

PROGRAM TO ILLUSTRATE THE COUNCEPT OF


SWAPPING TWO NUMBER USING POINTER

AIM:

To write a c program to illustrate the concept


of
swapping two number using pointer.

ALGORITHM:

STEP 1: Start the program

STEP 2: Declare the variables a,b

STEP 3: Read the input variable a,b

STEP 4: Swap(int*x,int*y)

STEP 5: Compute temp = *y;


*x = *y;
*y = temp;

STEP 6: Print the results

STEP 7: Stop the program.


PROGRAM:

#include<stdio.h>
swap(int*,int*);
void main()
{
int a,b;
printf(“\n enter value of a&b:”);
scanf(“%d%d”,&a,&b);
printf(“\n before swapping:\n”);
printf(“\n a=%d\n\nb=%d\n\n”,a,b);
swap(&a,&b);
printf(“\n after swapping:\n”);
printf(“\n a=%d\n\nb=%d\n\n”,a,b);
getch();
}
swap(int*x,int*y)
{
int t;
t=*x;
*x=*y;
*y=t;
retoun(0);
}
EX NO:13
DATE :

PROGRAM TO ARRAY TO FUNCTION

AIM:

To write a c program to find the largest


element
from the given array by using the concept
of
passing array to function.

ALGORITHM:

STEP 1: Start the program

STEP 2: Define the size=50

STEP 3: Declare the variable a[size],n,i,b;

STEP 4: Compute b=big(a,n);


Compute b=a[0];
compute if(a[i]>b);
b=a[i];

STEP 5: Print the results

STEP 6: Stop the program.


PROGRAM:

#include<stdio.h>
#define SIZE 50
int big(int[],int);
void main()
{
int a[SIZE],n,i,b;
printf(“\n enter size of array:”);
scanf(“%d”,&n);
printf(“\n enter elements:\n”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
b=big(a,n);
printf(“\n largest number:%d”,b);
}
int big(int a[],int n)
{
int b,i;
b=a[0];
for(i=0;i<n;i++)
if(a[i]>b)
b=a[i];
return b;
}
EX NO:14
DATE :

PROGRAM TO ARRAY WITH STRUCTURE

AIM:
To write a c program to array within structure
print to output.

ALGORITHM:

STEP 1: Start the program

STEP 2: Define the structure

STEP 3: Read the input

STEP 4: Condition for(i=0;i<max;i++)

STEP 5: Print the details to the student

STEP 6: Condition for(i=0;i,max;i++)

STEP 7: Print the results

STEP 8: Stop the program.


PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<string.h>
#define max2
struct student
{
char name[20];
int roll_no;
int marks;
};
int main()
{
clrscr();
{
struct student s[max];
int i;
for(i=o;i<max;i++)
{
printf(“\n enter details of student%d\n\n”,i);
printf(“enter name”);
scanf(“%s”,&s[i].name);
printf(“sudent marks”);
scanf(“%d”,&s[i].marks);
printf(“student roll_no”);
scanf(“%d”,&s[i].roll_no);
}
for(i=0;i<max;i++)
{
printf(“\n name=%s\n\n marks=%d\n\n roll_no=%d
\n”:’s[i].name,s[i].marks,s[i].roll_n0);
}
getch();
return 0;
}
}
EX NO:15
DATE :

PROGRAM TO POINTER IN STRUCTURE

AIM:

To write a c program to student detail using


pointer in structure on c program.

ALGORITHM:

STEP 1: Start the program

STEP 2: Define the structure

STEP 3: Read the input

STEP 4: Compute struct my structrue x ptr ptr=2 vari

STEP 5: Print the resulte

STEP 6: Stop the program.


PROGRAM:

#include<stdio.h>
#include<conio.h>
struct my_structure
{
char name[20];
int number;
int rank;
};
int main()
{
struct my_ structure variable={“study tonight”,35,1};
struct my_structure*ptr;
clrscr();
ptr=&variable
printf(“nmae:%s\n”,ptr->name);
printf(“number:%d\n”,ptr->number);
printf(“rank:%d”,ptr->rank);
return 0;
}
EX NO:16
DATE :

PROGRAM TO POINTER

AIM:
To write a c program for intraducing file
primitiues such as fopen,fclose,fprint and file
pointer.

ALGORITHM:

STEP 1: Start the program

STEP 2: Read the input

STEP 3: Compute fp=fopen(“std.dat”,”w”);

STEP 4: Condition if(fp=null)

STEP 5: Print(“file operating error”)

STEP 6: Print the results

STEP 7: Stop the program.


PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
FILE*FP;
int rno;
char name[20];
fp=fopen(“std.dat”,“w”);
if(fp==null)
{
printf(“file opreating error\n”);
exit(1);
printf(“enter rno,name...\n”);
scanf(“%d%d”,rno,name);
fclose(fp);
}
}
EX.NO: 17 ARRAY IMPLEMENTATION OF STACK ADT

DATE:

AIM :

To write a program in C to implement the stack ADT using array concept


that performs all the operations of stack.

ALGORITHM :

STEP 1: Define an array to store the element.

STEP 2: Get the users’ choice.

STEP 3: If the option is 1 perform creation operation and go to step4.

If the option is 2 perform insertion operation and go to step5.

If the option is 3 perform deletion operation and go to step6.

If the option is 4 perform display operation and go to step7.

STEP 4: Create the stack. Initially get the limit of stack and the get the items. If
the limit of stack is exceeds print the message unable to create the
stack.

STEP 5: Get the element to be pushed. If top pointer exceeds stack capacity.
Print Error message that the stack overflow. If not, increment the top
pointer by one and store the element in the position which is denoted
by top pointer.

STEP 6: If the stack is empty, then print error message that stack is empty. If not
fetch the element from the position which is denoted by top pointer and
decrement the top pointer by one

STEP 7: If the top value is not less than the 0 the stack is display otherwise print
the message “stack is empty”.

STEP 8: Stop the execution.


PROGRAM :

#include<stdio.h>
#include<conio.h>
#define max 20
int opt, a[20],i,top=0,n;

void main()
{
void create(),push(),pop(),disp();
int wish;
do
{
clrscr();
printf("\nMENU");
printf("\n1.Create\n2.Push\n3.pop\n4.Display\n5.Exit\n");
printf("\nEnter your option");
scanf("%d",&opt);
switch(opt)
{
case 1:create();break;
case 2:push();break;
case 3:pop();break;
case 4:disp();break;
case 5:exit(0);
}
printf("\nDo u want to cintinue(1/0):");
scanf("%d",&wish);
}while(wish==1);}

void create()
{
printf("\n Enter the limit of stack");
scanf("%d",&n);if(n<max)
{
printf("\nEnter the items");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
top=n-1;
printf("\nUnable to create the stack");
}

void push()
{
int x;
if(top<max)
{
printf("\nEnter the element to be pushed:");scanf("%d",&x);
top=top+1;
a[top]=x; n=top;

printf("\n Stack is full");

void pop()
{
if(top<0)
printf("\n Stack is empty");
else printf("\nThe element popped is %d",a[top]);
{ top=top-1;
n=top;

}
}

void disp()
{
if(top<0)
printf("\n Stack is empty");
else printf("\n The elements in the stack are:");
{ for(i=top;i>=0;i--)
printf("\n%d",a[i]);}
RESULT :

Thus a C program for Stack using ADT was implemented executed and verified
successfully.
EX.NO: 18 QUEUE ADT USING ARRAY

DATE :

AIM:
To write a program for Queue using array implementation.
ALGORITHM:
STEP 1 :Define a array which stores queue elements..
STEP 2:The operations on the queue are
a. a)INSERT data into the queue
b. b)DELETE data out of queue
STEP 3:INSERT DATA INTO queue
a. Enter the data to be inserted into queue.
b. If TOP is NULL
i. The input data is the first node in queue.
ii. The link of the node is NULL.
iii. TOP points to that node.
c. If TOP is NOT NULL
i. The link of TOP points to the new node.
ii. TOP points to that node.
STEP 4 :DELETE DATA FROM queue
a. If TOP is NULL
i. the queue is empty
b. If TOP is NOT NULL
i. The link of TOP is the current TOP.
ii. The pervious TOP is popped from queue.

STEP 5 : The queue represented by linked list is traversed to display its


content.

PROGRAM :

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define SIZE 5
int front = - 1;
int rear = - 1;
int q[SIZE];
void insert( );
void del( );
void display( );
void main( )
{
int choice;
do
{
printf("\t Menu");
printf("\n 1. Insert");
printf("\n 2. Delete");
printf("\n 3. Display ");
printf("\n 4. Exit");
printf("\n Enter Your Choice:");
scanf("%d", &choice);
switch(choice)
{
case 1:
insert( );
display( );
break;
case 2:
del( );
display( );
break;
case 3:
display( );
break;
case 4:
printf("End of Program... !!!!");
exit(0);
}
}

while(choice != 4);}
void insert( )
{
int no;
printf("\n Enter No.:");
scanf("%d", &no);
if(rear < SIZE - 1)
{
q[++rear]=no;
if(front == -1)
front=0;// front=front+1;
}
else
{
printf("\n Queue overflow");
}}
void del( )
{
if(front == - 1)
{
printf("\n Queue Underflow");
return;
}
else
{
printf("\n Deleted Item:-->%d\n", q[front]);
}
if(front == rear)
{
front = - 1;
rear = - 1;
}
else
{
front = front + 1;
}}
void display( )
{
int i;
if( front == - 1)
{
printf("\nQueue is empty... ");
return;
}
for(i = front; i<=rear; i++)
printf("\t%d",q[i]);}

RESULT :

Thus a C program for Queue using ADT was implemented executed and
verified successfully.
EX.NO: 19 ARRAY IMPLEMENTATION OF LIST ADT

DATE:

AIM:
To write a program for List using array implementation.
ALGORITHM:
STEP 1: Create nodes first, last; next, prev and cur then set the value
as NULL.
STEP 2: Read the list operation type.
STEP 3: If operation type is create then process the following steps.
1. Allocate memory for node cur.
2. Read data in cur's data area.
3. Assign cur node as NULL.
4. Assign first=last=cur.
STEP 4: If operation type is Insert then process the following steps.
1. Allocate memory for node cur.
2. Read data in cur's data area.
3. Read the position the Data to be insert.
4. Availability of the position is true then assing cur's node as first
and first=cur.
5. If availability of position is false then do following steps.
1. Assign next as cur and count as zero.
2. Repeat the following steps until count less than postion.
1 .Assign prev as next

2. Next as prev of node.


3. Add count by on
4. If prev as NULL then display the message INVALID
POSITION.
5.If prev not qual to NULL then do the following steps.
1. Assign cur's node as prev's node.
2. Assign prev's node as cur.
STEP 5: If operation type is delete then do the following steps.
1. Read the position .
2. Check list is Empty .If it is true display the message Listempty.
3. If position is first.
1. Assign cur as first.
2. Assign First as first of node.
3. Reallocate the cur from memory.
4. If position is last.
1. Move the current node to prev.
2. cur's node as Null.
3. Reallocate the Last from memory.
4. Assign last as cur.
5. If position is enter Mediate.
1. Move the cur to required postion.
2. Move the Previous to cur's previous position
3. Move the Next to cur's Next position.
4. Now Assign previous of node as next.
5. Reallocate the cur from memory.
STEP 6: If operation is traverse.
1. Assign current as first.
2. Repeat the following steps untill cur becomes NULL.

PROGRAM :
#include<stdio.h>
#include<conio.h>
#define MAX 10
void create();
void insert();
void deletion();
void search();
void display();
int a,b[20], n, p, e, f, i, pos;
void main()
{
clrscr();
int ch;
char g='y';
do
{
printf("\n main Menu");
printf("\n 1.Create \n 2.Delete \n 3.Search \n 4.Insert \n 5.Display\n 6.Exit \n");
printf("\n Enter your Choice");
scanf("%d", &ch);
switch(ch)
{
case 1:
create();
break;
case 2:
deletion();
break;
case 3:
search();
break;
case 4:
insert();
break;
case 5:
display();
break;

case 6:
exit();
break;
default:
printf("\n Enter the correct choice:");
}
printf("\n Do u want to continue:::");
scanf("\n%c", &g);
}
while(g=='y'||g=='Y');
getch();
}
void create()
{
printf("\n Enter the number of nodes");
scanf("%d", &n);
for (i=0;i<n;i++)
{
printf("\n Enter the Element:",i+1);
scanf("%d", &b[i]);
}
}

void deletion()
{
printf("\n Enter the position u want to delete::");
scanf("%d", &pos);
if(pos>=n)
{
printf("\n Invalid Location::");
}
else
{
for(i=pos+1;i<n;i++)
{
b[i-1]=b[i];
}
n--;
}
printf("\n The Elements after deletion");
for(i=0;i<n;i++)
{
printf("\t%d", b[i]);
}
}

void search()
{
printf("\n Enter the Element to be searched:");
scanf("%d", &e);
for(i=0;i<n;i++)
{
if(b[i]==e)
{
printf("Value is in the %d Position", i);
}}}
void insert()
{
printf("\n Enter the position u need to insert::");
scanf("%d", &pos);
if(pos>=n)
{
printf("\n invalid Location::");
}
else
{
for(i=MAX-1;i>=pos-1;i--)
{
b[i+1]=b[i];
}
printf("\n Enter the element to insert::\n");
scanf("%d",&p);
b[pos]=p;
n++;
}
printf("\n The list after insertion::\n");
display();
}
void display()
{
printf("\n The Elements of The list ADT are:");
for(i=0;i<n;i++)
{
printf("\n\n%d", b[i]);
}
}

RESULT:

Thus the C program for array implementation of List ADT was


created, executed and output was verified successfully.
EX. NO: 20 STACK ADT USING LINKED LIST

DATE :

AIM:
To write a C program for stack ADT using linked list implementation.
ALGORITHM:
STEP 1: 1. Define a struct for each node in the stack. Each node in the
stack contains data and link to the next node. TOP pointer
points to last node inserted in the stack.
STEP 2 : 2. The operations on the stack are
a. PUSH data into the stack
b. POP data out of stack
STEP 3: 3. PUSH DATA INTO STACK
a. Enter the data to be inserted into stack.
b. If TOP is NULL
i. The input data is the first node in stack.
ii. The link of the node is NULL.
iii. TOP points to that node.
c. If TOP is NOT NULL
i. The link of TOP points to the new node.
ii. TOP points to that node.
STEP 4: 4. POP DATA FROM STACK
a. 4a.If TOP is NULL
i. the stack is empty
b. 4b.If TOP is NOT NULL
i. The link of TOP is the current TOP.
ii. The pervious TOP is popped from stack.
STEP 5 : 5. The stack represented by linked list is traversed to display its
content.

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *next;
}*top,*new1,*first;
void main()
{
int wish,opt;
void create(),push(),pop(),view();

do
{
clrscr();
printf("Stack using linked list menu");
printf("\n1.Create\n2.Push\n3.Pop\n4.View\n5.Exit\n");
printf("\nEnter your option(1,2,3,4,5):");
scanf("%d",&wish);
switch(wish)
{
case 1:
create();
break;
case 2:
push();
break;
case 3:
pop();
break;
case 4:
view();
break;
case 5:
exit(0);
}
printf("\nDo you wnat to continue(0/1):");
scanf("%d",&opt);
}
while(opt==1);
}
void create()
{
int ch;
top=(struct node*)malloc(sizeof(struct node));
top->next=NULL;
do
{
clrscr();
printf("Enter the data:\n");
scanf("%d",&top->data);
printf("Do you want to insert another(1/0)\n");
scanf("%d",&ch);
if(ch==1)
{
new1=(struct node*)malloc(sizeof(struct node));
new1->next=top;
top=new1;
first=top;
}
else
break;
}
while(ch==1);
}
void push()
{
top=first;
new1=(struct node*)malloc(sizeof(struct node));
printf("Enter the element to be pushed:");
scanf("%d",&new1->data);
new1->next=top;
top=new1;
first=top;
}
void pop()
{
clrscr();
top=first;
if(top==NULL)
printf("\n Stack is empty");
else
{
printf("\nThe element popped out from stack is %d",top->data);
top=top->next;
first=top;
}
}
void view()
{
printf("\nStack contents\n");
while(top->next!=NULL)
{printf("%d->",top->data);
top=top->next;}
printf("%d\n",top->data);
getch();
}

RESULT:

Thus the C program for array implementation of Stack ADT was


created, executed and output was verified successfully
EX.NO :21 QUEUE ADT USING LINKED LIST

DATE :

AIM :
To write a C program for Queue using Linked implementation.
ALGORITHM:
STEP 1: Define a struct for each node in the queue. Each node in
the queue contains data and link to the next node. Front
and rear pointer points to first and last node inserted in the
queue.
STEP 2:The operations on the queue are
a. INSERT data into the queue
b. DELETE data out of queue
STEP 3: INSERT DATA INTO queue
a. Enter the data to be inserted into queue.
b. If TOP is NULL
i. The input data is the first node in queue.
ii. The link of the node is NULL.
iii. TOP points to that node. c. If TOP is NOT NULL
i. The link of TOP points to the new node.
ii. ii. TOP points to that node.
STEP 4 :DELETE DATA FROM queue a. If TOP is NULL
i. the queue is empty b. If TOP is NOT NULL
i. The link of TOP is the current TOP.
ii. The pervious TOP is popped from queue.
STEP 5 :The queue represented by linked list is traversed to display
its content

PROGRAM:
#include<stdio.h>
#include<conio.h>
struct node
{
int info;
struct node *link;
}*front = NULL, *rear = NULL;
void insert();
void delet();
void display();
int item;
void main()
{
int ch;
do
{
printf("\n\n1.\tEnqueue\n2.\tDequeue\n3.\tDisplay\n4.\tExit\n");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\n\nInvalid choice. Please try again...\n");
}
}
while(1);
getch();
}
void insert()
{
printf("\n\nEnter ITEM: ");
scanf("%d", &item);
if(rear == NULL)
{
rear = (struct node *)malloc(sizeof(struct node));
rear->info = item;
rear->link = NULL;
front = rear;
}
else
{
rear->link = (struct node *)malloc(sizeof(struct node));
rear = rear->link;
rear->info = item;
rear->link = NULL;
}}
void delet(){
struct node *ptr;
if(front == NULL)
printf("\n\nQueue is empty.\n");
else{
ptr = front;
item = front->info;
front = front->link;
free(ptr);
printf("\nItem deleted: %d\n", item);
if(front == NULL)
rear = NULL;
}}
void display()
{
struct node *ptr = front;
if(rear == NULL)
printf("\n\nQueue is empty.\n");
else
{
printf("\n\n");
while(ptr != NULL)
{
printf("%d\t",ptr->info);
ptr = ptr->link;
}
}
}

RESULT:

Thus the C program for array implementation of Queue ADT was


created, executed and output was created,executed and was verified successfully.
EX.NO:22 REPRESENT A POLYNOMIAL AS A

DATE : LINKED LIST

AIM:
To wrire program in C to convert given infix expression in to postfix notation

ALGORITHM:
STEP 1:Get the two polynomials. First polynomial is P1 and second polynomial is
P2
STEP 2: For addition of two polynomials if exponents of both the polynomials are
same then we add the coefficients. For storing the result we will create t
the third linked lists say P3.
STEP 3: If Exponent of P2 is greater than exponent of P1 then keep the P3 as P2.
STEP 4: If Exponent of P2 is greater than exponent of P1 then keep the P3 as P1
STEP 5:If Exponent of P2 is equal to the exponent of P1 then add the coefficient
of P1 and coefficient of P2 as coefficient of P3.
STEP 6: Continue the above step from 3 to 5 until end o the two polynomials.
STEP 7 :If any of the polynomial is ended keep P3 as the remaining polynomial.
STEP 8 :Stop the execution.
PROGRAM:
#include<stdio.h>
#include<conio.h>
main()
{
int a[10], b[10], c[10],m,n,k,k1,i,j,x;
clrscr();
printf("\n\tPolynomial Addition\n");
printf("\t===================\n");
printf("\n\tEnter the no. of terms of the polynomial:");
scanf("%d", &m);
printf("\n\tEnter the degrees and coefficients:");
for (i=0;i<2*m;i++)
scanf("%d", &a[i]);
printf("\n\tFirst polynomial is:");
k1=0;
if(a[k1+1]==1)
printf("x^%d", a[k1]);
else
printf("%dx^%d", a[k1+1],a[k1]);
k1+=2;
while (k1<i)
{
k+=2;
i+=2;
m--;
}
while (n>0)
{
c[k+1]=b[j+1];
c[k]=b[j];
k+=2;
j+=2;
n--;
}
printf("\n\n\n\n\tSum of the two polynomials is:");
k1=0;
if (c[k1+1]==1)
printf("x^%d", c[k1]);
else
printf("%dx^%d", c[k1+1],c[k1]);
k1+=2;
while (k1<k)
{
if (c[k1+1]==1)
printf("+x^%d", c[k1]);
else
printf("+%dx^%d", c[k1+1], c[k1]);
k1+=2;
}
getch();
return 0;
}
printf("+%dx^%d", a[k1+1],a[k1]);
k1+=2;
}
printf("\n\n\n\tEnter the no. of terms of 2nd polynomial:");
scanf("%d", &n);
printf("\n\tEnter the degrees and co-efficients:");
for(j=0;j<2*n;j++)
scanf("%d", &b[j]);
printf("\n\tSecond polynomial is:");
k1=0;
if(b[k1+1]==1)
printf("x^%d", b[k1]);
else
printf("%dx^%d",b[k1+1],b[k1]);
k1+=2;
while (k1<2*n)
{
printf("+%dx^%d", b[k1+1],b[k1]);
k1+=2;
}
i=0;
j=0;
k=0;
while (m>0 && n>0)
{
if (a[i]==b[j])
{
c[k+1]=a[i+1]+b[j+1];
c[k]=a[i];
m--;
n--;
i+=2;
j+=2;
}
else if (a[i]>b[j])
{
c[k+1]=a[i+1];
c[k]=a[i];
m--;
i+=2;
}
else
{
c[k+1]=b[j+1];
c[k]=b[j];
n--;
j+=2;
}
k+=2;
}
while (m>0)
{
c[k+1]=a[i+1];
c[k]=a[i];
k+=2;
i+=2;
m--;
}
while (n>0)
{
c[k+1]=b[j+1];
c[k]=b[j];
k+=2;
j+=2;
n--;
}
printf("\n\n\n\n\tSum of the two polynomials is:");
k1=0;
if (c[k1+1]==1)
printf("x^%d", c[k1]);
else
printf("%dx^%d", c[k1+1],c[k1]);
k1+=2;
while (k1<k)
{
if (c[k1+1]==1)
printf("+x^%d", c[k1]);
else
printf("+%dx^%d", c[k1+1], c[k1]);
k1+=2;
}
getch();
return 0;
}

RESULT:

Thus the program in C to convert given infix expression in to postfix


notation executed and verified .
EX.NO:23 CONVERSION OF INFIX EXPRESSION TO

DATE : POSTFIX NOTATION

AIM:
To write program in C to convert given infix expression to postfix notation

ALGORITHM:
STEP 1:Get an infix expression.
STEP 2: Scan the expression from left to right.
STEP 3:If any operands come display it.
STEP 4:If the incoming symbol in a operator and has more priority then the
symbol into the stack.
STEP 5:If the incoming operator has less priority than the stack symbol then copy
the symbol at the top of the stack and then print until the condition
becomes false and push the following operator on the stack.
STEP 6:If the symbol is ‘)’ then copy operators from top of the stack.
Deletion opening parenthesis is from top of the stack.
STEP 7:Stop the process.

PROGRAM:
#include<stdio.h>
char stack[20];
int top = -1;
void push(char x)
{
stack[++top] = x;
}
char pop()
{
if(top == -1)
return -1;
else
return stack[top--];
}
int priority(char x)
{
if(x == '(')
return 0;
if(x == '+' || x == '-')
return 1;
if(x == '*' || x == '/')
return 2;
}
main()
{
char exp[20];
char *e, x;
printf("Enter the expression :: ");
scanf("%s",exp);
e = exp;
while(*e != '\0')
{
if(isalnum(*e))
printf("%c",*e);
else if(*e == '(')
push(*e);
else if(*e == ')')
{
while((x = pop()) != '(')
printf("%c", x);
}
else
{
while(priority(stack[top]) >= priority(*e))
printf("%c",pop());
push(*e);
}
e++;
}
while(top != -1)
{
printf("%c",pop());
}}
RESULT:

Thus the program in C to convert given infix expression to postfix


notation executed and verified .
EX.NO:24 IMPLEMENTATION BINARY TREE AND

DATE : OPERATIONS OF BINARY TREES

AIM:
To write a C program Implementation Binary Tree And Operations Of Binary
Trees.
ALGORITHM:
STEP 1 :Start from root.
STEP 2: Compare the inserting element with root, if less than root, then recurse
for left, else recurse for right.
STEP 3: If element to search is found anywhere, return true, else return false

PROGRAM :
#include<stdio.h>
#include<stdlib.h>
struct tree
{
int data;
struct tree *left;
struct tree *right;
} *root = NULL, *node = NULL, *temp = NULL;

struct tree* insert(int key,struct tree *leaf)


{
if(leaf == 0)
{
struct tree *temp;
temp = (struct tree *)malloc(sizeof(struct tree));
temp->data = key;
temp->left = 0;
temp->right = 0;
printf("Data inserted!\n");
return temp;
}
else
{
if(key < leaf->data)
leaf->left = insert(key,leaf->left);
else
leaf->right = insert(key,leaf->right);
}
return leaf;
}
struct tree* search(int key,struct tree *leaf) {
if(leaf != NULL)
{
if(key == leaf->data)
{
printf("Data found!\n");
return leaf;
}
else
{
if(key < leaf->data)
return search(key,leaf->left);
else
return search(key,leaf->right);
}
}
else {
printf("Data not found!\n");return NULL;
}}
struct tree* minvalue(struct tree *node)
{
if(node == NULL)
return NULL;
if(node->left)
return minvalue(node->left);
else
return node;
}
/* Function for find maximum value from the Tree */
struct tree* maxvalue(struct tree *node)
{
if(node == NULL)
return NULL;
if(node->right)
return maxvalue(node->right);
else
return node;
}
void preorder(struct tree *leaf)
{
if(leaf == NULL)
return;
printf("%d\n",leaf->data);
preorder(leaf->left);
preorder(leaf->right);
}
void inorder(struct tree *leaf)
{
if(leaf == NULL)
return;
preorder(leaf->left);
printf("%d\n",leaf->data);
preorder(leaf->right);
}
void postorder(struct tree *leaf)
{
if(leaf == NULL)
return;
preorder(leaf->left);
preorder(leaf->right);
printf("%d\n",leaf->data);
}
struct tree* delete(struct tree *leaf, int key)
{
if(leaf == NULL)
printf("Element Not Found!\n");
else if(key < leaf->data)
leaf->left = delete(leaf->left, key);
else if(key > leaf->data)
leaf->right = delete(leaf->right, key);
else
{
if(leaf->right && leaf->left)
{
temp = minvalue(leaf->right);
leaf->data = temp->data;
leaf->right = delete(leaf->right,temp->data);
}
else {
temp = leaf;
if(leaf->left == NULL)
leaf = leaf->right;
else if(leaf->right == NULL)
leaf = leaf->left;
free(temp);
printf("Data delete successfully!\n");
}
}
}
int main()
{
int key, choice;

while(choice != 7)
{
printf("1. Insert\n2. Search\n3. Delete\n4. Display\n5. Min Value\n6.
Max Value\n7. Exit\n");
printf("Enter your choice:\n");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("\nEnter the value to insert:\n");
scanf("%d", &key);
root = insert(key, root);
break;
case 2:
printf("\nEnter the value to search:\n");
scanf("%d", &key);
search(key,root);
break;
case 3:
printf("\nEnter the value to delete:\n");
scanf("%d", &key);
delete(root,key);
break;
case 4:
printf("Preorder:\n");
preorder(root);
printf("Inorder:\n");
inorder(root);
printf("Postorder:\n");
postorder(root);
break;
case 5:
if(minvalue(root) == NULL)
printf("Tree is empty!\n");
else
printf("Minimum value is %d\n", minvalue(root)->data);
break;
case 6:
if(maxvalue(root) == NULL)
printf("Tree is empty!\n");
else
printf("Maximum value is %d\n", maxvalue(root)->data);
break;
case 7:
printf("Bye Bye!\n");
exit(0);
break;
default:
printf("Invalid choice!\n");
}
}
return 0;
}
}

RESULT :

Thus the program in C is implementated Binary Tree and Operations of


Binary Trees executed and verified .
EX. NO:2 5
DATE : IMPLEMENTATION OF BINARY SEARCH TREES

AIM:
To write a C program to implementation of binary search tree.

ALGORITHM:
STEP 1 :Declare function create (), search (), delete (), Display ().
STEP 2:Create a structure for a tree contains left pointer and right pointer.
STEP 3:Insert an element is by checking the top node and the leaf node and
the operation will be performed.
STEP 4:Deleting an element contains searching the tree and deleting the
item.
STEP 5 :Display the Tree elements.

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<alloc.h>

struct tree
{
int data;
struct tree *lchild;
struct tree *rchild;
}*t,*temp;
int element;
void inorder(struct tree *);
void preorder(struct tree *);
void postorder(struct tree *);
struct tree * create(struct tree *, int);
struct tree * find(struct tree *, int);
struct tree * insert(struct tree *, int);
struct tree * del(struct tree *, int);
struct tree * findmin(struct tree *);
struct tree * findmax(struct tree *);
void main()
{
int ch;
do
{
printf("\n\t\t\tBINARY SEARCH TREE");
printf("\n\t\t\t****** ****** ****");
printf("\nMain Menu\n");
printf("\n1.Create\n2.Insert\n3.Delete\n4.Find\n5.FindMin\n6.FindMax");
printf("\n7.Inorder\n8.Preorder\n9.Postorder\n10.Exit\n");
printf("\nEnter ur choice :");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter the data:");
scanf("%d",&element);
t=create(t,element);
inorder(t);
break;
case 2:
printf("\nEnter the data:");
scanf("%d",&element);
t=insert(t,element);
inorder(t);
break;
case 3:
printf("\nEnter the data:");
scanf("%d",&element);
t=del(t,element);
inorder(t);
break;
case 4:
printf("\nEnter the data:");
scanf("%d",&element);
temp=find(t,element);
if(temp->data==element)
printf("\nElement %d is at %d",element,temp);
else
printf("\nElement is not found");
break;
case 5:
temp=findmin(t);
printf("\nMax element=%d",temp->data);
break;
case 6:
temp=findmax(t);
printf("\nMax element=%d",temp->data);
break;
case 7:
inorder(t);
break;

case 8:
preorder(t);
break;
case 9:
postorder(t);
break;
case 10:
exit(0);
}
}
while(ch<=10);
}
struct tree * create(struct tree *t, int element)
{
t=(struct tree *)malloc(sizeof(struct tree));
t->data=element;
t->lchild=NULL;
t->rchild=NULL;
return t;
}
struct tree * find(struct tree *t, int element)
{
if(t==NULL)
return NULL;
if(element<t->data)
return(find(t->lchild,element));
else
if(element>t->data)
return(find(t->rchild,element));
else
return t;
}
struct tree *findmin(struct tree *t)
{
if(t==NULL)
return NULL;
else
if(t->lchild==NULL)
return t;
else
return(findmin(t->lchild));
}
struct tree *findmax(struct tree *t)
{
if(t!=NULL)
{
while(t->rchild!=NULL)
t=t->rchild;
}
return t;
}
struct tree *insert(struct tree *t,int element)
{
if(t==NULL)
{
t=(struct tree *)malloc(sizeof(struct tree));
t->data=element;
t->lchild=NULL;
t->rchild=NULL;
return t;
}
else
{
if(element<t->data)
{
t->lchild=insert(t->lchild,element);
}
else
if(element>t->data)
{
t->rchild=insert(t->rchild,element);
}
else
if(element==t->data)
{
printf("element already present\n");
}
return t;
}
}
struct tree * del(struct tree *t, int element)
{
if(t==NULL)
printf("element not found\n");
else
if(element<t->data)
t->lchild=del(t->lchild,element);
else
if(element>t->data)
t->rchild=del(t->rchild,element);
else
if(t->lchild&&t->rchild)
{
temp=findmin(t->rchild);
t->data=temp->data;
t->rchild=del(t->rchild,t->data);

}
else
{
temp=t;
if(t->lchild==NULL)
t=t->rchild;
else
if(t->rchild==NULL)
t=t->lchild;
free(temp);
}
return t;
}
void inorder(struct tree *t)
{
if(t==NULL)
return;
else
{
inorder(t->lchild);
printf("\t%d",t->data);
inorder(t->rchild);
}
}
void preorder(struct tree *t)
{
if(t==NULL)
return;
else
{
printf("\t%d",t->data);
preorder(t->lchild);
preorder(t->rchild);
}
}
void postorder(struct tree *t)
{
if(t==NULL)
return;
else
{
postorder(t->lchild);
postorder(t->rchild);
printf("\t%d",t->data);
}
}

RESULT:

Thus the C program for binary search tree was created, executed and
output was verified successfully.
EX.NO:26 IMPLEMENTATION OF SEARCHING
DATE : ALGORITHMS LINEAR SEARCH AND
BINARY SEARCH
AIM:
To write a C Program to implement different searching techniques –
Linear and Binary search
ALGORITHM:
Linear Search:
STEP 1:Read the search element from the user
STEP 2:Compare, the search element with the first element in the list.
STEP 3:If both are matching, then display "Given element found!!!" and
terminate the function
STEP 4:If both are not matching, then compare search element with the next
element in the list.
STEP 5:Repeat steps 3 and 4 until the search element is compared with the
last element in the list.
STEP 6:If the last element in the list is also doesn't match, then display
"Element not found!!!" and terminate the function.
Binary search is implemented using following steps...
STEP1:Read the search element from the user
STEP 2:Find the middle element in the sorted list
STEP 3:Compare, the search element with the middle element in the sorted
list. .
STEP 4:If both are matching, then display "Given element found!!!" and
terminate the function
STEP 5:If both are not matching, then check whether the search element is
smaller or larger than middle element.
STEP 6:If the search element is smaller than middle element, then repeat
steps 2, 3, 4 and 5 for the left sublist of the middle element.
STEP 7:If the search element is larger than middle element, then repeat
steps 2, 3, 4 and 5 for the right sublist of the middle element.
STEP 8:Repeat the same process until we find the search element in the list
or until sublist contains only one element.
STEP 9:If that element also doesn't match with the search element, then
display "Element not found in the list!!!" and terminate the function.

PROGRAM :
#include <stdio.h>
void sequential_search(int array[], int size, int n)
{
int i;
for (i = 0; i < size; i++)
{
if (array[i] == n)
{
printf("%d found at location %d.\n", n, i+1);
break;
}
}
if (i == size)
printf("Not found! %d is not present in the list.\n", n);
}
void binary_search(int array[], int size, int n)
{
int i, first, last, middle;
first = 0;
last = size - 1;
middle = (first+last) / 2;
while (first <= last) {
if (array[middle] < n)
first = middle + 1;
else if (array[middle] == n) {
printf("%d found at location %d.\n", n, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last) / 2;
}
if ( first > last )
printf("Not found! %d is not present in the list.\n", n);

}
int main()
{
int a[200], i, j, n, size;
printf("Enter the size of the list:");
scanf("%d", &size);
printf("Enter %d Integers in ascending order\n", size);
for (i = 0; i < size; i++)
scanf("%d", &a[i]);
printf("Enter value to find\n");
scanf("%d", &n);
printf("Sequential search\n");
sequential_search(a, size, n);
printf("Binary search\n");
binary_search(a, size, n);
return 0;
}

RESULT :

Thus the C Program to implement different searching techniques – Linear


and Binary search executed and verified .
EX.NO:27 MERGE SORT

DATE:

AIM:
To write a C program to implement the concept of merge sort.
ALGORITHM:
STEP 1:Start.
STEP 2:First you divide the number of elements by 2 and seperate them as
two.
STEP 3:Divide those two which are divided by 2.
STEP 4:Divide them until you get a single element.
STEP 5:Start comparing the starting two pair of elements with each other
and place them in ascending order.
STEP 6:When you combine them compare them so that you make sure they
are sorted.
STEP 7:When all the elements are compared the array will be surely sorted
in an ascending order.
STEP 8:Stop.

PROGRAM:
#include<stdio.h>
#include<conio.h>
void merge(int [],int ,int ,int );
void part(int [],int ,int );
void main(){
int arr[30];
int i,size;
printf("\n\t------- Merge sorting method ------- \n\n");
printf("Enter total no. of elements : ");
scanf("%d",&size);
for(i=0; i<size; i++){
printf("Enter %d element : ",i+1);
scanf("%d",&arr[i]);
}
part(arr,0,size-1);
printf("\n\t------- Merge sorted elements \n\n");
for(i=0; i<size; i++)
printf("%d ",arr[i]);
getch();
}
void part(int arr[],int min,int max){
int mid;
if(min<max){
mid=(min+max)/2;
part(arr,min,mid);
part(arr,mid+1,max);
merge(arr,min,mid,max);}}
void merge(int arr[],int min,int mid,int max){
int tmp[30];
int i,j,k,m;
j=min;
m=mid+1;
for(i=min; j<=mid && m<=max ; i++){
if(arr[j]<=arr[m]){
tmp[i]=arr[j];
j++;}
else{
tmp[i]=arr[m];
m++;
}}
if(j>mid){
for(k=m; k<=max; k++){
tmp[i]=arr[k];
i++;
}}
else{
for(k=j; k<=mid; k++){
tmp[i]=arr[k];
i++;
}}
for(k=min; k<=max; k++)
arr[k]=tmp[k];
}
RESULT:

Thus a C program for the concept of merge sort was implemented


executed and verified successfully.
EX.NO :28 QUICK SORT

DATE:

AIM:
To write a C program to implement the concept of Quick sort.
ALGORITHM:
STEP 1:Start.
STEP 2:Choose any element of the array to be the pivot.
STEP 3:Divide all other elements (except the pivot) into two partitions.
All elements less than the pivot must be in the first partition. All
elements greater than the pivot must be in the second partition.

STEP 4:Use recursion to sort both partitions.


STEP 5:Join the first sorted partition, the pivot, and the second sorted
partition.
STEP 6:Stop

PROGRAM:
#include<stdio.h>
#include<conio.h>
void qsort(int arr[20], int fst, int last);
void main(){
int arr[30];
int i,size;
printf("Enter total no. of the elements : ");
scanf("%d",&size);
printf("Enter total %d elements : \n",size);
for(i=0; i<size; i++)
scanf("%d",&arr[i]);
qsort(arr,0,size-1);
printf("Quick sorted elements are as : \n");
for(i=0; i<size; i++)
printf("%d\t",arr[i]);
getch();}
void qsort(int arr[20], int fst, int last){
int i,j,pivot,tmp;
if(fst<last){
pivot=fst;
i=fst;
j=last;
while(i<j){
while(arr[i]<=arr[pivot] && i<last)
i++;
while(arr[j]>arr[pivot])
j--;
if(i<j){
tmp=arr[i];
arr[i]=arr[j];
arr[j]=tmp;}}
tmp=arr[pivot];
arr[pivot]=arr[j];
arr[j]=tmp;
qsort(arr,fst,j-1);
qsort(arr,j+1,last);
}}

RESULT:

Thus the C program to implement the concept of Quick sort executed and
verified .
EX.NO : 29 HASHING TECHNIQUES

DATE:

AIM:

To write a C program to implement hash table.

ALGORITHM:

STEP 1:Create a structure, data (hash table item) with key and value as data.

STEP 2:Now create an array of structure, data of some certain size (10, in
this case). But, the size of array must be immediately updated to a
prime number just greater than initial array capacity (i.e 10, in this
case).

STEP 3:A menu is displayed on the screen.

STEP 4:User must choose one option from four choices given in the menu

STEP 5:Perform all the operations

STEP 6:Stop the program

PROGRAM :

#include<stdio.h>
#include<stdlib.h>
struct data
{
int key;
int value;
};
struct data *array;
int capacity = 10;
int size = 0;
/* this function gives a unique hash code to the given key */
int hashcode(int key)
{
return (key % capacity);
}
/* it returns prime number just greater than array capacity */
int get_prime(int n)
{
if (n % 2 == 0)
{
n++;
}
for (; !if_prime(n); n += 2);
return n;
}
/* to check if given input (i.e n) is prime or not */
int if_prime(int n)
{
int i;
if ( n == 1 || n == 0)
{
return 0;
}
for (i = 2; i < n; i++)
{
if (n % i == 0)
{
return 0;}}
return 1;
}
void init_array()
{
int i;
capacity = get_prime(capacity);
array = (struct data*) malloc(capacity * sizeof(struct data));
for (i = 0; i < capacity; i++)
{
array[i].key = 0;
array[i].value = 0;
}}
/* to insert a key in the hash table */
void insert(int key)
{
int index = hashcode(key);
n++;
}
for (; !if_prime(n); n += 2);
return n;
}
/* to check if given input (i.e n) is prime or not */
int if_prime(int n)
{
int i;
if ( n == 1 || n == 0)
{
return 0;
}
for (i = 2; i < n; i++)
{
if (n % i == 0)
{
return 0;}}
return 1;
}
void init_array()
{
int i;
capacity = get_prime(capacity);
array = (struct data*) malloc(capacity * sizeof(struct data));
for (i = 0; i < capacity; i++)
{
array[i].key = 0;
array[i].value = 0;
}}
/* to insert a key in the hash table */
void insert(int key)
{
int index = hashcode(key);
{
int i;for (i = 0; i < capacity; i++)
{
if (array[i].value == 0)
{
printf("\n Array[%d] has no elements \n");
}
else
{
printf("\n array[%d] has elements -:\n key(%d) and value(%d) \t", i,
array[i].key,
array[i].value);
}}}
int size_of_hashtable()
{
return size;
}
void main()
{
int choice, key, value, n, c;
init_array();
do {
printf("\n Implementation of Hash Table in C \n\n");
printf("MENU-: \n1.Inserting item in the Hash Table"
"\n2.Removing item from the Hash Table"
"\n3.Check the size of Hash Table"
"\n4.Display a Hash Table"
"\n\n Please enter your choice -:");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Inserting element in Hash Table\n");
printf("Enter key -:\t");
scanf("%d", &key);
insert(key);
break;
case 2:
printf("Deleting in Hash Table \n Enter the key to delete-:");
scanf("%d", &key);
remove_element(key);
break;
case 3:
n = size_of_hashtable();
printf("Size of Hash Table is-:%d\n", n);
break;
case 4:
display();
break;
default:
printf("Wrong Input\n");}
printf("\n Do you want to continue-:(press 1 for yes)\t");
scanf("%d", &c);
}while(c == 1);
getch();}

RESULT:

Thus the C program to implemented Hash Table executed and verified .


EX NO :30
DATE :

IMPLEMENTETION OF INSERTION SORT

AIM:

To write a c program to implementetion the concept of insertion sort.

ALGORITHM:

STEP 1:Start.
STEP 2:
STEP 3:

STEP 4:
STEP 5:

STEP 6:

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

void main()

int A[10],n,i;
void insert_sort(int A[10],int n);
clrscr();
printf(“\n\t\t insertion sort”);
printf(“\n how many elemants are there?”);
scanf(“%d”,&n);
printf(“\n enter the elements\n”);
fpr(i=0;i<n;i++)
scanf(“%d”,&A[i]);
insert_sort(A,n);
getch();
}
void insert_sort(int A[10],int n)
{
int i,j,temp;
for(i=1;i<=n-1;i++)
{
temp=A[i];
j=i-1;
while(j>=0)&&(A[j]>temp))
{
A[j+1]=A[j];
j=j-1;
}
A[j+1]=temp;
}
printf(“\n the sorted list of elements is...\n”);
for(i=0;i<n;i++)
printf(“\n%d”,A[i]);
}
RESULT:

Thus the C program to implement the concept of insertion sort executed and
verified .
RESULT:
Thus the above c program was Executed verified.

You might also like