0% found this document useful (0 votes)
62 views54 pages

4 PDS - LAB - Record

The document contains 6 programming exercises in C language with their aim, algorithm, program code and output for each exercise. The exercises include programs to: 1) Convert Celsius to Fahrenheit and vice versa 2) Find the largest of three numbers 3) Print the day of the week using switch case 4) Generate numbers divisible by 2 but not 3 and 5 5) Find the sum of n natural numbers using while loop 6) Check if a string is a palindrome

Uploaded by

Macos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views54 pages

4 PDS - LAB - Record

The document contains 6 programming exercises in C language with their aim, algorithm, program code and output for each exercise. The exercises include programs to: 1) Convert Celsius to Fahrenheit and vice versa 2) Find the largest of three numbers 3) Print the day of the week using switch case 4) Generate numbers divisible by 2 but not 3 and 5 5) Find the sum of n natural numbers using while loop 6) Check if a string is a palindrome

Uploaded by

Macos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 54

1

PROGRAMS USING BASIC C CONSTRUCT

EX.NO: 1(a)

DATE:

C PROGRAM TO CONVERT THE CELSIUS INTO FAHRENHEIT AND


FAHRENHEIT TO CELSIUS

AIM:

To write a C program to convert the Celsius into Fahrenheit and Fahrenheit to Celsius

ALGORITHM:

Step 1: Start
Step 2: Read temperature in celsius.
Step 3: Calculate Fahrenheit = (temp_c*1.8)+32
Step 4: Print the Fahrenheit.
Step 5: Read temperature in Fahrenheit
Step 6: Calculate Fahrenheit = (temp_f-32) / 1.8
Step 7: Print the Fahrenheit value.
Step 8: Stop

PROGRAM:

#include<stdio.h>
int main ()
{
float temp_c, temp_f;

printf ("Enter the value of Celsius: ");


scanf ("%f", &temp_c);
temp_f = (temp_c*1.8) + 32;
printf ("\n The value of Fahrenheit is: %f", temp_f);

printf("\n Enter the value of Fahrenheit:");


scanf("%f",&temp_f);
temp_c=(temp_f-32)/1.8;
printf ("\n The value of Celsius is: %f", temp_c);
return 0;
}

OUTPUT:

Enter the value of Celsius: 56


The value of Fahrenheit is: 132.800003

Enter the value of Fahrenheit: 132


The value of Celsius is: 55.555557

RESULT:

Thus C program to convert the Celsius into Fahrenheit and Fahrenheit into Celsius is
executed and the output is verified.

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


2

EX. NO: 1 (b)

DATE:

C PROGRAM TO FIND THE LARGEST OF THREE NUMBERS

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

ALGORITHM:

Step 1: Start
Step 2: Read a , b ,c.
Step 3: Check if(a>b) and if (a>c) then max=a.
Step 4: Check if(b>c) and if (b>a) then max=b.
Step 5: else max=c.
Step 5 : Then print maximum value .
Step 6 : Stop.

PROGRAM:
#include <stdio.h>
int main()
{
int a,b,c,max;
printf("enter three values");
scanf("%d %d %d",&a,&b,&c);
if (a>b)
{
if(a>c)
max=a;
else
max=c;
}
else
{
if(b>c)
max=b;
else
max=c;
}
printf("Maximum number is %d " , max);
return 0;
}
OUTPUT:
Enter three values 56 76 11
Maximum number is 76

RESULT:
Thus the C program to find the largest of three numbers is executed and the output is
verified.

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


3

EX. NO: 1 (c)

DATE:

C PROGRAM TO PRINT DAY OF WEEK USING SWITCH CASE

AIM:

To write a C program to print the day of a week using switch case.

ALGORITHM:

Step 1: Start.
Step 2: Read the choice.
Step 3: Using switch case print the day using the entered choice starting form 0 = Sunday till
6 = Saturday.
Step 4: else print invalid day.
Step 5: Stop.

PROGRAM:
#include <stdio.h>
#include <conio.h>
int main()
{
int ch ;
printf("Enter the Day number (Starting from 0 = Sunday) : ") ;
scanf("%d", &ch) ;
switch(ch)
{
case 0:
printf("\nSunday") ;
break ;
case 1:
printf("\nMonday") ;
break ;
case 2:
printf("\nTuesday") ;
break ;
case 3:
printf("\nWednesday") ;
break ;
case 4:
printf("\nThursday") ;
break ;
case 5:
printf("\nFriday") ;

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


4

break ;
case 6:
printf("\nSaturday") ;
break ;
default:
printf("INVALID DAY") ;
}
getch();
return 0;
}

OUTPUT:
Enter the Day number (Starting from 0 = Sunday): 1
Monday

RESULT:

Thus the C program to print the day of a week using switch case is executed and the
output is verified.

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


5

EX. NO: 1(d)


DATE:

C PROGRAM TO GENERATE NUMBERS DIVISIBLE BY 2 BUT NOT BY 3 AND 5

AIM:

To write a C program to generate numbers divisible by 2 but not by 3 and 5.

ALGORITHM:

Step 1: Start.
Step 2: Read N value.
Step 3: Check if((i%2==0)&&(i%3!=0)&&(i%5!=0)) then print i
Step 4: Stop.

PROGRAM:

#include<stdio.h>
int main()
{
int i,n;
printf("Enter n value : ");
scanf("%d",&n);
printf("\nThe numbers divisible by 2 not by 3 and 5 till %d are :\n",n);
for(i=1;i<=n;i++)
{
if((i%2==0)&&(i%3!=0)&&(i%5!=0))
{
printf(" %d",i);
}
}
return 0;
}
OUTPUT:
Enter n value: 100

The numbers divisible by 2 not by 3 and 5 till 100 are :

2 4 8 14 16 22 26 28 32 34 38 44 46 52 56 58 62 64 68 74 76 82 86 88 92 94 98

RESULT:

Thus the C program to print numbers divisible by 2 but not by 3 and 5 is executed and
the output is obtained.

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


6

EX. NO: 1(e)

DATE:

C PROGRAM TO PRINT SUM OF N NATURAL NUMBERS USING

WHILE LOOP

AIM:

To write a C program to find the sum of n natural numbers using while loop.

ALGORITHM:

Step 1: Start
Step 2: Read n .
Step 3: Initialize sum as 0 and the value of i as 1 .
Step 4: Iterate a while with a condition (i<=n) and update the value of sum = sum + i and
i++
Step 5: Print the value of sum.
Step 6: Stop.

PROGRAM:
#include<stdio.h>
#include<conio.h>
int main()
{
int n = 0 , sum = 0 , i = 1 ;
printf("Enter n value : ") ;
scanf("%d",&n) ;
while (i<=n)
{
sum += i ;
i++;
}
printf("\n1+......+%d = ",n) ;
printf("%d",sum);
getch();
}

OUTPUT:
Enter n value: 10
1+......+10 = 55

RESULT:

Thus the C program to find the sum of n natural numbers is executed and the output
is verified.

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


7

EX.NO: 2 (a)
DATE:
C PROGRAM TO CHECK WHETHER THE GIVEN STRING IS
PALINDROME OR NOT

AIM:

To write a C program to check whether the given string is palindrome or not.

ALGORITHM:

Step 1: Start
Step 2: Read the string from the user.
Step 3: Take a copy of the string.
Step 4: Reverse the copied string.
Step 5: If they have zero difference then print “Palindrome” else print “Not a palindrome”.
Step 6: Stop

PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char str1[30], str2[30];
printf("Enter a string: ");
scanf("%s",str1);
strcpy(str2, str1);
strrev(str2);
if(!strcmp(str1, str2))
printf("\n It's a Pallindrome!\n");
else
printf("\nIt's not a Pallindrome.\n");
return 0;
}

OUTPUT:
Enter a string: level
It's a Pallindrome!

RESULT:

Thus the C program to check the given string is palindrome or not is executed and the
output is verified.

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


8

EX.NO: 2(b)

DATE:

C PROGRAM USING POINTERS

TO SWAP TWO NUMBERS USING CALL BY VALUE AND CALL BY REFERENCE

AIM:

To write a C program to swap two numbers using call by value and call by reference.

ALGORITHM:

Step 1: Start.
Step 2: Read two values a and b
Step 3: swap using t=a; a=b; b=t by value
Step 4: swap using t=*a; *a=*b; *b=t; by address
Step 5: After swapping print values of a and b
Step 6: Stop.

PROGRAM:
#include <stdio.h>
int swap(int a,int b)
{
int t;
t=a;
a=b;
b=t;
printf("\n After Swap - Inside function A=%d and B=%d",a,b);
}

int swap1(int *a,int *b)


{
int t;
t=*a;
*a=*b;
*b=t;
printf("\n After Swap - Inside function A=%d and B=%d",*a,*b);
}

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


9

int main()
{
int x,y;
printf("Enter two value");
scanf("%d %d",&x,&y);

printf("\n Call by Value \n ***************** ");


printf("\n Before Swap - Main function A=%d and B=%d",x,y);
swap(x,y);
printf("\n After Swap - Main function A=%d and B=%d",x,y);

printf("\n Call by Reference \n ***************** ");


printf("\n Before Swap - Main function A=%d and B=%d",x,y);
swap1(&x,&y);
printf("\n After Swap - Main function A=%d and B=%d",x,y);

return 0;
}

OUTPUT:

Enter two value 5 6


56

Call by Value
*****************
Before Swap - Main function A=5 and B=6
After Swap - Inside function A=6 and B=5
After Swap - Main function A=5 and B=6
Call by Reference
*****************
Before Swap - Main function A=5 and B=6
After Swap - Inside function A=6 and B=5
After Swap - Main function A=6 and B=5

RESULT:

Thus C program to swap two numbers using call by value and call by reference
is executed and the output is verified.

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


10

EX.NO: 2(c)

DATE:

PROGRAM USING FUNCTIONS


C PROGRAM TO CALCULATE FACTORIAL OF A GIVEN NUMBER USING
RECURSION

AIM:
To write a C program to calculate the factorial of a given number using recursion.

ALGORITHM:

Step 1: Start
Step 2: Read n.
Step 3:if n=0 or n=1 print factorial as 1
else call the recursive function by passing the user input as the argument.
Step 4: Print the factorial of the number.
Step 5: Stop.

PROGRAM:
#include<stdio.h>
int fact(int n)
{
if (n==1)
return 1;
else
return n * fact (n-1);
}
int main()
{
int n;
printf("Enter Number: ");
scanf("%d",&n);
printf("%d ! = %d", n, fact(n));
return 0;
}

OUTPUT:

Enter Number: 5
5 ! = 120

RESULT:

Thus the C program to calculate the factorial of a given number using


recursion is executed and the output is verified.

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


11

EX.NO: 2(d)

DATE:

PROGRAM USING STRUCTURE

C PROGRAM TO CALCULATE TOTAL AND AVERAGE USING STRUCTURE

AIM:

To write a C program to calculate the total and average using structure.

ALGORITHM:

Step 1: Start
Step 2: create structure for storing the student details.
Step 3: Read two subject marks
Step 4: Calculate total and average and print the same.
Step 5: Stop

PROGRAM:

#include<stdio.h>
struct stu
{
int rollno, s1, s2, tot ;
char name[10] ;
float avg ;
} s[10] ;
int main()
{
int i, n ;

printf("Enter the number of students : ") ;


scanf("%d", &n) ;
for(i = 0 ; i < n ; i++)
{
printf("\nEnter the roll number : ") ;
scanf("%d", &s[i].rollno) ;
printf("\nEnter the name : ") ;
scanf("%s", s[i].name) ;
printf("\nEnter the marks in 2 subjects : ") ;
scanf("%d %d", &s[i].s1, &s[i].s2) ;
s[i].tot = s[i].s1 + s[i].s2 ;
s[i].avg = s[i].tot / 2.0 ;
}

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


12

printf("\nRoll No. Name \t\tSub1\t Sub2\t Total\t Average\n\n") ;


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

printf("%d \t %s \t\t %d \t %d \t %d \t %.2f \


n",s[i].rollno,s[i].name,s[i].s1,s[i].s2,s[i].tot,s[i].avg);

return 0;
}

OUTPUT:

Enter the number of students : 2


Enter the roll number : 121
Enter the name : Rakki
Enter the marks in 2 subjects : 98 99

Enter the roll number : 122


Enter the name : Arun
Enter the marks in 2 subjects : 97 96

Roll No. Name Sub1 Sub2 Total Average


121 Rakki 98 99 197 98.50
122 Arun 97 96 193 96.50

RESULT:

Thus the C program to calculate the total and average using structure is
executed and the output is verified.

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


13

EX.NO: 3

DATE:

APPLICATIONS USING FILE PROCESSING USING C

C PROGRAM TO PRINT STUDENT DETAILS USING SEQUENTIAL ACCESS FILE

AIM:
To write a c program to print student details using sequential access file.

ALGORITHM:

Step 1: Start
Step 2: Open a file *fp
Step 3: Read rollno, name and marks
Step 4: calculate total and average of marks.
Step 4: write all the contents into file named student.txt
Step 6: Print the student details from student.txt file
Step 7: Stop

PROGRAM:

#include<stdio.h>
struct stu
{
int rollno, s1, s2, tot ;
char name[10] ;
float avg ;
}s[10];
int main()
{
FILE *f;
int i,n;
clrscr();
f=fopen("student.txt","w");
if(f==NULL)
{
printf("\n Error opening student.txt\n\n");
exit(1);
}
printf("Enter the number of students : ") ;
scanf("%d", &n) ;
for(i = 0 ; i < n ; i++)
{
printf("\nEnter the roll number : ") ;
scanf("%d", &s[i].rollno) ;
printf("\nEnter the name : ") ;
scanf("%s", s[i].name) ;
21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE
14

printf("\nEnter the marks in 2 subjects : ") ;


scanf("%d %d", &s[i].s1, &s[i].s2) ;
s[i].tot = s[i].s1 + s[i].s2 ;
s[i].avg = s[i].tot / 2.0 ;
fwrite(&s,sizeof(struct stu),1,f);
}
fclose(f);
f=fopen("student.txt","r");
if(f==NULL)
{
printf("\n Error opening student.txt\n\n");
exit(1);
}
else
{
printf("\n Roll No Name \t Sub1 \t Sub2 \t Total \t Average \n\n") ;
for(i = 0 ; i < n ; i++)
{
fread(&s,sizeof(struct stu),1,f);
printf("%d \t %s \t %d \t %d \t %d \t %.2f \
n",s[i].rollno,s[i].name,s[i].s1,s[i].s2,s[i].tot,s[i].avg);
}
}
fclose(f);
return 0;
}
OUTPUT
Enter the number of students: 2
Enter the roll number: 121
Enter the name: Jack
Enter the marks in 2 subjects: 98 99

Enter the roll number: 122


Enter the name: Arun
Enter the marks in 2 subjects: 97 96

Roll No. Name Sub1 Sub2 Total Average


121 Jack 98 99 197 98.50
122 Arun 97 96 193 96.50

RESULT:

Thus the C program to find the total and average for two subject marks using
sequential access file is executed and verified.

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


15

EX.NO: 4(a)

DATE:

C PROGRAM USING ARRAY IMPLEMENTATION OF STACK ADT

AIM:

To write a c program to implement Stack ADT using Array.

ALGORITHM:

Step 1: Include all the header files which are used in the program and define a constant
'SIZE' with specific value.

Step 2: Declare all the functions used in stack implementation.

Step 3: Create a one dimensional array with fixed size (int stack[SIZE])

Step 4: Define an integer variable 'top' and initialize with '-1'. (int top = -1)

Step 5: In main method, display menu with list of operations and make suitable function calls
to perform operation selected by the user on the stack.

PROGRAM

#include<stdio.h>
#define SIZE 10
int stack[SIZE], top = -1;

int push(int value)


{
if(top == SIZE-1)
printf("\nStack is Full!!! Insertion is not possible!!!");
else
{
top++;
stack[top] = value;
printf("\nInsertion success!!!");
}
}
int pop()
{
if(top == -1)
printf("\nStack is Empty!!! Deletion is not possible!!!");
else
{
printf("\nDeleted : %d", stack[top]);
top--;
}

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


16

int display()
{
if(top == -1)
printf("\nStack is Empty!!!");
else
{
int i;
printf("\nStack elements are:\n");
for(i=top; i>=0; i--)
printf("%d\n",stack[i]);
}
}
int main()
{
int value, choice;
while(1)
{
printf("\n\n***** MENU *****\n");
printf("1. Push\n2. Pop\n3. Display\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the value to be insert: ");
scanf("%d",&value);
push(value);
break;
case 2: pop();
break;
case 3: display();
break;
case 4:
exit(0);
default: printf("\nWrong selection!!! Try again!!!");
}
}
}

OUTPUT

***** MENU *****


1. Push
2. Pop
3. Display

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


17

4. Exit

Enter your choice: 1


Enter the value to be insert: 56
Insertion success!!!

Enter your choice: 1


Enter the value to be insert: 78
Insertion success!!!

Enter your choice: 3


Stack elements are:
78
56

Enter your choice: 2


Deleted : 78

Enter your choice: 3


Stack elements are:
56

RESULT:

Thus the C program for array implementation of Stack ADT is executed and the
output is obtained.

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


18

EX.NO: 4 (b)

DATE:

C PROGRAMS FOR ARRAY IMPLEMENTATION OF QUEUE ADT

AIM:
To create a C program to implement queue using array.

ALGORITHM:

Step 1 - Include all the header files which are used in the program and define a constant 'n'
with specific value.

Step 2 - Declare all the user defined functions which are used in queue implementation.

Step 3 - Create a one dimensional array with above defined n (int queue[n])

Step 4 - Define two integer variables 'front' and 'rear' and initialize both with '0’.

Step 5 - Then implement main method by displaying menu of operations list and make
suitable function calls to perform operation selected by the user on queue.

PROGRAM

#include<stdio.h>
#define n 5
void main()
{
int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
printf("Queue using Array");
printf("\n1.Insertion \n2.Deletion \n3.Display \n4.Exit");
while(ch)
{
printf("\nEnter the Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(rear==x)
printf("\n Queue is Full");
else
{
printf("\n Enter no %d:",j++);
scanf("%d",&queue[rear++]);
}
break;
case 2:
if(front==rear)
printf("\n Queue is empty");
else
{

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


19

printf("\n Deleted Element is %d",queue[front++]);


x++;
}
break;
case 3:
printf("\n Queue Elements are:\n ");
if(front==rear)
printf("\n Queue is Empty");
else
{
for(i=front; i<rear; i++)
{
printf("%d",queue[i]);
printf("\t");
}
break;
case 4:
exit(0);
default:
printf("Wrong Choice: please see the options");
}
}
}
OUTPUT

Queue using Array


1.Insertion
2.Deletion
3.Display
4.Exit
Enter the Choice:1
Enter no 1:25
Enter the Choice:1
Enter no 2:36
Enter the Choice:1
Enter no 3:98
Enter the Choice:3
Queue Elements are:
25 36 98
Enter the Choice:2
Deleted Element is 25
Enter the Choice:3
Queue Elements are:
36 98

RESULT:
Thus the C program for implementation of queue using array is successfully executed and the
output is verified.

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


20

EX.NO: 5(a)

DATE:

C PROGRAM FOR LINKED LIST IMPLEMENTATION OF STACK ADT

AIM:

To write a c program to print linked list implementation of Stack ADT.

ALGORITHM:

Step 1 - Include all the header files which are used in the program. And declare all the user
defined functions.

Step 2 - Define a 'Node' structure with two members data and next.

Step 3 - Define a Node pointer 'top' and set it to NULL.

Step 4 - Implement the main method by displaying Menu with list of operations and make
suitable function calls in the main method.

PROGRAM

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

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

void main()
{
int choice, value;
printf("\n Stack using Linked List \n");
printf("\n****** MENU ******\n");
printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
while(1)
{
printf("\n Enter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the value to be insert: ");

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


21

scanf("%d", &value);
push(value);
break;
case 2: pop(); break;
case 3: display(); break;
case 4: exit(0);
default: printf("\nWrong selection!!! Please try again!!!\n");
}
}
}

void push(int value)


{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
if(top == NULL)
newNode->next = NULL;
else
newNode->next = top;
top = newNode;
printf("\nInsertion is Success!!!\n");
}

void pop()
{
if(top == NULL)
printf("\nStack is Empty!!!\n");
else
{
struct Node *temp = top;
printf("\nDeleted element: %d", temp->data);
top = temp->next;
free(temp);
}
}

void display()
{
if(top == NULL)
printf("\nStack is Empty!!!\n");
else
{
struct Node *temp = top;
while(temp->next != NULL)
{
printf("%d--->",temp->data);
temp = temp -> next;
}

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


22

printf("%d--->NULL",temp->data);
}
}

OUTPUT

Stack using Linked List

****** MENU ******


1. Push
2. Pop
3. Display
4. Exit

Enter your choice: 1


Enter the value to be insert: 25
Insertion is Success!!!

Enter your choice: 1


Enter the value to be insert: 36
Insertion is Success!!!

Enter your choice: 1


Enter the value to be insert: 89
Insertion is Success!!!

Enter your choice: 3


89--->36--->25--->NULL
Enter your choice: 2
Deleted element: 89
Enter your choice: 3
36--->25--->NULL
Enter your choice:

RESULT:

Thus the C program to print linked list implementation of Stack ADT is executed and
the output is verified.

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


23

EX.NO: 5 (b)

DATE:

C PROGRAMS FOR LINKED LIST IMPLEMENTATION OF QUEUE ADT

AIM:

To write a c program to print linked list implementation of Queue ADT.

ALGORITHM:

Step 1 - Include all the header files which are used in the program. And declare all the user
defined functions.

Step 2 - Define a 'Node' structure with two members data and next.

Step 3 - Define two Node pointers 'front' and 'rear' and set both to NULL.

Step 4 - Implement the main method by displaying Menu of list of operations and make
suitable function calls in the main method to perform user selected operation.

PROGRAM
#include<stdio.h>
struct Node
{
int data;
struct Node *next;
}*front = NULL,*rear = NULL;

void insert(int);
void delete();
void display();

void main()
{
int choice, value;
printf("\n Queue Implementation using Linked List \n");
printf("\n****** MENU ******\n");
printf("1. Insert\n2. Delete\n3. Display\n4. Exit\n");
while(1)
{
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the value to be insert: ");
scanf("%d", &value);

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


24

insert(value);
break;
case 2: delete(); break;
case 3: display(); break;
case 4: exit(0);
default: printf("\nWrong selection!!! Please try again!!!\n");
}
}
}
void insert(int value)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode -> next = NULL;
if(front == NULL)
front = rear = newNode;
else
{
rear -> next = newNode;
rear = newNode;
}
printf("\nInsertion is Success!!!\n");
}
void delete()
{
if(front == NULL)
printf("\nQueue is Empty!!!\n");
else
{
struct Node *temp = front;
front = front -> next;
printf("\nDeleted element: %d\n", temp->data);
free(temp);
}
}
void display()
{
if(front == NULL)
printf("\nQueue is Empty!!!\n");
else
{
struct Node *temp = front;
while(temp->next != NULL)
{
printf("%d--->",temp->data);
temp = temp -> next;
}
printf("%d--->NULL\n",temp->data);

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


25

}
}

OUTPUT

Queue Implementation using Linked List

****** MENU ******


1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter the value to be insert: 25
Insertion is Success!!!
Enter your choice: 1
Enter the value to be insert: 98
Insertion is Success!!!
Enter your choice: 1
Enter the value to be insert: 78
Insertion is Success!!!
Enter your choice: 3
25--->98--->78--->NULL
Enter your choice: 2
Deleted element: 25
Enter your choice: 3
98--->78--->NULL
Enter your choice: 4

RESULT:

Thus, the C program to print array implementation of Queue ADT is executed and the
output is verified.

EX.NO: 6

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


26

DATE:

IMPLEMENTATION OF BINARY TREES, PERFORMING VARIOUS OPERATIONS

AIM:

To write a c program to implement binary tree and its operations.

ALGORITHM:

1. Start

2. Declare the struct node.

3. Declare functions like insertion, deletion and display.

4. Write Insertion function is use to insert the value to the tree.

5. Write Delete function is use to delete the value from the tree.

6. Write Display function is use to print the tree.

7. Stop

PROGRAM

#include<stdio.h>
#include<stdlib.h>
struct node
{
int value;
struct node *left_child, *right_child;
};

struct node *new_node(int value)


{
struct node *tmp = (struct node *)malloc(sizeof(struct node));
tmp->value = value;
tmp->left_child = tmp->right_child = NULL;
return tmp;
}
void delete_tree(struct node *root_node)
{
if (root_node)
{
delete_tree(root_node->left_child);
delete_tree(root_node->right_child);
free(root_node);
}
}

void print(struct node *root_node)


{
21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE
27

if (root_node != NULL)
{
print(root_node->left_child);
printf("%d -->", root_node->value);
print(root_node->right_child);
}
}

struct node* insertNode(struct node* node, int value) // inserting nodes!


{
if (node == NULL)
return new_node(value);
if (value < node->value)
node->left_child = insertNode(node->left_child, value);
else if (value > node->value)
node->right_child = insertNode(node->right_child, value);
return node;
}

int main()
{
struct node *root = NULL;
int data;
char ch;

printf("\n 1.Create Tree \n 2.Display \n 3.Delete \n 4.Exit");


do
{
int choice;
printf("\n Enter ur choice");
scanf("%d",&choice);
switch (choice)
{
case 1 :
printf("\nEnter the value to be inserted\n");
scanf("%d",&data);
root = insertNode(root,data);
break;
case 2 :
printf("\n Construct Binary Tree::\n");
print(root);
break;
case 3 :
delete_tree(root);
printf("\n All data are removed");
break;
default :
printf("Wrong Entry\n");
break;

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


28

}
printf("\nDo you want to continue (Type y or n)\n");
scanf(" %c",&ch);
} while (ch == 'Y'|| ch == 'y');
return 0;
}

OUTPUT

1. Create Tree
2. Display
3. Delete
4. Exit

Enter ur choice1
Enter the value to be inserted
5
Do you want to continue (Type y or n)
Y
Enter ur choice1
Enter the value to be inserted
2
Do you want to continue (Type y or n)
y
Enter ur choice1
Enter the value to be inserted
1
Do you want to continue (Type y or n)
y
Enter ur choice1
Enter the value to be inserted
3
Do you want to continue (Type y or n)
y
Enter ur choice1
Enter the value to be inserted
6
Do you want to continue (Type y or n)
y
Enter ur choice1
Enter the value to be inserted
7
Do you want to continue (Type y or n)
y
Enter ur choice2
Construct Binary Tree::
1 -->2 -->3 -->5 -->6 -->7 -->
Do you want to continue (Type y or n)

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


29

y
Enter ur choice3
All data are removed
Do you want to continue (Type y or n)
y
Enter ur choice4
Wrong Entry

RESULT:
Thus the C program to create Binary Tree and various operations like, insert,
delete and display is obtained.

EX.NO: 7

DATE:

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


30

C PROGRAM TO IMPLEMENT BINARY SEARCH TREE AND ITS OPERATIONS

AIM:

To write a c program to implement binary tree and its operations.

ALGORITHM:

1. Start

2. Declare the struct node.

3. Declare the functions like insertion, deletion and search.

4. Insertion function is use to insert the value to the tree.

5. Search function is use to found the value or not.

6. Delete function is use to delete the value from the tree.

7. Display function is use to print the tree.

8. Stop

PROGRAM

#include<stdio.h>
#include<stdlib.h>
struct node
{
int value;
struct node *left_child, *right_child;
};

struct node *new_node(int value)


{
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->value = value;
temp->left_child = temp->right_child = NULL;
return temp;
}

struct node* search(struct node *root_node, int x)


{
if(root_node==NULL || root_node->value==x)
return root_node;
else if(x>root_node->value)
return search(root_node->right_child,x);
else
return search(root_node->left_child,x);

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


31

//function to find the minimum value in a node


struct node* find_minimum(struct node *root_node)
{
if(root_node == NULL)
return NULL;
else if(root_node->left_child != NULL) // node with minimum value will have no left child
return find_minimum(root_node->left_child); // left most element will be minimum
return root_node;
}
void print(struct node *root_node)
{
if (root_node != NULL)
{
print(root_node->left_child);
printf("%d -->", root_node->value);
print(root_node->right_child);
}
}
struct node* insertNode(struct node* node, int value) // inserting nodes!
{
if (node == NULL)
return new_node(value);
if (value < node->value)
node->left_child = insertNode(node->left_child, value);
else if (value > node->value)
node->right_child = insertNode(node->right_child, value);
return node;
}
struct node* delete(struct node *root_node, int x)
{
if(root_node==NULL)
return NULL;
if (x>root_node->value)
root_node->right_child = delete(root_node->right_child, x);
else if(x<root_node->value)
root_node->left_child = delete(root_node->left_child, x);
else
{
//No Children
if(root_node->left_child==NULL && root_node->right_child==NULL)
{
free(root_node);
return NULL;
}

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


32

//One Child
else if(root_node->left_child==NULL || root_node->right_child==NULL)
{
struct node *temp;
if(root_node->left_child==NULL)
temp = root_node->right_child;
else
temp = root_node->left_child;
free(root_node);
return temp;
}

//Two Children
else
{
struct node *temp = find_minimum(root_node->right_child);
root_node->value = temp->value;
root_node->right_child = delete(root_node->right_child, temp->value);
}
}
return root_node;
}
int main()
{
struct node *root = NULL;
int data;
char ch;
printf("\n 1.Create Tree \n 2.Construct Tree \n 3.Delete \n 4.Search \n 5.Exit");
do
{
int choice;
printf("\n Enter ur choice");
scanf("%d",&choice);
switch (choice)
{
case 1 :
printf("\nEnter the value to be inserted\n");
scanf("%d",&data);
root = insertNode(root,data);
break;
case 2 :
printf("\n Construct Tree::\n");
print(root);
break;
case 3 :
printf("\nEnter the value to be deleted\n");
scanf("%d",&data);
root=delete(root,data);
print(root);

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


33

break;
case 4:
printf("\n Enter the value to be searched");
scanf("%d",&data);
root=search(root,data);
print(root);
break;
default :
printf("Wrong Entry\n");
break;
}
printf("\nDo you want to continue (Type y or n)\n");
scanf(" %c",&ch);
} while (ch == 'Y'|| ch == 'y');
return 0;
}

OUTPUT

1. Create Tree
2. Construct Tree
3. Delete
4. Search
5. Exit

Enter ur choice1
Enter the value to be inserted
5
Do you want to continue (Type y or n)
y
Enter ur choice1
Enter the value to be inserted
2
Do you want to continue (Type y or n)
y
Enter ur choice1
Enter the value to be inserted
1
Do you want to continue (Type y or n)
y
Enter ur choice1
Enter the value to be inserted
3
Do you want to continue (Type y or n)
y
Enter ur choice1
Enter the value to be inserted
6
Do you want to continue (Type y or n)

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


34

y
Enter ur choice1
Enter the value to be inserted
7
Do you want to continue (Type y or n)
y
Enter ur choice2
Construct Tree::
1 -->2 -->3 -->5 -->6 -->7 -->
Do you want to continue (Type y or n)
y
Enter ur choice3
Enter the value to be deleted
3
1 -->2 -->5 -->6 -->7 -->
Do you want to continue (Type y or n)
y
Enter ur choice4
Enter the value to be searched6
6 -->7 -->
Do you want to continue (Type y or n)
y
Enter ur choice5
Wrong Entry

RESULT:
Thus the C program of Binary Search Tree implementation is executed and
the output is verified.

EX.NO: 8

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


35

DATE:

C PROGRAM TO IMPLEMENTATION TREE TRAVERSAL

AIM:

To write a C program to implement Binary Tree Traversal.

ALGORITHM:

1. Start the program.

2. Declare the struct node.

3. Declare the function in order,pre order and post order.

4. In order function perform (LDR)

5. Pre order function perform (DLR)

6. Post order function perform (LRD)

7. Display function is use to print the tree.

8. Stop

PROGRAM

#include<stdio.h>

#include<stdlib.h>

struct node

int value;

struct node *left_child, *right_child;

};

struct node *new_node(int value)

struct node *tmp = (struct node *)malloc(sizeof(struct node));

tmp->value = value;

tmp->left_child = tmp->right_child = NULL;

return tmp;

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


36

struct node* insertNode(struct node* node, int value)


{
if (node == NULL)
return new_node(value);
if (value < node->value)
node->left_child = insertNode(node->left_child, value);
else if (value > node->value)
node->right_child = insertNode(node->right_child, value);
return node;
}

void preorder(struct node * root_node)


{
if (root_node)
{
printf("%d-->",root_node->value);
preorder(root_node->left_child);
preorder(root_node->right_child);
}
}
void inorder(struct node * root_node)
{

if (root_node)

inorder(root_node->left_child);

printf("%d-->",root_node->value);

inorder(root_node->right_child);

void postorder(struct node * root_node)

if (root_node)

postorder(root_node->left_child);

postorder(root_node->right_child);

printf("%d-->",root_node->value);

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


37

int main()

struct node *root = NULL;

int data;

char ch;

printf("\n 1.Create Tree \n 2.Inorder Traversal \n 3.Preorder Traversal \n

4.Postorder Traversal \n 5.Exit");

do

int choice;

printf("\n Enter ur choice");

scanf("%d",&choice);

switch (choice)

case 1 :

printf("\nEnter the value to be inserted\n");

scanf("%d",&data);

root = insertNode(root,data);

break;

case 2 :

printf("\n Inorder Traversal::\n");

inorder(root);

break;

case 3 :

printf("\n Preorder Traversal::\n");

preorder(root);

break;

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


38

case 4 :

printf("\n Postorder Traversal::\n");

postorder(root);

break;

default :

printf("Wrong Entry\n");

break;

printf("\nDo you want to continue (Type y or n)\n");

scanf(" %c",&ch);

} while (ch == 'Y'|| ch == 'y');

return 0;

OUTPUT

1. Create Tree

2. Inorder Traversal

3. Preorder Traversal

4. Postorder Traversal

5. Exit

Enter ur choice1

Enter the value to be inserted

Do you want to continue (Type y or n)

Enter ur choice1

Enter the value to be inserted

Do you want to continue (Type y or n)

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


39

Enter ur choice1

Enter the value to be inserted

Do you want to continue (Type y or n)

Enter ur choice1

Enter the value to be inserted

Do you want to continue (Type y or n)

Enter ur choice1

Enter the value to be inserted

Do you want to continue (Type y or n)

Enter ur choice1

Enter the value to be inserted

Do you want to continue (Type y or n)

Enter ur choice2

Inorder Traversal::

1-->2-->3-->5-->6-->7-->

Do you want to continue (Type y or n)

Enter ur choice3

Preorder Traversal::

5-->2-->1-->3-->6-->7-->

Do you want to continue (Type y or n)

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


40

Enter ur choice4

Postorder Traversal::

1-->3-->2-->7-->6-->5-->

Do you want to continue (Type y or n)

Enter ur choice5

Wrong Entry\

RESULT:

Thus the above C program to implement Binary Tree Traversal implementation is executed
and the output is verified.

EX.NO: 9(a)

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


41

DATE:

IMPLEMENTATION OF GRAPH TRAVERSAL BREADTH FIRST SEARCH (BFS)

AIM
To write a C program for implementation of Breadth First Search (BFS) in graph
using adjacency matrix.

ALGORITHM

1. Start by putting any one of the graph's vertices at the back of a queue.
2. Take the front item of the queue and add it to the visited list.
3. Create a list of that vertex's adjacent nodes. Add the ones which aren't in the visited list to
the back of the queue.
4. Keep repeating steps 2 and 3 until the queue is empty.

PROGRAM

#include<stdio.h>
void BFS(int);
int graph[10][10], visited[10],total;

main()
{
int i,j;
printf("\nEnter the total number of vertices in graph\n");
scanf("%d",&total);

printf("\nEnter the adjacency matrix\n");


for(i=0;i<total;i++)
{
for(j=0;j<total;j++)
{
scanf("%d",&graph[i][j]);
}
}
for(i=0;i<total;i++)
{
visited[i] = 0;
}
printf("\nBFS traversal is \n");
BFS(0);
}

void BFS(int vertex)

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


42

{
int j;
printf("%d\t",vertex);
visited[vertex] = 1;
for(j=0;j<total;j++)
{
if(!visited[j] && graph[vertex][j] == 1 )
{
BFS(j);
}
}
}

OUTPUT

Enter the total number of vertices in graph


5

Enter the adjacency matrix


0 1 1 1 0
1 0 0 1 0
1 0 0 1 0
0 1 1 0 1
0 0 0 1 0

BFS traversal is
0 1 3 2 4

RESULT:

Thus the C program to implement the graph traversal (BFS) is executed and output is
verified.

EX.NO: 9(B)
DATE:

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


43

IMPLEMENTATION OF GRAPH TRAVERSAL DEPTH FIRST SEARCH (DFS)

AIM
To implement Depth First Search (DFS) in graph using adjacency matrix.

ALGORITHM

1. Start by putting any one of the graph's vertices on top of a stack.


2. Take the top item of the stack and add it to the visited list.
3. Create a list of that vertex's adjacent nodes. Add the ones which aren't in the visited list to
the top of the stack.
4. Keep repeating steps 2 and 3 until the stack is empty.
PROGRAM

#include<stdio.h>
#include<stdlib.h>
int graph[10][10], visited[10],total,arr[30];
static int k=0,count=0;
void DFS(int);
main()
{
int i,j;
printf("\nEnter the total number of vertices in graph\n");
scanf("%d",&total);
printf("\nEnter the adjacency matrix\n");
for(i=0;i<total;i++)
{
for(j=0;j<total;j++)
{
scanf("%d",&graph[i][j]);
}
}
for(i=0;i<total;i++)
{
visited[i] = 0;
}
printf("\nDFS traversal is \n");
DFS(0);
}
void DFS(int vertex)
{
int j,c=0;
count++;
printf("%d\t",vertex);
visited[vertex] = 1;
for(j=0;j<total;j++)
{
if(!visited[j] && graph[vertex][j] == 1)
{
arr[++k] = j;
c=1;
}
21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE
44

if(count == total)
{
exit(0);
}
}
if(c==1)
{
DFS(arr[k]);
}
else
{
k--;
DFS(arr[k]);
}
}

OUTPUT

Enter the total number of vertices in graph


5

Enter the adjacency matrix


0 1 1 1 0
1 0 0 1 0
1 0 0 1 0
0 1 1 0 1
0 0 0 1 0

DFS traversal is
0 3 4 2 1

RESULT:

Thus the C program to implement the graph traversal (DFS) is executed and the
output is verified.

EX.NO: 10

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


45

DATE:

C PROGRAM FOR IMPLEMENTATION OF HASHING TECHNIQUES

AIM:
To write a c program to implement hash table.

ALGORITHM:

1. Create a structure, with key and value as a pair.


2. Read the size of an array and then enter the array elements.
3. Calculate the value and key based on module function
4. Display the hash table.
5. Stop

PROGRAM:

#include <stdio.h>
struct pair
{
int key,value;
};
int main()
{
int size , i , numbers;

struct pair hash[size];

printf("\n Enter the size of the table");


scanf("%d",&size);

printf("\n Enter the elements :");


for(i=0;i<size;i++)
{
scanf("%d",&numbers);
hash[numbers%size].value = numbers;
hash[numbers%size].key= numbers % size;
}
printf("\n key \t value");
for(i=0;i<size;i++)
printf("\n %d \t %d",hash[i].key,hash[i].value);
return 0;
}

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


46

OUTPUT

Enter the size of the table 5


Enter the elements :
44
22
10
33
11

key value
0 10
1 11
2 22
3 33
4 44

RESULT:

Thus the C program to implement hash table is executed and the output is verified.

EX.NO: 11(a)

DATE:

C PROGRAM TO IMPLEMENT LINEAR SEARCH

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


47

AIM:
To write a c program to implement linear search.

ALGORITHM:

Step 1: First, read the search element (Target element) in the array.
Step 2: In the second step compare the search element with the first element in the
array.
Step 3: If both are matched, display "Target element is found" and terminate the
Linear Search function. 
Step 4: If both are not matched, compare the search element with the next element in
the array. 
Step 5: In this step, repeat steps 3 and 4 until the search (Target) element is compared
with the last element of the array.
Step 6 - If the last element in the list does not match, the Linear Search Function will
be terminated, and the message "Element is not found" will be displayed.

PROGRAM
#include <stdio.h>
int main()
{
int i,size,a[10],search;
printf("\n Enter the size of an array :");
scanf("%d",&size);
printf("\n Enter the value :");
for(i=0;i<size;i++)
scanf("%d",&a[i]);
printf("\n Find the search element : ");
scanf("%d",&search);
for(i=0;i<size;i++)
{
if(search==a[i])
{
printf("%d fount at the position %d ",search,i+1);
break;
}
}
if (i==size)
printf("element not found");
return 0;
}

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


48

OUTPUT

Enter the size of an array :5


Enter the value :
43
77
54
22
16

Find the search element : 22


22 fount at the position 4

----------

Enter the size of an array :3


Enter the value :
55
23
78

Find the search element : 44


element not found

RESULT:

Thus the C program to implementation linear search is executed and the output is
verified.

EX.NO: 11(b)

DATE:

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


49

IMPLEMENTATION OF LINEAR SEARCH

AIM:
To write a C program to implement binary search.

ALGORITHM:

1. We are given an input array that is supposed to be sorted in ascending order.


2. We take two variables which will act as a pointer i.e, start, and stop.
3. Beg will be assigned with 0 and the end will be assigned to the last index of the array.
4. Now we will introduce another variable mid which will mark the middle of the current
array. That will be computed as (start+stop)/2.
5. If the element present at the mid index is equal to the element to be searched, then just
return the mid index.
6. If the element to be searched is smaller than the element present at the mid index,
move end to mid-1, and all RHS will get discarded.
7. If the element to be searched is greater than the element present at the mid index,
move beg to mid+1, and all LHS will get discarded.

PROGRAM

#include <stdio.h>
int main()
{
int i,size,a[10],search,start,mid,stop,flag=0;

printf("\n Enter the size of an array :");


scanf("%d",&size);

printf("\n Enter the value (ascending order) :");


for(i=0;i<size;i++)
scanf("%d",&a[i]);

printf("\n Find the search element : ");


scanf("%d",&search);

start=0;
stop=size-1;

while(start<=stop)
{
mid=(start+stop)/2;
if(search==a[mid])
{
flag=1;
break;
}
else if (search<a[mid])

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


50

stop=mid-1;
else
start=mid+1;
}
if(flag==1)
printf("%d fount at the position %d ",search,mid+1);
else
printf("element not found");

return 0;
}

OUTPUT

Enter the size of an array :3

Enter the value (ascending order) :


1
4
5

Find the search element : 6


element not found

------

Enter the size of an array :4

Enter the value (ascending order) :


1
2
4
5

Find the search element : 5


5 fount at the position 4

RESULT:

Thus the C program for implementation of binary search is executed and the output is
verified.

EX.NO: 12(a)

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


51

DATE:

IMPLEMENTATION OF BUBBLE SORTING

AIM:
To write a c program to implement bubble sorting.

ALGORITHM:

1. Starts from the first index: a[0] and compares the first and second element: a[0] and a[1]
2. If a[0] is greater than a[1], they are swapped
3. Similarly, if a[1] is greater than a[2], they are swapped
4. The above process continues until the last element a[n-1]

PROGRAM

#include <stdio.h>
int main()
{
int a[50],n,i,j,temp;
printf("Enter Size of an Array :");
scanf("%d",&n);

printf("Values are");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("Ascending Order Using Bubble Sort \n");
for(i=0;i<n;i++)
printf("\t %d",a[i]);
return 0;
}

OUTPUT
Enter Size of an Array :8
Values are

25 57 48 37 12 92 86 33

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


52

Ascending Order Using Bubble Sort 12 25 33 37 48 57 86 92

RESULT:
Thus the C program to implementation bubble sort is executed and the output is
verified.

EX.NO: 12(b)

DATE:

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


53

IMPLEMENTATION OF SELECTION SORTING

AIM:
To write a C program to implement selection sort.

ALGORITHM:

Step 1 − Set min to the first location


Step 2 − Search the minimum element in the array
Step 3 – swap the first location with the minimum value in the array
Step 4 – assign the second element as min.
Step 5 − Repeat the process until we get a sorted array.

PROGRAM

#include <stdio.h>
int main()
{
int a[100], n, i, j, min,temp;
printf("Enter number of elements \n");
scanf("%d", &n);
printf("Enter Values");
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
for(i = 0; i < n - 1; i++)
{
min=i;
for(j = i + 1; j < n; j++)
{
if(a[j]<a[min])
min=j;
}
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
printf("Sorted Array:\n");
for(i = 0; i < n; i++)
printf("%d \n", a[i]);
return 0;
}

OUTPUT

Enter number of elements


5
Enter Values 77 -1 64 234 14
Sorted Array:
-1 14 64 77 234

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE


54

RESULT:
Thus the C program to implementation selection sort is executed and the output is
verified.

21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE

You might also like