0% found this document useful (0 votes)
42 views

C Programming and Data Structure Record

This document contains programs to demonstrate basic C programming concepts like data types, operators, conditional statements, loops, functions, arrays and structures. The programs include converting cases of characters, arithmetic operators, swapping variables, checking leap years, determining positive/negative/zero numbers, checking even/odd numbers, finding the largest of two numbers, calculating the sum of N numbers, finding the square of N numbers, matrix addition, calculating area of squares and circles, checking Armstrong numbers, calculating factorials, Fibonacci series, and storing student details in a structure.

Uploaded by

idab639
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

C Programming and Data Structure Record

This document contains programs to demonstrate basic C programming concepts like data types, operators, conditional statements, loops, functions, arrays and structures. The programs include converting cases of characters, arithmetic operators, swapping variables, checking leap years, determining positive/negative/zero numbers, checking even/odd numbers, finding the largest of two numbers, calculating the sum of N numbers, finding the square of N numbers, matrix addition, calculating area of squares and circles, checking Armstrong numbers, calculating factorials, Fibonacci series, and storing student details in a structure.

Uploaded by

idab639
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 111

1

Exp no: 1.a Converting lower case to upper case and vice versa

Program:
#include <stdio.h>
int main()
{
char a;
printf("enter a character:");
scanf("%c",&a);
2
if(islower(a))
{
printf("%c in uppercase is %c",a,toupper(a));
}
else
{
printf("%c in lowercase is %c",a,tolower(a));
}
}

Output:
enter a character:a
a in uppercase is A

1 SNA S.
311122105057
2
Exp no:1.b Arithmetic Operators

Program:
#include <stdio.h>
int main()
{
int a,b;
printf("enter two values separated by space ");
scanf("%d%d",&a,&b);
printf("sum of %d and %d is %d",a,b,a+b);
printf("subtraction of %d and %d is %d",a,b,a-b);
printf("product of %d and %d is %d",a,b,a*b);
printf("quotient of %d and %d is %d",a,b,a/b);
printf("remainder of %d and %d id %d",a,b,a%b);
}

Output:
enter two values separated by space 9 3
sum of 9 and 3 is 12
subtraction of 9 and 3 is 6
product of 9 and 3 is 27
quotient of 9 and 3 is 3
remainder of 9 and 3 id 0
Exp no:1.c Swap Two Variables

Program:
#include <stdio.h>
int main()
{
int a,b,t;
printf("enter two values separated by space ");
scanf("%d%d",&a,&b);
printf("values before swapping %d and %d",a,b);
t=a;
a=b;
b=t;
printf("values after swapping %d and %d",a,b);
}

Output:
enter two values separated by space 8 5
values before swapping 8 and 5
values after swapping 5 and 8
Exp no:1.d Leap Year

Program:
#include <stdio.h>
int main()
{
int a;
printf("enter a year ");
scanf("%d",&a);
if (a%4==0)
{
printf("%d is a leap year",a);
}
else
{
printf("%d is not a leap year",a);
}
}

Output:
enter a year 1992
1992 is a leap year

Exp no:1.e Negative , Positive or Zero


Program:
#include <stdio.h>
int main()
{
int a;
printf(" Enter any number:");
scanf("%d",&a);
if (a > 0)
printf("%d is positive number", a);
else if (a < 0){
printf("%d is a negative number.", a);
}
else{
printf(" You entered value zero.");
}
}

Output:
Enter any number: -8
-8 is a negative number.

Exp no:1.f Odd or Even


Program:
#include<stdio.h>
int main()
{
int a;
printf("Enter a number to check for even or odd:");
scanf("%d",&a);
if(a%2 == 0)
{
printf("%d is even",a);
}
else
{
printf("%d is odd",a);
}
}

Output:
Enter a number to check for even or odd:69
69 is odd
Exp no:1.g Largest Of Two Number

Program:
#include <stdio.h>
int main()
{
int a,b;
printf(" Enter the numbers :");
scanf("%d%d", &a, &b);
if(a > b)
printf("%d is Largest\n", a);
else if (b > a){
printf("%d is Largest\n", b);
}
else{
printf("Both are Equal\n");
}
}

Output:
Enter the numbers :9 5
9 is Largest
Exp no:1.h Sum of N numbers

Program:
#include <stdio.h>
int main()
{
int n,i,sum=0;
printf("enter the no of terms ");
scanf("%d",&n);
for (i=0;i<=n;i++)
{
sum=sum+i;
}
printf("sum of %d terms is %d",n,sum);
}

Output:
enter the no of terms 10
sum of 10 terms is 55
Exp no:2.a Square Of N Numbers

Program:
#include <stdio.h>
int main()
{
int i,n;
printf("enter the no.of terms");
scanf("%d",&n);
int arr[n];
for (i=0;i<n;i++){
printf("enter a value");
scanf("%d",&arr[i]);
}
for(i=0;i<n;i++){
printf("%d",arr[i]*arr[i]);
}
}

Output:
enter the no.of terms2
enter a value9
enter a value5
8125
Exp no.2.b Matrix Addition

Program:
#include <stdio.h>
int main()
{
int m1[3][3]={{1,2,3},{4,5,6},{1,2,3}}, m2[3][3]={{4,5,6},{1,2,3},
{7,8,9}},sum[3][3],i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
sum[i][j]=m1[i][j]+m2[i][j];
printf("%d\t",sum[i][j]);
}
printf("\n");
}
return 0;
}

Output:
5 7 9
5 7 9
8 10 12
Exp no:2.c Area of a Square/Circle

Program:
#include <stdio.h>
int s,r;
void aos()
{
printf("enter the side");
scanf("%d",&s);
printf("area = %d",s*s);

}
void aoc()
{
printf("enter the radius");
scanf("%d",&r);
printf("area = %d",3.14*r*r);
}
int main()
{
int opt;
printf("1.area of a square\n2.area of a circle\nenter the
option");
scanf("%d",&opt);
switch(opt)
{
case 1:
aos();
break;
case 2:
aoc();
break;
default:
printf("enter a valid option");
}
}

Output:
1.area of a square
2.area of a circle
enter the option1
enter the side2
area = 4
Exp no:2.d Armstrong Number

Program:
#include <stdio.h>
int main()
{
int a,b,c,d,e,sum,sum1,sum2,sum3;
printf("enter a number");
scanf("%d",&a);
b=a%10;
c=a/10;
d=c%10;
e=c/10;
sum1=d*d*d;
sum2=e*e*e;
sum3=b*b*b;
sum=sum1+sum2+sum3;
if(sum==a)
{
printf("%d is an armstrong number",a);
}
else{
printf("%d is not an armstrong number",a);
}
}
Output:
enter a number153
153 is an armstrong number

enter a number69
69 is not a armstrong number
Exp no:2.e Factorial

Program:

#include <stdio.h>
int main()
{
int fact(int);
int num,f;
printf(“Enter a value:”);
scanf(“%d”,& num);
f=fact(num);
printf(“The factorial is %d”,f);
return 0;
}
int fact(int n)
{
int s;
if(n==1)
return 1;
else
s= n*fact(n-1);
return s;
}
OUTPUT
Enter a value:5
The factorial is 120
Exp no:2.f Fibonacci Series

Program:
#include <stdio.h>
int main()
{
int num;
printf(“Enter the number of elements to be in the series : “);
scanf(“%d”, &num);
int i;
for (i = 0; i &lt; num; i++) {
printf(“%d \t “,fibonacci(i));
}
return 0;
}

int fibonacci(int num)


{
if (num == 0) {
return 0;
} else if (num == 1) {
return 1;
} else {
return fibonacci(num - 1) + fibonacci(num - 2);
}
}
OUTPUT
Enter the number of elements to be in the series : 5
01123

Enter the number of elements to be in the series : 7


0112358
Exp no:3.a Student Detail

Program:
#include <stdio.h>
int main()
struct student {
char name[50];
int age;
char dep[4];
int m1,m2,m3;
};
{
struct student s;
printf(“\nEnter student name: “);
scanf(“%s”,s.name);
printf(“\nEnter age of the student: “);
scanf(“%d”,&s.age);
printf(“\nenter department: “);
scanf(“%s”,s.dep);
printf(“\nenter mark_1: “);
scanf(“%d”,&s.m1);
printf(“\nenter mark_2: “);
scanf(“%d”,&s.m2);
printf(“\nenter mark_3: “);
scanf(“%d”,&s.m3);
printf(“\nname:%s”,s.name);
printf(“\nage:%d”,s.age);
printf(“\ndepartment:%s”,s.dep);
printf(“\nmark 1 :%d”,s.m1);
printf(“\nmark 2 :%d”,s.m2);
printf(“\nmark 3 :%d”,s.m3);
return 0;
}

OUTPUT
Enter student name: SARAH
Enter age of the student: 18
enter department: EEE
enter mark_1: 88
enter mark_2: 90
enter mark_3: 92
name:SARAH
age:18
department:EEE
mark 1 :88
mark 2 :90
mark 3 :92
Exp no:3.b Complex Numbers

Program:
#include <stdio.h>
typedef struct complexNumber {
int real;
int img;
} complex;
complex add(complex x, complex y);
int main()
{
complex a, b, sum;
printf(“Enter real part of a:”);
scanf(“%d”,&a.real);
printf(“Enter img part of a:”);
scanf(“%d”,&a.img);
printf(“Enter real part of b:”);
scanf(“%d”,& b.real);
printf(“Enter img part of a:”);
scanf(“%d”,& b.img);
printf(“\n a = %d + %di”, a.real, a.img);
printf(“\n b = %d + %di”, b.real, b.img);
sum = add(a, b);
printf(“\n sum = %d + %di”, sum.real, sum.img);
return 0;
}
complex add(complex x, complex y)
{
complex add;
add.real = x.real + y.real;
add.img = x.img + y.img;
return (add);
}

OUTPUT
Enter real part of a:35
Enter img part of a:12
Enter real part of b:24
Enter img part of a:11
a = 35 + 12i
b = 24 + 11i
sum = 59 + 23i
Exp no:3.c String Operations

Program:
#include <stdio.h>
int main()
{
char a[50],b[50],c[50];
int op,x;
printf(“Enter the first string: “);
scanf(“%s”,a);
printf(“Enter the second string: “);
scanf(“%s”,b);
printf(“Enter option: “);
scanf(“%d”,& op);
switch(op) {
case 1:
printf(“Length of the first string:%d”,strlen(a));
printf(“\nLength of the second string:%d”,strlen(b));
break;
case 2:
strcpy(c,a);
printf(“the copied string is %s”,c);
break;
case 3:
printf(“the concatenated string: %s”,strcat(a,b));
break;
case 4:
x=strcmp(a,b);
if (x==0)
printf(“Both the strings are equal”);
else
printf(“the given strings are not equal”);
break;
case 5:
printf(“The reversed strings are \n %s \n %s”,strrev(a),strrev(b));
break;
case 6:
printf(“invalid option”);
break;
}
return 0;
}

OUTPUT
Enter the first string: PROGRAM
Enter the second string: MING
Enter option: 3
the concatenated string: PROGRAMMING
Exp no:3.d Swapping With/Without Pointers

Program:
#include <stdio.h>
int main()
{
int a, b,s,opt;
printf(“\nEnter values for a and b = “);
scanf(“%d %d”, & a, & b);
printf(“\nBefore swapping: a = %d\nb = %d”,a,b);
printf(“\n1.for swapping with pointer\n2.for swapping without
pointer:\n”);
scanf(“%d”,& opt);

switch(opt)
{
case 1:
s=swap(&a, & b);
break;
case 2:
s=swap(a,b);
break;
default:
printf(“\nEnter valid option”);
break;
return 0;
}
}
int swap(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
printf(“\nAfter swapping: a = %d\nb = %d”,*x,*y);
}
int swapn(int a,int b)
{
a = a+b;
b = a-b;
a = a-b;
printf(“\nAfter swapping: a = %d\nb = %d”,a,b);
}

OUTPUT
Enter values for a and b = 123
234
Before swapping: a = 123
b = 234

1.for swapping with pointer


2.for swapping without pointer:
1
After swapping: a = 234
b = 123
Exp no:4.a File Handling

Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student {
int roll_no;
char name[20];
};
int main () {
FILE *of;
of= fopen ("structure.txt", "w");
if (of == NULL) {
fprintf(stderr, "Error to open the file");
exit (1);
}
struct Student inp1 = {311122106, "\nBergman"};
struct Student inp2 = {311122105, "\nSam"};
fwrite (&inp1, sizeof(struct Student), 1, of);
fwrite (&inp2, sizeof(struct Student), 1, of);
if(fwrite != 0)
printf("Contents to file written successfully !");
else
printf("Error writing file !");
fclose (of);
FILE *inf;
struct Student inp;
inf = fopen ("structure.txt", "r");
if (inf == NULL) {
fprintf(stderr, "Error to open the file");
exit (1);
}
while(fread(&inp, sizeof(struct Student), 1, inf))
printf ("roll_no = %d name = %s", inp.roll_no, inp.name);
fclose (inf);
}

Output:
Contents to file written successfully !
roll_no = 311122106
name = Bergman
roll_no = 311122105
name = Sam
Exp no:4.b Print Characters in a File

Program:
#include<stdio.h>
void main()
{
FILE *fp;
char ch,fname[20];
int n;
printf("Enter file Name :");
gets(fname);
fp=fopen(fname,"r");
if(fp==NULL)
{
printf("file can't be opened");
exit(0);
}
fseek(fp,0,0);
ch=fgetc(fp);
while(ch!=EOF)
{
n=ftell(fp);
printf("%c ",ch);
printf("%d \n",n);
ch=fgetc(fp);}
fclose(fp);}
Output:
Ex 4.c

FSEEK

PROGRAM

#include<stdio.h
> struct student
{
int rno;
char
name[50]; int
mark;
};
int main()
{
struct student s[20];
int i=0;
FILE *fp;
fp=fopen("out.txt","r"
); if (fp==NULL) {
printf("\nFile cannot be
opened"); return 0;
}
fseek(fp,0,SEEK_SET);
i=0;
while(1)
{ fread(&s[i],sizeof(s[i]),1,fp);
if(feof(fp))
break;
fflush(stdin
);
printf("\nroll no:%d",s[i].rno);
printf("\nname:%s",s[i].name);
printf("\nmarks:%d",s[i].mark);
i++;
}
fclose(fp);
return 0;
}
OUTPUT:

Exp no: 5 Real Time Application – Calculator


Program:
#include<stdio.h>
#include<math.h>
int main()
{
int a,b,c,d,x,y,opt;
printf("enter a");
scanf("%d",&a);
printf("enter b");
scanf("%d",&b);
printf("1.addition \n2.subtraction \n3.multiplication \
n4.division \n5.remainder \n6.squareroot \n7.exponent \
n8.sin \n9.cos \n10.tan");
scanf("%d",&opt);
switch(opt)
{
case 1:
printf("sum = %d",a+b);
break;
case 2:
printf("difference = %d",a-b);
break;
case 3:
printf("multiply = %d",a*b);
break;
case 4:
printf("quotient = %d",a/b);
break;
case 5:
printf("remainder = %d",a%b);
break;
case6:
printf("squareroot of a is %d and b is %d",sqrt(a),sqrt(b));
break;
case 7:
printf("power of a over b is %d and b over a is
%d",pow(a,b),pow(b,a));
break;
case 8:
c= a*180/3.14;
d= b*180/3.14;
printf("sin a = %f",sin(c));
printf("sin b = %f",sin(d));
break;
case 9:
c= a*180/3.14;
d= b*180/3.14;
printf("cos a = %f",cos(c));
printf("cos b = %f",cos(d));
break;
case 10:
c= a*180/3.14;
d= b*180/3.14;
printf("tan a = %f",tan(c));
printf("tan b = %f",tan(d));
break;
default:
printf("\nwrong option");
break;
}
}

Output:
enter a10
enter b25
1.addition
2.subtraction
3.multiplication
4.division
5.remainder
6.squareroot
7.exponent
8.sin
9.cos
10.tan3
multiply = 250
Exp no:6 Array Implementation of List

Program:
#include<stdio.h>
#define MAX 5
int a[MAX];
int n,i,po,pos=0,position,no,found=0,no_elm=0;
void create();
void insert();
void delete();
void search();
void display();
int main()
{
int op,con;
do
{
printf("\nOptions:\n");
printf("\n1.Create an array");
printf("\n2.Insert an array");
printf("\n3.Delete an array");
printf("\n4.Search an element");
printf("\n5.Display all elements");
printf("\nEnter an option:");
scanf("%d",&op);
switch(op)
{
case 1: create();
break;
case 2: insert();
break;
case 3: delete();

break;
case 4: search();
break;
case 5: display();
break;

default: printf("\nWrong option.Enter correct option.");


break;
}
printf("\n Do you want to continue(y/n):");
fflush(stdin);
scanf("%c",&con);
}while(con=='y');
return 0;
}
void create()
{
if(no_elm==0)
{
printf("\n Enter the number of elements:");
scanf("%d",&n);
if(n>MAX)
printf("\nThe limit is exceeded");
else
{
printf("\nEnter values:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
++no_elm;
//printf("\nNo of elements :%d\n",no_elm);

}
printf("\nThe elements of an array are sucessfully created");
}
}
else
printf("\n Cannot create array\n");
return;
}
void display()
{
if(no_elm==0)
{
printf("\nNo elements to print");

}
else
{
printf("\nThe elements are :");
for(i=0;i<no_elm;i++)
printf("%d\t",a[i]);}

return;
}
void insert()
{
if(no_elm==MAX)
printf("Array is full. cannot have further elements");
else
{
printf("\nEnter the element to be inserted and its position:(1 to
%d)",no_elm+1);
scanf("%d %d",&no,&position);

pos=position-1;
for(i=no_elm;i>pos;i--)
a[i]=a[i-1];
a[pos]=no;
++no_elm;
printf("\n no of elements : %d",no_elm);
printf("\nThe element is inserted successfully");
display();
}
return;
}
void delete()
{

if(no_elm==0)
printf("\nThe list is empty");
else
{
printf("Enter the position of the element to be deleted(1 to
%d)",no_elm);
scanf("%d",&position);
pos=position-1;
for(i=pos;i<no_elm;i++)
{
a[i]=a[i+1];
//printf("\ta[%d]=%d\t",pos,a[pos]);
}
--no_elm;
printf("\nThe element is deleted\n");
display();
}
return;

}
void search()
{

if(no_elm==0)
printf("\nNo elements in the list to search");
else
{
printf("\nEnter the elements to be searched:");
scanf("%d",&no);
for(i=0;i<no_elm;i++)
{
if(no==a[i])
{
printf("\nThe element is present in %d position ",i+1);
found=1;
break;
}
}
if(found==0)
printf("\nThe element is not present in the list");
}
return;
}
Output:
Options:
1.Create an array
2.Insert an array
3.Delete an array
4.Search an element
5.Display all elements
Enter an option:1

Enter the number of elements:3


Enter values:2
6
8

The elements of an array are sucessfully created


Do you want to continue(y/n):y

Options:

1.Create an array
2.Insert an array
3.Delete an array
4.Search an element
5.Display all elements
Enter an option:5

The elements are :6 8


Do you want to continue(y/n):
Exp no:7.a Array Implementation of Stack

Program:
#include<stdio.h>
#include<stdlib.h>
#define SIZE 3
int op,x,no,i,top=-1,a[SIZE];
void Push();
void Pop();
int IsEmpty();
int IsFull();
void Peek();
void display();

int main()
{
while(1)
{
printf("\n\nOptions\n");
printf("\n1.Push\n2.Pop\n 3.IsEmpty\n4.IsFull\n5.Peek\
n6.Display\n7.Exit\n");
printf("\nEnter an option:");
scanf("%d",&op);
switch(op)
{
case 1: Push(); break;
case 2:Pop();break;
case 3:x=IsEmpty();break;
case 4:x= IsFull();break;
case 5:Peek();break;
case 6: display();break;
case 7:exit(0);
default:printf("Wrong option. Try again....");
break;
}
}
return 0;
}
void Push()
{

if(!IsFull())
{
printf("\nEnter an element to insert:");
scanf("%d",&no);
//++top;

a[++top]=no;

printf("\nElement is inserted into the stack.");


}
return;
}
int IsFull()
{ //printf("\nTop====%d",top);
if(top>=SIZE-1)
{
printf("Overflow.Cannot insert elements");
return 1;
}
else
{
printf("\nNot full.Can Push elements");
return 0;
};
}
int IsEmpty()
{
if(top==-1)
{
printf("\nUnderflow.Stack is empty");
return 1;
}
else
{
printf("\nNot empty.Can Pop elements");
return 0;
}

}
void display()
{

if(!IsEmpty())
{
printf("\nThe Stack has ...\n");
for(i=top;i>=0;--i)
printf("\n| %5d |",a[i]);
}
//else
// printf("Stack is empty");
return;
}
void Pop()
{
if(!IsEmpty())
{
printf("\nThe top element %d is popped",a[top]);
--top;
}
return;
}
void Peek()
{
if(!IsEmpty())
printf("\nThe top element is %d",a[top]);
return;
}

Output:
Options

1.Push
2.Pop
3.IsEmpty
4.IsFull
5.Peek
6.Display
7.Exit

Enter an option:1

Not full.Can Push elements


Enter an element to insert:6
Element is inserted into the stack.

Options

1.Push
2.Pop
3.IsEmpty
4.IsFull
5.Peek
6.Display
7.Exit

Enter an option:1

Not full.Can Push elements


Enter an element to insert:8
Element is inserted into the stack.

Options

1.Push
2.Pop
3.IsEmpty
4.IsFull
5.Peek
6.Display
7.Exit

Enter an option:6
Not empty.Can Pop elements
The Stack has ...
8
6
Exp no:7.b Array Implementation of Queue

Program:
#include <stdio.h>
# define SIZE 3
void enqueue();
void dequeue();
void show();
int inp_arr[SIZE];
int Rear = - 1;
int Front = - 1;
main()
{
int ch;
while (1)
{
printf("1.Enqueue Operation\n");
printf("2.Dequeue Operation\n");
printf("3.Display the Queue\n");
printf("4.Exit\n");
printf("Enter your choice of operations : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
show();
break;
case 4:
exit(0);
default:

printf("Incorrect choice \n");


}
}
}

void enqueue()
{
int insert_item;
if (Rear == SIZE - 1)
printf("Overflow \n");
else
{
if (Front == - 1)
Front = 0;
printf("Element to be inserted in the Queue\n : ");
scanf("%d", &insert_item);
Rear = Rear + 1;
inp_arr[Rear] = insert_item;
}
}

void dequeue()
{
if (Front == - 1 || Front > Rear)
{
printf("Underflow \n");
return ;
}
else
{
printf("Element deleted from the Queue: %d\n", inp_arr[Front]);

Front = Front + 1;
}
}
void show()
{

if (Front == - 1)
printf("Empty Queue \n");
else
{
printf("Queue: \n");
for (int i = Front; i <= Rear; i++)
printf("%d ", inp_arr[i]);
printf("\n");
}
}

Output:
1.Enqueue Operation
2.Dequeue Operation
3.Display the Queue
4.Exit
Enter your choice of operations : 1
Element to be inserted in the Queue
:2
1.Enqueue Operation
2.Dequeue Operation
3.Display the Queue
4.Exit
Enter your choice of operations : 3
Queue:
2

Exp no: 8.a Linked List Implementation Of List

PROGRAM

#include
<stdio.h>
#include
<stdlib.h>
struct Node {
int data;
struct Node* next;
};

// Singly Linked List structure


struct SinglyLinkedList {
struct Node* head;
};

// Function to create a new node


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct
Node)); newNode->data = data;
newNode->next = NULL;
return newNode;
}
// Function to insert at the beginning of the linked list
void insertAtBeg(struct SinglyLinkedList* list, int data)
{
struct Node* newNode =
createNode(data); if (list->head == NULL)
{
list->head = newNode;
} else {
newNode->next = list-
>head; list->head =
newNode;
}
}

// Function to insert at the end of the linked list


void insertAtEnd(struct SinglyLinkedList* list, int data)
{ struct Node* newNode = createNode(data);
if (list->head == NULL)
{ list->head =
newNode;
} else {
struct Node* current = list-
>head; while (current->next !=
NULL) {
current = current->next;
}
current->next = newNode;
}
void insertAtDes(struct SinglyLinkedList* list, int data, int index)
{ struct Node* newNode = createNode(data);
struct Node* current = list-
>head; int position = 0;

if (position == index)
{ insertAtBeg(list,
data);
} else {
while (current != NULL && position + 1 != index)
{ position++;
current = current->next;
}
if (current != NULL) {
newNode->next = current-
>next; current->next =
newNode;
} else {
printf("Index is not present\n");
}
}
}
void update(struct SinglyLinkedList* list, int val, int index)
{ struct Node* current = list->head;
int position = 0;

while (current != NULL && position != index)


{ position++;
current = current->next;
}

if (current != NULL)
{ current->data =
val;
} else {
printf("Index is not present\n");
}
}

// Function to display the linked list


void display(struct SinglyLinkedList*
list) { struct Node* current = list-
>head; while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("\n");
}

void removeNode(struct SinglyLinkedList* list, int index)


{ struct Node* current = list->head;
int position = 0;

if (position == index) {
list->head = current-
>next; free(current);
} else {
while (current != NULL && position + 1 != index)
{ position++;
current = current->next;
}

if (current != NULL) {
struct Node* temp = current-
>next; current->next = temp-
>next; free(temp);
} else {
printf("Index is not present\n");
}
}
}
// Main function
int main() {
struct SinglyLinkedList
linkedList; linkedList.head =
NULL;

int n, i, choice, element, index;

printf("Enter the number of elements to insert in the linked list: ");


scanf("%d", &n);

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


{ printf("Enter the element:
"); scanf("%d", &element);
insertAtEnd(&linkedList, element);
}
printf("Original Linked List:\n");
display(&linkedList);

while (1) {
printf("\n1. Insert at the beginning\n2. Insert at the end\n3. Insert at desired position\
n”
“4. Update\n5. Remove\n6. Display\n7. Exit\n"); printf("Enter the choice: ");
scanf("%d", &choice);

switch (choice)
{ case 1:
printf("Enter the element: ");
scanf("%d", &element);
insertAtBeg(&linkedList,
element); break;
case 2:
printf("Enter the element: ");
scanf("%d", &element);
insertAtEnd(&linkedList,
element); break;
case 3:
printf("Enter the element: ");
scanf("%d", &element);
printf("Enter the index: ");
scanf("%d", &index);
insertAtDes(&linkedList, element, index);
break;
case 4:
printf("Enter the element: ");
scanf("%d", &element);
printf("Enter the index: ");
scanf("%d", &index);
update(&linkedList, element,
index); break;
case 5:
printf("Enter the index to remove:
"); scanf("%d", &index);
removeNode(&linkedList, index);
break;
case 6:
display(&linkedList);
break;
case 7:
return 0; // Exit the program
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}

OUTPUT :
Enter the number of elements to insert in the linked
list: 5 Enter the element: 5
Enter the
element: 4 Enter
the element: 3
Enter the
element: 2 Enter
the element: 1
Original Linked
List:
5 -> 4 -> 3 -> 2 -> 1 ->
1. Insert at the beginning
2. Insert at the end
3. Insert at desired position
4. Update
5. Remove
6. Display
7. Exit
Enter the choice: 1
Enter the element: 6
1. Insert at the beginning
2. Insert at the end
3. Insert at desired position
4. Update
5. Remove
6. Display
7. Exit
Enter the choice: 6
6 -> 5 -> 4 -> 3 -> 2 -> 1 ->

1. Insert at the beginning


2. Insert at the end
3. Insert at desired position
4. Update
5. Remove
6. Display
7. Exit
Enter the choice: 7

Exp no: 8.b Linked List Implementation Of Stack

Program:

#include <stdio.h>
#include <stdlib.h>
struct node {
int info;
struct node *ptr;
};
struct node *top, *top1, *temp;
int topelement();
void push(int data);
void pop();
void empty();
void display();
void destroy();
void stack_count();
void create();

int count = 0;

int main() {
int no, ch, e;
printf("\n 1 - Push \n 2 - Pop \n 3 - Top \n 4 - Is Empty \n 5 -
Exit \n 6 - Display \n 7 - Stack Count \n 8 - Destroy stack");
create();
while (1) {
printf("\n Enter choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("Enter data: ");
scanf("%d", &no);
push(no);
break;
case 2:
pop();
break;
case 3:
if (top == NULL)
printf("No elements in stack");
else {
e = topelement();
printf("\n Top element: %d", e);
}
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:
stack_count();
break;
case 8:
destroy();
break;
default:
printf(" Wrong choice, Please enter correct choice ");
break;
}
}

return 0;
}
void create() {
top = NULL;
}
void stack_count() {
printf("\n No. of elements in stack: %d", count);
}
void push(int data) {
if (top == NULL) {
top = (struct node *)malloc(1 * sizeof(struct node));
top->ptr = NULL;
top->info = data;
} else {
temp = (struct node *)malloc(1 * sizeof(struct node));
temp->ptr = top;
temp->info = data;
top = temp;
}

count++;
}
void display() {
top1 = top;
if (top1 == NULL) {
printf("Stack is empty\n");
return;
}
while (top1 != NULL) {
printf("%d ", top1->info);
top1 = top1->ptr;
}
}
void pop() {
top1 = top;
if (top1 == NULL) {
printf("\n Error: Trying to pop from empty stack\n");
return;
} else {
top = top1->ptr;
printf("\n Popped value: %d", top1->info);
free(top1);
count--;
}
}
int topelement() {
return (top->info);
}
void empty() {
if (top == NULL)
printf("\n Stack is empty");
else
printf("\n Stack is not empty with %d elements", count);
}
void destroy() {
top1 = top;
while (top1 != NULL) {
top1 = top->ptr;
free(top);
top = top1;
top1 = top1->ptr;
}
free(top1);
top = NULL;
printf("\n All stack elements destroyed");
count = 0;
}

Output:
1 - Push
2 - Pop
3 - Top
4 - Is Empty
5 - Exit
6 - Display
7 - Stack Count
8 - Destroy stack
Enter choice: 1
Enter data: 4
Enter choice: 1
Enter data: 8
Enter choice: 6
84
Enter choice: 3
Top element: 8
Enter choice:

Exp no: 8.c Linked List Implementation Of Queue

Program:

#include <stdio.h>
#include <stdlib.h>
struct node {
int info;
struct node *ptr;
};
struct node *front, *rear, *temp, *front1;
int frontelement();
void enq(int data);
void deq();
void empty();
void display();
void create();
void queuesize();

int count = 0;
int main() {
int no, ch, e;
printf("\n 1 - Enqueue\n 2 - Dequeue\n 3 - Front element\n 4 -
Empty\n 5 - Exit\n 6 - Display\n 7 - Queue size\n");
while (1) {
printf("\nEnter choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("Enter data: ");
scanf("%d", &no);
enq(no);
break;
case 2:
deq();
break;
case 3:
e = frontelement();
if (e != 0)
printf("Front element: %d\n", e);
else
printf("\n No front element in Queue as the queue is
empty\n");
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:
queuesize();
break;
default:
printf("Wrong choice, Please enter the correct choice\
n");
break;
}
}
return 0;
}
void create() {
front = rear = NULL;
}
void queuesize() {
printf("\n Queue size: %d\n", count);
}
void display() {
front1 = front;
if (front1 == NULL) {
printf("\n Error: Trying to display elements from an empty
queue\n");
return;
}
while (front1 != NULL) {
printf("%d ", front1->info);
front1 = front1->ptr;
}
printf("\n");
}
void enq(int data) {
if (rear == NULL) {
rear = (struct node *)malloc(1 * sizeof(struct node));
rear->ptr = NULL;
rear->info = data;
front = rear;
} else {
temp = (struct node *)malloc(1 * sizeof(struct node));
rear->ptr = temp;
temp->info = data;
temp->ptr = NULL;
rear = temp;
}

count++;
}
void deq() {
front1 = front;
if (front1 == NULL) {
printf("\n Error: Trying to dequeue from an empty queue\
n");
return;
} else if (front1->ptr != NULL) {
front1 = front1->ptr;
printf("\n Dequeued value: %d\n", front->info);
free(front);
front = front1;
} else {
printf("\n Dequeued value: %d\n", front->info);
free(front);
front = NULL;
rear = NULL;
}
count--;
}
int frontelement() {
if ((front != NULL) && (rear != NULL))
return (front->info);
else
return 0;
}
void empty() {
if ((front == NULL) && (rear == NULL))
printf("\n Queue empty\n");
else
printf("\n Queue not empty\n");
}

Output:
1 - Enqueue
2 - Dequeue
3 - Front element
4 - Empty
5 - Exit
6 - Display
7 - Queue size

Enter choice: 1
Enter data: 8
Enter choice: 1
Enter data: 4
Enter choice: 6
84

Enter choice: 7
Queue size: 2

Enter choice: 3
Front element: 8

Enter choice: 2

Exp no: 9.a Application Of List – Polynomial Addition


Program:
#include <stdio.h>
#include <stdlib.h>

// Structure to represent a term in a polynomial


struct Term {
int coefficient;
int exponent;
struct Term *next;
};

// Function to insert a new term into a polynomial


void insertTerm(struct Term **poly, int coef, int exp) {
struct Term *newTerm = (struct Term *)malloc(sizeof(struct
Term));
newTerm->coefficient = coef;
newTerm->exponent = exp;
newTerm->next = *poly;
*poly = newTerm;
}

// Function to add two polynomials


struct Term *addPolynomials(struct Term *poly1, struct Term
*poly2) {
struct Term *result = NULL;

while (poly1 != NULL || poly2 != NULL) {


if (poly1 == NULL) {
insertTerm(&result, poly2->coefficient, poly2-
>exponent);
poly2 = poly2->next;
} else if (poly2 == NULL) {
insertTerm(&result, poly1->coefficient, poly1-
>exponent);
poly1 = poly1->next;
} else {
if (poly1->exponent > poly2->exponent) {
insertTerm(&result, poly1->coefficient, poly1-
>exponent);
poly1 = poly1->next;
} else if (poly1->exponent < poly2->exponent) {
insertTerm(&result, poly2->coefficient, poly2-
>exponent);
poly2 = poly2->next;
} else {
// Add coefficients for same exponents
insertTerm(&result, poly1->coefficient + poly2-
>coefficient, poly1->exponent);
poly1 = poly1->next;
poly2 = poly2->next;
}
}
}

return result;
}

// Function to display a polynomial


void displayPolynomial(struct Term *poly) {
while (poly != NULL) {
printf("%dx^%d ", poly->coefficient, poly->exponent);
poly = poly->next;
if (poly != NULL) {
printf("+ ");
}
}
printf("\n");
}

// Function to free the memory allocated for a polynomial


void freePolynomial(struct Term *poly) {
struct Term *temp;
while (poly != NULL) {
temp = poly;
poly = poly->next;
free(temp);
}
}

int main() {
struct Term *poly1 = NULL;
struct Term *poly2 = NULL;
// Example polynomials: 3x^2 + 4x + 5 and 2x^3 + 1
insertTerm(&poly1, 3, 2);
insertTerm(&poly1, 4, 1);
insertTerm(&poly1, 5, 0);

insertTerm(&poly2, 2, 3);
insertTerm(&poly2, 1, 0);

printf("Polynomial 1: ");
displayPolynomial(poly1);

printf("Polynomial 2: ");
displayPolynomial(poly2);

struct Term *sum = addPolynomials(poly1, poly2);

printf("Sum: ");
displayPolynomial(sum);

// Free memory
freePolynomial(poly1);
freePolynomial(poly2);
freePolynomial(sum);

return 0;
}

Output:

Polynomial 1: 5x^0 + 4x^1 + 3x^2


Polynomial 2: 1x^0 + 2x^3
Sum: 3x^2 + 4x^1 + 2x^3 + 6x^0
Exp no: 9.e Application of Queue – First Come First Serve

Program:
#include<stdio.h>
int main()
{
int n,bt[30],wait_t[30],et[10],ct[10],i,j;
printf("Please enter the total number of processes(maximum
10):");
scanf("%d",&n);
printf("\nEnter The Process execution time");
for(i=0;i<n;i++)
{
printf("P[%d]:",i+1);
scanf("%d",&et[i]);
}
wait_t[0]=0;
for(i=1;i<n;i++)
{
wait_t[i]=0;
for(j=0;j<i;j++)
wait_t[i]+=et[j];
}
printf("\nProcess\tCompletion Time\tWaiting Time");
for(i=0;i<n;i++)
{
ct[i]=et[i]+wait_t[i];

printf("\nP[%d]\t\t%d\t\t%d",i+1,ct[i],wait_t[i]);
}
return 0;
}

Output:
Please enter the total number of processes(maximum 10):5
Enter The Process execution timeP[1]:2
P[2]:5
P[3]:6,
P[4]:9
P[5]:3
Process Completion Time Waiting Time
P[1] 2 0
P[2] 7 2
P[3] 13 7
P[4] 22 13
P[5] 25 22
Exp no: 9.b Application of Stack – Evaluation of Expression

Program:
#include <stdio.h>
#include <ctype.h>
#define MAX 20
typedef struct stack {
int data[MAX];
int top;
} stack;
void init(stack *);
int empty(stack *);
int full(stack *);
int pop(stack *);
void push(stack *, int);
int evaluate(char x, int op1, int op2);

int main() {
stack s;
char x;
int op1, op2, val;
init(&s);
printf("Enter the expression (e.g., 59+3*)\nSingle digit
operand and operators only: ");
while ((x = getchar()) != '\n') {
if (isdigit(x))
push(&s, x - '0');
else {
op2 = pop(&s);
op1 = pop(&s);
val = evaluate(x, op1, op2);
if (x == '/' && op2 == 0) {
printf("Error: Division by zero\n");
return 1;
}
push(&s, val);
}
}
val = pop(&s);
printf("\nValue of expression = %d\n", val);

return 0;
}
int evaluate(char x, int op1, int op2) {
if (x == '+')
return (op1 + op2);
if (x == '-')
return (op1 - op2);
if (x == '*')
return (op1 * op2);
if (x == '/')
return (op1 / op2);
if (x == '%')
return (op1 % op2);
return 0;
}
void init(stack *s) {
s->top = -1;
}
int empty(stack *s) {
return (s->top == -1);
}
int full(stack *s) {
return (s->top == MAX - 1);
}
void push(stack *s, int x) {
s->top = s->top + 1;
s->data[s->top] = x;
}
int pop(stack *s) {
int x;
x = s->data[s->top];
s->top = s->top - 1;
return x;
}

Output:
Enter the expression (e.g., 59+3*)
Single digit operand and operators only: 42+9*8-
Value of expression = 46
Exp no: 9.c Towers of Hanoi

Program:
#include <stdio.h>
void towers(int, char, char, char);
int main()
{
int num;
printf("Enter the number of disks : ");
scanf("%d", &num);
printf("The sequence of moves involved in the Tower of Hanoi
are :\n");
towers(num, 'A', 'C', 'B');
return 0;
}
void towers(int num, char frompeg, char topeg, char auxpeg)
{
if (num == 1)
{
printf("\n Move disk 1 from peg %c to peg %c", frompeg,
topeg);
return;
}
towers(num - 1, frompeg, auxpeg, topeg);
printf("\n Move disk %d from peg %c to peg %c", num,
frompeg, topeg);
towers(num - 1, auxpeg, topeg, frompeg);
}

Output:
Enter the number of disks : 4
The sequence of moves involved in the Tower of Hanoi are :

Move disk 1 from peg A to peg B


Move disk 2 from peg A to peg C
Move disk 1 from peg B to peg C
Move disk 3 from peg A to peg B
Move disk 1 from peg C to peg A
Move disk 2 from peg C to peg B
Move disk 1 from peg A to peg B
Move disk 4 from peg A to peg C
Move disk 1 from peg B to peg C
Move disk 2 from peg B to peg A
Move disk 1 from peg C to peg A
Move disk 3 from peg B to peg C
Move disk 1 from peg A to peg B
Move disk 2 from peg A to peg C
Move disk 1 from peg B to peg C
Exp no:9.d Conversion Of Infix To Postfix Expression

Program:
#include<stdio.h>
#include<ctype.h>
char stack[100];
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;
return 0;
}
int main()
{
char exp[100];
char *e, x;
printf("Enter the infix expression : ");
scanf("%s",exp);
printf("\n");
printf("Postfix expression = ");
e = exp;
while(*e != '\0')
{
if(isalnum(*e))
printf("%c ",*e);
else if(*e == '(')
push(*e);
else if(*e == ')')
{
while((x = pop()) != '(')
printf("%c ", x);
}
else
{
while(priority(stack[top]) >= priority(*e))
printf("%c ",pop());
push(*e);
}
e++;
}

while(top != -1)
{
printf("%c ",pop());
}return 0;
}

Output:
Enter the infix expression : 5*8+6/9
Postfix expression = 5 8 * 6 9 / +
Exp no: 10 Operations of Binary Trees

Program:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
//return a new node with the given value
struct node *createNode(int val)
{
struct node *newNode;
newNode = malloc(sizeof(struct node));
newNode->data = val;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
//inserts nodes in the binary search tree
struct node *insertNode(struct node *root, int val)
{
if(root == NULL)
return createNode(val);
if(root->data < val)
root->right = insertNode(root->right,val);
if(root->data > val)
root->left = insertNode(root->left,val);
return root;
}
//inorder traversal of the binary search tree
void inorder(struct node *root)
{
if(root == NULL)
return;
//traverse the left subtree
inorder(root->left);
//visit the root
printf("%d ",root->data);
//traverse the right subtree
inorder(root->right);
}
//preorder traversal of the binary search tree
void preorder(struct node *root)
{
if(root == NULL)
return;
//visit the root
printf("%d ",root->data);
//traverse the left subtree
preorder(root->left);
//traverse the right subtree
preorder(root->right);
}
//postorder traversal of the binary search tree
void postorder(struct node *root)
{
if(root == NULL)
return;
//traverse the left subtree
postorder(root->left);
//traverse the right subtree
postorder(root->right);
//visit the root
printf("%d ",root->data);
}
int main()
{
struct node *root = NULL;
int data;
char ch;
/* Do while loop to display various options to select from to
decide the input */
do
{
printf("\nSelect one of the operations::");
printf("\n1. insert a new node");
printf("\n2. display the nodes\n\t 2:Inorder Traversal\
t3:Postorder Traversal\t4.Preorder Traversal\n");
int choice;
printf("\nEnter an option:");
scanf("%d",&choice);
switch (choice)
{
case 1 :
printf("\nEnter the value to be inserted\n");
scanf("%d",&data);
root = insertNode(root,data);
break;
case 2 :
printf("\nInorder Traversal of the Binary Tree::\n");
inorder(root);
break;
case 3 :
printf("\nPostorder Traversal of the Binary Tree::\n");
postorder(root);
break;
case 4 :
printf("\nPreorder Traversal of the Binary Tree::\n");
preorder(root);
break;
default :
printf("Wrong Entry\n");
break;
}
printf("\nDo you want to continue (Type y or n)\n");
scanf(" %c",&ch);
} while (ch == 'Y'|| ch == 'y');
return 0;
}

Output:
Select one of the operations::
1. insert a new node
2. display the nodes
2:Inorder Traversal 3:Postorder
Traversal 4.Preorder Traversal

Enter an option:1
Enter the value to be inserted
5
Do you want to continue (Type y or n)
y
Select one of the operations::
1. insert a new node
2. display the nodes
2:Inorder Traversal 3:Postorder
Traversal 4.Preorder Traversal

Enter an option:1
Enter the value to be inserted
23
Do you want to continue (Type y or n)
y
Select one of the operations::
1. insert a new node
2. display the nodes
2:Inorder Traversal 3:Postorder
Traversal 4.Preorder Traversal

Enter an option:2
Inorder Traversal of the Binary Tree::
5 23
Do you want to continue (Type y or n)
y
Select one of the operations::
1. insert a new node
2. display the nodes
2:Inorder Traversal 3:Postorder
Traversal 4.Preorder Traversal

Enter an option:3
Postorder Traversal of the Binary Tree::
23 5
Do you want to continue (Type y or n)
Exp no: 11 Binary Search Tree

Program:

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

struct node {
int key;
struct node *left, *right;
};
struct node *newNode(int item) {
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
void inorder(struct node *root) {
if (root != NULL) {
inorder(root->left);
printf("%d -> ", root->key);
inorder(root->right);
}
}
struct node *insert(struct node *node, int key) {
// Return a new node if the tree is empty
if (node == NULL) return newNode(key);

// Traverse to the right place and insert the node


if (key < node->key)
node->left = insert(node->left, key);
else
node->right = insert(node->right, key);
return node;
}
// Find the inorder successor
struct node *minValueNode(struct node *node) {
struct node *current = node;
// Find the leftmost leaf
while (current && current->left != NULL)
current = current->left;
return current;
}
// Deleting a node
struct node *deleteNode(struct node *root, int key) {
// Return if the tree is empty
if (root == NULL) return root;
// Find the node to be deleted
if (key < root->key)
root->left = deleteNode(root->left, key);
else if (key > root->key)
root->right = deleteNode(root->right, key);
else {
// If the node is with only one child or no child
if (root->left == NULL) {
struct node *temp = root->right;
free(root);
return temp;
} else if (root->right == NULL) {
struct node *temp = root->left;
free(root);
return temp;
}

// If the node has two children


struct node *temp = minValueNode(root->right);
// Place the inorder successor in position of the node to be
deleted
root->key = temp->key;
// Delete the inorder successor
root->right = deleteNode(root->right, temp->key);
}
return root;
}

// Driver code
int main() {
struct node *root = NULL;
root = insert(root, 8);
root = insert(root, 3);
root = insert(root, 1);
root = insert(root, 6);
root = insert(root, 7);
root = insert(root, 10);
root = insert(root, 14);
root = insert(root, 4);

printf("Inorder traversal: ");


inorder(root);

printf("\nAfter deleting 10\n");


root = deleteNode(root, 10);
printf("Inorder traversal: ");
inorder(root);
}

Output:
Inorder traversal: 1 -> 3 -> 4 -> 6 -> 7 -> 8 -> 10 -> 14 ->
After deleting 10
Inorder traversal: 1 -> 3 -> 4 -> 6 -> 7 -> 8 -> 14 ->

Exp no:12.a Linear Search

Program:
#include<stdio.h>
int main()
{
int array[100], search, i, n, count = 0;
printf("Enter number of elements in array\n");
scanf("%d", &n);

printf("Enter %d numbers\n", n);

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


scanf("%d", &array[c]);

printf("Enter a number to search\n");


scanf("%d", &search);

for (i = 0; i < n; i++) {


if (array[i] == search) {
printf("%d is present at location %d.\n", search, c+1);
count++;
}
}
if (count == 0)
printf("%d isn't present in the array.\n", search);
else
printf("%d is present %d times in the array.\n", search,
count);

return 0;
}

Output:
Enter number of elements in array
5
Enter 5 numbers
98
63
51
20
47
Enter a number to search
20
20 is present at location 4.
20 is present 1 times in the array.
Exp no:12.b Binary Search

Program:
#include<stdio.h>
void binarysearch ();

int a[50], n, item, loc, beg, mid, end, i;


int main ()
{
printf ("\nEnter size of an array: ");
scanf ("%d", &n);
printf ("\nEnter elements of an array in sorted form:\n");
for (i = 0; i < n; i++)
scanf ("%d", &a[i]);
printf ("\nEnter ITEM to be searched: ");
scanf ("%d", &item);
0.

binarysearch ();
return 0;
}
void binarysearch ()
{
beg = 0;
end = n - 1;
mid = (beg + end) / 2;
while ((beg <= end) && (a[mid] != item))
{
if (item < a[mid])
end = mid - 1;
else
beg = mid + 1;
mid = (beg + end) / 2;
}
if (a[mid] == item)
printf ("\n\nITEM found at location %d", mid + 1);
else
printf ("\n\nITEM doesn't exist");
}

Output:
Enter size of an array: 7
Enter elements of an array in sorted form:
53
98
61
27
36
85
45
Enter ITEM to be searched: 27
ITEM found at location 4
Exp no: 13.a Sorting Techniques - Insertion Sort

Program:
#include<stdio.h>
int main()
{
int i,j,n,temp,a[30];
printf("Enter the number of elements:");
scanf("%d",&n);
printf("\nEnter the elements:");
for(i=0; i<n; i++) {
scanf("%d",&a[i]);
}
for(i=1; i<=n-1; i++) {
temp=a[i];
j=i-1;
while((temp<a[j])&&(j>=0)) {
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
printf("\nSorted list is:\n");
for(i=0; i<n; i++) {
printf("%d ",a[i]);
}
return 0;
}

Output:
Enter the number of elements:5

Enter the elements:5


6
3
9
7

Sorted list is:


35679
Exp no: 13.b Quick Sort

Program:
#include<stdio.h>
void quicksort(int num[25],int first,int last)
{
int i, j, pivot, temp;
if(first<last) {
pivot=first;
i=first;
j=last;
while(i<j) {
while(num[i]<=num[pivot]&&i<last)
i++;
while(num[j]>num[pivot])
j--;
if(i<j) {
//swap(temp,num[j]);
temp=num[i];
num[i]=num[j];
num[j]=temp;
}
}
(num[pivot],num[j]);
temp=num[pivot];
num[pivot]=num[j];
num[j]=temp;

quicksort(num,first,j-1);
quicksort(num,j+1,last);
}
}

int main()
{
int i,n, number[25];
printf("Enter the number of elements:");
scanf("%d",&n);
printf("Enter %d elements: ", n);
for(i=0; i<n; i++)
scanf("%d",&number[i]);
quicksort(number,0,n-1);
printf("Order of Sorted elements: ");
for(i=0; i<n; i++)
printf(" %d",number[i]);
return 0;
}

Output:
Enter the number of elements:5
Enter 5 elements: 9
6
5
78
21
Order of Sorted elements: 5 6 9 21 78
Exp no: 13.c Merge sort

Program:
#include<stdio.h>
void mergesort(int a[],int i,int j);
void merge(int a[],int i1,int j1,int i2,int j2);
int main()
{
int a[30],n,i;
printf("Enter the number of elements:");
scanf("%d",&n);
printf("Enter the elements of the array:");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
mergesort(a,0,n-1);
printf("\nSorted array is :");
for(i=0; i<n; i++)
printf("%d ",a[i]);
return 0;
}
void mergesort(int a[],int i,int j)
{
int mid;
if(i<j) {
mid=(i+j)/2;
mergesort(a,i,mid);
mergesort(a,mid+1,j);
merge(a,i,mid,mid+1,j);
}
}
void merge(int a[],int i1,int j1,int i2,int j2)
{
int temp[50];
int i,j,k;
i=i1;
j=i2;
k=0;
while(i<=j1 && j<=j2) {
if(a[i]<a[j])
temp[k++]=a[i++];

else
temp[k++]=a[j++];
}
while(i<=j1)
temp[k++]=a[i++];
while(j<=j2)
temp[k++]=a[j++];
for(i=i1,j=0; i<=j2; i++,j++)
a[i]=temp[j];
}

Output:
Enter the number of elements:5
Enter the elements of the array:45
558
3
21
6

Sorted array is :3 6 21 45 558

Exp no:14.a Hashing Table Collision Techniques – Linear Probing

Program:
#include <stdio.h>
#include<stdlib.h>
#define TABLE_SIZE 10
int h[TABLE_SIZE]={0};
void insert()
{
int key,index,i,flag=0,hkey;
printf("\nenter a value to insert into hash table\n");
scanf("%d",&key);
//hkey=key%TABLE_SIZE;
for(i=0;i<TABLE_SIZE;i++)
{
index=(key+i)%TABLE_SIZE;
if(h[index] == 0)
{
h[index]=key;
break;
}
}
if(i == TABLE_SIZE)
printf("\nelement cannot be inserted\n");
}
void display()
{
int i;
printf("\nelements in the hash table are \n");
for(i=0;i< TABLE_SIZE; i++)
printf("\nat index %d \t value = %d",i,h[i]);
}
int main()
{
int opt,i;
while(1)
{
printf("\nPress 1. Insert\t 2. Display \t3.Exit \n");
scanf("%d",&opt);
switch(opt)
{
case 1:
insert();
break;
case 2:
display();
break;
case 4:exit(0);
}
}
return 0;
}

Output:
Press 1. Insert 2. Display 3.Exit
1
enter a value to insert into hash table
2
Press 1. Insert 2. Display 3.Exit
1
enter a value to insert into hash table
78
Press 1. Insert 2. Display 3.Exit
1
enter a value to insert into hash table
89
Press 1. Insert 2. Display 3.Exit
1
enter a value to insert into hash table
55
Press 1. Insert 2. Display 3.Exit
2
elements in the hash table are

at index 0 value = 0
at index 1 value = 0
at index 2 value = 2
at index 3 value = 0
at index 4 value = 0
at index 5 value = 5
at index 6 value = 55
at index 7 value = 0
at index 8 value = 78
at index 9 value = 89
Press 1. Insert 2. Display 3.Exit

Exp no: 14.b Quadratic Probing

Program:
#include <stdio.h>
#include<stdlib.h>
#define TABLE_SIZE 10

int h[TABLE_SIZE]={0};

void insert()
{

int key,index,i,c1=1,c2=3;
printf("\nenter a value to insert into hash table\n");
scanf("%d",&key);
//hkey=key%TABLE_SIZE;
for(i=0;i<TABLE_SIZE;i++)
{

index=(key+c1*i+c2*i*i)%TABLE_SIZE;

if(h[index] == 0)
{
h[index]=key;
break;
}
}
if(i == TABLE_SIZE)
printf("\nelement cannot be inserted\n");
}

void display()
{
int i;

printf("\nelements in the hash table are \n");

for(i=0;i< TABLE_SIZE; i++)

printf("\nat index %d \t value = %d",i,h[i]);

}
int main()
{
int opt,i;
while(1)
{
printf("\nPress 1. Insert\t 2. Display \t3. Exit \n");
scanf("%d",&opt);
switch(opt)
{
case 1:
insert();
break;
case 2:
display();
break;

case 3:exit(0);
}
}
return 0;
}

Output:
Press 1. Insert 2. Display 3. Exit
1

enter a value to insert into hash table


8

Press 1. Insert 2. Display 3. Exit


1

enter a value to insert into hash table


6

Press 1. Insert 2. Display 3. Exit


1

enter a value to insert into hash table


5

Press 1. Insert 2. Display 3. Exit


1

enter a value to insert into hash table


79

Press 1. Insert 2. Display 3. Exit


1

enter a value to insert into hash table


96

Press 1. Insert 2. Display 3. Exit


2

elements in the hash table are

at index 0 value = 96
at index 1 value = 0
at index 2 value = 0
at index 3 value = 0
at index 4 value = 0
at index 5 value = 5
at index 6 value = 6
at index 7 value = 0
at index 8 value = 8
at index 9 value = 79
Press 1. Insert 2. Display 3. Exit

You might also like