Cs3362 CD Lab Manual Final Copy-1
Cs3362 CD Lab Manual Final Copy-1
STRUCTURES LABORATORY
(Regulations 2021)
NAME:
REG.NO:
PROG:
YEAR/SEM:
INDEX
0 0 3 1.5
COURSE OBJECTIVES:
• To develop applications in C
• To implement linear and non-linear data structures
• To understand the different operations of search trees
• To get familiarized to sorting and searching algorithms
LIST OF EXPERIMENTS
TOTAL: 45 PERIODS
COURSE OUTCOMES:
CO2 Write functions to implement linear and non-linear data structure operations
CO3 Suggest and use the appropriate linear / non-linear data structure operations for a given
problem
CO4 Apply appropriate hash functions that result in a collision free scenario for data storage and
Retrieval
CO5 Implement Sorting and searching algorithms for a given application
MAPPING OF COs WITH POs AND PSOs
v) Fibonacci Series
vi) Armstrong Number
vii) Largest Of Three Numbers
viii) Sum Of First N Natural Numbers.
ix) Area And Circumference Of A Circle.
2. (a) PROGRAM USING FUNCTION
i) Factorial of a given number using Recursive function
ii)
Pascal triangle
2.b)
PROGRAM USING ARRAYS
i) Addition of two matrices.
ii) Matrix Multiplication using two dimensional arrays.
iii) Transpose of a matrix.
3.a) PROGRAM USING POINTERS
Swapping of two numbers using pass by value and references.
3.b) PROGRAM USING STRUCTURES
Student details
4 PROGRAM USING FILE
i) Copies one file to another.
5. REAL TIME APPLICATION OF C
Payroll application
6. Array implementation of List ADT
7. ARRAY IMPLEMENTATION
AIM:
To execute a simple calculator programme using switch case statement in c language.
ALGORITHM:
STEP-1: Start the program.
STEP-2: Prompt the user to enter an operator (+, -, *, /).
STEP-3: Read the operator input.
STEP-4: Prompt the user to enter two operands (numbers).
STEP-5: Read the two operands.
STEP-6: Use a switch statement to determine the operation to perform based on the input operator.
STEP-7: Perform the corresponding arithmetic operation (addition, subtraction, multiplication, division).
STEP-8: Print the result of the operation in the format "operand1 operator operand2 = result".
STEP-9: Stop the program.
PROGRAM CODE:
#include <stdio.h>
int main()
{
char operation;
double n1, n2;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &operation);
printf("Enter two operands: ");
scanf("%lf %lf",&n1, &n2);
switch(operation)
{
case '+':
printf("%.1lf + %.1lf = %.1lf",n1, n2, n1+n2);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf",n1, n2, n1-n2);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf",n1, n2, n1*n2);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf",n1, n2, n1/n2);
break;
default:
printf("Error! operator is not correct");
}
return 0;
}
OUTPUT:
RESULT:
Thus the C program to use simple calculator using switch case statement was written and executed successfully.
EX.NO:1)ii)
GIVEN STRING IS PALINDROME OR NOT
DATE:
AIM:
To write a C program to check whether the given string is palindrome or not.
ALGORITHM;
PROGRAM CODE:
#include <stdio.h>
#include <string.h>
int main()
{
char string1[20];
int i, length;
int flag = 0;
printf("Enter a string: ");
scanf("%s", string1);
length = strlen(string1);
for (i = 0; i < length / 2; i++)
{
if (string1[i] != string1[length - i - 1])
{
flag = 1;
break;
}
}
if (flag)
{
printf("%s is not a palindrome\n", string1);
}
else
{
printf("%s is a palindrome\n", string1);
}
return 0;
}
OUTPUT:
RESULT:
Thus the C program to find whether the given string is palindrome or not was written executed and
output verified successfully.
EX.NO:1)iii) PRIME OR COMPOSITE NUMBER
DATE:
AIM:
To write a C program to find whether the given number is prime or composite number using decision
making statements.
ALOGORITHM:
#include<stdio.h>
int main()
{
int i,n,c=0;
printf("Enter a number:");
scanf("%d",&n);
if(n==1||n==0)
c=0;
else
for(i=2;i<=n;i++)
if(n%i==0)
c=c+1;
if(c==0)
printf("Neither prime nor composite number");
else if(c==1)
printf("%d is prime number",n);
else
printf("%d is composite number",n);
return 0;
}
OUTPUT:
RESULT:
Thus, the above C program to find whether a given number is prime or composite number using
decision making statements written executed and output verified successfully.
EX.NO:1)iv) ROOTS OF THE QUADRATIC EQUATION
DATE:
AIM:
To calculate to roots of a quadratic using c program.
ALGORITHM:
PROGRAM CODE:
#include <math.h>
#include <stdio.h>
int main()
{
double a, b, c, discriminant, root1, root2, realPart, imagPart;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf", &a, &b, &c);
discriminant = b * b - 4 * a * c;
if (discriminant > 0)
{
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("root1 = %.2lf and root2 = %.2lf", root1, root2);
}
else if (discriminant == 0)
{
root1 = root2 = -b / (2 * a);
printf("root1 = root2 = %.2lf;", root1);
}
else {
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imagPart, realPart, imagPart);
}
return 0;
}
OUTPUT:
RESULT:
Thus, the C program to find the root of the quadratic equation was written executed and output
verified successfully.
EX.NO:1)v) FIBONACCI SERIES
DATE:
AIM:
To generate the Fibonacci series using C program.
ALGORITHM:
STEP-1: Start the program.
STEP-2: Ask the user to input the number of terms in the Fibonacci series.
STEP-3: Initialize the first two terms, t1 and t2, to 0 and 1, respectively.
STEP-4: Calculate the next term in the series by adding t1 and t2.
STEP-5: Print the first two terms, followed by the next term.
STEP-6: Repeat steps 3-4 until the desired number of terms is reached, updating t1 and t2 to the last two terms in
the series.
STEP-7: Output the Fibonacci series up to the given number of terms.
STEP-8: Stop the program.
PROGRAM CODE:
#include <stdio.h>
int main()
{
int i, n;
int t1 = 0, t2 = 1;
int nextTerm = t1 + t2;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: %d, %d, ", t1, t2);
for (i = 3; i <= n; ++i)
{
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
return 0;
}
OUTPUT:
RESULT:
Thus the C program to find Fibonacci series was written and executed successfully.
EX.NO: 1)vi) ARMSTRONG NUMBER
DATE:
AIM:
To find the Armstrong number using C program.
ALGORITHM:
PROGRAM CODE:
#include <stdio.h>
#include <math.h>
int main()
{
int originalNum, num, lastDigit, digits, sum;
printf("Enter any number to check Armstrong number: ");
scanf("%d", &num);
sum = 0;
originalNum = num;
digits = (int) log10(num) + 1;
while(num > 0)
{
lastDigit = num % 10;
sum = sum + round(pow(lastDigit, digits));
num = num / 10;
}
if(originalNum == sum)
{
printf("%d is ARMSTRONG NUMBER", originalNum);
}
else
{
printf("%d is NOT ARMSTRONG NUMBER", originalNum);
}
return 0;
}
OUTPUT:
RESULT:
Thus the C program to find the Armstrong number was written and executed and output verified
successfully.
EX.NO: 1)vii) LARGEST OF THREE NUMBERS
DATE:
AIM:
ALGORITHM:
PROGRAM CODE:
#include <stdio.h>
int main()
{
double n1, n2, n3;
printf("Enter three numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
if (n1 >= n2 && n1 >= n3)
printf("%.2lf is the largest number.", n1);
else if (n2 >= n1 && n2 >= n3)
printf("%.2lf is the largest number.", n2);
else
printf("%.2lf is the largest number.", n3);
return 0;
}
OUTPUT:
RESULT:
Thus the C program to find the largest among the three numbers was written and executed successfully.
EX.NO:1)viii) SUM OF FIRST N NATURAL NUMBERS
DATE:
AIM:
To calculate the sum of first N natural numbers using C program.
ALGORITHM:
STEP-1: Start the program.
STEP-2: Define a function sum that takes an integer n as input.
STEP-3: Inside the sum function, initialize a variable add to 0.
STEP-4: Use a loop to iterate from 1 to n, adding each number to add.
STEP-5: Return the final value of add as the sum.
STEP-6: In the main function, ask the user for the upper limit of the range.
STEP-7: Call the sum function with the user's input and store the result.
STEP-8: Print the sum formula and result.
STEP-9: Stop the program.
PROGRAM CODE:
#include<stdio.h>
int sum(int n)
{
int add = 0;
for(int i=1; i<=n; i++)
{
add += i;
}
return add;
}
int main()
{
int range, result;
printf("Upto which number you want to find sum: ");
scanf("%d", &range);
result = sum(range);
printf("1+2+3+….+%d+%d = %d",range-1, range, result);
}
OUTPUT:
Upto which number you want to find sum: 7
1+2+3+….+6+7 = 28
RESULT:
Thus the C program to find the sum of n natural numbers was written and executed successfully.
EX.NO:1)ix) AREA AND CIRCUMFERENCE OF A CIRCLE
DATE:
AIM:
To determine the area and circumference of the circle using C program.
ALGORITHM:
STEP-1: Start the program.
STEP-2: Define the value of pi (approximately 3.14).
STEP-3: Ask the user to input the radius of the circle.
STEP-4: Calculate the area of the circle using the formula A = πr^2.
STEP-5: Calculate the circumference of the circle using the formula C = 2πr.
STEP-6: Print the area and circumference of the circle.
STEP-7: Stop the program.
PROGRAM CODE:
#include <stdio.h>
int main()
{
int radius;
float PI = 3.14, area, circumference;
printf("Enter the radius of circle: ");
scanf("%d", &radius);
area = PI * radius * radius;
printf("The Area of circle is: %f", area);
circumference = 2 * PI * radius;
printf("\nThe Circumference of circle is: %f", circumference);
return 0;
}
OUTPUT:
RESULT:
Thus the C program to find the area and circumference of the circle was written and executed successfully.
EX.NO:2)a)i) PROGRAM USING FUNCTION
DATE: FACTORIAL OF A GIVEN NUMBER USING RECURSIVE FUNCTION
AIM:
To determine the factorial of a given number using recursive function in C program.
ALGORITHM:
STEP-1: Start the program.
STEP-2: Define a recursive function multiplyNumbers that takes an integer n as input.
STEP-3: If n is greater than or equal to 1, the function calls itself with n-1 and multiplies the result by n.
STEP-4: If n is less than 1, the function returns 1 (the base case).
STEP-5: Ask the user to input a positive integer n.
STEP-6: Call the multiplyNumbers function with n to calculate the factorial.
STEP-7: Print the factorial of n.
STEP-8: Stop the program.
PROGRAM CODE:
#include<stdio.h>
long int multiplyNumbers(int n);
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;
}
RESULT:
Thus the C program to find the factorial of a number using recursive function was written and executed
successfully.
EX.NO:2)a)ii) PASCAL TRIANGLE
DATE:
AIM:
To generate pascals triangle for a given row using C program.
ALGORITHM:
STEP-1: Start the program.
STEP-2: Define a function generate Pascals Triangle that takes the number of rows as input.
STEP-3: For each row, print leading spaces for alignment.
STEP-4: Calculate and print the coefficients for each row using the formula.
STEP-5: Update the coefficient for the next iteration.
STEP-6: Print a newline character after each row.
STEP-7: In the main function, define the number of rows.
STEP-8: Call the generate Pascals Triangle function with the number of rows.
STEP-9: Output Pascal's Triangle up to the given number of rows.
STEP-10: Stop the program.
PROGRAM CODE:
#include <stdio.h>
void generatePascalsTriangle(int rows)
{
for (int i = 0; i < rows; i++)
{
int coefficient = 1;
for (int j = 0; j < rows - i - 1; j++)
{
printf(" ");
}
for (int j = 0; j <= i; j++)
{
printf("%6d", coefficient);
coefficient = coefficient * (i - j) / (j + 1);
}
printf("\n");
}
}
int main() {
int numRows = 5;
generatePascalsTriangle(numRows);
return 0;
}
OUTPUT:
RESULT:
DATE:
ADDITION OF TWO MATRICES
AIM:
ALGORITHIM:
STEP-2: Ask the user to input the elements of the first 3x3 matrix (matrix A).
STEP-3: Ask the user to input the elements of the second 3x3 matrix (matrix B).
STEP-6: For each element, add the corresponding elements of matrices A and B and store the result in
matrix C.
PROGRAM CODE:
#include <stdio.h>
#include <conio.h>
int main()
{
int a[3][3], b[3][3], c[3][3], i, j;
printf("Enter the elements of 3*3 matrix a \n"); for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
scanf("%d", &a[i][j]);
}
}
printf("Enter the elements of 3*3 matrix b \n"); for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
scanf("%d", &b[i][j]);
}
}
for(i = 0; i < 3; i++)
}
for(j = 0; j < 3; j++)
{
c[i][j] = a[i][j] + b[i][j];
}
}
printf("The resultant 3*3 matrix c is \n"); for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
printf("%d\t", c[i][j]);
}
printf("\n");
}
getch();
}
OUTPUT:
RESULT:
Thus the C program to add two matrices was written and executed was successfully.
EX.NO:2)b)ii)
MATRIX MULTIPLICATION USING TWO DIMENSIONAL ARRAYS.
DATE:
AIM:
To perform matrix multiplication using two dimensional arrays in C program.
ALGORITHM:
STEP-2: Ask the user to input the elements of the first 3x3 matrix (matrix A).
STEP-3: Ask the user to input the elements of the second 3x3 matrix (matrix B).
STEP-6: For each element, iterate through each element of the rows of matrix A and
columns of matrix B.
STEP-7: Multiply corresponding elements and add them to the result element.
PROGRAM CODE:
#include<stdio.h>
#include<conio.h>
int main()
{
int a[3][3], b[3][3], c[3][3], i, j, k;
printf("Enter the elements of 3*3 matrix a \n"); for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
scanf("%d", &a[i][j]);
}
}
printf("Enter the elements of 3*3 matrix b \n"); for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
scanf("%d", &b[i][j]);
}
}
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
c[i][j] = 0;
for(k = 0; k < 3; k++)
{
c[i][j] = c[i][j] + (a[i][k] * b[k][j]);
}
}
}
printf("The resultant 3*3 matrix c is \n"); for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
printf("%d\t", c[i][j]);
}
printf("\n");
}
OUTPUT:
RESULT:
Thus the C program to multiply the wo matrices was written and executed successfully.
EX.NO:2)b)iii)
TRANSPOSE OF A MATRIX
DATE:
AIM:
To transpose a matrix using array in C program.
ALGORITHM:
STEP-2: Ask the user to input the number of rows and columns.
PROGRAM CODE:
#include <stdio.h>
int main() {
int a[10][10], transpose[10][10], r, c;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
printf("\nEnter matrix elements:\n");
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
printf("\nEntered matrix: \n");
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
printf("%d ", a[i][j]);
if (j == c - 1)
printf("\n");
}
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
transpose[j][i] = a[i][j];
}
printf("\nTranspose of the matrix:\n");
for (int i = 0; i < c; ++i)
for (int j = 0; j < r; ++j) {
printf("%d ",
transpose[i][j]);
if (j == r - 1)
printf("\n");
}
return 0;
}
OUTPUT:
RESULT:
Thus the C program to find the transpose of the matrix was written and executed successfully.
EX.NO:3a PROGRAM USING POINTERS
SWAP TWO NUMBER USING PASS BY VALUE AND REFERENCES
DATE:
AIM:
To write a C program to swap two number using pass by value and references.
ALGORITHM:
Swap by Value Algorithm
PROGRAM CODE:
#include <stdio.h>
void swap_by_value(int a, int b) {
int temp = a;
a = b;
b = temp;
printf("Values inside swap_by_value: a = %d, b = %d\n", a, b);
}
void swap_by_reference(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
printf("Values inside swap_by_reference: a = %d, b = %d\n", *a, *b);
}
int main() {
int a = 10;
int b = 20;
printf("Original values: a = %d, b = %d\n", a, b);
swap_by_value(a, b);
printf("Values after swap_by_value: a = %d, b = %d\n", a, b);
swap_by_reference(&a, &b);
printf("Values after swap_by_reference: a = %d, b = %d\n", a, b);
return 0;
}
OUTPUT:
RESULT:
Thus the C program to swap two number using pass by value and references was executed successfully.
EX.NO: 3)b) PROGRAM USING STRUCTURES
DATE:
STUDENT DETAILS
AIM:
ALOGIRTHM:
#include<stdio.h>
struct student
{
char firstname[50];
int roll;
float marks;
}
s[5];
int main()
{
int i;
printf("Enter student details:\n");
for(i=0;i<3;i++)
{
s[i].roll=i+1;
printf("\nFor roll number %d\n",s[i].roll);
printf("Enter student name:");
scanf("%s",&s[i].firstname);
printf("Enter marks out of 100:");
scanf("%f",&s[i].marks);
}
printf("\n\nDisplaying Information:\n\n");
for(i=0;i<3;i++)
{
printf("\n\nrollnumber:%d,\n",i+1);
printf("student name");
puts(s[i].firstname);
printf("marks:%f",s[i].marks);
printf("\n");
}
return 0;
}
OUTPUT:
RESULT:
Thus the C program to display the student details using structure was written , executed and oiutput
verified successfully.
PROGRAM USING FILE
EX.NO:4)
COPIES ONE FILE TO ANOTHER
DATE:
AIM:
ALGORITHM:
PROGRAM CODE:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *sourceFile;
FILE *destFile;
char sourcePath[100];
char destPath[100];
char ch;
printf("Enter source file path: ");
scanf("%s", sourcePath);
printf("Enter destination file path: ");
scanf("%s", destPath);
* Open source file in 'r' and
* destination file in 'w' mode
sourceFile = fopen(sourcePath, "r");
destFile = fopen(destPath, "w");
if (sourceFile == NULL || destFile == NULL)
{
printf("\nUnable to open file.\n");
printf("Please check if file exists and you have read/write privilege.\n");
exit(EXIT_FAILURE);
}
ch = fgetc(sourceFile);
while (ch != EOF)
{
fputc(ch, destFile);
ch = fgetc(sourceFile);
}
printf("\nFiles copied successfully.\n");
fclose(sourceFile);
fclose(destFile);
return 0;
}
OUTPUT:
RESULT:
Thus the C program to copy the files was written and executed successfully.
EX.NO:5 REAL TIME APPLICATION OF C
DATE PAYROLL APPLICATION
AIM:
ALGORITHM:
STEP-1. Define a structure Employee to represent an employee's data, including their ID, name, basic pay,
allowances(HRA, DA, PF, and LIC), gross salary, and net salary.
STEP-2. Input data for each employee, including their ID, name, and basic pay.
STEP-3. Calculate the allowances (HRA, DA, PF, and LIC) for each employee based on their basic pay.
- HRA = 15% of basic pay
- DA = 20% of basic pay
- PF = 10% of basic pay
- LIC = 8% of basic pay
STEP-4. Calculate the gross salary for each employee by adding their basic pay, HRA, and DA.
STEP-5. Calculate the net salary for each employee by subtracting their PF and LIC from their gross salary.
STEP-6. Print each employee's data, including their ID, name, basic pay, allowances (HRA, DA, PF, and LIC),
grosssalary, and net salary.
PROGRAM CODE:
#include <stdio.h>typedef struct {
int id;
char name[50];
float basicPay;
float hra;
float da;
float pf;
float lic;
float grossSalary;
float netSalary;
} Employee;
void calculateAllowances(Employee *emp) {emp->hra = 0.15 * emp->basicPay;
emp->da = 0.20 * emp->basicPay;
emp->pf = 0.10 * emp->basicPay;
emp->lic = 0.08 * emp->basicPay;
}
void calculateSalaries(Employee *emp) {
emp->grossSalary = emp->basicPay + emp->hra + emp->da;
emp->netSalary = emp->grossSalary - emp->pf - emp->lic;
}
i nt main() {
Employee employees[5];
for (int i = 0; i < 5; i++) {
printf("Enter ID: ");
scanf("%d", &employees[i].id);
printf("Enter Name: ");
scanf("%s", employees[i].name);
printf("Enter Basic Pay: ");
scanf("%f", &employees[i].basicPay);
calculateAllowances(&employees[i]);
calculateSalaries(&employees[i]);
printf("ID: %d\n", employees[i].id);
printf("Name: %s\n", employees[i].name);
printf("Basic Pay: %.2f\n", employees[i].basicPay);
printf("HRA: %.2f\n", employees[i].hra);
printf("DA: %.2f\n", employees[i].da);
printf("PF: %.2f\n", employees[i].pf);
printf("LIC: %.2f\n", employees[i].lic);
printf("Gross Salary: %.2f\n", employees[i].grossSalary);
printf("Net Salary: %.2f\n\n", employees[i].netSalary);
}
return 0;
}
UTPUT:
RESULT
Thus the C program to implement payroll application using structure was written and executed successfully.
EX.NO: 6) ARRAY IMPLEMENTATION OF LIST ADT
DATE:
AIM:
To write a C program to implement like ADT using array.
ALOGORITHM:
STEP-1: Start the program
STEP-2: Declare the variable a, b, n, p, e, f, i, pos, ch and g.
STEP-3: Display the option
STEP-4: Get a choice from the user.
STEP-5: Declare the switch(ch) statement to choose the given option.
STEP-6: When choice equals to 1 then read the number of nodes and get the elements of
list in a loop
STEP-7: When choice equals to 2 then read the position to delete the element when the
location is valid delete the element and display the element after deletion
STEP-8: When choice equals to 3 then read the elements to search, when elements is in the
list display position, otherwise display element not found
STEP-9: When choice equals to 4 then read a position to insert the element, when position
is not invalid get the element to insert in the particular position and display the
list elements.
STEP-10: When choice equals to 5 then display the list elements
STEP-11: When choice equals to 6 break the choice.
STEP-12: Otherwise display invalid choice.
STEP-13: Stop the program.
PROGRAM CODE:
#include<stdio.h>
#define MAX 10
void create();
void insert();
void deletion();
void search();
void display();
int a,b[20],n,p,e,f,i,pos,h;
int main()
{
int ch;
char g='y';
do
{
printf("\n1.Create\n2.Delete\n3.Search\n4.Insert\n5.Display\n6.Exit\n");
printf("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:
break;
default:
printf("\nEnter your choice:");
}
printf("\nDo you want to continue(Y/N)");
scanf("\n %c",&g);
}
while(g=='Y'||g=='y');
}
void create()
{
printf("\nEnter the number of nodes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the element=",i+1);
scanf("%d",&b[i]);
}
}
void deletion()
{
printf("\nEnter the position you want to delete:");
scanf("%d",&pos);
if(pos>=n)
{
printf("\nInvalid location");
}
else
{
for(i=pos+1;i<n;i++)
{
b[i-1]=b[i];
}
n--;
}
if(h==1)
{
printf("Value is in %d position",i);
}
else
{
printf("Value not found");
}
}
void insert()
{
printf("\nEnter the position to insert:");
scanf("%d",&pos);
if(pos>=n)
{
printf("Invalid location");
}
else
{
for(i=MAX-1;i>=pos-1;i--)
{
b[i+1]=b[i];
}
printf("\nEnter the element to insert:");
scanf("%d",&p);
b[pos]=p;
n++;
}
printf("\nThe list after insertion:");
display();
}
void display()
{
printf("\nThe elements of list ADT are:");
for(i=0;i<n;i++)
{
printf(" %d",b[i]);
}
}
OUTPUT:
RESULT:
Thus the above C program to implement list ADT using array was written, executed and output verified
successfully.
EX.NO: 7)i) ARRAY IMPLEMENTATION OF STACK ADT
DATE:
AIM:
#include<stdio.h>
int stack[10],choice,a,h,top,x,i,n;
void push();
void pop();
void display();
int main()
{
top=-1;
printf("\nEnter the size of stack:");
scanf("%d",&n);
printf("\nStack implementation using array\n");
do
{
printf("\n1.Push\n2.Pop\n3.Display\n4.Exit\n");
printf("\nEnter the choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
break;
default:
printf("\nInvalid Choice");
}
}
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{
printf("\nStack overflow");
}
else
{
printf("Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
printf("\nStack underflow");
}
else
{
printf("\nThe popped element is %d",stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\nElements in the stack=");
for(i=top;i>=0;i--)
{
printf("%d ",stack[i]);
}
}
else
{
printf("\nEmpty Stack\n");
}
}
OUTPUT:
RESULT:
Thus the above C program to implement stack ADT was written, executed
and output verifiedsuccessfully.
EX.NO: 7)ii) ARRAY IMPLEMENTATION OF QUEUE ADT
DATE:
AIM:
ALOGIRTHM:
STEP-1: Start the program
STEP-2: Declare choice
STEP-3: Display all the choices.
STEP-4: Get the choice from the user.
STEP-5: Call switch(choice)
STEP-6: Repeat step5 until the condition is false.
STEP-7: When choice equals to 1.
i) Get an element as add_item.
#include<stdio.h>
#define max 50
void insert();
void remove();
void display();
int queue_array[max];
int rear=-1;
int front=-1;
int main()
int choice;
while(1)
printf("4.quit\n");
scanf("%d",& choice);
switch(choice)
case 1:
insert();
break;
case 2:
remove();
break;
case 3:
display();
break;
case 4:
return 0;
break;
default:
void insert()
int add_item;
if(rear==max-1)
else
if(front==-1)
front=0;
scanf("%d",&add_item);
rear=rear+1;
queue_array[rear]=add_item;
void remove()
if(front==-1)
{
return;
else
if(front==rear)
front=-1;
rear=-1;
else
front=front+1;
void display()
int i;
if(front==-1)
printf("queue is empty\n");
else
printf("queue is \n");
for(i=front;i<=rear;i++)
printf("%d\t",queue_array[i]);
printf("\n");
OUTPUT:
RESULT:
Thus the C program to implement queue ADT was written, executed and output verified successfully.
EX.NO: 8)i) LINKED LIST IMPLEMENTATION OF LIST ADT
DATE:
AIM:
ALOGIRTHM:
STEP-1: Start the program.
STEP-2: Declare nodes and the variable data option
STEP-3: Display all the option and get an option from user
STEP-4: When option equals to 1,
• Read an element from the user
• Insert the element to the list
STEP-5: When option equals to 2,
• Read the position of element to delete
• Delete the element from the list
STEP-6: When option equals to 3,
• When count of element is zero, display empty list
• Display the number of elements in list
STEP-7: When option equals to 4,
• Display the elements in the list
STEP-8: Display all result
STEP-9: Stop the program.
PROGRAM CODE:
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
struct node
{
int value;
struct node *next;
};
void insert();
void delet();
void display();
int count();
typedef struct node data_node;
data_node *headnode,*firstnode,*tempnode=0,*prevnode,*nextnode;
int data;
int main()
{
int option=0;
while(option<5)
{
printf("\n1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.Count\n");
printf("5.Exit\n");
printf("\nEnter your option:");
scanf("%d",&option);
switch(option)
{
case 1:
insert();
break;
case 2:
delet();
break;
case 3:
display();
break;
case 4:
count();
break;
case 5:
return 0;
break;
default:
break;
}
}
return 0;
}
void insert()
{
printf("Enter element to insert:");
scanf("%d",&data);
printf("\n");
tempnode=(data_node*)malloc(sizeof(data_node));
tempnode->value=data;
if(firstnode==0)
firstnode=tempnode;
else
headnode->next=tempnode;
tempnode->next=0;
headnode=tempnode;
fflush(stdin);
}
void delet()
{
int countvalue,pos,i=0;
tempnode=firstnode;
while(tempnode!=0)
{
countvalue++;
tempnode=tempnode->next;
}
tempnode=firstnode;
if(tempnode==0)
{
printf("Empty list\n\n");
return;
}
else
{
printf("Enter the position of element to delete:");
scanf("%d",&pos);
if(pos>0&&pos<=countvalue)
{
if(pos==1)
{
tempnode=tempnode->next;
firstnode=tempnode;
printf("Successfully Deleted\n\n");
}
else
{
while(tempnode!=0)
{
if(i==(pos-1))
{
prevnode->next=tempnode->next;
if(i==(countvalue-1))
headnode=prevnode;
printf("Deleted Successfully\n\n");
break;
}
else
{
i++;
prevnode=tempnode;
tempnode=tempnode->next;
}
}
}
}
else
{
printf("Invalid Position\n\n");
}
}
}
void display()
{
tempnode=firstnode;
if(tempnode==0)
printf("Empy list\n");
else
{
while(tempnode!=0)
{
printf("%d\t",tempnode->value);
tempnode=tempnode->next;
}
}
}
int count()
{
int count=0;
tempnode=firstnode;
while(tempnode!=0)
{
count++;
tempnode=tempnode->next;
}
printf("No. of items in Linked List: %d\n",count);
return count;
}
OUTPUT:
RESULT:
Thus the C program for linked list implementation of list ADT was successfully written and executed.
EX.NO: 8)ii) LINKED LIST IMPLEMENTATION OF STACK ADT
DATE:
AIM:
ALOGIRTHM:
#include<stdio.h>
#include<malloc.h>
struct node
{
int data;
struct node*next;
}*top,*new1,*first;
int main()
{
int wish,opt;
void create(),push(),pop(),view();
do
{
printf("Stack using linked list");
printf("\n1.Create\n2.Push\n3.Pop\n4.View\n5.Exit\n\n");
printf("Enter your chocie:");
scanf("%d",&wish);
switch(wish)
{
case 1:
create();
break;
case 2:
push();
break;
case 3:
pop();
break;
case 4:
view();
break;
case 5:
return 0;
break;
}
RESULT:
Thus the C program for implementing queue with linked list was executed, and output verified
successfully
EX.NO: 8)iii) LINKED LIST IMPLEMENTATION OF QUEUE ADT
DATE:
AIM:
ALOGRITHM:
#include<stdio.h>
#include<malloc.h>
struct node
{
int info;
struct node*link;
}*front=NULL,*rear=NULL;
void insert();
void delet();
void display();
int item;
int main()
{
int ch;
do
{
printf("\n1.Enqueue\n2.Dequeue\n3.Display\n4.exit\n");
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert();
break;
case 2:
delet();
break;
case 3:
display();
break;
case 4:
return 0;
break;
default:
printf("\n\nInvalid choice");
}
}
while(1);
}
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");
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;
}
}
}
OUTPUT:
RESULT:
Thus the C program for implementing queue with linked list was executed, and output verified
successfully.
EX.NO:9 i) APPLICATION OF LIST, STACK & QUEUE ADT
AIM:
ALGORITHM:
STEP-1. Create a linked list to store the terms of the first polynomial.
STEP-2. Create a linked list to store the terms of the second polynomial.
STEP-3. Compare the exponents of the corresponding terms in the two polynomials.
STEP-4. If the exponents are equal, add the coefficients of the terms and store the result in a new linked list.
STEP-5. If the exponents are not equal, store the term with the higher exponent in the new linked list.
STEP-6. Repeat steps 3-5 until all terms have been processed.
PROGRAM CODE:
#include <stdio.h>
#include <stdlib.h>
typedef struct Term {
int coeff;
int exp;
struct Term* next;
} Term;
void addTerm(Term** poly, Term* newTerm) {
if (*poly == NULL) {
*poly = newTerm;
} else {
Term* current = *poly;
while (current->next != NULL) {
current = current->next;
}
current->next = newTerm;
}
}
Term* addPolynomials(Term* poly1, Term* poly2) {
Term* result = NULL;
Term* current1 = poly1;
Term* current2 = poly2;
while (current1 != NULL && current2 != NULL) {
Term* newTerm = (Term*) malloc(sizeof(Term));
newTerm->coeff = current1->coeff + current2->coeff;
newTerm->exp = current1->exp;
newTerm->next = NULL;
addTerm(&result, newTerm);
current1 = current1->next;
current2 = current2->next;
}
while (current1 != NULL) {
addTerm(&result, current1);
current1 = current1->next;
}
while (current2 != NULL) {
addTerm(&result, current2);
current2 = current2->next;
}
return result;
}
void printPolynomial(Term* poly) {
while (poly != NULL) {
printf("%dx^%d ", poly->coeff, poly->exp);
poly = poly->next;
}
printf("\n");
}
int main() {
int n1, n2;
printf("Enter number of terms in polynomial 1: ");
scanf("%d", &n1);
Term* poly1 = NULL;
for (int i = 0; i < n1; i++) {
Term* newTerm = (Term*) malloc(sizeof(Term));
printf("Enter coefficient and exponent for term %d: ", i+1);
scanf("%d %d", &newTerm->coeff, &newTerm->exp);
newTerm->next = NULL;
addTerm(&poly1, newTerm);
}
printf("Enter number of terms in polynomial 2: ");
scanf("%d", &n2);
Term* poly2 = NULL;
for (int i = 0; i < n2; i++) {
Term* newTerm = (Term*) malloc(sizeof(Term));
printf("Enter coefficient and exponent for term %d: ", i+1);
scanf("%d %d", &newTerm->coeff, &newTerm->exp);
newTerm->next = NULL;
addTerm(&poly2, newTerm);
}
Term* result = addPolynomials(poly1, poly2);
printf("Result: ");
printPolynomial(result);
return 0;
}
OUTPUT:
RESULT:
Thus the C program to addition of two polynomials using linked list was written and executed successfully.
EX.NO:9i)
DATE: SUBRACTION OF TWO POLYNOMIALS
AIM:
ALGORITHM:
PROGRAMCODE:
#include <stdio.h>
#include <stdlib.h>
typedefstruct Term {
intcoeff;
intexp;
struct Term* next;
} Term;
Term* subtract(Term* p1, Term* p2) {
Term* result = NULL;
Term* current = NULL;
while (p1 != NULL && p2 != NULL) {
Term* newTerm = (Term*) malloc(sizeof(Term));
newTerm->coeff = p1->coeff - p2->coeff;
newTerm->exp = p1->exp;
newTerm->next = NULL;
if (result == NULL) {
result = newTerm;
} else {
current->next = newTerm;
}
current = newTerm;
p1 = p1->next;
p2 = p2->next;
}
return result;
}
int main() {
int n1, n2;
printf("Enter number of terms in polynomial 1: ");
scanf("%d", &n1);
Term* p1 = NULL;
for (inti = 0; i< n1; i++) {
Term* newTerm = (Term*) malloc(sizeof(Term));
printf("Enter coefficient and exponent for term %d: ", i+1);
scanf("%d %d", &newTerm->coeff, &newTerm->exp);
newTerm->next = p1;
p1 = newTerm;
}
printf("Enter number of terms in polynomial 2: ");
scanf("%d", &n2);
Term* p2 = NULL;
for (inti = 0; i< n2; i++) {
Term* newTerm = (Term*) malloc(sizeof(Term));
printf("Enter coefficient and exponent for term %d: ", i+1);
scanf("%d %d", &newTerm->coeff, &newTerm->exp);
newTerm->next = p2;
p2 = newTerm;
}
Term* result = subtract(p1, p2);
printf("Result: ");
while (result != NULL) {
printf("%dx^%d ", result->coeff, result->exp);
result = result->next;
}
return 0;
}
OUTPUT:
RESULT:
Thus C program to subtract two polynomials using linked list was written and executed successfully.
EX.NO: 9)iii) APPLICATION OF STACK ADT
AIM:
ALOGIRTHM:
#include <stdio.h>
#include <ctype.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;
}
int main()
{
char exp[20];
char *e, x;
printf("Enter the expression:");
scanf("%s", &exp);
e = exp;
printf("Postfix expression:");
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());
return 0;
}
OUTPUT:
RESULT:
Thus the C program to convert infix into postfix expression using stack was written, executed, and
output verified successfully.
EX.NO: 10) IMPLEMENTATION OF BINARY TREES AND OPERATIONS OF
AIM:
ALGORITHM:
#include<stdio.h>
#include<stdlib.h>
struct tnode
{
int data;
struct tnode *right;
struct tnode *left;
};
struct tnode *CreateTree(struct tnode *, int);
void Inorder(struct tnode *);
void Preorder(struct tnode *);
void Postorder(struct tnode *);
int main()
{
struct tnode *root = NULL; int choice, item, n, i;
do
{
printf("\n ********** BINARY TREE TRAVERSALS *********** \n"); printf("\n1.
Creation of Binary Tree");
printf("\n2. Traverse in Inorder");
printf("\n3. Traverse in Preorder");
printf("\n4. Traverse in Postorder");
printf("\n5. Exit\n");
printf("\nEnter Choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
root = NULL;
printf("\n\nHow Many Nodes ? "); scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\nEnter data for node %d : ",i);
scanf("%d",&item);
root=CreateTree(root,item);
}
printf("\n Tree with %d nodes is ready to Use!!\n",n);
break;
case 2:
printf("\nTraversal in INORDER \n");
Inorder(root);
break;
case 3:
printf("\nTraversal in PREORDER \n");
Preorder(root);
break;
case 4:
printf("\nTraversal in POSTORDER \n");
Postorder(root);
break;
case 5:
printf("\n\n Terminating \n\n");
break;
default:
printf("\n\nInvalid Option !!! Try Again !! \n\n");
break;
}
}
while(choice != 5);
return 0;
}
struct tnode *CreateTree(struct tnode *root, int item)
{
if(root==NULL)
{
root=(struct tnode *)malloc(sizeof(struct tnode));
root->left=root->right=NULL;
root->data=item;
return root;
}
else
{
if(item<root->data )
root->left=CreateTree(root->left,item);
else if(item>root->data )
root->right=CreateTree(root->right,item);
else
printf(" Duplicate Element !! Not Allowed !!!");
return(root);
}
}
}
}
OUTPUT:
RESULT:
Thus the C program for implement binary trees and operation of binary tree was implemented and
output is verified successfully.
EX.NO: 11) IMPLEMENTATION OF BINARY SEARCH TREE
DATE:
AIM:
ALOGIRTHM:
#include <stdio.h>
#include <stdlib.h>
struct BST
{
int data;
struct BST *left;
struct BST *right;
};
typedef struct BST NODE;
NODE *node;
NODE *createtree(NODE *node,int data)
{
if (node==NULL)
{
NODE *temp;
temp=(NODE * )malloc(sizeof(NODE));
temp->data=data;
temp->left=temp->right=NULL;
return temp;
}
if (data<(node->data))
node -> left = createtree(node -> left, data);
else if (data>node->data)
node->right=createtree(node->right,data);
return node;
}
NODE *search(NODE *node, int data)
{
if (node==NULL)
printf("\nElement not found");
else if(data<node->data)
node->left=search(node->left,data);
else if (data > node -> data)
node->right=search(node->right,data);
else
printf("\nElement found is: %d",node->data);
return node;
}
NODE *findMin(NODE *node)
{
if (node==NULL)
return NULL;
if (node->left)
return findMin(node->left);
else
return node;
}
NODE *del(NODE *node, int data)
{
NODE *temp;
if (node==NULL)
printf("\nElement not found");
else if(data<node->data)
node->left=del(node->left,data);
else if(data>node->data)
node->right=del(node->right,data);
else
{
if (node->right&&node->left)
{
temp=findMin(node->right);
node->data = temp->data;
node->right=del(node->right,temp->data);
}
else
{
temp=node;
if (node->left==NULL)
node=node->right;
else if (node->right==NULL)
node=node->left;
}
}
return node;
}
int main()
{
int data,ch,i,n;
NODE *root=NULL;
while(1)
{
printf("\n**********BINARY SEARCH TREE (BST) *********** \n");
printf("\n 1.Insertion in Binary Search Tree");
printf("\n 2.Search Element in Binary Search Tree");
printf("\n 3.Delete Element in Binary Search Tree");
printf("\n 4.Exit");
printf("\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter N value: ");
scanf("%d",&n);
printf("\nEnter the values to create BST:)\n");
for(i=0;i<n;i++)
{
scanf("%d",&data);
root=createtree(root,data);
}
break;
case 2:
printf("\nEnter the element to search: ");
scanf("%d",&data);
root = search(root,data);
break;
case 3:
printf("\nEnter the element to delete: ");
scanf("%d",&data);
root = del(root,data);
break;
case 4:
exit(0);
default:
printf("\nWrong option");
break;
}
}
}
OUTPUT:
RESULT:
Thus the above C program for implementation of binary search tree was implemented and output is
verified successfully.
EX.NO: 12)i) SEARCHING TECHNIQUES- BINARY SEARCH
DATE:
AIM:
ALGORITHM:
STEP-1: Start the program.
STEP-2: Find the middle element of array using mid=(init_val+end_val)/2.
STEP-3: If the middle is greater than the given input last=mid-1.
STEP-4: If the middle is lesser than the given input last=mid+1.
STEP-5: Until the condition satisfies repeat the main function.
STEP-6: End the Program.
PROGRAM CODE:
#include<stdio.h>
int main()
{
int arr[50],i,n,x,flag=0,first,last,mid;
printf("Enter size of array:");
scanf("%d",&n);
printf("\nEnter array element(ascending order)\n");
for (i=0;i<n;++i)
scanf("%d",&arr[i]);
printf("\nEnter the element to search:");
scanf("%d",&x);
first=0;
last=n-1;
while(first<=last)
{
mid=(first + last)/2;
if (x==arr[mid])
{
flag=1;
break;
}
else
{
if (x>arr[mid])
first=mid+1;
else
last=mid-1;
}
}
if (flag==1)
printf("\nElement found at position %d", mid + 1);
else
printf("\nElement not found");
return 0;
}
OUTPUT:
RESULT:
Thus the C program for searching a number using binary search was implemented and output is verified
successfully.
EX.NO: 12)ii) SEARCHING TECHNIQUES- LINEAR SEARCH
DATE:
AIM:
ALOGIRTHM:
STEP-1: Start the program.
STEP-2: Declare the variables.
STEP-3: Iterate forloop to get input from user and store in array.
STEP-4: Iterate forloop to search a number in array.
STEP-5: Until the condition satisfies repeat the function.
STEP-6: End the Program.
PROGRAM CODE:
#include<stdio.h>
int main()
{
int i,array[50],search,size;
printf("Enter Size of array:");
scanf("%d", & size);
printf("Enter %d element in array: \n",size);
for(i=0;i<size;i++)
scanf("%d",&array[i]);
printf("Enter element for search: ");
scanf("%d",&search);
for (i=0;i<size;i++)
{
if(array[i]==search)
{
printf("\n%d is present at %d position",search,i+1);
break;
}
}
if(i==size)
printf("\n %d is not found", search);
}
OUTPUT:
RESULT:
Thus the C program for searching a number using linear search was implemented and output is verified
successfully.
EX.NO: 13)i) SORTING ALGORITHM – INSERTION SORT
DATE:
AIM:
ALOGIRTHM:
STEP-1: Start the program.
STEP-3: Iterate the forloop to get input from user and store in array.
PROGRAM CODE:
#include<stdio.h>
int main()
{
int data[100],n,temp,i,j;
printf("** ** ** ** ** * INSERTION SORT ** ** ** ** ** * ");
printf("\nEnter number of terms: ");
scanf("%d",&n);
printf("Enter elements: ");
for(i=0;i<n;i++)
scanf("%d",&data[i]);
for(i=1;i<n;i++)
{
temp=data[i];
j=i-1;
while (temp<data[j]&&j>=0)
{
data[j+1]=data[j];
--j;
}
data[j+1]=temp;
}
printf("Sorted list in ascending order: ");
for(i=0;i<n;i++)
printf("%d\t",data[i]);
return 0;
}
OUTPUT:
RESULT:
Thus the C program for sorting a list using insertion sort was implemented and output is verified
successfully.
EX.NO: 13)ii) SORTING ALGORITHM – QUICK SORT
DATE:
AIM:
ALOGIRTHM:
STEP-1: Start the program.
STEP-2: Declare the function.
STEP-3: Select an element from an array, call it as pivot element.
STEP-4: Divide the unsorted array element into two arrays.
STEP-5: Check the conditions with pivot element.
STEP-6: End the Program.
PROGRAM CODE:
#include<stdio.h>
int Partition(int a[],int beg,int end)
{
int i=beg,pivot=a[beg],j;
for(j=beg+1;j<=end;j++)
{
if(pivot>a[j])
{
a[i]=a[j];
a[j]=a[i+1]; a[i+1]=pivot; i=i+1;
}
}
return i;
}
void QuickSort(int a[],int beg,int end)
{
if(beg<end)
{
int p=Partition(a,beg,end);
QuickSort(a,beg,p-1);
(a,p+1,end);
}
}
int main()
{
int a[100],i,n;
printf("\n--------QUICK SORT --------- \n\n");
printf("Enter the No. of Elements : ");
scanf("%d",&n);
printf("Enter %d elements: ",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
QuickSort(a,0,n-1);
printf("\n Order of Sorted elements: \n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}
OUTPUT:
RESULT:
Thus the C program for sorting a list using quick sort was implemented and output is verified
successfully.
EX.NO: 13)iii) SORTING ALGORITHM – MERGE SORT
DATE:
AIM:
#include<stdio.h>
void mergesort(int array[],int first,int last);
void merge(int array[],int first,int mid,int last);
int main()
{
int array[30],n,i;
printf("\n ******** MERGE SORT ************\n");
printf("Enter no of elements:");
scanf("%d",&n);
printf("Enter array elements:");
for (i=0;i<n;i++)
scanf("%d", & array[i]);
mergesort(array, 0, n - 1);
printf("\nSorted array is :");
for(i=0;i<n;i++)
printf("%d ",array[i]);
return 0;
}
void mergesort(int array[],int first,int last)
{
int mid;
if (first<last)
{
mid=(first+last)/2;
mergesort(array,first,mid);
mergesort(array,mid+1,last);
merge(array,first,mid,last);
}
}
void merge(int array[],int first,int mid,int last)
{
int temp[50];
int i,j,k,s;
i=first;
j=mid+1;
k=first;
while (i<=mid&&j<=last)
{
if(array[i]<array[j])
{
temp[k]=array[i];
i++;
}
else
{
temp[k]=array[j];
j++;
}
k++;
}
if(i>mid)
{
for(s=j;s<=last;s++)
{
temp[k]=array[s];
k++;
}
}
else
{
for(s=i;s<=mid;s++)
{
temp[k]=array[s];
k++;
}
}
for(k=first;k<=last;k++)
array[k] = temp[k];
}
OUTPUT:
RESULT:
Thus the C program for sorting a list using merge sort was implemented and output is verified successfully.
EX.NO: 14) IMPLEMENTATION OF HASHING TECHNIQUES
AIM:
#include <stdio.h>
int tsize;
int hasht(int key)
{
int i ;
i=key%tsize;
return i;
}
int rehashl(int key)
{
int i ;
i=(key+1)%tsize;
return i;
}
int rehashq(int key,int j)
{
int i;
i=(key+(j*j))%tsize;
return i;
}
int main()
{
int key,arr[20],hash[20],i,n,s,op,j,k;
printf("Enter the size of the hash table: ");
scanf("%d",&tsize);
printf("\nEnter the number of elements: ");
scanf("%d",&n);
for(i=0;i<tsize;i++)
hash[i]=-1 ;
printf("Enter Elements: ");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
do
{
printf("\n\n1.Linear Probing\n2.Quadratic Probing \n3.Exit \nEnter your option: ");
scanf("%d",&op);
switch(op)
{
case 1:
for(i=0;i<tsize;i++)
hash[i]=-1 ;
for(k=0;k<n;k++)
{
key=arr[k];
i=hasht(key);
while(hash[i]!=-1)
{
i=rehashl(i);
}
hash[i]=key;
}
printf("\nThe elements in the array are: ");
for(i=0;i<tsize;i++)
printf("\n Element at position %d: %d",i,hash[i]);
break;
case 2:
for(i=0;i<tsize;i++)
hash[i]=-1 ;
for(k=0;k<n;k++)
{
j=1;
key=arr[k] ;
i=hasht(key);
while(hash[i]!=-1)
{
i=rehashq(i,j);
j++;
}
hash[i]=key;
}
printf("\nThe elements in the array are: ");
for(i=0;i<tsize;i++)
{
printf("\n Element at position %d: %d",i,hash[i]);
}
break;
}
}
while(op!=3);
}
OUTPUT:
RESULT:
Thus the C program for linear and quadratic probing using hash table was implemented and output is
verified successfully.