Lab Manual PCDS-1
Lab Manual PCDS-1
TECHNOLOGY
SUBMITTED BY:
BHAVYASREE (23BBTCS354)
COURSE FACULTY:
DR. MOHANAPRAKASH T A
2
35 Write a C program to Create a Circular 68
Linked Lists - Insert a node at Begin,insert
a node at End, Insert a node at Position
and Count number of nodes
36 Write a C program to demonstrate the 77
applications of Lists - Polynomial
Operations usingLinked Lists
37 Write a C program to implement stack 79
using Arrays
38 Write a C program to implement stack 81
using Linked List
39 Write a C program to implement stack 84
using Linked List
40 Write a C program to implement Linear 87
queue using Array
41 Write a C program to implement Linear 89
queue using Linked List
42 Write a C program to implement 91
insertions and deletions in a circular
Queue
43 Write a C program to perform search and 96
count operations in a circular queue
44 Write a C program to implement 97
insertions and deletions in a Deque using
array
45 Write a C program that uses functions to 100
perform the following:
a) Create a binary search tree of integers.
Traverse the above Binary search tree
recursively in Inorder, Preorder and
Postorder
46 Write a C program to demonstrate Graph 104
traversals implementation - Breadth First
Search
47 Write a C program to demonstrate Graph 107
traversals implementation - Depth First
Search
48 Write a C program to demonstrate 109
Minimum spanning tree using Prim’s and
Kruskal’s algorithm
49 Write a C Program to Perform Linear 114
search and Binary Search
50 Write a program to implement the 117
following a)Bubble sort b) Insertion sort
c) Selection sort
3
Ex.No: 1 a) Program to Converting Miles to Kilometers
#include <stdio.h>
int main()
{
float miles, kilometers, celsius, fahrenheit;
// Convert Miles to Kilometers
printf("Enter distance in miles: ");
scanf("%f", &miles);
kilometers = miles * 1.60934;
printf("%.2f miles is equal to %.2f kilometers\n", miles, kilometers);
// Convert Celsius to Fahrenheit
printf("Enter temperature in Celsius: ");
scanf("%f", &celsius);
fahrenheit = (celsius * 9/5) + 32;
printf("%.2f Celsius is equal to %.2f Fahrenheit\n", celsius, fahrenheit);
return 0;
}
Output:
Enter distance in miles: 10
10.00 miles is equal to 16.09 kilometers
Enter temperature in Celsius: 25
25.00 Celsius is equal to 77.00 Fahrenheit
4
Ex.No:2 Program to Find the total and average of marks scored by a student,check
whether the given number is odd or even
#include <stdio.h>
int main()
{
int marks[5], i, total = 0, num;
float average;
// Total and Average of Marks
printf("Enter marks for 5 subjects: ");
for (i = 0; i < 5; i++)
{
scanf("%d", &marks[i]);
total += marks[i];
}
average = total / 5.0;
printf("Total: %d, Average: %.2f\n", total, average);
// Check Odd or Even
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0)
{
printf("%d is even\n", num);
} else
{
printf("%d is odd\n", num);
}
return 0;
}
OUTPUT
Enter marks for 5 subjects: 80 85 90 75 70
Total: 400, Average: 80.00
Enter a number: 5
5 is odd
5
Ex.No:3 Program to Prints the number only when it is divisible by 3
#include <stdio.h>
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 3 == 0)
{
printf("%d is divisible by 3\n", num);
}
return 0;
}
OUTPUT
Enter a number: 9
9 is divisible by 3
6
Ex.No:4 Program to check whether a given account balance is greater or lesser
than the minimum balance
#include <stdio.h>
int main()
{
float accountBalance, minimumBalance;
printf("Enter account balance: ");
scanf("%f", &accountBalance);
printf("Enter minimum balance: ");
scanf("%f", &minimumBalance);
if (accountBalance >= minimumBalance)
{
printf("Account balance is greater than or equal to the minimum balance.\n");
}
else
{
printf("Account balance is less than the minimum balance.\n");
}
return 0;
}
OUTPUT
Enter account balance: 1000
Enter minimum balance: 500
Account balance is greater than or equal to the minimum balance.
7
Ex.No:5 Program to find the largest of three numbers
#include <stdio.h>
int main()
{
int num1, num2, num3;
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);
if (num1 >= num2 && num1 >= num3)
{
printf("%d is the largest number\n", num1);
}
else if (num2 >= num1 && num2 >= num3)
{
printf("%d is the largest number\n", num2);
} else
{
printf("%d is the largest number\n", num3);
}
return 0;
}
Output
Enter three numbers: 10 20 15
20 is the largest number
8
Ex.No:6 Program to know the working of #if, #elif and #endif preprocessor
directives
#define VALUE 10
int main()
{
#if VALUE == 10
printf("VALUE is 10\n");
#elif VALUE == 20
printf("VALUE is 20\n");
#else
printf("VALUE is not 10 or 20\n");
#endif
return 0;
}
OUTPUT
VALUE is 10
9
Ex.No.7: Program uses if-else statement to check whether the given integer
number is a valid leap year or not
#include <stdio.h>
int main()
{
int year;
printf("Enter a year: ");
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
printf("%d is a leap year\n", year);
} else
{
printf("%d is not a leap year\n", year);
}
return 0;
}
OUTPUT
Enter a year: 2020
2020 is a leap year
10
Ex.No:8 Program to calculate the value of an, given two positive non-zero
integers a and n.
#include <stdio.h>
int main()
{
int a, n, result = 1;
printf("Enter base (a): ");
scanf("%d", &a);
printf("Enter exponent (n): ");
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
result *= a;
}
printf("%d^%d = %d\n", a, n, result);
return 0;
}
OUTPUT
Enter base (a): 2
Enter exponent (n): 3
2^3 = 8
11
Ex.No : 9 Write a program that will print all the English alphabets from A to Z,
each in a new line
#include <stdio.h>
int main()
{
char ch;
for (ch = 'A'; ch <= 'Z'; ch++)
{
printf("%c\n", ch);
}
return 0;
}
Output:
A B C ... Z
12
Ex.No : 10 Program to check whether the given number is a strong number or not.
A strong number is a number in which the sum of the factorial of individual digits is equal to the number itself.
#include <stdio.h>
int factorial(int num)
int fact = 1;
fact *= i;
return fact;
int main()
scanf("%d", &num);
temp = num;
while (temp != 0)
sum += factorial(digit);
temp /= 10;
if (sum == num)
else
{
printf("%d is not a strong number\n", num);
}
return 0;
}
Output: Enter a number: 145
13
Ex.No:11 Write a Program to use a switch-case construct to print the corresponding English words for the digits (1 to
10) read from the standard input
#include <stdio.h>
int main()
{
int num;
switch (num)
{
case 1: printf("One\n"); break;
case 2: printf("Two\n"); break;
case 3: printf("Three\n"); break;
case 4: printf("Four\n"); break;
case 5: printf("Five\n"); break;
case 6: printf("Six\n"); break;
case 7: printf("Seven\n"); break;
case 8: printf("Eight\n"); break;
case 9: printf("Nine\n"); break;
case 10: printf("Ten\n"); break;
default: printf("Invalid input\n");
}
return 0;
}
OUTPUT
Enter a number (1-10): 3
Three
14
Ex.No : 12 Program to read and print one dimension array elements
#include <stdio.h>
int main()
{
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
printf("Array elements are:\n");
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
OUTPUT
Enter the number of elements: 5
Enter 5 elements: 1 2 3 4 5
Array elements are: 1 2 3 4 5
15
Ex.No : 13 Write a program to find the sum of elements of positive and negative
elements of a one- dimensional array
#include <stdio.h>
int main()
{
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n], positiveSum = 0, negativeSum = 0;
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
if (arr[i] > 0) {
positiveSum += arr[i];
} else {
negativeSum += arr[i];
}
}
printf("Sum of positive elements: %d\n", positiveSum);
printf("Sum of negative elements: %d\n", negativeSum);
return 0;
}
Output
Enter the number of elements:
6
Enter 6 elements: 1 -2 3 -4 5 -6
Sum of positive elements: 9
Sum of negative elements: -12
16
Ex.No : 14 Program to read and print two-dimensional array elements
int main()
{
int rows, cols;
printf("Enter the number of rows and columns: ");
scanf("%d %d", &rows, &cols);
int arr[rows][cols];
printf("Enter elements of the array:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &arr[i][j]);
}
}
printf("Array elements are:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT
Enter the number of rows and columns: 2 3
Enter elements of the array: 1 2 3 4 5 6
Array elements are: 1 2 3 4 5 6
17
Ex.No: 15 Program for matrix multiplication
#include <stdio.h>
int main() {
int r1, c1, r2, c2;
printf("Enter rows and columns for first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and columns for second matrix: ");
scanf("%d %d", &r2, &c2);
if (c1 != r2) {
printf("Matrix multiplication not possible.\n");
return 0;
}
int a[r1][c1], b[r2][c2], result[r1][c2];
printf("Enter elements of first matrix:\n");
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c1; j++) {
scanf("%d", &a[i][j]);
}
}
printf("Enter elements of second matrix:\n");
for (int i = 0; i < r2; i++) {
for (int j = 0; j < c2; j++) {
scanf("%d", &b[i][j]);
}
}
// Initialize result matrix to 0
for (int i = 0; i < r1; i++) {
18
for (int j = 0; j < c2; j++) {
result[i][j] = 0;
}
}
// Matrix Multiplication
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
for (int k = 0; k < c1; k++) {
result[i][j] += a[i][k] * b[k][j];
}
}
}
printf("Resultant matrix after multiplication:\n");
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}
return 0;
}
Output:
Enter rows and columns for first matrix: 2 3
Enter rows and columns for second matrix: 3 2
Enter elements of first matrix: 1 2 3 4 5 6
Enter elements of second matrix: 7 8 9 10 11 12
Resultant matrix after multiplication: 58 64 139 154
19
Ex.No : 16 Write a program which reads integers from the standard input into an
array (with max size 10). Let the program read another integer value
from the standard input and then print true if that value is the first or
the last element in the array and print false for other cases
#include <stdio.h>
void main() {
int arr[10], i, n, value;
printf("Enter size of the array : ");
scanf("%d",&n);
if(n <= 0 || n >10){
printf("Invalid size. Size must be between 1 and 10.\n");
}
printf("Enter array elements : ");
for(i = 0; i < n; i++)
{
scanf("%d",&arr[i]);
}
printf("Enter an integer value : ");
scanf(" %d",&value);
if (arr[0] == value || arr[n - 1] == value) {
printf("true\n");
}else {
printf("false\n");
}}
Output
Enter size of the array : 3
Enter array elements : 10 20 30
nter an integer value : 30
true
20
Ex.No: 17 Write a program to read and display the string using gets () and puts ().
#include <stdio.h>
int main()
{
char str[100];
printf("Enter a string: ");
gets(str); // Note: gets() is unsafe and not recommended for use in real programs
printf("You entered: ");
puts(str);
return 0;
}
Output
Enter a string: Hello, World!
You entered: Hello, World!
21
Ex.No:18 Write a program to read and display a string using scanf() and printf()
#include <stdio.h>
int main()
{
char str[100];
printf("Enter a string: ");
scanf("%s", str); // This will only read up to the first whitespace
printf("You entered: %s\n", str);
return 0;
}
Output
Enter a string: Hello
You entered: Hello
22
Ex.No: 19 Write a Program to search the occurrence of a given character in a
given string.
#include <stdio.h>
int main() {
char str[100], ch;
int i, count = 0;
printf("Enter a string: ");
gets(str); // Note: gets() is unsafe and not recommended for use in real
programs
printf("Enter a character to search: ");
scanf("%c", &ch);
for (i = 0; str[i] != '\0'; i++) {
if (str[i] == ch) {
count++;
}
}
printf("The character '%c' occurred %d times.\n", ch, count);
return 0;
}
Output
Enter a string: Hello, World!
Enter a character to search: o
The character 'o' occurred 2 times.
23
Ex.No: 20 Write a Program find out the sum of two integers using pointers
#include <stdio.h>
int main()
{
int a, b, sum;
int *ptr1, *ptr2;
printf("Enter two integers: ");
scanf("%d %d", &a, &b);
ptr1 = &a;
ptr2 = &b;
sum = *ptr1 + *ptr2;
printf("Sum: %d\n", sum);
return 0;
}
Output
Enter two integers: 3 4
Sum: 7
24
Ex.No: 21 Write a Program to swap two values by using call by value and call by
address methods.
#include <stdio.h>
void swapByValue(int x, int y) { // Call by value
int temp;
temp = x;
x = y;
y = temp;
}
void swapByAddress(int *x, int *y) { // Call by address
int temp;
temp = *x;
*x = *y;
*y = temp;
}
int main() {
int a, b;
printf("Enter two integers: ");
scanf("%d %d", &a, &b);
printf("Before swap by value: a = %d, b = %d\n", a, b);
swapByValue(a, b);
printf("After swap by value: a = %d, b = %d\n", a, b);
printf("Before swap by address: a = %d, b = %d\n", a, b);
swapByAddress(&a, &b);
printf("After swap by address: a = %d, b = %d\n", a, b);
return 0;
}
Output
Enter two integers: 3 4
Before swap by value: a = 3, b = 4
After swap by value: a = 3, b = 4
Before swap by address: a = 3, b = 4
After swap by address: a = 4, b = 3
25
Ex.No: 22 Write a Program to know how the one-dimensional array works using
pointers.
#include <stdio.h>
int main()
{
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
for (int i = 0; i < 5; i++)
{
printf("Element %d: %d\n", i, *(ptr + i));
}
return 0;
}
Output:
Element 0: 1
Element 1: 2
Element 2: 3
Element 3: 4
Element 4: 5
26
Ex.No.23 Write a Program to read and display the elements of an array in
reverse order using pointers
#include <stdio.h>
int main()
{
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
for (int i = 4; i >= 0; i--)
{
printf("Element %d: %d\n", i, *(ptr + i));
}
return 0;
}
Output
Element 4: 5
Element 3: 4
Element 2: 3
Element 1: 2
Element 0: 1
27
Ex.No:24 Write a Program which will demonstrates about character pointer
#include <stdio.h>
int main()
{
char str[] = "Hello, World!";
char *ptr = str;
printf("String: %s\n", ptr);
return 0;
}
Output
String: Hello, World!
28
Ex.No: 25 Write a Program to know how to call a function
#include <stdio.h>
// Function declaration
void sayHello();
int main()
{
// Function call
sayHello();
return 0;
}
// Function definition
void sayHello()
{
printf("Hello, World!\n");
}
Output
Hello, World!
29
Ex.No: 26 Write a Program to know how to make function declarations before the
main () function, made calls to the functions within main() function and
write the definitions of the functions at the outside of the main()
function
#include <stdio.h>
// Function declarations
void add(float a, float b);
void subtract(float a, float b);
void multiply(float a, float b);
void divide(float a, float b);
int main()
{
float x, y;
printf("Enter two float numbers: ");
scanf("%f %f", &x, &y);
add(x, y);
subtract(x, y);
multiply(x, y);
divide(x, y);
return 0;
}
// Function definitions
void add(float a, float b) {
printf("Addition: %.2f\n", a + b);
}
30
void subtract(float a, float b) {
printf("Subtraction: %.2f\n", a - b);
}
OUTPUT
31
Ex.No: 27 Write a Program to find the addition, subtraction, multiplication and
division of two float variables using functions
#include <stdio.h>
int sum(int, int);
int sub(int, int);
int mul(int, int);
int div(int, int);
void main() {
int a, b;
printf("Enter two values : ");
scanf("%d%d", &a, &b);
printf("Addition of two values = %d\n", sum(a, b));
printf("Subtraction of two values = %d\n", sub(a, b));
printf("multiplication of two values = %d\n", mul(a, b));
printf("division of two values = %d\n", div(a, b));
}
sum(int a,int b)
{
return a+b ;
}
sub(int a, int b )
{
return a-b;
}
mul(int a, int b )
{
return a*b ;
}
div(int a, int b )
{
return a/b;
}
Output
Enter two values : 20 5
Addition of two values = 25
Subtraction of two values = 15
multiplication of two values = 100
division of two values = 4
32
Ex.No:28 Write a Program to know about different categories of functions
#include <stdio.h>
int main() {
int sum = add(3, 4);
printf("Sum from function with return value: %d\n", sum);
printSum(3, 4);
int five = getFive();
printf("Value from function with return value: %d\n", five);
sayHello();
return 0;
}
Output
Sum from function with return value: 7
Sum: 7
Value from function with return value: 5
Hello!
33
Ex.No: 29 Write a C program to understand about function with arguments and
with return value
#include <stdio.h>
int main()
{
int x, y, sum;
printf("Enter two integers: ");
scanf("%d %d", &x, &y);
sum = add(x, y);
printf("Sum: %d\n", sum);
return 0;
}
Output
Enter two integers: 5 6
Sum: 11
34
Ex.No: 30) Write a C program to understand about function without arguments and
without return value
#include <stdio.h>
void sayHello()
{
printf("Hello, World!\n");
}
int main()
{
sayHello();
return 0;
}
Output
Hello, World!
35
Ex.No: 31. Write a C program to Create a Single Linked Lists - Creation of a node,
Add nodes and Traversing List of nodes
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *next;
};
typedef struct node *NODE;
void main() {
NODE first = NULL;
int x;
printf("Enter elements up to -1 : ");
scanf("%d", &x);
while (x != -1) {
first = addNodes(first, x);
scanf("%d", &x);
}
if (first == NULL) {
printf("Single Linked List is empty\n");
} else {
printf("The elements in SLL are : ");
traverseList(first);
}
}
NODE createNode() {
NODE temp = (NODE)malloc(sizeof(struct node));
temp->next=NULL;
36
return temp;
}
NODE addNodes(NODE first, int x) {
NODE temp,last=first;
temp=createNode();
temp->data=x;
if(first==NULL){
first=temp;
}else{
while(last->next!=NULL){
last=last->next;
}
last->next=temp;
}
return first;
}
void traverseList(NODE first) {
NODE temp=first;
while(temp!=NULL){
printf("%d --> ",temp->data);
temp=temp->next;
}
printf("NULL\n");
}
Output
Enter elements up to -1 : 9 18 27 36 45 -1
The elements in SLL are : 9 --> 18 --> 27 --> 36 --> 45 --> NULL
37
Ex.No:32 . Write a C program to demonstrate
a) Delete a node at Begin in Single Linked Lists
b) Delete a node at End in Single Linked Lists
c) Delete a node at Position in Single Linked Lists
d) Search the Position of an Element in Single Linked Lists
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
typedef struct node *NODE;
void main() {
NODE first = NULL;
int x, op;
while(1) {
printf("1.Insert At Begin 2.Delete at Begin 3.Traverse the List
4.Exit\n");
printf("Enter your option : ");
scanf("%d", &op);
switch(op) {
case 1: printf("Enter an element : ");
scanf("%d", &x);
first = insertAtBegin(first, x);
break;
case 2: if (first == NULL) {
printf("Single Linked List is empty so
deletion is not possible\n");
} else {
first = deleteAtBegin(first);
}
38
break;
case 3: if (first == NULL) {
printf("Single Linked List is empty\n");
} else {
printf("The elements in SLL are : ");
traverseList(first);
}
break;
case 4: exit(0);
}
}
}
NODE createNode() {
NODE temp;
temp = (NODE) malloc(sizeof(struct node));
temp -> next = NULL;
return temp;
}
39
void traverseList(NODE first) {
NODE temp = first;
while (temp != NULL) {
printf("%d --> ",temp -> data);
temp = temp -> next;
}
printf("NULL\n");
}
Output
1.Insert·At·Begin·2.Delete·at·Begin·3.Traverse·the·List·4.Exit
Enter·your·option·:·1
Enter·an·element·:·10
1.Insert·At·Begin·2.Delete·at·Begin·3.Traverse·the·List·4.Exit
Enter·your·option·:·3
The·elements·in·SLL·are·:·10·-->·NULL
1.Insert·At·Begin·2.Delete·at·Begin·3.Traverse·the·List·4.Exit
Enter·your·option·:·2
The·deleted·element·from·SLL·:·10
1.Insert·At·Begin·2.Delete·at·Begin·3.Traverse·the·List·4.Exit
Enter·your·option·:·3
Single·Linked·List·is·empty
1.Insert·At·Begin·2.Delete·at·Begin·3.Traverse·the·List·4.Exit
Enter·your·option·:·4
40
b) Delete a node at End in Single Linked Lists
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
typedef struct node *NODE;
void main() {
NODE first = NULL;
int x, op;
while(1) {
printf("1.Insert At End 2.Delete at End 3.Traverse the List 4.Exit\n");
printf("Enter your option : ");
scanf("%d", &op);
switch(op) {
case 1: printf("Enter an element : ");
scanf("%d", &x);
first = insertAtEnd(first, x);
break;
case 2: if (first == NULL) {
printf("Single Linked List is empty so
deletion is not possible\n");
} else {
first = deleteAtEnd(first);
}
break;
case 3: if (first == NULL) {
printf("Single Linked List is empty\n");
} else {
printf("The elements in SLL are : ");
traverseList(first);
}
break;
case 4: exit(0);
}
}
}
41
NODE createNode() {
NODE temp;
temp = (NODE) malloc(sizeof(struct node));
temp -> next = NULL;
return temp;
}
Output
1.Insert·At·End·2.Delete·at·End·3.Traverse·the·List·4.Exit
Enter·your·option·:·1
Enter·an·element·:·55
1.Insert·At·End·2.Delete·at·End·3.Traverse·the·List·4.Exit
Enter·your·option·:·1
Enter·an·element·:·66
1.Insert·At·End·2.Delete·at·End·3.Traverse·the·List·4.Exit
Enter·your·option·:·2
The·deleted·item·from·SLL·:·66
1.Insert·At·End·2.Delete·at·End·3.Traverse·the·List·4.Exit
Enter·your·option·:·2
The·deleted·item·from·SLL·:·55
1.Insert·At·End·2.Delete·at·End·3.Traverse·the·List·4.Exit
Enter·your·option·:·3
Single·Linked·List·is·empty
1.Insert·At·End·2.Delete·at·End·3.Traverse·the·List·4.Exit
Enter·your·option·:·4
43
c) Delete a node at Position in Single Linked Lists
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
typedef struct node *NODE;
void main() {
NODE first = NULL;
int x, op, pos;
while(1) {
printf("1.Insert At End 2.Delete at Position 3.Traverse the List
4.Exit\n");
printf("Enter your option : ");
scanf("%d", &op);
switch(op) {
case 1: printf("Enter an element : ");
scanf("%d", &x);
first = insertAtEnd(first, x);
break;
case 2: if (first == NULL) {
printf("Single Linked List is empty so
deletion is not possible\n");
} else {
printf("Enter position : ");
scanf("%d", &pos);
first = deleteAtPosition(first, pos);
}
break;
case 3: if (first == NULL) {
printf("Single Linked List is empty\n");
} else {
printf("The elements in SLL are : ");
traverseList(first);
}
break;
44
case 4: exit(0);
}
}
}
NODE createNode() {
NODE temp;
temp = (NODE) malloc(sizeof(struct node));
temp -> next = NULL;
return temp;
}
pre=last;
last=last->next;
45
}
if(last==NULL||pos<=0){
printf("No such position in SLL so deletion is not possible\n");
return first;
}else{
pre->next=last->next;
}
}
Output
47
No such position in SLL so deletion is not possible
1.Insert At End 2.Delete at Position 3.Traverse the List 4.Exit
Enter your option : 3
The elements in SLL are : 11 --> 22 --> NULL
1.Insert At End 2.Delete at Position 3.Traverse the List 4.Exit
Enter your option : 2
Enter position : 1
The deleted element from SLL : 11
1.Insert At End 2.Delete at Position 3.Traverse the List 4.Exit
48
d) Search the Position of an Element in Single Linked Lists
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *next;
};
typedef struct node *NODE;
void main() {
NODE first = NULL;
int x, pos, op;
while(1) {
printf("1.Insert At Begin 2.Search an element Position 3.Traverse the
List 4.Exit\n");
printf("Enter your option : ");
scanf("%d", &op);
switch(op) {
case 1: printf("Enter an element : ");
scanf("%d", &x);
first = insertAtBegin(first, x);
break;
case 2: printf("Enter search element : ");
scanf("%d", &x);
pos = searchPosOfEle(first, x);
if (pos == 0) {
printf("The given element %d is not found
in the given SLL\n", x);
} else {
printf("The given element %d is found at
position : %d\n", x, pos);
}
break;
case 3: if (first == NULL) {
printf("Single Linked List is empty\n");
} else {
printf("The elements in SLL are : ");
traverseList(first);
}
49
break;
case 4: exit(0);
}
}
}
ODE createNode() {
NODE temp;
temp = (NODE) malloc(sizeof(struct node));
temp -> next = NULL;
return temp;
}
Output
51
Ex.No:33 Write a C program to Create a doubly Linked Lists - Creation of a node,
Add nodes and Traversing List of nodes
a) Creating a Node
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *prev;
struct node *next;
};
typedef struct node * NODE;
void main() {
NODE first = NULL,second = NULL;
int x;
//Creating first node of DLL
first = createNodeInDLL();
printf("Enter element of first node : ");
scanf("%d", &x);
first -> data = x;
//Creating Second Node of DLL
second = createNodeInDLL();
printf("Enter element of second node : ");
scanf("%d", &x);
second -> data = x;
//creating Links
first -> next = second;
second -> prev = first;
printf("The Double Linked List is : %d <--> %d <--> NULL\n", first -
> data, first -> next -> data);
}
NODE createNodeInDLL() {
NODE temp;
temp=(NODE)malloc(sizeof(struct node));
temp -> prev=NULL;
temp->next=NULL;
return temp;
}
52
Output
Enter·element·of·first·node·:·10
Enter·element·of·second·node·:·15
The·Double·Linked·List·is·:·10·<-->·15·<-->·NULL
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *prev;
struct node *next;
};
typedef struct node * NODE;
void main() {
NODE first = NULL;
int x, op;
while(1) {
printf("1.Insert At Begin 2.Count Number of Nodes 3.Traverse
the List 4.Exit\n");
printf("Enter your option : ");
scanf("%d", &op);
switch(op) {
case 1: printf("Enter an element : ");
scanf("%d", &x);
first = insertAtBeginInDLL(first, x);
break;
case 2: printf("The number of nodes in a DLL are :
%d\n", countInDLL(first));
break;
case 3: if (first == NULL) {
printf("Double Linked List is
empty\n");
} else {
printf("The elements in DLL are : ");
traverseListInDLL(first);
}
break;
53
case 4: exit(0);
}
}
}
NODE createNodeInDLL() {
NODE temp;
temp = (NODE)malloc(sizeof(struct node));
temp -> prev = NULL;
temp -> next = NULL;
return temp;
}
Expected Output
1.Insert At Begin 2.Count Number of Nodes 3.Traverse the List 4.Exit
Enter your option : 1
Enter an element : 15
1.Insert At Begin 2.Count Number of Nodes 3.Traverse the List 4.Exit
Enter your option : 1
Enter an element : 16
1.Insert At Begin 2.Count Number of Nodes 3.Traverse the List 4.Exit
Enter your option : 1
Enter an element : 17
1.Insert At Begin 2.Count Number of Nodes 3.Traverse the List 4.Exit
Enter your option : 3
The elements in DLL are : 17 <--> 16 <--> 15 <--> NULL
1.Insert At Begin 2.Count Number of Nodes 3.Traverse the List 4.Exit
Enter your option : 2
The number of nodes in a DLL are : 3
1.Insert At Begin 2.Count Number of Nodes 3.Traverse the List 4.Exit
Enter your option : 1
Enter an element : 18
1.Insert At Begin 2.Count Number of Nodes 3.Traverse the List 4.Exit
Enter your option : 1
Enter an element : 19
1.Insert At Begin 2.Count Number of Nodes 3.Traverse the List 4.Exit
55
Enter your option : 3
The elements in DLL are : 19 <--> 18 <--> 17 <--> 16 <--> 15 <--> NULL
1.Insert At Begin 2.Count Number of Nodes 3.Traverse the List 4.Exit
Enter your option : 2
The number of nodes in a DLL are : 5
1.Insert At Begin 2.Count Number of Nodes 3.Traverse the List 4.Exit
Enter your option : 4
56
Ex.No 34 :Write a C program to demonstrate Double Linked Lists - Delete a node at Begin,
delete a node at End, Delete a node at Position and Search the Position of an Element
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *prev;
struct node *next;
};
typedef struct node * NODE;
void main() {
NODE first = NULL;
int x, op;
while(1) {
printf("1.Insert At Begin 2.Delete at Begin 3.Traverse the List 4.Exit\n");
printf("Enter your option : ");
scanf("%d", &op);
switch(op) {
case 1: printf("Enter an element : ");
scanf("%d", &x);
first = insertAtBeginInDLL(first, x);
break;
case 2: if (first == NULL) {
printf("Double Linked List is empty so deletion
is not possible\n");
} else {
first = deleteAtBeginInDLL(first);
}
break;
case 3: if (first == NULL) {
printf("Double Linked List is empty\n");
} else {
printf("The elements in DLL are : ");
traverseListInDLL(first);
}
break;
case 4: exit(0);
}
}
}
NODE createNodeInDLL() {
NODE temp;
temp = (NODE)malloc(sizeof(struct node));
temp->prev = NULL;
temp->next = NULL;
return temp;
}
57
NODE insertAtBeginInDLL(NODE first, int x) {
NODE temp;
temp=createNodeInDLL();
temp->data=x;
if(first!=NULL){
temp->next=first;
first->prev=temp;
}
first=temp;
return first;
}
Output
Expected Output
58
1.Insert At Begin 2.Delete at Begin 3.Traverse the List 4.Exit
59
Enter your option : 1
Enter an element : 30
1.Insert At Begin 2.Delete at Begin 3.Traverse the List 4.Exit
Enter your option : 3
The elements in DLL are : 30 <--> 20 <--> 10 <--> NULL
1.Insert At Begin 2.Delete at Begin 3.Traverse the List 4.Exit
Enter your option : 2
The deleted element from DLL : 30
1.Insert At Begin 2.Delete at Begin 3.Traverse the List 4.Exit
Enter your option : 2
The deleted element from DLL : 20
1.Insert At Begin 2.Delete at Begin 3.Traverse the List 4.Exit
Enter your option : 3
The elements in DLL are : 10 <--> NULL
1.Insert At Begin 2.Delete at Begin 3.Traverse the List 4.Exit
Enter your option : 2
The deleted element from DLL : 10
1.Insert At Begin 2.Delete at Begin 3.Traverse the List 4.Exit
Enter your option : 3
Double Linked List is empty
1.Insert At Begin 2.Delete at Begin 3.Traverse the List 4.Exit
Enter your option : 2
Double Linked List is empty so deletion is not possible
1.Insert At Begin 2.Delete at Begin 3.Traverse the List 4.Exit
Enter your option : 4
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *prev;
struct node *next;
};
typedef struct node * NODE;
void main() {
NODE first = NULL;
int x, op;
while(1) {
60
printf("1.Insert At End 2.Delete at End 3.Traverse the List 4.Exit\n");
printf("Enter your option : ");
scanf("%d", &op);
switch(op) {
case 1: printf("Enter an element : ");
scanf("%d", &x);
first = insertAtEndInDLL(first, x);
break;
case 2: if (first == NULL) {
printf("Double Linked List is empty so deletion
is not possible\n");
} else {
first = deleteAtEndInDLL(first);
}
break;
case 3: if (first == NULL) {
printf("Double Linked List is empty\n");
} else {
printf("The elements in DLL are : ");
traverseListInDLL(first);
}
break;
case 4: exit(0);
}
}
}
NODE createNodeInDLL() {
NODE temp;
temp = (NODE)malloc(sizeof(struct node));
temp->prev = NULL;
temp->next = NULL;
return temp;
}
}
last->next=temp;
temp->prev=last;
}
return first;
61
}
62
NODE deleteAtEndInDLL(NODE first) {
NODE temp,last=first;
if(last->next==NULL){
first=NULL;
}else{
while(last->next!=NULL){
temp=last;
last=last->next;
}
temp->next=NULL;
}
printf("The deleted element from DLL : %d\n",last->data);
free(last);
return first;
}
Expected Output
63
1.Insert At End 2.Delete at End 3.Traverse the List 4.Exit
Enter your option : 2
The deleted element from DLL : 20
1.Insert At End 2.Delete at End 3.Traverse the List 4.Exit
Enter your option : 3
The elements in DLL are : 10 <--> NULL
1.Insert At End 2.Delete at End 3.Traverse the List 4.Exit
Enter your option : 4
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *prev;
struct node *next;
};
typedef struct node * NODE;
void main() {
NODE first = NULL;
int x, op, pos;
while(1) {
printf("1.Insert At End 2.Delete at Position 3.Traverse the List 4.Exit\n");
printf("Enter your option : ");
scanf("%d", &op);
switch(op) {
case 1: printf("Enter an element : ");
scanf("%d", &x);
first = insertAtEndInDLL(first, x);
break;
case 2: if (first == NULL) {
printf("Double Linked List is empty so deletion
is not possible\n");
} else {
printf("Enter position : ");
scanf("%d", &pos);
first = deleteAtPositionInDLL(first, pos);
}
break;
case 3: if (first == NULL) {
printf("Double Linked List is empty\n");
} else {
printf("The elements in DLL are : ");
traverseListInDLL(first);
64
}
break;
case 4: exit(0);
}
}
}
NODE createNodeInDLL() {
NODE temp;
temp = (NODE)malloc(sizeof(struct node));
temp->prev = NULL;
temp->next = NULL;
return temp;
}
for(int i=1;i<position;i++){
if(last==NULL){
printf("No such position in DLL so deletion is not
possible\n");
return first;
}else{
prev=last;
last=last->next;
}
}
if(last==NULL||position<=0){
printf("No such position in DLL so deletion is not possible\n");
return first;
}else if(last->next==NULL){
prev->next=NULL;
}else{
prev->next=last->next;
prev->next->prev=last->prev;
}
}
}
}
Expected Output
66
1.Insert At End 2.Delete at Position 3.Traverse the List 4.Exit
Enter your option : 3
67
The elements in DLL are : 11 <--> 22 <--> 33 <--> NULL
1.Insert At End 2.Delete at Position 3.Traverse the List 4.Exit
Enter your option : 2
Enter position : 2
The deleted element from DLL : 22
1.Insert At End 2.Delete at Position 3.Traverse the List 4.Exit
Enter your option : 3
The elements in DLL are : 11 <--> 33 <--> NULL
1.Insert At End 2.Delete at Position 3.Traverse the List 4.Exit
Enter your option : 2
Enter position : 1
The deleted element from DLL : 11
1.Insert At End 2.Delete at Position 3.Traverse the List 4.Exit
Enter your option : 3
The elements in DLL are : 33 <--> NULL
1.Insert At End 2.Delete at Position 3.Traverse the List 4.Exit
Enter your option : 4
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *prev;
struct node *next;
};
typedef struct node * NODE;
void main() {
NODE first = NULL;
int x, pos, op;
while(1) {
printf("1.Insert At Begin 2.Search an element Position 3.Traverse the List
4.Exit\n");
printf("Enter your option : ");
scanf("%d", &op);
switch(op) {
case 1: printf("Enter an element : ");
scanf("%d", &x);
first = insertAtBeginInDLL(first, x);
break;
68
case 2: printf("Enter search element : ");
scanf("%d", &x);
pos = searchPosOfEleInDLL(first, x);
if (pos == 0) {
printf("The given element %d is not found in the
given DLL\n", x);
} else {
printf("The given element %d is found at
position : %d\n", x, pos);
}
break;
case 3: if (first == NULL) {
printf("Double Linked List is empty\n");
} else {
printf("The elements in DLL are : ");
traverseListInDLL(first);
}
break;
case 4: exit(0);
}
}
}
NODE createNodeInDLL() {
NODE temp;
temp = (NODE)malloc(sizeof(struct node));
temp->prev = NULL;
temp->next = NULL;
return temp;
}
}else{
c++;
curr=curr->next;
}
}
return c+1;
}
}
Expected Output
70
Enter your option : 1
Enter an element : 4
1.Insert At Begin 2.Search an element Position 3.Traverse the List 4.Exit
Enter your option : 3
The elements in DLL are : 4 <--> 3 <--> 6 <--> 5 <--> NULL
1.Insert At Begin 2.Search an element Position 3.Traverse the List 4.Exit
Enter your option : 2
Enter search element : 5
The given element 5 is found at position : 4
1.Insert At Begin 2.Search an element Position 3.Traverse the List 4.Exit
Enter your option : 2
Enter search element : 3
The given element 3 is found at position : 2
1.Insert At Begin 2.Search an element Position 3.Traverse the List 4.Exit
Enter your option : 2
Enter search element : 7
The given element 7 is not found in the given DLL
1.Insert At Begin 2.Search an element Position 3.Traverse the List 4.Exit
Enter your option : 4
71
Ex.No: 35 Write a C program to Create a Circular Linked Lists - Insert a node at
Begin, insert a node at End, Insert a node at Position and Count number of
nodes
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
typedef struct node *NODE;
void main() {
NODE first = NULL;
int x, op;
while(1) {
printf("1.Insert At Begin 2.Count Number of Nodes 3.Traverse the
List 4.Exit\n");
printf("Enter your option : ");
scanf("%d", &op);
switch(op) {
case 1: printf("Enter an element : ");
scanf("%d", &x);
first = insertAtBeginInCLL(first, x);
break;
case 2: printf("The number of nodes in a CLL are :
%d\n", countInCLL(first));
break;
case 3: if (first == NULL) {
printf("Circular Linked List is empty\n");
} else {
printf("The elements in CLL are : ");
traverseListInCLL(first);
}
break;
case 4: exit(0);
}
}
}
NODE createNodeInCLL() {
NODE temp;
temp = (NODE) malloc(sizeof(struct node));
72
temp -> next = NULL;
return temp;
}
temp->data=x;
if(first==NULL){
temp->next=temp;
first=temp;
}else{
while(lastNode->next!=first){
lastNode=lastNode->next;
}
temp->next=first;
first=temp;
lastNode -> next = first;
}
return first;
}
if(first == NULL){
return 0;
}
do{
count++;
current=current -> next;
}while(current != first);
return count;
}
73
temp = temp -> next;
} while (temp != first);
printf("\n");
}
Expected Output
74
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *next;
};
typedef struct node *NODE;
void main() {
NODE first = NULL;
int x, op;
while(1) {
printf("1.Insert At End 2.Traverse the List 3.Exit\n");
printf("Enter your option : ");
scanf("%d", &op);
switch(op) {
case 1: printf("Enter an element : ");
scanf("%d", &x);
first = insertAtEndInCLL(first, x);
break;
case 2: if (first == NULL) {
printf("Circular Linked List is empty\n");
} else {
printf("The elements in CLL are : ");
traverseListInCLL(first);
}
break;
case 3: exit(0);
}
}
}
NODE createNodeInCLL() {
NODE temp;
temp = (NODE) malloc(sizeof(struct node));
temp -> next = NULL;
return temp;
}
if(first==NULL){
75
first=newNode;
newNode->next=first;
}else{
NODE temp=first;
while(temp->next!=first){
temp=temp->next;
}
temp->next=newNode;
newNode->next=first;
}
return first;
}
do{
printf("%d --> ",temp->data);
temp=temp->next;
}while(temp!=first);
printf("\n");
}
}
Expected Output
76
Enter your option : 2
The elements in CLL are : 12 --> 13 --> 14 -->
1.Insert At End 2.Traverse the List 3.Exit
Enter your option : 1
Enter an element : 15
1.Insert At End 2.Traverse the List 3.Exit
Enter your option : 2
The elements in CLL are : 12 --> 13 --> 14 --> 15 -->
1.Insert At End 2.Traverse the List 3.Exit
Enter your option : 3
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *next;
};
typedef struct node *NODE;
void main() {
NODE first = NULL;
int x, pos, op;
while(1) {
printf("1.Insert At specified position 2.Traverse the List 3.Exit\n");
printf("Enter your option : ");
scanf("%d", &op);
switch(op) {
case 1: printf("Enter a position : ");
scanf("%d", &pos);
if (pos <= 0) {
printf("No such position in CLL so
insertion is not possible\n");
} else {
printf("Enter an element : ");
scanf("%d", &x);
first = insertAtPositionInCLL(first, pos,
x);
}
break;
77
case 2: if (first == NULL) {
78
printf("Circular Linked List is empty\n");
} else {
printf("The elements in CLL are : ");
traverseListInCLL(first);
}
break;
case 3: exit(0);
}
}
}
NODE createNodeInCLL() {
NODE temp;
temp = (NODE) malloc(sizeof(struct node));
temp -> next = NULL;
return temp;
}
if(first==NULL) {
temp->next=temp;
first=temp;
}else if (pos==1){
NODE curr=first;
while(curr->next!=first){
curr=curr->next;
}
temp->next=first;
first=temp;
curr->next=first;
}else {
NODE curr=first;
int count=1;
while(count<pos-1&& curr->next!=first){
curr=curr->next;
count++;
}if(count==pos-1){
temp->next=curr->next;
curr->next=temp;
79
}
else {
printf("No such position in CLL so insertion is not
possible\n");
}
}
return first;
}
Expected Output
80
The elements in CLL are : 23 --> 45 --> 34 -->
1.Insert At specified position 2.Traverse the List 3.Exit
Enter your option : 3
81
Ex.No :36 6.Write a C program to demonstrate the applications of Lists -
Polynomial Operations using Linked Lists
#include <stdio.h>
#include <stdlib.h>
#define max 20
struct polynomial {
int coeff;
int exp;
struct polynomial *next;
};
typedef struct polynomial *poly;
poly create(poly head) {
poly temp;
char ch;
int coeff, exp;
do {
temp = (poly)malloc(sizeof(struct polynomial));
printf("Enter coeff and exp of node : ");
scanf("%d%d", &coeff, &exp);
temp -> coeff = coeff;
temp -> exp = exp;
temp -> next = NULL;
head = addTerm(head, temp);
printf("Do u want another node (y/n) : ");
scanf(" %c", &ch);
} while(ch != 'n');
return head;
}
void main() {
poly head = NULL;
int ch;
head = create(head);
printf("The polynomial is : ");
print(head);
}
poly addTerm(poly head, poly temp) {
poly p1,p2;
p1=p2=head;
if(p1==NULL){
82
head=temp;
}else{
while(p1!=NULL && p1->exp>temp->exp){
p2=p1;
p1=p1->next;
}
if(p1==NULL){
p2->next=temp;
}else if(p1->exp==temp->exp){
p1->coeff=p1->coeff+temp->coeff;
}else if(p1->exp<temp->exp){
if(p2==p1){
temp->next=p1;
head=temp;
}else{
temp->next=p1;
p2->next=temp;
}
}
}
return head;
}
83
Ex.No : 37 Write a C program to implement stack using Arrays
#include <stdio.h>
#include <stdlib.h>
#define STACK_MAX_SIZE 10
int arr[STACK_MAX_SIZE];
int top = -1;
void display() {
if(top==-1){
printf("Stack is empty.\n");
}else{
printf("Elements of the stack are : ");
int index;
for(index=top;index>=0;index--){
printf("%d ",arr[index]);
}
printf("\n");
}
}
int main() {
int op, x;
while(1) {
printf("1.Push 2.Display 3.Exit\n");
printf("Enter your option : ");
scanf("%d", &op);
switch(op) {
case 1:
printf("Enter element : ");
scanf("%d", &x);
push(x);
break;
84
case 2:
display();
break;
case 3:
exit(0);
}
}
}
Expected Output
85
Ex.No : 38 Write a C program to implement stack using Linked List
#include <stdio.h>
#include <stdlib.h>
struct stack {
int data;
struct stack *next;
};
stk push(int x) {
stk temp;
temp = (stk)malloc(sizeof(struct stack));
if(temp==NULL){
printf("Stack is overflow.\n");
}else{
temp->data = x;
temp->next = top;
top = temp;
printf("Successfully pushed.\n");
}
}
void display() {
stk temp=top;
if(temp==NULL){
printf("Stack is empty.\n");
}else{
printf("Elements of the stack are : ");
while(temp!=NULL){
printf("%d ",temp->data);
temp=temp->next;
}
printf("\n");
}
}
int main() {
int op, x;
while(1) {
printf("1.Push 2.Display 3.Exit\n");
86
printf("Enter your option : ");
scanf("%d",&op);
switch(op) {
case 1:
printf("Enter element : ");
scanf("%d",&x);
push(x);
break;
case 2:
display();
break;
case 3:
exit(0);
}
}
}
Expected Output
87
Successfully pushed.
1.Push 2.Display 3.Exit
Enter your option : 2
Elements of the stack are : 5 4 3 2 1
1.Push 2.Display 3.Exit
Enter your option : 1
Enter element : 5
Successfully pushed.
1.Push 2.Display 3.Exit
Enter your option : 1
Enter element : 4
Successfully pushed.
1.Push 2.Display 3.Exit
Enter your option : 1
Enter element : 3
Successfully pushed.
1.Push 2.Display 3.Exit
Enter your option : 1
Enter element : 2
Successfully pushed.
1.Push 2.Display 3.Exit
Enter your option : 1
Enter element : 1
Successfully pushed.
1.Push 2.Display 3.Exit
Enter your option : 2
Elements of the stack are : 1 2 3 4 5 5 4 3 2 1
1.Push 2.Display 3.Exit
Enter your option : 3
88
Ex.No : 39 Write a C program to convert infix expression to postfix expression
using stack
#include<stdio.h>
#include<stdlib.h>
void evaluate();
void push(char);
char pop();
int prec(char);
char infix[30], postfix[30], stack[30];
int top = -1;
void main()
{
printf("\nEnter the valid infix expression:\t");
scanf("%s", infix);
evaluate();
printf("\nThe entered infix expression is :\n %s \n", infix);
printf("\nThe corresponding postfix expression is :\n %s \n", postfix);
}
void evaluate()
{
int i = 0, j = 0;
char symb, temp;
push('#');
for(i=0; infix[i] != '\0'; i++)
{
symb = infix[i];
switch(symb)
{
case '(' : push(symb);
break;
case ')' : temp = pop();
while(temp != '(' )
{
postfix[j] = temp;
j++;
temp = pop();
}
break;
case '+' :
case '-' :
case '*' :
case '/' :
89
case '%' :
case '^' :
case '$' : while( prec(stack[top]) >= prec(symb) )
{
temp = pop();
postfix[j] = temp;
j++;
}
push(symb);
break;
default: postfix[j] = symb;
j++;
}
}
while(top > 0)
{
temp = pop();
postfix[j] = temp;
j++;
}
postfix[j] = '\0';
}
void push(char item)
{
top = top+1;
stack[top] = item;
}
char pop()
{
char item;
item = stack[top];
top = top-1;
return item;
}
int prec(char symb)
{
int p;
switch(symb)
{
case '#' : p = -1;
break;
case '(' :
case ')' : p = 0;
90
break;
case '+' :
case '-' : p = 1;
break;
case '*' :
case '/' :
case '%' : p = 2;
break;
case '^' :
case '$' : p = 3;
break;
}
return p;
}
Output:
Enter the valid infix expression: (a+b)+c/d*e
The entered infix expression is : (a+b)+c/d*e
The corresponding postfix expression is : ab+cd/e*+
91
Ex.No : 40 Write a C program to implement Linear queue using Array
#include <stdlib.h>
#include <stdio.h>
//This function should insert the x into the Queue. if success print "Successfully
void enqueue(int x) {
if(re==MAX-1){
printf("Queue is overflow.\n");
} else {
re=re+1;
qu[re]=x;
printf("Successfully inserted.\n"); }
if(fr==-1){
fr=fr+1;
}
}
//This displays all the elements of the queue.//If the queue is empty
display "Queue is empty
void display() {
if(fr==-1 && re==-1){
printf("Queue is empty.\n");
}
printf("Elements in the queue : ");
for(int i=fr;i<=re;i++){
printf("%d ",qu[i]); }
printf("\n");}int main() {
int op, x;
while(1) {
printf("1.Enqueue 2.Display 3.Exit\n");
printf("Enter your option : ");
scanf("%d",&op);
switch(op) {
case 1:
printf("Enter element : ");
scanf("%d",&x);
enqueue(x);
break;
case 2: display();
break;
case 3: exit(0);
}
}
92
}
Expected Output
93
Ex.No : 41 Write a C program to implement Linear queue using Linked List
#include <stdlib.h>
#include <stdio.h>
struct queue {
int data;
struct queue *next;
};
temp=temp->next;
}
printf("\n");
}
94
}
int main() {
int op, x;
while(1) {
printf("1.Enqueue 2.Display 3.Exit\n");
printf("Enter your option : ");
scanf("%d",&op);
switch(op) {
case 1:
printf("Enter element : ");
scanf("%d",&x);
enqueue(x);
break;
case 2:
display();
break;
case 3:
exit(0);
}
}}
Expected Output
95
Ex.No:42 Write a C program to implement insertions and deletions in a circular
Queue
#include <stdio.h>
#include <stdlib.h>
#define m 5
int q[m];
int fr=-1,re=-1;
void dequeue(){ if(fr==-1){
printf("Circular queue is underflow.\n");
} else {
printf("Deleted element = %d\n",q[fr]);
if(re==fr){
fr=re=-1;
} else{
if(fr==m-1){
fr=0;
}else{
fr++;
}
}
}
}
void size(){
if(fr==-1 && re==-1){
printf("Circular queue size : 0\n");
} else {
if(fr<=re){
printf("Circular queue size : %d\n",re-fr+1);
} else {
printf("Circular queue size : %d\n",m-fr+re+1);
}
void isEmpty(){
if(fr==-1 && re==-1){
printf("Circular queue is empty.\n");
} else {
96
printf("Circular queue is not empty.\n");
}
}
void enqueue(int x){
if((re==m-1)&&(fr==0||re+1==fr)){
printf("Circular queue is overflow.\n");
}else{
if(re==m-1){
re=-1;
}else if(fr==-1){
fr=0;
}
re++;
q[re]=x;
printf("Successfully inserted.\n");
}
}
void display(){
if(fr==-1 && re==-1){
printf("Circular queue is empty.\n");
} else {
printf("Elements in the circular queue : ");
if(fr<=re){
for(int i=fr;i<=re;i++){
printf("%d ",q[i]);
}
} else {
for(int i=fr;i<=m-1; i++){
printf("%d ",q[i]);
}
for(int i=0;i<=re; i++){
printf("%d ",q[i]);
}
}
printf("\n");
}
97
int main() {
int op, x;
while(1) {
printf("1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size
6.Exit\n");
printf("Enter your option : ");
scanf("%d",&op);
switch(op) {
case 1:
printf("Enter element : ");
scanf("%d",&x);
enqueue(x);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
isEmpty();
break;
case 5:
size();
break;
case 6: exit(0);
}
}
}
Expected Output
98
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option : 1
Enter element : 12
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option : 1
Enter element : 34
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option : 1
Enter element : 56
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option : 3
Elements in the circular queue : 12 34 56
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option : 1
Enter element : 38
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option : 1
Enter element : 25
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option : 3
Elements in the circular queue : 12 34 56 38 25
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option : 1
Enter element : 56
Circular queue is overflow.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option : 2
Deleted element = 12
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option : 2
99
Deleted element = 34
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option : 3
Elements in the circular queue : 56 38 25
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option : 4
Circular queue is not empty.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option : 5
Circular queue size : 3
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option : 1
Enter element : 11
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option : 3
Elements in the circular queue : 56 38 25 11
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option : 6
100
Ex.No:43
#include <conio.h>
#include <stdio.h>
#include "QueueOperationsSTK.c"
int main() {
int op, x;
que = (Q)malloc(sizeof(struct queue));
que->stack1 = NULL;
que->stack2 = NULL;
while(1) {
printf("1.Enqueue 2.Dequeue 3.Display 4.Exit\n");
printf("Enter your option : ");
scanf("%d",&op);
switch(op) {
case 1:
printf("Enter element : ");
scanf("%d",&x);
enqueue(x);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4: exit(0);
}
}
}
101
Ex.No:44 Write a C program to implement insertions and deletions in a Deque
using array
#include <stdio.h>
#include <conio.h>
#define m 10
int d[m];
int r =-1,f=-1;
void inject(int x) {
if(r==m-1){
printf("Double ended queue is overflow.\n");
} else{
r++;
d[r]=x;
if(f==-1){
f=0;
}
printf("Successfully inserted at rear side.\n");
}
}
void eject() {
if(r==-1){
printf("Double ended queue is underflow.\n");
} else{
printf("Deleted element from the rear side = %d\n",d[r]);
if(f==r){
f=r=-1;
} else{
r--;
}
}
void display() {
if(f==-1 && r==-1){
printf("Double ended queue is empty.\n");
} else {
printf("Elements in the double ended queue : ");
for(int i=f;i<=r;i++){
printf("%d ",d[i]);
102
}
printf("\n");
}int main() {
int op, x;
while (1) {
printf("1.Inject 2.Eject 3.Display 4.Exit\n");
printf("Enter your option : ");
scanf("%d", & op);
switch (op) {
case 1:
printf("Enter element : ");
scanf("%d", & x);
inject(x);
break;
case 2:
eject();
break;
case 3:
display();
break;
case 4:
exit(0);
} }}
Expected Output
103
Successfully inserted at rear side.
1.Inject 2.Eject 3.Display 4.Exit
Enter your option : 3
Elements in the double ended queue : 13 14 15
1.Inject 2.Eject 3.Display 4.Exit
Enter your option : 2
Deleted element from the rear side = 15
1.Inject 2.Eject 3.Display 4.Exit
Enter your option : 3
Elements in the double ended queue : 13 14
1.Inject 2.Eject 3.Display 4.Exit
Enter your option : 1
Enter element : 26
Successfully inserted at rear side.
1.Inject 2.Eject 3.Display 4.Exit
Enter your option : 1
Enter element : 67
Successfully inserted at rear side.
1.Inject 2.Eject 3.Display 4.Exit
Enter your option : 3
Elements in the double ended queue : 13 14 26 67
1.Inject 2.Eject 3.Display 4.Exit
Enter your option : 2
Deleted element from the rear side = 67
1.Inject 2.Eject 3.Display 4.Exit
Enter your option : 2
Deleted element from the rear side = 26
1.Inject 2.Eject 3.Display 4.Exit
Enter your option : 2
Deleted element from the rear side = 14
1.Inject 2.Eject 3.Display 4.Exit
Enter your option : 3
Elements in the double ended queue : 13
1.Inject 2.Eject 3.Display 4.Exit
Enter your option : 4
104
Ex.No: 45 .Write a C program that uses functions to perform the following:
a) Create a binary search tree of integers.
b) Traverse the above Binary search tree recursively in Inorder, Preorder and Postorder
BST
{
int data;
struct BST *lchild;
struct BST *rchild;
};
typedef struct BST * NODE;
NODE create()
{
NODE temp;
temp = (NODE) malloc(sizeof(struct BST));
printf("\nEnter The value: ");
scanf("%d", &temp->data);
temp->lchild = NULL;
temp->rchild = NULL;
return temp;
}
void insert(NODE root, NODE newnode);
void inorder(NODE root);
void preorder(NODE root);
void postorder(NODE root);
void search(NODE root);
void insert(NODE root, NODE newnode)
{
/*Note: if newnode->data == root->data it will be skipped. No duplicate nodes are allowed */
105
NODE cur;
if(root == NULL)
{
printf("\nBST is empty.");
return;
}
printf("\nEnter Element to be searched: ");
scanf("%d", &key);
cur = root;
while (cur != NULL)
{
if (cur->data == key)
{
printf("\nKey element is present in BST");
return;
}
if (key < cur->data)
cur = cur->lchild;
else
cur = cur->rchild;
}
printf("\nKey element is not found in the BST");
}
void inorder(NODE root)
{
if(root != NULL)
{
inorder(root->lchild);
printf("%d ", root->data);
inorder(root->rchild);
}
}
void preorder(NODE root)
{
if (root != NULL)
{
printf("%d ", root->data);
preorder(root->lchild);
preorder(root->rchild);
}
}
void postorder(NODE root)
{
if (root != NULL)
{
postorder(root->lchild);
postorder(root->rchild);
printf("%d ", root->data);
}
}
106
void main()
{
int ch, key, val, i, n;
NODE root = NULL, newnode;
while(1)
{
printf("\n~~~~BST MENU~~~~");
printf("\n1.Create a BST");
printf("\n2.Search");
printf("\n3.BST Traversals: ");
printf("\n4.Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: printf("\nEnter the number of elements: ");
scanf("%d", &n);
for(i=1;i<=n;i++)
{
newnode = create();
if (root == NULL)
root = newnode;
else
insert(root, newnode);
}
break;
case 2: if (root == NULL)
printf("\nTree Is Not Created");
else
{
printf("\nThe Preorder display : ");
preorder(root);
printf("\nThe Inorder display : ");
inorder(root);
printf("\nThe Postorder display : ");
postorder(root);
}
break;
case 3: search(root);
break;
case 4: exit(0);
}
}
}
Output:
~~~~BST MENU~~~~
1.Create a BST
2.Search
3.BST Traversals:
4.Exit
107
Enter your choice: 1
Enter the number of elements: 12
Enter The value: 6
Enter The value: 9
Enter The value: 5
Enter The value: 2
Enter The value: 8
Enter The value: 15
Enter The value: 24
Enter The value: 14
Enter The value: 7
Enter The value: 8
Enter The value: 5
Enter The value: 2
~~~~BST MENU~~~~
1.Create a BST
2.Search
3.BST Traversals:
4.Exit
Enter your choice: 3
The Preorder display: 6 5 2 9 8 7 15 14 24
The Inorder display: 2 5 6 7 8 9 14 15 24
The Postorder display: 2 5 7 8 14 24 15 9 6
~~~~BST MENU~~~~
1.Create a BST
2.Search
3.BST Traversals:
4.Exit
Enter your choice: 2
Enter Element to be searched: 66
Key element is not found in the BST
~~~~BST MENU~~~~
1.Create a BST
2.Search
3.BST Traversals:
4.Exit
Enter your choice: 2
Enter Element to be searched: 14
Key element is present in BST
~~~~BST MENU~~~~
1.Create a BST
2.Search
3.BST Traversals:
4.Exit
Enter your choice: 4
108
Ex.No:46 Write a C program to demonstrate Graph traversals implementation - Breadth First
Search
#include <stdio.h>
#include <stdlib.h>
#define MAX 99
struct node {
struct node *next;
int vertex;
};
typedef struct node * GNODE;
GNODE graph[20];
int visited[20];
int queue[MAX], front = -1,rear = -1;
int n;
int isEmptyQueue() {
if(front==-1||front>rear)
return 1;
else
return 0;
}
int deleteQueue() {
int x;
if(front==-1||front>rear){
printf("Queue Underflow.\n");
exit(0);
}else{
x=queue[front];
front++;
return x;
}
}
void BFS(int v) {
int w;
insertQueue(v);
109
while(!isEmptyQueue()){
110
v=deleteQueue();
printf("%d\n",v);
visited[v]=1;
GNODE g=graph[v];
for(;g!=NULL;g=g->next){
w=g->vertex;
if(visited[w]==0){
insertQueue(w);
visited[w]=1;
}
}
}
}
void main() {
int N, E, s, d, i, j, v;
GNODE p, q;
printf("Enter the number of vertices : ");
scanf("%d",&N);
printf("Enter the number of edges : ");
scanf("%d",&E);
for(i=1;i<=E;i++) {
printf("Enter source : ");
scanf("%d",&s);
printf("Enter destination : ");
scanf("%d",&d);
q=(GNODE)malloc(sizeof(struct node));
q->vertex=d;
q->next=NULL;
if(graph[s]==NULL) {
graph[s]=q;
} else {
p=graph[s];
while(p->next!=NULL)
p=p->next;
p->next=q;}
}
for(i=1;i<=n;i++)
visited[i]=0;
printf("Enter Start Vertex for BFS : ");
scanf("%d", &v);
printf("BFS of graph : \n");
BFS(v);
printf("\n");
}
Expected Output
111
Enter the number of edges : 5
Enter source : 1
Enter destination : 2
Enter source : 1
Enter destination : 4
Enter source : 4
Enter destination : 2
Enter source : 2
Enter destination : 3
Enter source : 4
Enter destination : 5
Enter Start Vertex for BFS : 1
BFS of graph :
1
2
4
3
5
112
Ex.No:47 Write a C program to demonstrate Graph traversals implementation - Depth First
Search
#include<stdio.h>
#include<stdlib.h>
struct node {
struct node *next;
int vertex;
};
typedef struct node * GNODE;
GNODE graph[20];
int visited[20];
int n;
void DFS(int i) {
GNODE p=graph[i];
printf("%d\n",i);
visited[i]=1;
while(p!=NULL){
i=p->vertex;
if(visited[i]!=1)
DFS(i);
p=p->next;
}
}
void main() {
int N,E,i,s,v,d;
GNODE q,p;
printf("Enter the number of vertices : ");
scanf("%d",&N);
printf("Enter the number of edges : ");
scanf("%d",&E);
for(i=1;i<=E;i++) {
printf("Enter source : ");
scanf("%d",&s);
printf("Enter destination : ");
scanf("%d",&d);
q=(GNODE)malloc(sizeof(struct node));
q->vertex=d;
q->next=NULL;
if(graph[s]==NULL)
graph[s]=q;
else {
p=graph[s];
while(p->next!=NULL)
p=p->next;
p->next=q;
}
}
113
for(i=0;i<n;i++)
visited[i]=0;
printf("Enter Start Vertex for DFS : ");
scanf("%d",&v);
printf("DFS of graph : \n");
DFS(v);
}
Expected Output
114
Ex.No :48 Write a C program to demonstrate Minimum spanning tree using Prim’s and
Kruskal’s algorithm
Prims
#include<stdio.h>
#include<conio.h>
int a,b,u,v,n,i,j,ne=1,e,s,d,w;
int visited[10]={0},min,mincost=0,cost[10][10];
void prims() {
visited[1]=1;
while(ne<n){
min=999;
for(i=1;i<=n;i++)
if(visited[i]==1)
for(j=1;j<=n;j++)
if(visited[j]==0 && cost[i][j]<min){
min=cost[i][j];
a=u=i;
b=v=j;
}
printf("Edge cost from %d to %d : %d\n",a,b,cost[a][b]);
ne++;
mincost+=cost[a][b];
visited[b]=1;
cost[a][b]=cost[b][a]=999;
}
printf("Minimum cost of spanning tree = %d\n",mincost);
}
void main() {
printf("Enter the number of vertices : ");
scanf("%d",&n);
printf("Enter the number of edges : ");
scanf("%d",&e);
for(i=1;i<=e;i++) {
printf("Enter source : ");
scanf("%d",&s);
printf("Enter destination : ");
scanf("%d",&d);
printf("Enter weight : ");
scanf("%d",&w);
if(s<=0 || d<=0 || s> n || d > n || w < 0 ) {
printf("Invalid data.Try again.\n");
i--;
continue;
}
cost[d][s]=w;
cost[s][d]=w;
}
for(i=1;i<=n;i++) {
115
for(j=1;j<=n;j++) {
if(cost[i][j]==0)
cost[i][j]=999;
}
}
printf("The edges of Minimum Cost Spanning Tree are : \n");
prims();
}
Expected Output
116
Enter destination : 6
Enter weight : 8
Enter source : 5
Enter destination : 7
Enter weight : 9
Enter source : 6
Enter destination : 7
Enter weight : 11
The edges of Minimum Cost Spanning Tree are :
Edge cost from 1 to 4 : 5
Edge cost from 4 to 6 : 6
Edge cost from 1 to 2 : 7
Edge cost from 2 to 5 : 7
Edge cost from 5 to 3 : 5
Edge cost from 5 to 7 : 9
Minimum cost of spanning tree = 39
Kruskals
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int i,j,k,a,b,u,v,n,e,s,d,w,ne=1;
int min,mincost=0,cost[9][9],parent[9];
void kruskal() {
while(ne<n){
min=999;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]<min){
min=cost[i][j];
a=u=i;
b=v=j;
}
u=find(u);
v=find(v);
if(uni(u,v)){
printf("Edge cost from %d to %d : %d\n",a,b,min);
mincost+=min;
ne++;
}
cost[a][b]=cost[b][a]=999;
117
}
printf("Minimum cost of spanning tree = %d\n",mincost);
}
int find(int i) {
while(parent[i])
i=parent[i];
return i;
}
int uni(int i,int j) {
if(i!=j) {
parent[j]=i;
return 1;
}
return 0;
}
void main() {
printf("Enter the number of vertices : ");
scanf("%d",&n);
printf("Enter the number of edges : ");
scanf("%d",&e);
for(i=1;i<=e;i++) {
printf("Enter source : ");
scanf("%d",&s);
printf("Enter destination : ");
scanf("%d",&d);
printf("Enter weight : ");
scanf("%d",&w);
if(s<=0 || d<=0 || s> n || d > n || w < 0 ) {
printf("Invalid data.Try again.\n");
i--;
continue;
}
cost[s][d]=w;
}
for(i=1;i<=n;i++) {
for(j=1;j<=n;j++) {
if(cost[i][j]==0)
cost[i][j]=999;
}
}
printf("The edges of Minimum Cost Spanning Tree are : \n");
kruskal();
}
Expected Output
118
Enter source : 2
Enter destination : 7
Enter weight : 14
Enter source : 1
Enter destination : 2
Enter weight : 28
Enter source : 1
Enter destination : 6
Enter weight : 10
Enter source : 5
Enter destination : 6
Enter weight : 25
Enter source : 4
Enter destination : 5
Enter weight : 22
Enter source : 4
Enter destination : 7
Enter weight : 18
Enter source : 2
Enter destination : 3
Enter weight : 16
Enter source : 3
Enter destination : 4
Enter weight : 12
Enter source : 5
Enter destination : 7
Enter weight : 24
The edges of Minimum Cost Spanning Tree are :
Edge cost from 1 to 6 : 10
Edge cost from 3 to 4 : 12
Edge cost from 2 to 7 : 14
Edge cost from 2 to 3 : 16
Edge cost from 4 to 5 : 22
Edge cost from 5 to 6 : 25
Minimum cost of spanning tree = 99
119
Ex.No:49 Write a C Program to Perform Linear search and Binary Search
Linear search
#include<stdio.h>
void main() {
int a[20], i, n, key, flag = 0, pos;
printf("Enter value of n : ");
scanf("%d", &n);
for (i=0;i<n;i++) { //Complete the code in for
printf("Enter element for a[%d] : ", i);
scanf("%d", &a[i]);
}
printf("Enter key element : ");
scanf("%d", &key);
for (i=0;i<n;i++) { //Complete the code in for
if (a[i]==key) { //Write the condition part
flag =1; // Complete the statement
pos = i; // Complete the statement
break;
}
}
if (flag==1) { //Write the condition part
printf("The key element %d is found at the position %d\n", key, pos);
} else {
printf("The key element %d is not found in the array\n", key);
}
}
Expected Output
Enter value of n : 4
Enter element for a[0] : 2
Enter element for a[1] : 8
Enter element for a[2] : 5
Enter element for a[3] : 1
Enter key element : 1
The key element 1 is found at the position 3
120
Binary Search
#include<stdio.h>
void main() {
int a[20], i, j, n, key, flag = 0, low, high, mid, temp;
printf("Enter value of n : ");
scanf("%d", &n);
for (i=0;i<n;i++) { // Complete the code in for
printf("Enter element for a[%d] : ", i);
scanf("%d",&a[i] ); // Complete the statement
}
printf("Enter key element : ");
scanf("%d", &key);
// Bubble sort process
for (int i=0;i<n-1;i++ ) { // Complete the code in for
for (int j=0;j<n-i-1;j++ ) { // Complete the code in for
if (a[j]>a[j+1]) { // Write the condition part
temp =a[j] ; // Complete the statement
a[j] = a[j+1]; // Complete the statement
a[j+1] = temp; // Complete the statement
}
}
}
printf("After sorting the elements in the array are\n");
for (int i=0;i<n;i++ ) { // Complete the code in for
printf("Value of a[%d] = %d\n", i, a[i]);
}
low = 0; // Complete the statement
high =n-1 ; // Complete the statement
while ( low<=high) { // Complete the condition part in while
mid = (low+high)/2; // Complete the statement
if ( a[mid]==key) { // Write the condition part
flag = 1; // Complete the statement
break;
} else if ( a[mid]<key ){ // Write the condition part
low = mid+1; // Complete the statement
} else if ( a[mid]>key) { // Write the condition part
high = mid-1; // Complete the statement
}
}
if (flag==1) { // Write the condition part
121
printf("The key element %d is found at the position %d\n", key,
mid);
} else {
printf("The Key element %d is not found in the array\n", key);
}
}
Expected Output
Enter value of n : 3
Enter element for a[0] : 10
Enter element for a[1] : 15
Enter element for a[2] : 12
Enter key element : 13
After sorting the elements in the array are
Value of a[0] = 10
Value of a[1] = 12
Value of a[2] = 15
The Key element 13 is not found in the array
122
Ex.No: 50 Write a C program to implement the following
a)Bubble sort b) Insertion sort c) Selection sort
a) Bubble sort
#include<stdio.h>
void main() {
int a[20], i, n, j, temp;
printf("Enter value of n : ");
scanf("%d", &n);
for (i = 0; i < n; i++ ) { // Complete the code in for
printf("Enter element for a[%d] : ", i);
scanf("%d", &a[i]); // Complete the statement
}
printf("Before sorting the elements in the array are\n");
for (i = 0; i < n; i++ ) { // Complete the code in for
printf("Value of a[%d] = %d\n", i, a[i] ); // Complete the statement
}
for (i=0;i<n-1;i++ ) { // Complete the code in for
for (j=0;j<n-i-1;j++ ) { // Complete the code in for
if (a[j]>a[j+1] ) {
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("After sorting the elements in the array are\n"); for (i = 0;i < n; i++ ) {
printf("Value of a[%d] = %d\n",i,a[i] ); }}// Complete the statement
Expected Output
Enter value of n : 3
Enter element for a[0] : -10
Enter element for a[1] : 30
Enter element for a[2] : -30
Before sorting the elements in the array are
Value of a[0] = -10
Value of a[1] = 30
123
Value of a[2] = -30
After sorting the elements in the array are
Value of a[0] = -30
Value of a[1] = -10
Value of a[2] = 30
b) Insertion sort
#include<stdio.h>
void main() {
int a[20], i, n, j, temp;
printf("Enter value of n : ");
scanf("%d", &n);
for (i = 0; i < n; i++ ) { // Complete the code in for
printf("Enter element for a[%d] : ", i);
scanf("%d", &a[i] ); // Complete the statement
}
printf("Before sorting the elements in the array are\n");
for (i = 0; i < n; i++ ) { // Complete the code in for
printf("Value of a[%d] = %d\n",i,a[i] ); // Complete the statement
}
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (a[j] > a[j+1]) {
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
printf("After sorting the elements in the array are\n");
for ( i = 0; i < n; i++) { // Complete the code in for
printf("Value of a[%d] = %d\n", i,a[i]); // Complete the statement
}
}
Expected Output
Enter value of n : 4
124
Enter element for a[0] : 5
Enter element for a[1] : 2
Enter element for a[2] : 4
Enter element for a[3] : 1
Before sorting the elements in the array are
Value of a[0] = 5
Value of a[1] = 2
Value of a[2] = 4
Value of a[3] = 1
After sorting the elements in the array are
Value of a[0] = 1
Value of a[1] = 2
Value of a[2] = 4
Value of a[3] = 5
c) Selection sort
#include<stdio.h>
void main() {
int a[20], i, n, j, small, index;
printf("Enter value of n : ");
scanf("%d", &n);
for ( i = 0; i < n; i++) { // Complete the code in for
printf("Enter element for a[%d] : ", i);
scanf("%d", &a[i]);
}
printf("Before sorting the elements in the array are\n");
for ( i = 0; i < n; i++) { // Complete the code in for
printf("Value of a[%d] = %d\n", i, a[i]);
}
for (i = 0; i < n-1; i++ ) { // Complete the code in for
small =a[i] ; // Complete the statement
index = i; // Complete the statement
for (j = i+1; j < n; j++ ) { // Complete the code in for
if (a[j] < small ) { // Complete the condition part
small = a[j]; // Complete the statement
125
index =j ; // Complete the statement
}
}
a[index] = a[i]; // Complete the statement
a[i] = small; // Complete the statement
}
printf("After sorting the elements in the array are\n");
for (i = 0; i < n; i++ ) { // Complete the code in for
printf("Value of a[%d] = %d\n", i, a[i]);
}
}
Expected Output
Enter value of n : 5
Enter element for a[0] : 4
Enter element for a[1] : 8
Enter element for a[2] : 1
Enter element for a[3] : 5
Enter element for a[4] : 2
Before sorting the elements in the array are
Value of a[0] = 4
Value of a[1] = 8
Value of a[2] = 1
Value of a[3] = 5
Value of a[4] = 2
After sorting the elements in the array are
Value of a[0] = 1
Value of a[1] = 2
Value of a[2] = 4
Value of a[3] = 5
Value of a[4] = 8
126