Nothing Special   »   [go: up one dir, main page]

Cds Lab Manual

Download as pdf or txt
Download as pdf or txt
You are on page 1of 101

WEEK-1(A)

i) Program to add two integer numbers


Aim: To write a C program to add two numbers
Flowchart:

Algorithm:

Algorithm to find the sum of two numbers:

Step 1: Start
Step 2: Declare variables num1, num2 and sum.
Step 3: Read values for num1, num2.
Step 4: Add num1 and num2 and assign the result to a variable sum.
Step 5: Display sum
Step 6: Stop

Program:
#include <stdio.h>
#include<conio.h>
int main()
{
int num1, num2, sum;
clrscr();
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
sum = num1 + num2;
printf("Sum of the entered numbers: %d", sum);
return 0;
}
Output:
Enter first number: 20
Enter second number: 19
Sum of the entered numbers: 39
Result:
Thus the given sum of two numbers is executed successfully.
ii) Write a program to find area and circumference of circle
Aim:-
To write a C program to find area and circumference of circle whose radius is entered
by the user
Flowchart:

Algorithm: -
Step 1: Start
Step 2: input R
Step 3: let pi = 3.14
Step 4: area = pi * R * R
Step 5: circum = 2 * pi * R
Step 6: print area, circum ,
Step 7: stop
Program:-
#include<stdio.h>
#include<conio.h>
#define PI 3.14
void main()
{
int rad;
float area, circum;
printf("\nEnter radius of circle: ");
scanf("%d", &rad);
area = PI * rad * rad;
printf("\nArea of circle : %f ", area);
circum= 2 * PI * rad;
printf("\nCircumference : %f ", circum);
getch();
}
Output:-
Enter radius of a circle : 10
Area of circle : 31.4
Circumference : 62.8
Result:-
Thus the given area and circumference of circle are executed successfully.
iii) Write a program to swap two numbers

Aim: To write a C program to swap two numbers

Flowchart:-

Start

Read X and Y

Z=X

X=Y

Y=Z

Print X and Y

Stop
Algorithm:-
Step 1 : Start
Step 2 : READ X and Y
Step 3 :Z = X
Step 4 :X = Y
Step 5 :Y = Z
Step 6 : PRINT X , Y
Step 7 : Stop
Program:-
#include<stdio.h>
#include<conio.h>

void main()
{
int x, y, z;
clrscr();
printf("\nEnter the values of x and y \n");
scanf("%d%d", &x,&y);
printf("Values before swapping - \n x = %d, y = %d \n\n", x, y);
z = x;
x = y;
y = z;
printf("Values after swapping - \n x = %d, y = %d \n", x, y);
getch();
}

Output:-
Enter the values of x and y
Values before swapping -
x = 11, y = 99

Values after swapping -


x = 99, y = 11

Result:-
Thus the given swapping of two numbers is executed successfully.
WEEK-1(B)

i)Write a Program to find the Largest among three numbers.

Aim: To write a C program to find the largest of three numbers.

Flowchart:

Algorithm:
Step 1 : Start
Start 2 : Input a, b, c
Start 3 : if a > b goto step 4, otherwise goto step 5
Start 4 : if a > c goto step 6, otherwise goto step 8
Start 5 : if b > c goto step 7, otherwise goto step 8
Start 6 : Output "a is the largest", goto step 9
Start 7 : Output "b is the largest", goto step 9
Start 8 : Output " c is the largest", goto step 9
Start 9 : Stop

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

void main()
{
int a, b, c;
clrscr();
printf("Enter three different numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a >= b && a >= c)
printf("%d is the largest number.", a);
else if (b >= a && b >= c)
printf("%d is the largest number.", b);
else
printf("%d is the largest number.", c);;

getch();
}

OUTPUT:
Enter three different numbers: 10 20 30
30 is the largest number.

RESULT:
Thus the largest among the three numbers program is executed successfully.

ii) Write a program check whether a given character is vowel or not.

Aim:- To write a program in C to check whether the character entered by the user is
vowel or not.

Flowchart:

Algorithm:

Step 1: Start
Step 2: Declare character type variable ch
Step 3: Read ch from User
Step 4: // Checking both lower and upper case vowels.
IF (ch == 'a' || ch == 'A' ||
ch == 'e' || ch == 'E' ||
ch == 'i' || ch == 'I' ||
ch == 'o' || ch == 'O' ||
ch == 'u' || ch == 'U' )

Print "Vowel"

ELSE

Print "Consonant"

Step 5: Stop

Program :
#include <stdio.h>
#include<conio.h>
Void main()
{
charch;
clrscr();
printf("\n Enter any character : ");
scanf("%c", &ch);
switch(ch)
{
case ‘A’:
case ‘a’:
printf("\n %c is VOWEL", ch);
break;
case ‘E’:
case ‘e’:
printf("\n %c is VOWEL", ch);
break;
case ‘I’:
case ‘i’:
printf("\n %c is VOWEL", ch);
break;
case ‘O’:
case ‘o’:
printf("\n %c is VOWEL", ch);
break;
case ‘U’:
case ‘u’:
printf("\n %c is VOWEL", ch);
break;
default: printf("\n %c is not a vowel", ch);
}
getch();
}

Output:
Enter any character : j
j is not a vowel
WEEK - 2
Write C programs that use both recursive and non-recursive functions
i) To find the factorial of a given integer.
ii) To solve Towers of Hanoi problem.

i) To find the factorial of a given integer.


Aim- To write a C program to find the Factorial of a given integer in recursive function

Flowchart:

Algorithm:

Step 1: Start
Step 2: Read number n
Step 3: Call factorial(n)
Step 4: Print factorial f
Step 5: Stop

factorial(n)
Step 1: If n==1 then return 1
Step 2: Else
f=n*factorial(n-1)
Step 3: Return f

Program:

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

int fact(int n)
{
if (n==0)
return 1;
return fact(n-1)*n;
}

void main()
{
int n;
clrscr();
printf("Enter a positive number : ");
scanf("%d",&n);
printf("\nThe factorial of a number is %d",fact(n));
getch();
}

Output:
Enter a positive number : 5
The factorial of a number is 120

Result:
Thus the Factorial of a given integer using recursion is executed successfully

i) To find the factorial of a given integer.


Aim- To write a C program to find the Factorial of a given integer in non-recursive function
Flowchart:
Algorithm:

1. Start - our algorithm starts here.


2. We load input data - a natural number n, which is an argument to the function factorial.
3. We initiate two auxiliary variables:
i - it will accept subsequent natural values from 1 (this value is initially set) to n,
s - in this variable the value of the product of consecutive natural numbers is stored, we
start from 1.
4. We check if the value of variable i is less than or equal to n. If the condition from point 4
is satisfied, we multiply the value s of the product of the numbers by i. Then we increase
the value of the variable i by 1, that is, we move to the next natural number and return to
point 4 of the algorithm. Points 4-5 will be executed as long as the value of the variable i
exceed the value stored in the variable n.
5. After calculating the product of consecutive natural numbers from 1 to n, we print the
result contained in the variable s.
6. Stop - end of the algorithm.

Program:
#include <stdio.h>
#include<conio.h>
void main() {
int n, i;
unsigned long long fact = 1;
clrscr();
printf("Enter an integer: ");
scanf("%d", &n);

// shows error if the user enters a negative integer


if (n < 0)
printf("Error! Factorial of a negative number doesn't exist.");
else {
for (i = 1; i <= n; ++i) {
fact *= i; //fact=fact*i
}
printf("Factorial of %d = %llu", n, fact);
}
getch();
}

Output:
Enter an integer: 6
Factorial of 6 = 720

Result:
The Factorial of a given integer using non-recursion is executed successfully.
ii) To solve Towers of Hanoi problem.
Aim: To write a C program to solve the Tower of Hanoi problem
Flowchart:
Algorithm:
START
Procedure Hanoi(disk, source, dest, aux)

IF disk == 1, THEN
move disk from source to dest
ELSE
Hanoi(disk - 1, source, aux, dest) // Step 1
move disk from source to dest // Step 2
Hanoi(disk - 1, aux, dest, source) // Step 3
END IF

END Procedure
STOP

Program:
#include<stdio.h>
#include<conio.h>
void towers(int, char, char, char);

void main()
{
int n;
char source,dest, aux;
clrscr();
printf("Enter the number of disks : ");
scanf("%d", &n);
printf("\nSOURCE = %c DESTINATION = %c AUXILIARY = %c\n", 'A', 'C', 'B');
towers(n,'A','C','B');
getch();
}

void towers(int n, char source, char dest, char aux)


{

if(n == 1)
printf("\nMove from %c to %c", source,dest);
else
{
towers(n-1,source, aux, dest);
printf("\nMove from %c to %c", source,dest);
towers(n-1,aux, dest, source);
}
printf("\n");
}
Output:
Enter the number of disks : 3

SOURCE = A DESTINATION = C AUXILIARY = B

Move from A to C

Move from A to B
Move from C to B

Move from A to C
Move from B to A

Move from B to C
Move from A to C

Result:
The Tower of Hanoi problem is successfully executed.
WEEK-3
a) Write a C program to find both the largest and smallest number in a list of integers.
b) Write a C program that uses functions to perform the following:
i) Addition of Two Matrices ii) Multiplication of Two Matrices

a) Write a C program to find both the largest and smallest number in a list of integers.

Aim: To write a C program to find the largest and smallest number in a list of
integers.
Algorithm:

1. Input the array elements.


2. Initialize small = large = arr[0]
3. Repeat from i = 2 to n
4. if(arr[i] > large)
5. large = arr[i]
6. if(arr[i] < small)
7. small = arr[i]
8. Print small and large.

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

void main()
{
int a[50],i,n,large,small;
clrscr();
printf(“\nEnter the number of elements : “);
scanf(“%d”,&n);
printf(“\nInput the array elements : “);
for(i=0;i<n;++i)
scanf(“%d”,&a[i]);

large=small=a[0];

for(i=1;i<n;++i)
{
if(a[i]>large)
large=a[i];

if(a[i]<small)
small=a[i];
}

printf(“\nThe smallest element is %d\n”,small);


printf(“\nThe largest element is %d\n”,large);

getch();
}

Output:
Enter the number of elements : 5

Input the array elements : 1 2 3 4 5

The smallest element is 1

The largest element is 5

Result:
Thus the largest and smallest integer from the list of integers in successfully executed.

b) Write a C program that uses functions to perform the following:


i) Addition of Two Matrices ii) Multiplication of Two Matrices

i) Addition of Two Matrices


Aim: To write a C program to add matrices
Flowchart:
Algorithm:
Step 1: Start

Step 2: Declare matrix A[r][c];


and matrix B[r][c];
and matrix C[r][c]; r= no. of rows, c= no. of columns

Step 3: Read r, c, A[][] and B[][]

Step 4: Declare variable i=0, j=0

Step 5: Repeat until i < r

5.1: Repeat until j < c

C[i][j]=A[i][j] + B[i][j]

Set j=j+1

5.2: Set i=i+1

Step 6: C is the required matrix after addition

Step 7: Stop

Program:-
#include <stdio.h>
#include<conio.h>
int main() {
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
clrscr();
printf("Enter the number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter the number of columns (between 1 and 100): ");
scanf("%d", &c);

printf("\nEnter elements of 1st matrix:\n");


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element a[%d][%d]: ", i , j );
scanf("%d", &a[i][j]);
}

printf("Enter elements of 2nd matrix:\n");


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element b[%d][%d]: ", i , j );
scanf("%d", &b[i][j]);
}

// adding two matrices


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
sum[i][j] = a[i][j] + b[i][j];
}

// printing the result


printf("\nSum of two matrices: \n");
for (i = 0; i < r; ++i)
{
printf("\n");
for (j = 0; j < c; ++j) {
printf("%d ", sum[i][j]);

}
}
getch();
}

Output:
Enter the number of rows (between 1 and 100): 2
Enter the number of columns (between 1 and 100): 2

Enter elements of 1st matrix:


Enter element a[0][0]: 1
Enter element a[0][1]: 2
Enter element a[1][0]: 3
Enter element a[1][1]: 4
Enter elements of 2nd matrix:
Enter element b[0][0]: 5
Enter element b[0][1]: 6
Enter element b[1][0]: 7
Enter element b[1][1]: 8

Sum of two matrices:

6 8
10 12

Result:
The sum of two matrices is successfully executed.
ii) Multiplication of two matrices

Aim: To write a C Program to multiply two matrices

Flowchart:

Algorithm:
Step 1: Start the Program.

Step 2: Enter the row and column of the first (a) matrix.

Step 3: Enter the row and column of the second (b) matrix.

Step 4: Enter the elements of the first (a) matrix.

Step 5: Enter the elements of the second (b) matrix.

Step 6: Print the elements of the first (a) matrix in matrix form.
Step 7: Print the elements of the second (b) matrix in matrix form.

Step 8: Set a loop up to row.

Step 9: Set an inner loop up to the column.

Step 10: Set another inner loop up to the column.

Step 11: Multiply the first (a) and second (b) matrix and store the element in the third matrix

(c)

Step 12: Print the final matrix.

Step 13: Stop the Program.

Program:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void main(){
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
clrscr();
printf("enter the number of row=");
scanf("%d",&r);
printf("enter the number of column=");
scanf("%d",&c);
printf("enter the first matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the second matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}

printf("multiply of the matrix=\n");


for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}
}
//for printing result
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
getch();
}

Output:
enter the number of row=2
enter the number of column=2
enter the first matrix element=
1
2
3
4
enter the second matrix element=
1
2
3
4
multiply of the matrix=
7 10
15 22

Result:- The multiplication of two matrices is successfully executed.


WEEK -4
Write a C program that uses functions to perform the following operations:
i) To insert a sub-string in to a given main string from a given position.
ii) Given a string ―a$bcd./fg‖ find its reverse without changing the position of special
characters. (Example input:a@gh%;j and output:j@hg%;a)

i) To insert a sub-string in to a given main string from a given position.


Aim: To write C Program to insert a sub-string into a given main string from a
given position
Program:-
#include<stdio.h>
#include<conio.h>

void main()
{
char str[100], pat[100], insert_str[200];
inti,j,k,pos;
clrscr();
printf("\nEnter the main string: ");
gets(str);

printf("\nEnter the string to be in inserted: ");


gets(pat);

printf("\nEnter the position to be inserted: ");


scanf("%d",&pos);

for(i=0,k=0;i<pos;i++,k++)
insert_str[k]=str[i];

for(j=0;pat[j]!='\0';j++,k++)
insert_str[k]=pat[j];

for(;str[i]!='\0';i++,k++)
insert_str[k]=str[i];

insert_str[k]='\0';

printf("\nThe main string after inserting string is: ");


puts(insert_str);

getch();
}

Output:
Enter the main string: Hello Earth
Enter the string to be in inserted: Mars and

Enter the position to be inserted: 6

The main string after inserting string is: Hello Mars and Earth

Result:
The insertion of string in the main string is successfully executed.

ii) Given a string ―a$bcd./fg‖ find its reverse without changing the position of special
characters. (Example input:a@gh%;j and output:j@hg%;a)

Aim: To write a C program to find the reverse without changing the position of special
characters.
Program:

#include<stdio.h>
#include<string.h>
#include<stdbool.h>

// Returns true if x is an aplhabetic


// character, false otherwise
bool isAlphabet(char x)
{
return ( (x >= 'A' && x <= 'Z') ||
(x >= 'a' && x <= 'z') );
}

void reverse(char str[])


{
// Initialize left and right pointers
int r = strlen(str) - 1, l = 0;

// Traverse string from both ends until


// 'l' and 'r'
while (l < r)
{
// Ignore special characters
if (!isAlphabet(str[l]))
l++;
else if(!isAlphabet(str[r]))
r--;

else
{
chartmp = str[l];
str[l] = str[r];
str[r] = tmp;

l++;
r--;
}
}
}

int main()
{
charstr[] = "a@gh%;j";
puts(str);
reverse(str);
puts(str);
return 0;
}

Output:
a@gh%;j
j@hg%;a

Result:
The reverse of the string without effecting the special character is successfully executed.
WEEK-5
From a given paragraph perform the following using built-in functions:
a. Find the total number of words.
b. Capitalize the first word of each sentence.
c. Replace a given word with another word.

a. Find the total number of words.


Aim: To write a C Program to find the total number of words in a given paragraph.

Program:

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

void main()
{
intwc,i=0;
charstr[100];
clrscr();
printf("Enter the string :");
gets(str);
while(str[i]!='\0')
{
if(str[i]==' ' &&str[i+1]!=' ')
wc++;
i++;
}
wc++;
printf("World count=%d",wc);
getch();
}

Output:
Enter the string :An Apple a day keeps the Doctor Away
World count=8
Result:
The word count in a string is successfully executed.
b. Capitalize the first word of each sentence.
Aim: To write a C Program to Capitalize the first word of each sentence
Program:
#include <stdio.h>
Int firstupper(char str[], int n) {
int i;
for(i = 0; i<n; i++) {
if (i == 0 &&str[i] != ' ' || str[i] != ' ' &&str[i-1] == ' ') {
if(str[i] >= 'a' &&str[i]<='z') {
str[i] = (char)(('A'-'a') + str[i] );
}
} else if (str[i] >= 'A' &&str[i] <= 'Z') {
str[i] = (char)(str[i] + ('a' - 'A'));
}
}
return 0;
}
void main() {
charstr[] = "my favourite fruit is apple";
int n = sizeof(str)/sizeof(str[0]);
firstupper(str, n);
printf("%s\n", str);
}
Output:
My Favourite Fruit Is Apple

c. Replace a given word with another word.


Aim: To write a C program to replace a word with another word
Program:

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

voidReadStrings();
voidPatternMatching(char[], char[], char[]);
char STR[100],PAT[100],REP[100];

int main()
{
ReadStrings(STR,PAT,REP);
PatternMatching(STR, PAT, REP);
}
voidReadStrings(char STR[100], char PAT[100], char REP[100])
{
printf("Enter the main string STR : ");
gets(STR);
printf("Enter the pattern string PAT : ");
gets(PAT);
printf("Enter the replace string REP : ");
gets(REP);
}

voidPatternMatching(char STR[100], char PAT[100], char REP[100])


{
inti,j,k,m,c,flag,count;
i=m=c=j=count=flag=0;
char RESULT[100];
while(STR[c] != '\0')
{
if(STR[m] == PAT[i])
{
i++;
m++;

if(PAT[i] =='\0')
{
for(k=0;REP[k] !='\0';k++,j++)
RESULT[j]=REP[k];
i=0;
c=m;
count++;
flag =1;
}

}
else
{

RESULT[j]=STR[c];
j++;
c++;
m=c;
i=0;
}
}
if(flag == 0)
printf("Pattern does not found\n");
else
{
RESULT[j]='\0';
printf("Pattern found %d times\n",count);
printf("The resultant string is\n%s\n",RESULT);
}
}
Output:
Enter the main string STR :SpiderMan
Enter the pattern string PAT : Spider
Enter the replace string REP : Bat
Pattern found 1 times
The resultant string is
BatMan
WEEK-6
a) Write a C Program to perform various arithmetic operations on pointer variables.
b) Write a C Program to demonstrate the following parameter passing mechanisms:
i) call-by-value ii) call-by-reference

a) Write a C Program to perform various arithmetic operations on pointer variables.


Aim: To write a C program to perform arithmetic operations on pointer variables
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int num1=2,num2=3,*ptr1,*ptr2,sum=0,mul=1,div=1;
clrscr();
ptr1=&num1;
ptr2=&num2;
sum=*ptr1+*ptr2;
mul=sum*(*ptr2);
*ptr1+=1;
div=30+(*ptr1)/(*ptr2);
printf("Sum=%d\n",sum);
printf("Multiply=%d\n",mul);
printf("Division=%d",div);
getch();
}
OUTPUT:
Sum=5
Multiply=15
Division=31
b) Write a C Program to demonstrate the following parameter passing mechanisms:
i) call-by-value ii) call-by-reference

i) Aim: To write a C program to demonstrate call by value mechanism

Program:
#include<stdio.h>
#include<conio.h>
void swapv(int,int);

void main()
{
int a,b;
clrscr();
printf("Enter the value of a: ");
scanf("%d",&a);
printf("\nEnter the value of b: ");
scanf("%d",&b);
printf("Before Swapping, FirstNumber=%d SecondNumber=%d",a,b);
swapv(a,b);
getch();
}

void swapv(int x,int y)


{
int t;
t=x;
x=y;
y=t;
printf("\nAfter Swapping, First Number=%d SecondNumber=%d",x,y);
}

Output:
Enter the value of a: 2
Enter the value of b: 4

Before Swapping, FirstNumber=2 SecondNumber=4


After Swapping, First Number=4 SecondNumber=2

ii) Aim: To write a C program to demonstrate call by reference mechanism

Program:
#include<stdio.h>
void swapr(int*,int*);
void main()
{
int a,b;
printf("Enter the value of a: ");
scanf("%d",&a);
printf("\nEnter the value of b: ");
scanf("%d",&b);
printf("Before Swapping, FirstNumber=%d SecondNumber=%d",a,b);
swapr(&a,&b);
printf("\n\nAfter Swapping, FirstNumber=%d SecondNumber=%d",a,b);
}

void swapr(int *x,int *y)


{
int t;
t=*x;
*x=*y;
*y=t;
}

Output:
Enter the value of a: 5
Enter the value of b: 6

Before Swapping, FirstNumber=5 SecondNumber=6

After Swapping, FirstNumber=6 SecondNumber=5


WEEK- 7
Write C programs that implement stack (its operations) using
i) Arrays
ii) Pointers

Write C programs that implement stack (its operations) usingArrays


Aim: To write a C program to implement stack using Arrays
Program:

#include<stdio.h>
#include<conio.h>
#define MAX 5
int top=-1,s[MAX];

void push();
void pop();
void display();

int main()
{
int choice;
clrscr();

do{

printf("\n\n1.Push an element in a stack\n");


printf("2. Pop an element from stack\n");
printf("3. Display elements from stack\n");
printf("4. Exit\n");

printf("\nEnter your choice...\n");


scanf("%d",&choice);

switch(choice)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
default:printf("invalid choice entered\n");
break;

}
}while ( choice !=4);
getch();
return 0;
}

void push()
{
intelem;
if(top==MAX-1)
{
printf("\nOverflow");
return;

}
else{
printf("Enter the element to be pushed\n");
scanf("%d",&elem);
top=top+1;
s[top]=elem;

void pop()
{
if(top==-1)
{

printf("Underflow\n");
}
else{
printf("Element popped is %d",s[top]);
top--;
}

}
void display()
{
if(top==-1)
{
printf("Stack is empty\n");
}
else{
for(int i=0;i<=top;i++)
{
printf("%d ",s[i]);

}
}
}
Output:
1.Push an element in a stack
2. Pop an element from stack
3. Display elements from stack
4. Exit

Enter your choice...


1
Enter the element to be pushed
10

1.Push an element in a stack


2. Pop an element from stack
3. Display elements from stack
4. Exit

Enter your choice...


1
Enter the element to be pushed
20

1.Push an element in a stack


2. Pop an element from stack
3. Display elements from stack
4. Exit

Enter your choice...


1
Enter the element to be pushed
30

1.Push an element in a stack


2. Pop an element from stack
3. Display elements from stack
4. Exit

Enter your choice...


1
Enter the element to be pushed
40

1.Push an element in a stack


2. Pop an element from stack
3. Display elements from stack
4. Exit

Enter your choice...


1
Enter the element to be pushed
50

1.Push an element in a stack


2. Pop an element from stack
3. Display elements from stack
4. Exit

Enter your choice...


1

Overflow

1.Push an element in a stack


2. Pop an element from stack
3. Display elements from stack
4. Exit

Enter your choice...


3
10 20 30 40 50

1.Push an element in a stack


2. Pop an element from stack
3. Display elements from stack
4. Exit

Enter your choice...


2
Element popped is 50

1.Push an element in a stack


2. Pop an element from stack
3. Display elements from stack
4. Exit

Enter your choice...


3
10 20 30 40
1.Push an element in a stack
2. Pop an element from stack
3. Display elements from stack
4. Exit

Enter your choice...


2
Element popped is 40

1.Push an element in a stack


2. Pop an element from stack
3. Display elements from stack
4. Exit

Enter your choice...


3
10 20 30

1.Push an element in a stack


2. Pop an element from stack
3. Display elements from stack
4. Exit

Enter your choice...


2
Element popped is 30

1.Push an element in a stack


2. Pop an element from stack
3. Display elements from stack
4. Exit

Enter your choice...


3
10 20

1.Push an element in a stack


2. Pop an element from stack
3. Display elements from stack
4. Exit

Enter your choice...


2
Element popped is 20
1.Push an element in a stack
2. Pop an element from stack
3. Display elements from stack
4. Exit

Enter your choice...


3
10

1.Push an element in a stack


2. Pop an element from stack
3. Display elements from stack
4. Exit

Enter your choice...


2
Element popped is 10

1.Push an element in a stack


2. Pop an element from stack
3. Display elements from stack
4. Exit

Enter your choice...


3
Stack is empty

1.Push an element in a stack


2. Pop an element from stack
3. Display elements from stack
4. Exit

Enter your choice...


2
Underflow

1.Push an element in a stack


2. Pop an element from stack
3. Display elements from stack
4. Exit

Enter your choice...


4
Write C programs that implement stack (its operations) using Pointers
Aim: To write a C program to implement stack using Pointers.
Program:

#include<stdio.h>
#include<stdlib.h>
struct stack *push(struct stack *, int );
struct stack *push(struct stack *, int);
struct stack *display(struct stack *);
struct stack *pop(struct stack *);
int peek(struct stack *);
struct stack{
int data;
struct stack *next;
};
struct stack *top=NULL;
int main()
{
intval,choice;
do{
printf("\n*****Main Menu*****");
printf("\n1.Push");
printf("\n2.Pop");
printf("\n3.Peek");
printf("\n4.Display");
printf("\n5.Exit");
printf("\n\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("\nEnter the number to be pushed onto stack: ");
scanf("%d",&val);
top=push(top,val);
break;
case 2: top=pop(top);
break;
case 3: val=peek(top);
if(val!=-1)
printf("\nThe value at the top of the stack: %d",val);
else
printf("\nStack is empty");
break;

case 4:
top=display(top);
break;
}
//return 0;
}while(choice!=5);
}
struct stack *push(struct stack *top, intval)
{
struct stack *ptr;
ptr=(struct stack *)malloc(sizeof(struct stack));
ptr->data=val;
if(top==NULL)
{
ptr->next=NULL;
top=ptr;
}
else{
ptr->next=top;
top=ptr;
}
return top;
}

struct stack *pop(struct stack *top)


{
struct stack *ptr;
ptr=top;
if(top==NULL)
printf("\n STACK UNDERFLOW");
else{
top=top->next;
printf("\nThe value being deleted is:%d",ptr->data);
free(ptr);
}
return top;
}
struct stack *display(struct stack *top)
{
struct stack *ptr;
ptr = top;
if(top == NULL)
printf("\n STACK IS EMPTY");
else
{

while(ptr != NULL)
{
printf("\n %d", ptr -> data);
ptr = ptr -> next;
}
}
return top;
}
int peek(struct stack *top)
{
if(top==NULL)
return -1;
else
return top ->data;
}

Output:
*****Main Menu*****
1.Push
2.Pop
3.Peek
4.Display
5.Exit

Enter your choice: 1

Enter the number to be pushed onto stack: 10

*****Main Menu*****
1.Push
2.Pop
3.Peek
4.Display
5.Exit

Enter your choice: 1

Enter the number to be pushed onto stack: 20

*****Main Menu*****
1.Push
2.Pop
3.Peek
4.Display
5.Exit

Enter your choice: 1

Enter the number to be pushed onto stack: 30


*****Main Menu*****
1.Push
2.Pop
3.Peek
4.Display
5.Exit

Enter your choice: 4

30
20
10
*****Main Menu*****
1.Push
2.Pop
3.Peek
4.Display
5.Exit

Enter your choice: 2

The value being deleted is:30


*****Main Menu*****
1.Push
2.Pop
3.Peek
4.Display
5.Exit

Enter your choice: 4

20
10
*****Main Menu*****
1.Push
2.Pop
3.Peek
4.Display
5.Exit

Enter your choice: 3

The value at the top of the stack: 20


*****Main Menu*****
1.Push
2.Pop
3.Peek
4.Display
5.Exit

Enter your choice: 2

The value being deleted is:20


*****Main Menu*****
1.Push
2.Pop
3.Peek
4.Display
5.Exit

Enter your choice: 2

The value being deleted is:10


*****Main Menu*****
1.Push
2.Pop
3.Peek
4.Display
5.Exit

Enter your choice: 2

STACK UNDERFLOW
*****Main Menu*****
1.Push
2.Pop
3.Peek
4.Display
5.Exit

Enter your choice: 4

STACK IS EMPTY
*****Main Menu*****
1.Push
2.Pop
3.Peek
4.Display
5.Exit

Enter your choice: 5


WEEK -8
Write C programs that implement Queue (its operations) using
i) Arrays
ii) Pointers
Write C programs that implement Queue (its operations) using Arrays
Aim: To write a C program to implement Queue using Arrays
Program:
#include<stdio.h>
#include<stdlib.h>

int q[4];
int n=4;
int f=0,r=-1;

void insert();
void delete();
void disp();

void main()
{

int choice;

while(1)
{

printf("\n\nPress 1 to insert\nPress 2 to delete\nPress 3 to display\nPress 4 to stop\n");

printf("\nEnter the choice: ");


scanf("%d",&choice);

switch(choice)
{

case 1: insert();
break;
case 2: delete();
break;
case 3: disp();
break;
case 4:exit(0);
default: printf("Invalid choice\n");
break;
}
}
}

void insert()
{
int elem;
if(r==n-1)
printf("Insertion not possible\n");
else if(f==-1 && r==-1)
f=r=0;
else
{
printf("\nEnter the element to be inserted: ");
scanf("%d",&elem);
q[++r]=elem;
}
}

void delete()
{

if(f==-1 || f>r)
printf("Deletion not possible");

else
{
printf("Element deleted is %d",q[f]);
++f;
}
}

void disp()
{
int i;
if(f==-1 || f>r)
{
printf("Queue is empty");
}
else
{
for(i=f;i<=r;i++)
{
printf("%d ",q[i]);
}
}
}

Output:
Press 1 to insert
Press 2 to delete
Press 3 to display
Press 4 to stop

Enter the choice: 1


Enter the element to be inserted: 10

Press 1 to insert
Press 2 to delete
Press 3 to display
Press 4 to stop

Enter the choice: 1

Enter the element to be inserted: 20

Press 1 to insert
Press 2 to delete
Press 3 to display
Press 4 to stop

Enter the choice: 1

Enter the element to be inserted: 30

Press 1 to insert
Press 2 to delete
Press 3 to display
Press 4 to stop

Enter the choice: 1

Enter the element to be inserted: 40

Press 1 to insert
Press 2 to delete
Press 3 to display
Press 4 to stop

Enter the choice: 1


Insertion not possible

Press 1 to insert
Press 2 to delete
Press 3 to display
Press 4 to stop

Enter the choice: 50


Invalid choice
Press 1 to insert
Press 2 to delete
Press 3 to display
Press 4 to stop

Enter the choice: 1


Insertion not possible

Press 1 to insert
Press 2 to delete
Press 3 to display
Press 4 to stop

Enter the choice: 3


10 20 30 40

Press 1 to insert
Press 2 to delete
Press 3 to display
Press 4 to stop

Enter the choice: 2


Element deleted is 10

Press 1 to insert
Press 2 to delete
Press 3 to display
Press 4 to stop

Enter the choice: 3


20 30 40

Press 1 to insert
Press 2 to delete
Press 3 to display
Press 4 to stop

Enter the choice: 2


Element deleted is 20

Press 1 to insert
Press 2 to delete
Press 3 to display
Press 4 to stop

Enter the choice: 3


30 40
Press 1 to insert
Press 2 to delete
Press 3 to display
Press 4 to stop

Enter the choice: 4

Result:
Therefore, we have successfully executed the program on Queue using Arrays.

Write C programs that implement Queue (its operations) using Pointers


Aim: To write a C program to implement Queue using Pointers
Program:
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<process.h>
#include<ctype.h>

struct linear_queue
{
int info;
struct linear_queue *next;
}*front,*rear,*newnode,*ptr;

void menu();
void display();
int underflow();
void enqueue(int);
void dequeue();

void main()
{

menu();
}

void menu()
{
int choice,item;
printf("\n\nMENU");
printf("\n1. Insert into the queue");
printf("\n2. Delete from queue");
printf("\n3. Display");
printf("\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:

printf("\nEnter the item to be inserted: ");


scanf("%d",&item);
enqueue(item);
printf("\nAfter inserting queue is:\n");
display();
getch();
menu();
break;
case 2:
if(underflow()==1)
{
dequeue();
if(underflow()==1)
{
printf("\nAfter deletion queue is:\n");
display();
}
}
getch();

menu();
break;
case 3:

if(underflow()==1)
{
printf("The queue is:\n");
display();
}
getch();

menu();
break;
case 4:
exit(1);
default:

printf("Your choice is wrong\n\n");


menu();
}
}

int underflow()
{
if((front==NULL)&&(rear==NULL))
{
printf("\nQueue is empty");
return(0);
}
else
{
return(1);
}
}

void enqueue(int item)


{
newnode=(struct linear_queue*)malloc(sizeof(struct linear_queue));
newnode->info=item;
if((front==NULL)&&(rear==NULL))
{
front=newnode;
rear=newnode;
newnode->next=NULL;
}
else
{
rear->next=newnode;
newnode->next=NULL;
rear=newnode;
}
}

void dequeue()
{
if(front==rear)
{
front=NULL;
rear=NULL;
}
else
{
front=front->next;
}
}

void display()
{
int i;
ptr=front;
i=1;
while(ptr!=NULL)
{
printf("\nNode %d : %d",i,ptr->info);
ptr=ptr->next;
i++;
}
}
Output:

MENU
1. Insert into the queue
2. Delete from queue
3. Display
4. Exit
Enter your choice: 1

Enter the item to be inserted: 10

After inserting queue is:

Node 1 : 10

MENU
1. Insert into the queue
2. Delete from queue
3. Display
4. Exit
Enter your choice: 1

Enter the item to be inserted: 20

After inserting queue is:

Node 1 : 10
Node 2 : 20

MENU
1. Insert into the queue
2. Delete from queue
3. Display
4. Exit
Enter your choice: 1

Enter the item to be inserted: 30

After inserting queue is:

Node 1 : 10
Node 2 : 20
Node 3 : 30
MENU
1. Insert into the queue
2. Delete from queue
3. Display
4. Exit
Enter your choice: 1
Enter the item to be inserted: 40
After inserting queue is:
Node 1 : 10
Node 2 : 20
Node 3 : 30
Node 4 : 40
MENU
1. Insert into the queue
2. Delete from queue
3. Display
4. Exit
Enter your choice: 1
Enter the item to be inserted: 50
After inserting queue is:
Node 1 : 10
Node 2 : 20
Node 3 : 30
Node 4 : 40
Node 5 : 50
MENU
1. Insert into the queue
2. Delete from queue
3. Display
4. Exit
Enter your choice: 3
The queue is:
Node 1 : 10
Node 2 : 20
Node 3 : 30
Node 4 : 40
Node 5 : 50
MENU
1. Insert into the queue
2. Delete from queue
3. Display
4. Exit
Enter your choice: 2
After deletion queue is:
Node 1 : 20
Node 2 : 30
Node 3 : 40
Node 4 : 50

MENU
1. Insert into the queue
2. Delete from queue
3. Display
4. Exit
Enter your choice: 2
After deletion queue is:

Node 1 : 30
Node 2 : 40
Node 3 : 50

MENU
1. Insert into the queue
2. Delete from queue
3. Display
4. Exit
Enter your choice: 2

After deletion queue is:

Node 1 : 40
Node 2 : 50

MENU
1. Insert into the queue
2. Delete from queue
3. Display
4. Exit
Enter your choice: 2

After deletion queue is:

Node 1 : 50
MENU
1. Insert into the queue
2. Delete from queue
3. Display
4. Exit
Enter your choice: 2
Queue is empty
MENU
1. Insert into the queue
2. Delete from queue
3. Display
4. Exit
Enter your choice: 2
Queue is empty
MENU
1. Insert into the queue
2. Delete from queue
3. Display
4. Exit
Enter your choice: 4
WEEK -9
Write a C program that uses Stack operations to perform the following:
i) Converting infix expression into postfix expression
ii) Evaluating the postfix expression

Write a C program that uses Stack operations to Convertinfix expression into postfix
expression.
Aim:- To write a C program to convert infix expression into postfix expression.
Program:-

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

int stack[20];
int top = -1;
void push(char);
char pop();
int incmprc(char);
int instprc(char);
void IntoPost(char[], char[]);

int main()
{
char infix[20], postfix[20];
printf("Enter the valid infix expression : ");
scanf("%s",infix);
push('#');
IntoPost(infix,postfix);
printf("The corresponding postfix expression is : \n%s\n",postfix);
}

void push(char ch)


{
stack[++top]=ch;
}

char pop()
{
return stack[top--];
}

int incmprc(char ch)


{
switch(ch)
{
case '+' :
case '-' : return 1;
case '*' :
case '/' :
case '%' : return 2;
case '^' : return 3;
}
}

int instprc(char ch)


{
switch(ch)
{
case '#' :
case '(' : return 0;
case '+' :
case '-' : return 1;
case '*' :
case '/' :
case '%' : return 2;
case '^' : return 3;
}
}

Void IntoPost(char infix[20], char postfix[20])


{
int i=0,j=0;
charch;
ch = infix[i++];
while(ch !='\0')
{
switch(ch)
{
case '(': push(ch);
break;
case ')': while(stack[top]!='(')
postfix[j++] = pop();
pop();
break;
case '^':
case '+':
case '-':
case '*':
case '/':
case '%': while(incmprc(ch) <= instprc(stack[top]))
postfix[j++] = pop();
push(ch);
break;
default : postfix[j++] = ch;
}
ch = infix[i++];
}
while(stack[top] != '#')
postfix[j++] = pop();
postfix[j]= '\0';
}

Output:
Enter the valid infix expression :a+b*c-(d/e^f)*g*h
The corresponding postfix expression is :
abc*+def^/g*h*-

Result:
Therefore, the conversion of infix expression to postfix expression is successfully executed.

Write a C program that uses Stack operations to evaluating the postfix expression
Aim: To write a C program to evaluate the postfix expression
Program:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<ctype.h>

int stack[50];
int top = -1;
void push(int);
int pop();

void push(intnum)
{
stack[++top] = num;
}

int pop()
{
return stack[top--];
}

int main()
{
char suffix[50],ch;
int p1,p2,p3,i;
printf("Enter the valid suffix expression : ");
gets(suffix);
//scanf("%s",suffix);
for(i=0;suffix[i]!='\0';i++)
{
ch = suffix[i];

if(isdigit(ch))
push(ch-'0');//ascii value of ch -ascii value of 0
else
{
p2 = pop();
p1 = pop();
switch(ch)
{
case '+': push(p1+p2);
//printf("%d",pop());
break;
case '-': push(p1-p2);
break;
case '*': push(p1*p2);
break;
case '/': if(p2 == 0)
{
printf("Divide by Zero error\n");
exit(0);
}
push(p1/p2);
break;
case '%': if(p2 == 0)
{
printf("Divide by Zero error\n");
exit(0);
}
push(p1%p2);
break;
case '^': push(pow(p1,p2));
break;
default :printf("Invalid Expression\n");
}
}
}
printf("The value of given suffix expression is : %d\n",pop());
}

Output:
Enter the valid suffix expression : 93/1-24^+91*2%-
The value of given suffix expression is : 17
WEEK -10
Write a C program that uses functions to perform the following operations on Singly Linked
list. i)Creation ii) Insertion iii) Deletion iv) Traversal

Aim:- To write a C program to implement Singly Linked List.


Program:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head;
void beginsert ();
void lastinsert ();
void randominsert();
void begin_delete();
void last_delete();
void random_delete();
void display();
void search();
void main ()
{
int choice =0;
while(choice != 9)
{
printf("\n\n*********Main Menu*********\n");
printf("\nChoose one option from the following list ...\n");
printf("\n===============================================\n");
printf("\n1.Insert in begining\n2.Insert at last\n3.Insert at any random location\n4.Delete from
Beginning\n5.Delete from last\n6.Delete node after specified location\n7.Search for an
element\n8.Show\n9.Exit\n");
printf("\nEnter your choice?\n");
scanf("\n%d",&choice);
switch(choice)
{
case 1:
beginsert();
break;
case 2:
lastinsert();
break;
case 3:
randominsert();
break;
case 4:
begin_delete();
break;
case 5:
last_delete();
break;
case 6:
random_delete();
break;
case 7:
search();
break;
case 8:
display();
break;
case 9:
exit(0);
break;
default:
printf("Please enter valid choice..");
}
}
}
void beginsert()
{
struct node *ptr;
int item;
ptr = (struct node *) malloc(sizeof(struct node *));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value\n");
scanf("%d",&item);
ptr->data = item;
ptr->next = head;
head = ptr;
printf("\nNode inserted");
}
}
void lastinsert()
{
struct node *ptr,*temp;
int item;
ptr = (struct node*)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value?\n");
scanf("%d",&item);
ptr->data = item;
if(head == NULL)
{
ptr -> next = NULL;
head = ptr;
printf("\nNode inserted");
}
else
{
temp = head;
while (temp -> next != NULL)
{
temp = temp -> next;
}
temp->next = ptr;
ptr->next = NULL;
printf("\nNode inserted");
}
}
}
void randominsert()
{
int i,loc,item;
struct node *ptr, *temp;
ptr = (struct node *) malloc (sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter element value");
scanf("%d",&item);
ptr->data = item;
printf("\nEnter the location after which you want to insert ");
scanf("\n%d",&loc);
temp=head;
for(i=0;i<loc;i++)
{
temp = temp->next;
if(temp == NULL)
{
printf("\ncan't insert\n");
return;
}
}
ptr ->next = temp ->next;
temp ->next = ptr;
printf("\nNode inserted");
}
}
void begin_delete()
{
struct node *ptr;
if(head == NULL)
{
printf("\nList is empty\n");
}
else
{
ptr = head;
head = ptr->next;
free(ptr);
printf("\nNode deleted from the begining ...\n");
}
}
void last_delete()
{
struct node *ptr,*ptr1;
if(head == NULL)
{
printf("\nlist is empty");
}
else if(head -> next == NULL)
{
head = NULL;
free(head);
printf("\nOnly node of the list deleted ...\n");
}
else
{
ptr = head;
while(ptr->next != NULL)
{
ptr1 = ptr;
ptr = ptr ->next;
}
ptr1->next = NULL;
free(ptr);
printf("\nDeleted Node from the last ...\n");
}
}
void random_delete()
{
struct node *ptr,*ptr1;
int loc,i;
printf("\n Enter the location of the node after which you want to perform deletion \n");
scanf("%d",&loc);
ptr=head;
for(i=0;i<loc;i++)
{
ptr1 = ptr;
ptr = ptr->next;
if(ptr == NULL)
{
printf("\nCan't delete");
return;
}
}
ptr1 ->next = ptr ->next;
free(ptr);
printf("\nDeleted node %d ",loc+1);
}
void search()
{
struct node *ptr;
int item,i=0,flag;
ptr = head;
if(ptr == NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
while (ptr!=NULL)
{
if(ptr->data == item)
{
printf("item found at location %d ",i+1);
flag=0;
}
else
{
flag=1;
}
i++;
ptr = ptr -> next;
}
if(flag==1)
{
printf("Item not found\n");
}
}
}
void display()
{
struct node *ptr;
ptr = head;
if(ptr == NULL)
{
printf("Nothing to print");
}
else
{
printf("\nprinting values . . . . .\n");
while (ptr!=NULL)
{
printf("\n%d",ptr->data);
ptr = ptr -> next;
}
}
}
Output:
*********Main Menu*********
Choose one option from the following list ...
===============================================
1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete node after specified location
7.Search for an element
8.Show
9.Exit
Enter your choice?
1
Enter value
10
Node inserted
*********Main Menu*********
Choose one option from the following list ...
===============================================
1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete node after specified location
7.Search for an element
8.Show
9.Exit
Enter your choice?
1
Enter value
20
Node inserted
*********Main Menu*********
Choose one option from the following list ...
===============================================
1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete node after specified location
7.Search for an element
8.Show
9.Exit
Enter your choice?
2
Enter value?
60
Node inserted
*********Main Menu*********
Choose one option from the following list ...
===============================================
1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete node after specified location
7.Search for an element
8.Show
9.Exit
Enter your choice?
3
Enter element value75
Enter the location after which you want to insert 2
Node inserted
*********Main Menu*********
Choose one option from the following list ...
===============================================
1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete node after specified location
7.Search for an element
8.Show
9.Exit
Enter your choice?
8
printing values . . . . .
20
10
60
75
*********Main Menu*********
Choose one option from the following list ...
===============================================
1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete node after specified location
7.Search for an element
8.Show
9.Exit
Enter your choice?
7
Enter item which you want to search?
75
item found at location 4
*********Main Menu*********
Choose one option from the following list ...
===============================================
1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete node after specified location
7.Search for an element
8.Show
9.Exit
Enter your choice?
9
WEEK-11
Write a C program that uses functions to perform the following operations on Doubly
Linked list. i)Creation ii) Insertion iii) Deletion iv) Traversal

Aim: To write a C Program to implement Doubly Linked List


Program:
#include<stdio.h>
#include<stdlib.h>
struct node
{
struct node *prev;
struct node *next;
int data;
};
struct node *head;
void insertion_beginning();
void insertion_last();
void insertion_specified();
void deletion_beginning();
void deletion_last();
void deletion_specified();
void display();
void search();
void main ()
{
int choice =0;
while(choice != 9)
{
printf("\n*********Main Menu*********\n");
printf("\nChoose one option from the following list ...\n");
printf("\n===============================================\n");
printf("\n1.Insert in begining\n2.Insert at last\n3.Insert at any random location\n4.Delete
from Beginning\n5.Delete from last\n6.Delete the node after the given
data\n7.Search\n8.Show\n9.Exit\n");
printf("\nEnter your choice?\n");
scanf("\n%d",&choice);
switch(choice)
{
case 1:
insertion_beginning();
break;
case 2:
insertion_last();
break;
case 3:
insertion_specified();
break;
case 4:
deletion_beginning();
break;
case 5:
deletion_last();
break;
case 6:
deletion_specified();
break;
case 7:
search();
break;
case 8:
display();
break;
case 9:
exit(0);
break;
default:
printf("Please enter valid choice..");
}
}
}
void insertion_beginning()
{
struct node *ptr;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter Item value");
scanf("%d",&item);

if(head==NULL)
{
ptr->next = NULL;
ptr->prev=NULL;
ptr->data=item;
head=ptr;
}
else
{
ptr->data=item;
ptr->prev=NULL;
ptr->next = head;
head->prev=ptr;
head=ptr;
}
printf("\nNode inserted\n");
}
}
void insertion_last()
{
struct node *ptr,*temp;
int item;
ptr = (struct node *) malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value");
scanf("%d",&item);
ptr->data=item;
if(head == NULL)
{
ptr->next = NULL;
ptr->prev = NULL;
head = ptr;
}
else
{
temp = head;
while(temp->next!=NULL)
{
temp = temp->next;
}
temp->next = ptr;
ptr ->prev=temp;
ptr->next = NULL;
}

}
printf("\nnode inserted\n");
}
void insertion_specified()
{
struct node *ptr,*temp;
int item,loc,i;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\n OVERFLOW");
}
else
{
temp=head;
printf("Enter the location");
scanf("%d",&loc);
for(i=0;i<loc;i++)
{
temp = temp->next;
if(temp == NULL)
{
printf("\n There are less than %d elements", loc);
return;
}
}
printf("Enter value");
scanf("%d",&item);
ptr->data = item;
ptr->next = temp->next;
ptr -> prev = temp;
temp->next = ptr;
temp->next->prev=ptr;
printf("\nnode inserted\n");
}
}
void deletion_beginning()
{
struct node *ptr;
if(head == NULL)
{
printf("\n UNDERFLOW");
}
else if(head->next == NULL)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{
ptr = head;
head = head -> next;
head -> prev = NULL;
free(ptr);
printf("\nnode deleted\n");
}
}
void deletion_last()
{
struct node *ptr;
if(head == NULL)
{
printf("\n UNDERFLOW");
}
else if(head->next == NULL)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{
ptr = head;
if(ptr->next != NULL)
{
ptr = ptr -> next;
}
ptr -> prev -> next = NULL;
free(ptr);
printf("\nnode deleted\n");
}
}
void deletion_specified()
{
struct node *ptr, *temp;
int val;
printf("\n Enter the data after which the node is to be deleted : ");
scanf("%d", &val);
ptr = head;
while(ptr -> data != val)
ptr = ptr -> next;
if(ptr -> next == NULL)
{
printf("\nCan't delete\n");
}
else if(ptr -> next -> next == NULL)
{
ptr ->next = NULL;
}
else
{
temp = ptr -> next;
ptr -> next = temp -> next;
temp -> next -> prev = ptr;
free(temp);
printf("\nnode deleted\n");
}
}
void display()
{
struct node *ptr;
printf("\n printing values...\n");
ptr = head;
while(ptr != NULL)
{
printf("%d\n",ptr->data);
ptr=ptr->next;
}
}
void search()
{
struct node *ptr;
int item,i=0,flag;
ptr = head;
if(ptr == NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
while (ptr!=NULL)
{
if(ptr->data == item)
{
printf("\nitem found at location %d ",i+1);
flag=0;
break;
}
else
{
flag=1;
}
i++;
ptr = ptr -> next;
}
if(flag==1)
{
printf("\nItem not found\n");
}
}

OUTPUT:-

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete the node after the given data
7.Search
8.Show
9.Exit

Enter your choice?


1

Enter Item value10

Node inserted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete the node after the given data
7.Search
8.Show
9.Exit

Enter your choice?


1

Enter Item value20

Node inserted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete the node after the given data
7.Search
8.Show
9.Exit

Enter your choice?


2
Enter value50

node inserted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete the node after the given data
7.Search
8.Show
9.Exit
Enter your choice?
8
printing values...
20
10
50
*********Main Menu*********
Choose one option from the following list ...
===============================================
1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete the node after the given data
7.Search
8.Show
9.Exit
Enter your choice?
9
WEEK -12
Write a C program that uses functions to perform the following operations on circular
linked list. i) Creation ii) Insertion iii) Deletion iv) Traversal
Aim: To write a C program to implement Circular Linked List

Program:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head;
void beginsert ();
void lastinsert ();
void randominsert();
void begin_delete();
void last_delete();
void random_delete();
void display();
void search();
void main ()
{
int choice =0;
while(choice != 7)
{
printf("\n*********Main Menu*********\n");
printf("\nChoose one option from the following list ...\n");
printf("\n===============================================\n");
printf("\n1.Insert in begining\n2.Insert at last\n3.Delete from Beginning\n4.Delete from
last\n5.Search for an element\n6.Show\n7.Exit\n");
printf("\nEnter your choice?\n");
scanf("\n%d",&choice);
switch(choice)
{
case 1:
beginsert();
break;
case 2:
lastinsert();
break;
case 3:
begin_delete();
break;
case 4:
last_delete();
break;
case 5:
search();
break;
case 6:
display();
break;
case 7:
exit(0);
break;
default:
printf("Please enter valid choice..");
}
}
}
void beginsert()
{
struct node *ptr,*temp;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter the node data?");
scanf("%d",&item);
ptr -> data = item;
if(head == NULL)
{
head = ptr;
ptr -> next = head;
}
else
{
temp = head;
while(temp->next != head)
temp = temp->next;
ptr->next = head;
temp -> next = ptr;
head = ptr;
}
printf("\nnode inserted\n");
}
}
void lastinsert()
{
struct node *ptr,*temp;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
}
else
{
printf("\nEnter Data?");
scanf("%d",&item);
ptr->data = item;
if(head == NULL)
{
head = ptr;
ptr -> next = head;
}
else
{
temp = head;
while(temp -> next != head)
{
temp = temp -> next;
}
temp -> next = ptr;
ptr -> next = head;
}
printf("\nnode inserted\n");
}
}
void begin_delete()
{
struct node *ptr;
if(head == NULL)
{
printf("\nUNDERFLOW");
}
else if(head->next == head)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{ ptr = head;
while(ptr -> next != head)
ptr = ptr -> next;
ptr->next = head->next;
free(head);
head = ptr->next;
printf("\nnode deleted\n");
}
}
void last_delete()
{
struct node *ptr, *preptr;
if(head==NULL)
{
printf("\nUNDERFLOW");
}
else if (head ->next == head)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{
ptr = head;
while(ptr ->next != head)
{
preptr=ptr;
ptr = ptr->next;
}
preptr->next = ptr -> next;
free(ptr);
printf("\nnode deleted\n");
}
}
void search()
{
struct node *ptr;
int item,i=0,flag=1;
ptr = head;
if(ptr == NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
if(head ->data == item)
{
printf("item found at location %d",i+1);
flag=0;
}
else
{
while (ptr->next != head)
{
if(ptr->data == item)
{
printf("item found at location %d ",i+1);
flag=0;
break;
}
else
{
flag=1;
}
i++;
ptr = ptr -> next;
}
}
if(flag != 0)
{
printf("Item not found\n");
}
}
}
void display()
{
struct node *ptr;
ptr=head;
if(head == NULL)
{
printf("\nnothing to print");
}
else
{
printf("\n printing values ... \n");
while(ptr -> next != head)
{
printf("%d\n", ptr -> data);
ptr = ptr -> next;
}
printf("%d\n", ptr -> data);
}
}
Output:
*********Main Menu*********
Choose one option from the following list ...
===============================================
1.Insert in begining
2.Insert at last
3.Delete from Beginning
4.Delete from last
5.Search for an element
6.Show
7.Exit

Enter your choice?


1
Enter the node data?10
node inserted
*********Main Menu*********
Choose one option from the following list ...
===============================================
1.Insert in begining
2.Insert at last
3.Delete from Beginning
4.Delete from last
5.Search for an element
6.Show
7.Exit

Enter your choice?


1
Enter the node data?20
node inserted
*********Main Menu*********
Choose one option from the following list ...
===============================================
1.Insert in begining
2.Insert at last
3.Delete from Beginning
4.Delete from last
5.Search for an element
6.Show
7.Exit
Enter your choice?
1
Enter the node data?30
node inserted
*********Main Menu*********
Choose one option from the following list ...
===============================================
1.Insert in begining
2.Insert at last
3.Delete from Beginning
4.Delete from last
5.Search for an element
6.Show
7.Exit
Enter your choice?
1
Enter the node data?40
node inserted
*********Main Menu*********
Choose one option from the following list ...
===============================================
1.Insert in begining
2.Insert at last
3.Delete from Beginning
4.Delete from last
5.Search for an element
6.Show
7.Exit
Enter your choice?
6
printing values ...
40
30
20
10
*********Main Menu*********
Choose one option from the following list ...
===============================================
1.Insert in begining
2.Insert at last
3.Delete from Beginning
4.Delete from last
5.Search for an element
6.Show
7.Exit
Enter your choice?
2
Enter Data?100
node inserted
*********Main Menu*********
Choose one option from the following list ...
===============================================
1.Insert in begining
2.Insert at last
3.Delete from Beginning
4.Delete from last
5.Search for an element
6.Show
7.Exit
Enter your choice?
8
Please enter valid choice..
*********Main Menu*********
Choose one option from the following list ...
===============================================
1.Insert in begining
2.Insert at last
3.Delete from Beginning
4.Delete from last
5.Search for an element
6.Show
7.Exit
Enter your choice?
2
Enter Data?150
node inserted
*********Main Menu*********
Choose one option from the following list ...
===============================================
1.Insert in begining
2.Insert at last
3.Delete from Beginning
4.Delete from last
5.Search for an element
6.Show
7.Exit
Enter your choice?
6
printing values ...
40
30
20
10
100
150
*********Main Menu*********
Choose one option from the following list ...
===============================================
1.Insert in begining
2.Insert at last
3.Delete from Beginning
4.Delete from last
5.Search for an element
6.Show
7.Exit
Enter your choice?
3
node deleted
*********Main Menu*********
Choose one option from the following list ...
===============================================
1.Insert in begining
2.Insert at last
3.Delete from Beginning
4.Delete from last
5.Search for an element
6.Show
7.Exit
Enter your choice?
6
printing values ...
30
20
10
100
150
*********Main Menu*********
Choose one option from the following list ...
===============================================
1.Insert in begining
2.Insert at last
3.Delete from Beginning
4.Delete from last
5.Search for an element
6.Show
7.Exit
Enter your choice?
3
node deleted
*********Main Menu*********
Choose one option from the following list ...
===============================================
1.Insert in begining
2.Insert at last
3.Delete from Beginning
4.Delete from last
5.Search for an element
6.Show
7.Exit
Enter your choice?
6
printing values ...
20
10
100
150
*********Main Menu*********
Choose one option from the following list ...
===============================================
1.Insert in begining
2.Insert at last
3.Delete from Beginning
4.Delete from last
5.Search for an element
6.Show
7.Exit
Enter your choice?
7

Result:
Therefore, the implementation of Circular Linked List is successfully executed.
WEEK-13
Write a C program that uses functions to perform the following:
i) Creating a Binary Tree of integers
ii) Traversing the above binary tree in preorder, inorder and postorder.
Aim:- To write a C program to implement a Binary Tree and traverse the binary tree.

Program:
#include<stdio.h>
#include <stdlib.h>
#define MAX 20

struct node
{
int data;
struct node *lchild, *rchild;
};
typedefstruct node *NODE;
NODE tree = NULL;
voidCreateBST (int a[MAX], int n)
{
NODE temp, p, q;
int i;
for(i=0;i<n;i++)
{
temp =(struct node *)malloc(sizeof(struct node*));
temp->data = a[i];
temp->lchild = temp->rchild = NULL;
if(tree == NULL)
tree = temp;
else
{
p = q = tree;
while(q != NULL)
{
p=q;
if(a[i] < p->data)
q = p->lchild;
else if(a[i] > p->data)
q = p->rchild;
else
{
free(temp);
break;
}
}
if( q == NULL)
{
if(a[i] < p->data)
p->lchild = temp;
else
p->rchild = temp;
}
}
}
printf("Binary Search Tree created\n\n");
}

voidInorder(NODE tree)
{
if(tree != NULL)
{
Inorder(tree->lchild);
printf("%d ",tree->data);
Inorder(tree->rchild);
}
}

voidPreorder(NODE tree)
{
if(tree != NULL)
{
printf("%d ",tree->data);
Preorder(tree->lchild);
Preorder(tree->rchild);
}
}

voidPostorder(NODE tree)
{
if(tree != NULL)
{
Postorder(tree->lchild);
Postorder(tree->rchild);
printf("%d ",tree->data);
}
}

int main()
{
int a[MAX], n, i, choice;
while(1)
{
printf("\n\n**********************MENU*****************");
printf("\n1. Create a BST of n integers\n2. Traverse the BST in Inorder\n3. Traverse
the BST in Preorder\n4. Traverse the BST in Postorder\n5. Exit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 : printf("Enter the number of integers : ");
scanf("%d",&n);
printf("Enter the elements\n");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
CreateBST(a, n);
break;
case 2 : printf("Inorder Traversal :\n");
Inorder(tree);
break;
case 3 : printf("Preorder Traversal :\n");
Preorder(tree);
break;
case 4 : printf("Postoder Traversal :\n");
Postorder(tree);
break;
case 5 : exit(0);
break;
}
}
}

Output:-

**********************MENU*****************
1. Create a BST of n integers
2. Traverse the BST in Inorder
3. Traverse the BST in Preorder
4. Traverse the BST in Postorder
5. Exit
Enter your choice : 1
Enter the number of integers : 12
Enter the elements
6
9
5
2
8
15
24
14
7
8
5
2
Binary Search Tree created

**********************MENU*****************
1. Create a BST of n integers
2. Traverse the BST in Inorder
3. Traverse the BST in Preorder
4. Traverse the BST in Postorder
5. Exit
Enter your choice : 2
InorderTraversal :
2 5 6 7 8 9 14 15 24

**********************MENU*****************
1. Create a BST of n integers
2. Traverse the BST in Inorder
3. Traverse the BST in Preorder
4. Traverse the BST in Postorder
5. Exit
Enter your choice : 3
PreorderTraversal :
6 5 2 9 8 7 15 14 24

**********************MENU*****************
1. Create a BST of n integers
2. Traverse the BST in Inorder
3. Traverse the BST in Preorder
4. Traverse the BST in Postorder
5. Exit
Enter your choice : 4
PostoderTraversal :
2 5 7 8 14 24 15 9 6

**********************MENU*****************
1. Create a BST of n integers
2. Traverse the BST in Inorder
3. Traverse the BST in Preorder
4. Traverse the BST in Postorder
5. Exit
Enter your choice : 5

Result:-
Therefore the binary tree program was successfully executed.
WEEK-14
i) Linear search
ALGORITHM :
LINEAR_SEARCH(A, N, VAL)
Step 1: [INITIALIZE] SET POS = -1
Step 2: [INITIALIZE] SET I = 1
Step 3: Repeat Step 4 while I<=N
Step 4: IF A[I] = VAL
SET POS = I
PRINT POS
Go to Step 6
[END OF IF]
SET I = I + 1
[END OF LOOP]
Step 5: IF POS = –1
"PRINT VALUE IS NOT PRESENT IN THE ARRAY"
[END OF IF]
Step 6: EXIT

Write a program to search an element in an array using the linear search technique.

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define size 20 // Added so the size of the array can be altered more easily
int main()
{
int arr[size], num, i, n, found = 0, pos = -1;
printf("\n Enter the number of elements in the array : ");
scanf("%d", &n);
printf("\n Enter the elements: ");
for(i=0;i<n;i++)
{
scanf("%d", &arr[i]);
}
printf("\n Enter the number that has to be searched : ");
scanf("%d", &num);
for(i=0;i<n;i++)
{
if(arr[i] == num)
{
found =1;
pos=i;
printf("\n %d is found in the array at position= %d", num,pos+1);
/* +1 added in line 23 so that it would display the number in
the first place in the array as in position 1 instead of 0 */
break;
}
}
if (found == 0)
printf("\n %d does not exist in the array", num);
return 0;
}

OUTPUT:
Enter the number of elements in the array : 4
Enter the elements: 10 15 72 20
Enter the number that has to be searched :72
72 is found in the array at position= 3

Week 14 ii) Binary search


ALGORITHM :
BINARY_SEARCH(A, lower_bound, upper_bound, VAL)
Step 1: [INITIALIZE] SET BEG = lower_bound
END = upper_bound, POS = - 1
Step 2: Repeat Steps 3 and 4 while BEG <= END
Step 3: SET MID = (BEG + END)/2
Step 4: IF A[MID] = VAL
SET POS = MID
PRINT POS
Go to Step 6
ELSE IF A[MID] > VAL
SET END = MID - 1
ELSE
SET BEG = MID + 1
[END OF IF]
[END OF LOOP]
Step 5: IF POS = -1
PRINT “VALUE IS NOT PRESENT IN THE ARRAY”
[END OF IF]
Step 6: EXIT

Write a program to search an element in an array using binary search.

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define size 10 // Added to make changing size of array easier
int smallest(int arr[], int k, int n); // Added to sort array
void selection_sort(int arr[], int n); // Added to sort array

int main()
{
int arr[size], num, i, n, beg, end, mid, found=0;
printf("\n Enter the number of elements in the array: ");
scanf("%d", &n);
printf("\n Enter the elements: ");
for(i=0;i<n;i++)
{
scanf("%d", &arr[i]);
}
selection_sort(arr, n); // Added to sort the array
printf("\n The sorted array is: \n");
for(i=0;i<n;i++)
printf(" %d\t", arr[i]);
printf("\n\n Enter the number that has to be searched: ");
scanf("%d", &num);
beg = 0, end = n-1;
while(beg<=end)
{
mid = (beg + end)/2;
if (arr[mid] == num)
{
printf("\n %d is present in the array at position %d", num, mid+1);
found =1;
break;
}
else if (arr[mid]>num)
end = mid-1;
else
beg = mid+1;
}
if (beg > end && found == 0)
printf("\n %d does not exist in the array", num);
return 0;
}

int smallest(int arr[], int k, int n)


{
int pos = k, small=arr[k], i;
for(i=k+1;i<n;i++)
{
if(arr[i]< small)
{
small = arr[i];
pos = i;
}
}
return pos;
}

void selection_sort(int arr[],int n)


{
int k, pos, temp;
for(k=0;k<n;k++)
{
pos = smallest(arr, k, n);
temp = arr[k];
arr[k] = arr[pos];
arr[pos] = temp;
}
}

OUTPUT :
Enter the number of elements in the array:4
Enter the elements: 10 15 72 20
The sorted array is: 10 15 20 72
Enter the number that has to be searched:20
20 is present in the array at position 3
WEEK-15
i) Bubble sort
ALGORITH:
BUBBLE_SORT(A, N)
Step 1: Repeat Step 2 For I = 0 to N-1
Step 2: Repeat For J = 0 to N - I
Step 3: IF A[J] > A[J + 1]
SWAP A[J] and A[J+1]
[END OF INNER LOOP]
[END OF OUTER LOOP]
Step 4: EXIT
Write a program to enter n numbers in an array. Redisplay the array with elements being
sorted in ascending order.
#include <stdio.h>
#include <conio.h>
int main()
{
int i, n, temp, j, arr[10];
clrscr();
printf("\n Enter the number of elements in the array : ");
scanf("%d", &n);
printf("\n Enter the elements: ");
for(i=0;i<n;i++)
{
scanf("%d", &arr [i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n–i–1;j++)
{
if(arr[j] > arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
printf("\n The array sorted in ascending order is :\n");
for(i=0;i<n;i++)
printf("%d\t", arr[i]);
getch();
return 0;
}
Output
Enter the number of elements in the array : 10
Enter the elements : 8 9 6 7 5 4 2 3 1 10
The array sorted in ascending order is :
1 2 3 4 5 6 7 8 9 10

Week 15 ii) Selection sort


ALGORITHM :
SMALLEST (ARR, K, N, POS)
Step 1: [INITIALIZE] SET SMALL = ARR[K]
Step 2: [INITIALIZE] SET POS = K
Step 3: Repeat for J = K+1 to N-1
IF SMALL > ARR[J]
SET SMALL = ARR[J]
SET POS = J
[END OF IF]
[END OF LOOP]
Step 4: RETURN POS

SELECTION SORT(ARR, N)
Step 1: Repeat Steps 2 and 3 for K = 1 to N-1
Step 2: CALL SMALLEST(ARR, K, N, POS)
Step 3: SWAP A[K] with ARR[POS]
[END OF LOOP]
Step 4: EXIT

Write a program to sort an array using selection sort algorithm.


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

int smallest(int arr[], int k, int n);


void selection_sort(int arr[], int n);
void main()
{
int arr[10], i, n;
printf("\n Enter the number of elements in the array: ");
scanf("%d", &n);
printf("\n Enter the elements of the array: ");
for(i=0;i<n;i++)
{
scanf("%d", &arr[i]);
}

selection_sort(arr, n);
printf("\n The sorted array is: \n");
for(i=0;i<n;i++)
printf(" %d\t", arr[i]);
}
int smallest(int arr[], int k, int n)
{
int pos = k, small=arr[k], i;
for(i=k+1;i<n;i++)
{
if(arr[i]< small)
{
small = arr[i];
pos = i;
}
}
return pos;
}
void selection_sort(int arr[],int n)
{
int k, pos, temp;
for(k=0;k<n;k++)
{
pos = smallest(arr, k, n);
temp = arr[k];
arr[k] = arr[pos];
arr[pos] = temp;
}
}
Week 15 iii) Insertion sort
ALGORITHM :
INSERTION-SORT (ARR, N)
Step 1: Repeat Steps 2 to 5 for K = 1 to N – 1
Step 2: SET TEMP = ARR[K]
Step 3: SET J = K - 1
Step 4: Repeat while TEMP <= ARR[J]
SET ARR[J + 1] = ARR[J]
SET J = J - 1
[END OF INNER LOOP]
Step 5: SET ARR[J + 1] = TEMP
[END OF LOOP]
Step 6: EXIT

Write a program to sort an array using insertion sort algorithm.


#include <stdio.h>
#include <conio.h>
#define size 5
void insertion_sort(int arr[], int n);
void main()
{
int arr[size], i, n;
printf("\n Enter the number of elements in the array: ");
scanf("%d", &n);
printf("\n Enter the elements of the array: ");
for(i=0;i<n;i++)
{
scanf("%d", &arr[i]);
}
insertion_sort(arr, n);
printf("\n The sorted array is: \n");
for(i=0;i<n;i++)
printf(" %d\t", arr[i]);
getch();
}
void insertion_sort(int arr[], int n)
{
int i, j, temp;
for(i=1;i<n;i++)
{
temp = arr[i];
j = i-1;
while((temp < arr[j]) && (j>=0))
{
arr[j+1] = arr[j];
j--;
}
arr[j+1] = temp;
}
}
Output
Enter the number of elements in the array : 5
Enter the elements of the array : 500 1 50 23 76
The sorted array is :
1 23 50 76 500
WEEK 16 (CASE STUDY)

Create a ―Railway reservation system‖ with the following modules


i) Booking
ii) Availability checking
iii) Cancellation
iv) Prepare chart
Program
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define size 5

typedefstruct NODE
{
intreg_no;
int age;
char name[20];
struct NODE *next;
} node;

node* deq();
void create();
int reserve(node*);
int cancel(int);
void enq(node*);
void display();

node *start;
node *front;
node *rear;
int count=0;
intnum=0;

void create( )
{
node *new;
new=(node *)malloc(sizeof(node));
new->reg_no=1;
printf("Name: ");
scanf("%s", new->name);
printf("Age : ");
scanf("%d", &new->age);
start=new;
new->next=NULL;
num++;

}
int reserve(node *start)
{

if(start==NULL)
{
create(start);
return 1;
}
else
{

node *temp, *new_node;


temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
}

new_node=(node *)malloc(sizeof(node));

printf("Name: ");
scanf("%s", new_node->name);
printf("Age : ");
scanf("%d", &new_node->age);
new_node->next=NULL;
if(num<=size)
{
num++;
new_node->reg_no=num;
temp->next=new_node;

return 1;
}
else
{
enq(new_node);
return 0;
}
}
}

int cancel(intreg)
{
node *ptr, *preptr, *new;
ptr=start;
preptr=NULL;
if(start==NULL)
return -1;
if(ptr->next==NULL &&ptr->reg_no==reg)
{
start=NULL;
num--;
free(ptr);
return 1;

else{
while(ptr->reg_no!=reg&&ptr->next!=NULL)
{
preptr=ptr;
ptr=ptr->next;
}
if(ptr==NULL &&ptr->reg_no!=reg)
return -1;
else
preptr->next=ptr->next;
free(ptr);
new=deq();
while(preptr->next!=NULL)
preptr=preptr->next;
preptr->next=new;
num--;
return 1;

}
}

void enq(node *new_node)


{
if(rear==NULL)
{
rear=new_node;
rear->next=NULL;
front=rear;
}
else
{
node *temp;
temp=new_node;
rear->next=temp;
temp->next=NULL;
rear=temp;
}
count++;
}

node* deq()
{
node *front1;
front1=front;
if(front==NULL)
return NULL;
else
{
count-- ;
if(front->next!=NULL)
{
front=front->next;
front1->next=NULL;
return front1;
}
else
{
front=NULL;
rear=NULL;

return front1;
}
}

void display()
{
node *temp;
temp=start;
while(temp!=NULL)
{
printf("\nRegistration Number: %d\n", temp->reg_no);
printf("Name : %s\n\n", temp->name);
temp=temp->next;
}

int main()
{
int choice, status=0,canc=0, reg=0;
start=NULL;
rear=NULL;
front=NULL;

printf("\t\t\t***RAILWAY RESERVATION***\t\t\t\t\n");
intch =0;
while(ch!=4)
{
printf("\n\nDo you want to - \n 1. Reserve a ticket? \n 2.Cancel Booking \n 3. Display
passenger details \n 4. exit\n\n");
scanf("%d", &choice);
switch(choice)
{
case 1 : status=reserve(start);
if(status==0)
printf("\nBooking Full!! \nYouare added to waiting list. Waiting list number %d", count);
else
printf(" \nBooking Successful!!! Enjoy your journey! Your Reg No is %d\n\n", num);

break;

case 2: printf(" \n Give the Registration number to be cancelled\n");


scanf(" %d", &reg);
if(reg>num)
printf("Invalid!!");
else
{
canc=cancel(reg);
if(canc==-1)
printf("\nRegistration number invalid!!\n");
else
printf("\nRegistration cancelled successfully\n");
}
break;

case 3: display();
break;
case 4: exit(0);
break;
default: printf("\nWrong choice!\n");

You might also like