KEMBAR78
Cs3362 CD Lab Manual Final Copy-1 | PDF | Queue (Abstract Data Type) | Time Complexity
0% found this document useful (0 votes)
63 views107 pages

Cs3362 CD Lab Manual Final Copy-1

The document outlines the C Programming and Data Structures Laboratory course at the Government College of Engineering-Tirunelveli, detailing its objectives, experiments, and expected outcomes. It includes a comprehensive list of programming tasks that students will complete, such as implementing data structures, sorting algorithms, and real-time applications. The course aims to equip students with practical skills in C programming and data structure operations by the end of the semester.

Uploaded by

Selva
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)
63 views107 pages

Cs3362 CD Lab Manual Final Copy-1

The document outlines the C Programming and Data Structures Laboratory course at the Government College of Engineering-Tirunelveli, detailing its objectives, experiments, and expected outcomes. It includes a comprehensive list of programming tasks that students will complete, such as implementing data structures, sorting algorithms, and real-time applications. The course aims to equip students with practical skills in C programming and data structure operations by the end of the semester.

Uploaded by

Selva
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/ 107

GOVERNMENT COLLEGE OF ENGINEERING-TIRUNELVELI

DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING

CS3362– C PROGRAMMING AND DATA

STRUCTURES LABORATORY

(Regulations 2021)

II Year / Semester III

NAME:

REG.NO:

PROG:

YEAR/SEM:
INDEX

S.NO. DATE NAME OF THE EXPERIMENTS PAGE NO. MARKS SIGNATURE


CS3362 C PROGRAMMING AND DATA STRUCTURES LABORATORY LTPC

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

1. Practice of C programming using statements, expressions, decision making and iterative


statements
2. Practice of C programming using Functions and Arrays
3. Implement C programs using Pointers and Structures
4. Implement C programs using Files
5. Development of real time C applications
6. Array implementation of List ADT
7. Array implementation of Stack and Queue ADTs
8. Linked list implementation of List, Stack and Queue ADTs
9. Applications of List, Stack and Queue ADTs
10.Implementation of Binary Trees and operations of
Binary Trees
11. Implementation of Binary Search Trees
12. Implementation of searching techniques
13. Implementation of Sorting algorithms : Insertion Sort, Quick Sort, Merge Sort
14. Implementation of Hashing – any two collision techniques

TOTAL: 45 PERIODS

COURSE OUTCOMES:

At the end of the course, the students will be able to:

CO1 Use different constructs of C and develop applications

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

COs POs PSOs


PO PO PO3 PO4 PO PO6 PO7 PO8 PO9 PO10 PO1 PO1 PSO PS PSO3
1 2 5 1 2 1 O2
1 2 3 1 2 2 1 1 - 1 2 1 3 2 1 3
2 1 2 1 2 2 - - - 1 1 1 2 2 2 2
3 2 3 1 2 3 - - - 1 1 1 2 2 1 2
4 2 1 - 1 1 - - - 2 1 1 2 2 3 1
5 1 2 1 2 2 1 1 - 1 2 1 3 2 2 3
Avg. 2 2 1 2 2 1 1 - 1 1 1 2 2 2 2
INDEX

NAME OF THE EXPERIMENT PAGE


EX.NO. NO.
1 SIMPLE C PROGRAM
i) Simple Calculator Using Switch Case Statement
ii) Given String Is Palindrome Or Not
iii) Prime Or Composite Number

iv) Roots Of The Quadratic Equation.

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

i) Array implementation of Stack ADT


ii) Array implementation of Queue ADT
8. LINKED LIST IMPLEMENTATION
.i) Linked list implementation of List ADT
ii) Linked list implementation of Stack ADT
iii) Linked list implementation of Queue ADT
9. APPLICATION OF LIST, STACK & QUEUE ADT
.i) Addition of two polynomials using linked list.
ii) Subtraction of two polynomials using linked list.
iii) Conversion of infix to postfix expression
10. Implementation of Binary tree and operation of Binary tree
11. Implementation of Binary search tree
12. SEARCHING TECHNIQUES
i) Binary search
ii) Linear search
13. SORTING ALGORITHM
i) Insertion sort
ii) Quick sort
iii) Merge sort
14. IMPLEMENTATION OF HASHING
Linear and quadratic probing
EX.NO:1)i)
DATE:
SIMPLE CALCULATOR USING SWITCH CASE STATEMENT

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;

STEP-1: Start the program.


STEP-2: Prompt the user to enter a string.
STEP-3: Read the string input.
STEP-4: Calculate the length of the input string.
STEP-5: Initialize a flag variable to 0 (assuming the string is a palindrome).
STEP-6: Iterate through the first half of the string.
STEP-7: Compare each character with its corresponding character from the end of the string.
STEP-8: If a mismatch is found, set the flag to 1 and break the loop.
STEP-9: If the flag is 1, print that the string is not a palindrome.
STEP-10: Otherwise, print that the string is a palindrome.
STEP-11: Stop the program.

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:

STEP-1: Start the program


STEP-2: Declare variables i , n and c=0
STEP-3: Get a number from user as n
STEP-4: When n equals to 1 or assign c=0
STEP-5: Otherwise starting from i=2, divide n by each number till n
STEP-6: When the number is divisible by n measure c=c+1
STEP-7: When c equals to 0, display neither prime nor composite
STEP-8: When c equals to 1, display the number is prime
STEP-9 : Otherwise display the number is composite
STEP-10 : Stop the program.
PROGRAM CODE:

#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:

STEP-1: Start the program.


STEP-2: Prompt the user to enter the coefficients a, b, and c of the quadratic equation.
STEP-3: Read the input values of a, b, and c.
STEP-4: Calculate the discriminant using the formula b^2 - 4ac.
STEP-5: Check the discriminant value:
- If it's positive, proceed to calculate two distinct real roots.
- If it's zero, proceed to calculate one repeated real root.
- If it's negative, proceed to calculate two complex roots.
STEP-6: Calculate the roots according to the discriminant value:
- For real roots, use the quadratic formula to find two distinct roots.
- For a repeated real root, calculate the single root value.
- For complex roots, calculate the real and imaginary parts of the roots.
STEP-7: Output the roots in the appropriate format:
- For real roots, display both roots with two decimal places.
- For a repeated real root, display the single root value with two decimal places.
- For complex roots, display both roots with real and imaginary parts, each with two decimal places.
STEP-8: Stop the program.

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:

STEP-1: Start the program.


STEP-2: Ask the user to input a number to check.
STEP-3: Calculate the number of digits in the input number
STEP-4: Initialize a variable to store the original number and another variable to store the sum of the digits'
powers.
STEP-5: Extract each digit from the input number, calculate its power (raised to the number of digits), and add it
to the sum.
STEP-6: Repeat step 4 until all digits have been processed.
STEP-7: Check if the original number is equal to the sum of the digits' powers.
STEP-8: If they are equal, print that the number is an Armstrong number; otherwise, print that it is not.
STEP-9: Stop the Program.

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:

To determine the largest of three numbers using C program.

ALGORITHM:

STEP-1: Start the program.


STEP-2: Ask the user to input three numbers.
STEP-3: Read the three numbers into variables n1, n2, and n3.
STEP-4: Compare the numbers to find the largest one.
STEP-5: If n1 is the largest, print it.
STEP-6: If n2 is the largest, print it.
STEP-7: If n3 is the largest, print it.
STEP-8: Output the largest number.
STEP-9: Stop the program.

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;
}

long int multiplyNumbers(int n) {


if (n>=1)
return n*multiplyNumbers(n-1);
else
return 1;
}
OUTPUT:

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:

Thus the C program for Pascals triangle is executed successfully.


EX NO:2)b)i) PROGRAM USING ARRAYS

DATE:
ADDITION OF TWO MATRICES

AIM:

To perform addition of two matrices using array in C program.

ALGORITHIM:

STEP-1: Start the program.

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-4: Create a new 3x3 matrix (matrix C) to store the result.

STEP-5: Iterate through each element of the matrices.

STEP-6: For each element, add the corresponding elements of matrices A and B and store the result in
matrix C.

STEP-7: Print the resultant matrix C.

STEP-8: Wait for user input before exiting

STEP-9: Stop the program.

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-1: Start the program.

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-4: Create a new 3x3 matrix (matrix C) to store the result.

STEP-5: Iterate through each element of the result matrix.

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.

STEP-8: Print the resultant matrix C.

STEP-9: Wait for user input before exiting.

STEP-10: Stop the program.

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-1: Start the program.

STEP-2: Ask the user to input the number of rows and columns.

STEP-3: Ask the user to input the elements of the matrix.

STEP-4: Display the original matrix

STEP-5: Create a new matrix (transpose) with swapped rows


and columns.

STEP-6: For each element, assign the corresponding element


from the original matrix to the transposed matrix.

STEP-7: Display the transposed matrix.

STEP-8: Stop the program.

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

STEP-1. Input: two numbers a and b


STEP-2. Function swap_by_value(a, b)
- temp = a
-a=b
- b = temp
- Print a and b (Note: this will not reflect the original values)
STEP-3. End Function
STEP-4. Print original values of a and b (will be unchanged)

Swap by Reference Algorithm

STEP-1. Input: two numbers a and b


STEP-2. Function swap_by_reference(&a, &b)
- temp = *a
- *a = *b
- *b = temp
- Print *a and *b (will reflect the swapped values)
STEP-3. End Function
STEP-4. Print original values of a and b (will be swapped)

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:

To write a C program using structure to display student details.

ALOGIRTHM:

STEP-1: Start the program


STEP-2: Declare a structure named student
STEP-3: Declare the variable firstname, roll, marks
STEP-4: Declare s[ ]=i
STEP-5: Get student information like name and marks from the user by looping
STEP-6: Display the received information
STEP-7: Stop the program
PROGRAM CODE:

#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:

To copy the contents of one file to another using C program.

ALGORITHM:

STEP-1: Start the program.


STEP-2: Ask the user for the source and destination file paths.
STEP-3: Open the source file in read-only mode.
STEP-4: Open the destination file in write-only mode.
STEP-5: Check if either file cannot be opened due to errors or permissions.
STEP-6: If successful, read each character from the source file and write it to the destination file.
STEP-7: Close both files after copying.
STEP-8: Print a success message indicating that the files have been copied.
STEP-9: Stop the program.

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:

Enter source file path: data\file2.txt

Enter destination file path: data\copy-file.txt

Files copied successfully.

142 characters copied.

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:

To write a C program to implement payroll application using structure.

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--;
}

printf("\nThe element after deletion=");


for(i=0;i<n;i++)
{
printf(" %d",b[i]);
}
}
void search()
{
printf("\nEnter the element to be searched:");
scanf("%d",&e);
for(i=0;i<n;i++)
{
if(b[i]==e)
{
h=1;
break;
}
else
{
h=0;
}
}

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:

To write a C program to implement stack ADT.


ALOGIRTHM:
STEP-1: Start the program
STEP-2: Declare top as -1
STEP-3: Get the size of stack a sn
STEP-4: Display the choices
STEP-5: Get the choice from the use
STEP-6: Call switch(ch)
STEP-7: Repeat the step 6 until the condition is false
STEP-8: When choice equals to1
i) when top>=n-1,display stack overflow otherwise add element to the stack by calling
push()
ii) break the case.
STEP9: When choice equals to 2
i) when top<=n-1,display stack underflow otherwise delete element from the stack by
calling pop()
ii) break the case.

STEP10: When choice equals to 3


i)when top>=0, display elements in the stack using for loop.
ii)otherwise display the stack is empty.
iii) break the case.

STEP 11: When choice equals to 4,


i) break the case.

STEP 12: Otherwise display invalid choice


STEP 13: Stop the program
PROGRAM CODE:

#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:

To write a C program to implement queue ADT using arrays

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.

ii) when rear equals to max-1 display queue overflow.

iii)otherwise in queue the element is the queue.


iv)break the choice.
STEP-8: When choice equals to2
i)when front equals to 1and front is greater than rear display queue underflow
ii)otherwise dequeue the element from the queue.
iii)break the choice.
STEP-9: When choice equals to 3
i) Declare i

ii) when front equals to1display empty queue.

iii)otherwise display the queue using forloop.


STEP-10: When choice equals to 4, break the choice
STEP-11: Otherwise display wrong choice.
STEP-12: Stop the program.
PROGRAM CODE:

#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("\n\n1.insert element to queue\n");

printf("2.delete element from queue\n");

printf("3.display all elements in the queue\n");

printf("4.quit\n");

printf("enter your chioce");

scanf("%d",& choice);

switch(choice)

case 1:

insert();

break;

case 2:

remove();

break;

case 3:

display();
break;

case 4:
return 0;

break;

default:

printf("current choice \n");

void insert()

int add_item;

if(rear==max-1)

printf("Queue overflow \n");

else

if(front==-1)

front=0;

printf("Insert elememnt in Queue:");

scanf("%d",&add_item);

rear=rear+1;

queue_array[rear]=add_item;

void remove()

if(front==-1)
{

printf("The Queue underflow\n");

return;

else

printf("elements delete from queue is %d \n",queue_array[front]);

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:

To write a C program to implement linked list using a list.

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:

To implement stack operation using a linked list.

ALOGIRTHM:

STEP-1: Start the program


STEP-2: Declare the variable wish, opt
STEP-3: Read an option from user as wish
STEP-4: When wish equals to 1,
• Read the element from the user
• Insert the element in the node
STEP-5: When wish equals to 2,
• Read the element as new 1
• Push the element in the stack
STEP-6: When wish equals to 3,
• Remove the element that was recetntly added in stack
STEP-7: When wish equals to 4,
• Display the element of stack
STEP-8: When wish equals to 5,
• Break the choice
STEP-9: If any other choice is entered display invalid choice
STEP-10: Stop the program.
PROGRAM CODE:

#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;
}

printf("\nDo you want 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
{
printf("Enter the data:");
scanf("%d",&top->data);
printf("Do you want to continue(1/0):");
scanf("%d",&ch);
if(ch==1)
{
new1=(struct node*)malloc(sizeof(struct node));
new1->next=top;
top=new1;
first=top;
}
}
while(ch==1);
}
void pop()
{
top=first;
if(top==NULL)
printf("\nStack is empty\n");
else
{
printf("\nThe element popped out of stack: %d",top->data);
top=top->next;
first=top;
}
}
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 view()
{
printf("\nStack Contents\n");
while(top->next!=NULL)
{
printf("%d->",top->data);
top=top->next;
}
printf("%d\t\n",top->data);
}
OUTPUT:

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:

To write a C program to implement queue operator using linked list.

ALOGRITHM:

STEP-1: Start the program


STEP-2: Declare variable ch,item
STEP-3: Display the variable choice and get choice from user as ch
STEP-4: When choice equals to 1,
• Read an element as item
• Insert the element o the road of linked list
STEP-5: When choice equals to 2,
• Display the queue element
STEP-6: When choice equals to 4,
• Break the choice
STEP-7: When any other choice is entered display invalid choice
STEP-8: Stop the program.
PROGRAM CODE:

#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

DATE: ADDITION OF TWO POLYNOMIALS

AIM:

To write a C program to add two polynomials using linked list.

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.

STEP-7. Return the new linked list as the result.

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:

To write a C program to subtract two polynomials using linked list.

ALGORITHM:

STEP-1:Input two polynomials, P1 and P2, in the format ax^2 + bx + c


STEP-2:Create a linked list to store the terms of each polynomial
STEP-3:Subtract the coefficients of corresponding terms in P1 and P2
STEP-4: If the result is non-zero, add the term to the result polynomial
STEP-5:Repeat steps 3-4 until all terms have been processed
STEP-6:Print the result polynomial

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

DATE: CONVERSION OF INFIX TO POSTFIX EXPRESSION

AIM:

To write a C program to convert an infix o postfix expression using stack ADT.

ALOGIRTHM:

STEP-1: Start the program


STEP-2: Declare stack[20] and top=-1
STEP-3: Get an infix expression from user
STEP-4: Tranverse through expression
STEP-5: If symbol has more prioritiy than it is pushed into stack
STEP-6: If symbol has less priority than symbol is pushed into top
STEP-7: Display the posfix notation
STEP-8: Stop the program.
PROGRAM CODE:

#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

DATE: BINARY TREES

AIM:

To write a C program to implement binary trees and operation of binary trees.

ALGORITHM:

STEP-1: Start the program


STEP-2: Create a structure
STEP-3: For inorder traversal
• Traversal left subtree
• Visit root
• Traversal right subtree
STEP-4: For preorder traversal
• Visit root
• Traversal left subtree
• Traversal right subtree
STEP-5: For postorder traversal
• Traversal left subtree
• Traversal right subtree
• Visit root
STEP-6: End the program.
PROGRAM CODE:

#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);
}
}

void Inorder(struct tnode *root)


{
if(root!=NULL)
{
Inorder(root->left);
printf("%d ",root->data);
Inorder(root->right);
}
}
void Preorder(struct tnode *root)
{
if(root!=NULL)
{
printf("%d ",root->data);
Preorder(root->left);
Preorder(root->right);
}
}
void Postorder(struct tnode *root)
{
if(root!=NULL)
{
Postorder(root->left);
Postorder(root->right);
printf("%d ",root->data);

}
}
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:

To write a C program for implementation of binary search tree.

ALOGIRTHM:

STEP-1: Start the program


STEP-2: Create a structure
STEP-3: Read the node to be inserted
STEP-4: For inorder traversal
• Traversal left subtree
• Visit root
• Traversal right subtree
STEP-5: For preorder traversal
• Visit root
• Traversal left subtree
• Traversal right subtree
STEP-6: For postorder traversal
• Traversal left subtree
• Traversal right subtree
• Visit root
STEP-7: End the program.
PROGRAM CODE:

#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:

To write a C program for searching an number using binary search.

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:

To write a C program for searching an number using linear search.

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:

To write a C program for sorting a list using insertion sort.

ALOGIRTHM:
STEP-1: Start the program.

STEP-2: Declare the variables.

STEP-3: Iterate the forloop to get input from user and store in array.

STEP-4: Sort in ascending order using looping.

STEP-5: End the Program.

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:

To write a C program for sorting list using quick sort.

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:

To write a C program for sorting list using merge sort.


ALGORITHM:
STEP-1: Start the program.
STEP-2: Declare the functions.
STEP-3: Iterate the forloop to get input from user and store in array.
STEP-4: Sort it by left recursion, right recursion and merging of two sorted sub-arrays.
STEP-5: End the Program
PROGRAM CODE:

#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

DATE: ( LINEAR AND QUADRAIC PROBING )

AIM:

To write a C program for hashing using linear and quadratic probing.


ALOGIRTHM:

STEP-1: Start the program.


STEP-2: Declare the functions.
STEP-3: Iterate the forloop to get input from user and store in array.
STEP-4: Using looping, hash the value and store it in the hash table.
STEP-5: End the Program.
PROGRAM CODE:

#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.

You might also like