KEMBAR78
Data Structure Lab Programs | PDF | Queue (Abstract Data Type) | Software Engineering
0% found this document useful (0 votes)
7 views41 pages

Data Structure Lab Programs

Data structure lab programs 2nd sem BCA

Uploaded by

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

Data Structure Lab Programs

Data structure lab programs 2nd sem BCA

Uploaded by

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

*********************Program 1 ***************

#include<stdio.h>
#include<stdlib.h>
int A[100],n,key;
int 1search (int A[],int n,int key)
{
int i=-1;
while (i<n) {/*Traversing till the end of table*/
if (A[++i] == key) /* element is found */
return i;
}
return -1; /* item is not found */
}

int binsearch (int A[], int n, int key)


{
int first,last,mid,i;
first=0;
last=n-1;
while (first<=last) {
mid=(first+last)/2;
if(key==A[mid])
return mid;
else if (key<A[mid])
last=mid -1;
else
first=mid+1;
}
return-1;
}
void acceptInput()
{
int i;
printf("Enter Number of Elements :");
scanf ("%d", &n);
for (i=0;i<n; i++) {
printf ("Enter Element %d:",i+1);
scanf ("%d",&A[i]);
}
printf ("Enter an Element to be Searched:");
scanf ("%d",&key);
}
void main()
{
int ch,flag;
while(1)
{
printf("\n Searching Techniques");
printf("\n**********************");
printf("\n 1.Linear Search ");
printf("\n 2.Binary Search ");
printf("\n 3.Exit ");
printf("\n Enter your choice : ");
scanf("%d:",&ch);
switch(ch)
{
case 1: acceptInput();
flag=lsearch(A,n,key);
if(flag==-1)
printf("\n Search is Unsuccessful.");
else
printf("\n An Element %d Found at Position :
%d",key, flag+1);
break;
case 2: printf("\n Enter Element in Ascending Order for
Binary Search\n");
acceptInput();
flag = binsearch(A,n,key);
if (flag==-1)
printf("%d not found in array \n",key);
else
printf("An Element %d Found at
Position:%d\n",key,flag+1);
break;
case 3: exit(0);
}
}
}
***************************Output:**************
Searching Techniques
**********************
1.Linear Search
2.Binary Search
3.Exit
Enter your choice:1
Enter Number of Elements:5
Enter Element 1:90
Enter Element 2:40
Enter Element 3:20
Enter Element 4:80
Enter Element 5:70
Enter an Element to be Searched:20
An Element 20 Found at Position :3
************Program 2**************
#include<stdio.h>
void bubble_sort(int a[ ], int n)
{
int pass, temp, j;
for (pass =1; pass <= n-1; pass++)
{
for (j=0; j<= n-pass-1; j++)
{
if (a[j]>a[j+1])
{
temp =a[j];
a[j]=a[j+1];
a[j+1] =temp;
}
}
}
}
int main()
{
int i,j,a[20],n,temp;
printf("\n Enter the number of elements: ");
scanf("%d",&n);
printf("\n Enter the array elements: \n");
for(i=0;i<n;i++)
scanf("%d",&a[1]);
bubble_sort(a,n);
printf("\n The sorted elements are: \n");
for(i=0;i<n;i++)
printf( "%d",a[i]);
************Output:******************
Enter the number of elements:7
Enter the array elements:5 3 1 6 0 2 4
The sorted elements are:0 1 2 3 4 5 6
****************Program 3**************
#include<stdio.h>
#include<stdlib.h>
int a[100],n;

/* To find the location of max element */


int max (int a[],int k,int n)
{
int loc,j,max;
max=a[k];
loc=k;
for (j=k+1; j<=n-1; j++)
if(max<a[j]) { /*use max>a[j]for ascending order */
max=a[j];
loc=j;
}
return (loc);
}
void insertion_sort (int a[],int n)
{
int pass,K,temp,j;
for (pass=1; pass<n; pass++) {
K = a[pass]; /* K is to be inserted at proper place */
for (j=pass-1; j>=0 && K>a[j]; j--)/* Use K<a[j] for ascending
order */
a[j+1]=a[j];
a[j+1]=K; /* inserting K in to proper position */
}
}
void acceptInput()
{
int i;
printf("Enter the number of elements:");
scanf("%d",&n);
printf("\n Enter the array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
}
void display()
{
int i;
printf("\n The Sorted Array is:");
for(i=o;i<n;i++)
printf(" %d ",a[i]);
}

void main()
{
int k,temp,loc,ch;
while(1)
{
printf("\n Sorting Techiques");
printf("\n*********************");
printf("\n 1. Insertion Sort ");
printf("\n 2. Selection Sort ");
printf("\n 3. Exit ");
printf("\n Enter your choice :");
scanf("%d",&ch);
switch(ch)
{
case 1: acceptInput();
insertion_sort(a,n);
display();
break;
case 2: acceptInput();
for(k=0; k<n; k++)
{
loc=max(a,k,n);/* find the largest element location */
temp=a[k];
a[k]=a[loc];
a[loc]=temp;
}
display();
break;
case 3: exit(0);
}
}
}

*******************Output******************
Sorting Techniques
************************
1. Insertion Sort
2. Selection Sort
3. Exit
Enter your choice:1
Enter the number of elements:8
Enter the array elements:75 8 1 16 48 3 7 0
The Sorted Array is:75 48 16 8 7 3 1 0
**************Program 4***********
#include<stdio.h>
#include<stdlib.h>
struct node
{
int INFO;
struct node *LINK;
};
typedef struct node NODE;
NODE *start=NULL;

void main()
{
int ch, item, pos;
while(1)
{
printf("\n 1. Create a Linked List ");
printf("\n 2. Display ");
printf("\n 3. Insert First Node ");
printf("\n 4. Insert at the End ");
printf("\n 5. insert at the Specified Position ");
printf("\n 6. Delete First Node ");
printf("\n 7. Delete Last Node ");
printf("\n 8. Delete at the Specified Position ");
printf("\n 9. Delete a Node When Item is Given ");
printf("\n 10. Exit");
printf("\n Enter Your Choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: start=NULL;
create();
break;
case 2: display();
break;
case 3: printf("\n Enter the Item to Insert at the Beginning:
");
scanf("%d", &item);
printf("\n Linked List before Insertion is: \n");
display();
insert_beg(item);
printf("\n Linked List after Insertion is: \n");
display();
break;
case 4: printf("\n Enter the Item to Insert at the End: ");
scanf("%d", &item);
printf("\n Linked List before Insertion is:\n");
display();
insert_end(item);
printf("\n Linked Lst after Insertion is: \n");
display();
break;
case 5: printf("\n Enter an Item to Insert at Certain Position:
");
scanf("%d", &item);
printf("\n Enter a Valid Position: ");
scanf("%d",&pos);
if((pos==0)||(pos>length()+1))
{
printf("\n It is Invalid Position \n");\
break;
}
else
{
printf("\n Linked List before Insertion is: \n");
display();
insert_pos(item, pos);
printf("\n Linked List after Insertion is: \n");
display();
break;
}
case 6: printf("\n Linked List before Deletion is: \n");
display();
delete_beg();
printf("\n Linked List after Deletion is: \n");
display();
break;
case 7: printf("\n Linked List before Deletion is: \n");
display();
delete_end();
printf("\n Linked List after Deletion is: \n");
display();
break;
case 8: printf("\n Enter a Valid Position to Delete: \n");
scanf("%d",&pos);
if((pos==0)||(pos>length()))
printf("\n It is Invalid position \n");
break;
}
else
{
printf("\n Linked List before Deletion is: \n");
display();
delete_pos(pos);
printf("\n Linked List after Deletion is: \n");
display();
break;
}
case 9: printf("\n Linked List before Deletion is: \n");
display();
printf("\n Enter an Item to be Deleted: \n");
scanf("%d",&item);
delete_item(item);
printf("\n Linked list after Deletion is: \n");
display();
break;
case 10: exit(0);
}
}
}

*****************Output:****************
1.Create a Linked List
2.Display
3.Insert First Node
4.Insert at the End
5.insert at the Specified Position
6.Delete First Node
7.Delete Last Node
8.Delete at the Specified Position
9.Delete a Node When Item is Given
10. Exit
Enter Your Choice: 1
Enter the node 1:61
Do you wish to add one more node (Y/N): y
Enter the node 2:16
Do you wish to add one more node (Y/N): y
Enter the node 3 18
Do you wish to add one more node (Y/N): y
Enter the node 4:27
Do you wish to add one more node (Y/N): n
1. Create a Linked List
2. Display
3. Insert First Node
4. Insert at the End
5. insert at the Specified Position
6. Delete First Node
7. Delete Last Node
8. Delete at the Specified Position
9. Delete a Node When Item is Given
10. Exit
Enter Your Choice: 2
6116-8-27-> NULL
1. Create a Linked List
2. Display
3. Insert First Node
4. Insert at the End
5. insert at the Specified Position
6. Delete First Node
7. Delete Last Node
8. Delete at the Specified Position
9. Delete a Node When Item is Given
10. Exit
Enter Your Choice: 8
***************Program 5***************
#include<stdio.h>
#include<stdlib.h>
#define N. 10
int QUEUE[N], FRONT-0, REAR--1, ITEM;
REAR P maximum
not full Full to pr
void main()
{
int ch;
while(1)
{
printf("\n Queue Implementation using Array");
printf("\n***********************************");
printf("\n 1. Insert into Queue ");
printf("\n 2. Delete from Queue ");
printf("\n 3. Display Queue ");
printf("\n 4. Exit ");
printf("\n Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: Qinsert();
Qdisplay();
break;
case 2: Qdelete();
Qdisplay();
break;
case 3: Qdisplay();
break;
case 4: exit(0);
}
}
}

***************Output:***************
Queue Implementation using Array
***************************************
1. Insert into Queue
2. Delete from Queue
3. Display Queue
4. Exit
Enter your choice:1
Enter an Item:45
Queue:45
*****************Program 6****************
#include<stdio.h>
#include<stdlib.h>
#define N 10

int QUEUE [N], FRONT=-1, REAR--1, ITEM; /*queue is initially empty*/

/*Function to insert an ITEM into a circular queue */


void CQinsert()
{
if ((FRONT==(REAR+1) % N)) /* to check overflow condition*/
printf("\n Queue Overflow");
else {
printf("\n Enter the Element to be Inserted:");
scanf("\n%d",&ITEM);
if (FRONT==-1)
FRONT REAR 0;
else
REAR (REAR+1)%N;
QUEUE (REAR]-ITEM;
}
}
void CQdisplay()
{
int i;
if (FRONT==-1)
printf("\n No elements in the (Queue");
else {
printf("\n Circular Queue: ");
if(FRONT<=REAR) {
for(i=FRONT;1<=REAR;i++)
printf("\t%d", QUEUE[1]);
}
if(FRONT>REAR) {
for(i=FRONT;i<=N-1;i++)
printf("\t%d", QUEUE [1]);
for(i=0;i<=REAR; i++)
printf("\t%d", QUEUE[1]);
}
printf("\n Front Element of the CQueue is: %d", QUEUE [FRONT]);
printf("\n Rear Element of the CQueue is %d", QUEUE [REAR]);
}
}
void CQdelete()
{
if (FRONT-1)
printf("\n Queue Underflow");
else
{
ITEM-QUEUE[FRONT];
printf("\n The Deleted Item is: %d", QUEUE [FRONT]);
if(FRONT=-REAR)
FRONT-REAR=-1;
else
FRONT (FRONT+1)%N;
}
}
void main()
{
int ch;
while(1)
{
printf("\n Circular Queue implementation using Array");
printf("\n******************************************");
printf("\n 1. Circular Queue Insert ");
printf("\n 2. Circular Queue Delete ");
printf("\n 3. Circular Queue Display ");
printf("\n 4. Exit ");
printf("\n Enter your Choice: ");
scanf("%d",&ch);

switch(ch)
{
case 1: CQinsert();
CQdisplay();
break;
case 2: CQdelete();
CQdisplay();
break;
case 3: CQdisplay();
break;
case 4: exit(0);
}
}
}

***************Output***************
Circular Queue implementation using Array
********************************************
1. Circular Queue Insert
2. Circular Queue Delete
3. Circular Queue Display
4. Exit
Enter your Choice:1
Enter the Element to be Inserted:61
Circular Queue:61
Front Element of the CQueue is:61
Rear Element of the CQueue is:61
Circular Queue implementataion using Array
****************Program 7***************
#include<stdio.h>
#include<stdlib.h>
struct node
{
int INFO;
struct node *LINK;
};
typedef struct node NODE;
NODE *start-NULL;
void insertOrdered(int data)
{
NODE *NEWNODE (NODE *)malloc(sizeof(NODE));
NEWNODE-INFO= data;
if(start==NULL)
{
start NEWNODE;
start-> LINK NULL;
}
else if(data start->INFO)
{
NEWNODE->LINK=start;
start=NEWNODE;
}
else
{
NODE PREVPTR start;
NODE CURRPTR start->LINK;
while (CURRPTR != NULL && data > CURRPTR->INFO)
{
PREVPTR CURRPTR;
CURRPTR CURRPTR->LINK;
}
PREVPTR->LINK = NEWNODE;
NEWNODE->LINK = CURRPTR;
}
}
void deleteOrdered(int data)
{
NODE PREVPTR = start;
NODE CURRPTR start->LINK;
if(start--NULL)
printf("\n List is Empty");
else if(data start->INFO)
{
start-CURRPTR;
free(PREVPTR);
}
else
{
while (CURRPTR 1- NULL && CURRPTR->INFO 1= data)
{
PREVPTR CURRPTR;
CURRPTR CURRPTR->LINK;
}
if(CURRPTR != NULL)
{
PREVPTR->LINK CURRPTR->LINK;
free(CURRPTR);
}
else
printf("\n Data Not Found in the List");
}
}
void display()
{
NODE CURRPTR = start;
if (CURRPTR NULL)
printf(" Empty");
else
{
while (CURRPTR 1= NULL)
{
printf("%d", CURRPTR -> INFO);
printf(" -> ");
CURRPTR CURRPTR -> LINK;
}
printf ("NULL");
}
}
int main()
{
int ch, data;
while(1)
{
printf("\n Ordered Linked List Operations");
printf("\n********************************");
printf("\n 1. Insert");
printf("\n 2. Delete");
printf("\n 3. Display");
printf("\n 4. Exit");
printf("\n Enter Your Choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\n Enter Data to be Inserted: ");
scanf("%d", &data);
printf("\n Linked List before Insertion is: \n");
display();
insertOrdered(data);
printf("\n Linked List after Insertion is: \n");
display();
break;
case 2: printf("\n Enter Data to be Deleted: ");
scanf("%d",&data);
printf("\n Linked List before Deletion is: \n");
display();
deleteOrdered(data);
printf("\n Linked List after Deletion is: \n");
display();
break;
case 3: display();
break;
case 4: exit(0);
}
}
}

***************Output****************
Ordered Linked List Operations
*********************************
1. Insert
2. Delete
3. Display
4. Exit
Enter Your Choice:1
Enter Data to be Inserted:61
Linked List before Insertion:Empty
Linked List after Insertion is:61->NULL
***************Program 8***************
#include<stdio.h>
void toh(int,char,char,char);
int count=0;
void main()
{
char source='S'temp='T',dest='D';
int n;
printf ("Enter the number of disks:");
scanf ("%d",&n);
printf("\n Sequence is :");
toh(n,source,temp,dest);
printf("\n The Number of Moves:%d",count);
void toh(int n,char source,char temp,char dest)
{
if (n>0)
{
toh (n-1,source,dest,temp);
printf("\n Move Disk %d %c->%c \n",n,source,dest);
count++;
toh (n-1,temp,source,dest);
}
}

*******************Output*************
Enter the number of disks:3
Sequence is:
Move Disk 1 S->D
Move Disk 2 S->T
Move Disk 1 D->T
Move Disk 3 S->D
Move Disk 1 T->S
Move Disk 2 T->D
Move Disk 1 S->D
The Number of Moves:7
*******************program 9*******************

#include <stdio.h>

int GCD(int m, int n)

if (n==0)

return(m);

else if (n > m)

return(GCD(n, m));

else

return (GCD(n, m % n));

void main()

int k, m, n;

printf ("Enter Three Numbers: \n");

scanf ("%d %d %d", &k, &m,&n);

printf ("\n GCD (%d,%d,%d) = %d \n", k, m, n, GCD(k, GCD(m,n)));

*******************Output***********************

Enter Three Numbers: 10 20 100

GCD (10,20,100) = 10
**********************program10*************************

#include <stdio.h>

#include <stdlib.h>

#include <conio.h>

#include <malloc.h>

struct stack

int info;

struct stack * link;

};

struct stack *TOP = NULL;

void main()

int item, choice;

do

printf("\n "MAIN MENU*");

printf("\n 1. PUSH");

printf("\n 2. POP");

printf("\n 3. DISPLAY");

printf("\n 4. EXIT");

printf("\n Enter your option: ");

scanf("%d", &choice);

switch(choice);

case 1: printf("\n Enter the number to be pushed on stack: ");


scanf("%d", &item);

push(item);

break;

case 2: item pop();

if(item!=-1)

printf("\n Popped Item is: %d", item);

break;

case 3: display();

break;

}while(choice!=4);

********************Output***********************

*MAIN MENU*

1. PUSH

2. POP

3. DISPLAY

4. EXIT

Enter your option: 1

Enter the number to be pushed on stack: 10

*MAIN MENU*

1. PUSH

2. POP

3. DISPLAY

4. EXIT

Enter your option: 1


Enter the number to be pushed on stack: 20

*MAIN MENU*

1. PUSH

2. POP

3. DISPLAY

4. EXIT

Enter your option: 1

Enter the number to be pushed on stack: 30

*MAIN MENU*

1. PUSH

2. POP

3. DISPLAY

4. EXIT

Enter your option: 3

Stack elements are:

30

20

10
********************program11********************

#include<stdio.h>

#include<string.h>

#define MAX 20

Char s[MAX];

Int top=0;

Int precedence(char elem);

Void push(char elem);

Int pop();

Void main()

Char infix [MAX], postfix [MAX],ch, elem;

int i=0,j=0;

Printf(“\n\t\tProgram to Convert infix to Postfix Expression.”);

printf(“\n\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~”);

printf(“\n Enter the infix expression: \n”); “);

Scanf(“%s”, infix);

Push(‘#’);

for(i=0; i<strlen(infix); i++)

Ch=infix[i];

If(isalnum(ch))

Postfix[j++]=ch;

Else if(ch==’(‘)

Push(ch);

Else if(ch==’)’)
{ while(s[top]!=’(‘)

postfix[j++] = pop();

elem = pop();

Else

While(precedence(s[top]) > precedence(ch))

Postfix[j++] pop();

Push(ch);

While(s[top] !=’#’)

Postfix[j++]-pop();

Postfix[j]=’\0’;

Printf(“\n Postfix Expression is: \n %s \n”,postfix);

Int precedence(char elem)

Switch(elem)

Case ‘+’:

Case ‘-‘ : return(1);

Case’*’ :

Case ‘/’: return(2);

Case’^’: return(3);

Case ‘(‘:
Case ‘#’ : return(0);

void push(char elem)

++top;

s[top]=elem;

Int pop()

char elem;

elem=s[top];

--top;

return(elem);

**********************Output**********************

Program to Convert infix to Postfix Expression.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Enter the infix expression : x^y/(5*z)+2

Postfix Expression is: xy^5z*/2+


***********************program12**************************

#include<stdio.h>

#include<string.h>

#include<math.h>

#define MAX 20

int s[MAX], top=0;

void main()

char postfix [MAX],ch;

int i,op1, op2, res;

printf("\n\t\tProgram to Evaluate Postfix Expression.");

Printf(“\n\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~”);

printf("\n Enter the postfix expression : \n ");

scanf("%s", &postfix);

for(i=0; i<strlen(postfix);i++)

ch=postfix[i];

if(isdigit(ch))

push(ch-'0');

else

op2=pop();

op1=pop();

switch(ch)

case '+': res = op1 + op2; break;


case ‘-‘ : res = op1 op2; break;

case ‘*’ : res = op1 * op2; break;

case '/': res = op1 / op2; break;

case ‘^’ : res = pow(op1,op2); break;

default: printf(" Invalid Character \n");

} push(res);

printf("Result of above expression is: %d \n",pop());

**********************Output***********************

Program to Evaluate Postfix Expression.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Enter the postfix expression : 53+82-*

Result of above expression is : 48


**********************program13*********************
*

#include<stdio.h>

#include<stdlib.h>

Struct node

Int info;

Struct node *left;

Struct node *right;

};

Typedef struct node NODE;

NODE *root NULL;

NODE *getInSuccessor (NODE *ptr) {

While(ptr->left != NULL)

Ptr ptr->left;

Return ptr;

NODE deletion (NODE *p, int item)

NODE *temp;

If(!p) {

Printf(“Unable to delete. No such key exists.\n”);

Return p;

else if(item> p->info)

p->right deletion(p->right, item);


else if(item < p->info)

p->left deletion(p->left, item);

else {

if(p->left= NULL) {

temp p->right;

free(p);

return temp;

else if(p->right == NULL) {

temp = p->left;

free(p);

return temp;

temp = getInSuccessor (p->right);

pinfo temp->info;

p->right deletion(p->right, temp->info);

return p;

void main() {

int item,ch,n,1;

while(1) {

printf("\n Binary Search Tree Menu");

printf("\n----------------------------“);

printf("\n 1. Insert ");


printf("\n 2. Delete ");

printf("\n 3. Display ");

printf("\n 4. Exit ");

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

scanf("%d",&ch);

switch(ch) {

case 1 :printf("\n Enter the Number of Nodes:");

scanf("%d",&n);

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

printf("\n Enter the data for the node: ");

scanf("%d",&item);

create(item);

break;

case 2: printf("\n Enter an Item to be deleted: ");

scanf("%d",&item);

root deletion(root, item);

disp(root, 1);

break;

case 3: printf("\n The Binary Tree nodes

are:\n\n\n\n");

disp(root,1);

break;

case 4: exit(1);

}
}

******************* Output***********************

Binary Tree Menu

----------------------------

1. Insert

2. Delete

3. Display

4. Exit

Enter the choice : 1

Enter the Number of Nodes : 7

Enter the data for the node: 18

Enter the data for the node: 15

Enter the data for the node: 40

Enter the data for the node: 50

Enter the data for the node : 30

Enter the data for the node: 17

Enter the data for the node : 41

Enter the choice : 3

The Binary Tree nodes are :

50

40 41

18 30

17

15
***********************program 14*************************

#include<stdio.h>

#include<stdlib.h>

struct node

struct node *left;

struct node *right;

int info;

};

typedef struct node NODE;

NODE *root=NULL;

Void main() {

int item,i,n;

printf("\n Enter the Number of Nodes:");

scanf("%d",&n);

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

printf("\n Enter Data for the Node %d: ",i+1);

scanf("%d",&item);

create(item);

printf("\n The Binary Search Tree is: \n\n\n\n");

disp(root, 1);

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

in_order(root);
printf("\n Preorder Traversal is: ");

pre_order(root);

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

post_order(root);

*********************Output*********************

Enter the Number of Nodes : 7

Enter Data for the Node 1: 2

Enter Data for the Node 2: 5

Enter Data for the Node 3: 1

Enter Data for the Node 4: 3

Enter Data for the Node 5: 9

Enter Data for the Node 6:0

Enter Data for the Node 7: 6

The Binary Search Tree is :

5 6

Inorder Traversal is: 0 1 2 3 5 6 9

Preorder Traversal is: 2 1 0 5 3 9 6

Postorder Traversal is: 0 1 3 6 9 5 2


*************************program15**************************

#include <stdio.h>

void heapify(int arr[], int n, int i) {

int largest =i;

int left = 2i + 1

int right = 2i + 2 ;

int temp;

if (left < n && arr[left] > arr[largest])

largest = left;

if (right < n && arr[right] > arr[largest])

largest right;

if (largest l = i ) {

temp=arr[i];

arr[i]=arr[largest];

arr[largest]=temp;

heapify(arr, n, largest);

void heapSort(int arr[], int n)

int i, temp;

for ( 1 = n / 2 - 1 i >= 0 i --)

heapify(arr, n,i);

for ( 1 = n - 1 1 >= 0 i ..) {

temp=arr[0];

arr[0]=arr[1];
arr[i]=temp;

heapify(arr, i, 0);

int main()

int arr[20];

int n, i;

printf("Enter the size of the array : ");

scanf("%d",&n);

printf("\nEnter the elements : ");

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

scanf("%d",&arr[i]);

heapSort(arr, n);

printf("Sorted array is: ");

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

printf("%d", arr[i]);

***********************Output ************************

Enter the size of the array : 8

Enter the elements : 50 80 60 20 10 40 30 70

Sorted array is : 10 20 30 40 50 60 70 80
********************** program16*********************

#include <stdio.h>

Int stringLength(char str[]) (

Int length 0;

While (str[length] = ‘\0’) {

Length++;

Return length;

Void stringConcat(char S 1[] char s2[], char result[]) {

Int I = 0 j=0;

While ( s * 1[1] != ‘\0’) {

Result[i] = s 1[i] ;

i++;

} while (s2[j] != ‘\0’) {

Result[1] s2[j];

i++;

j++;

Result[1]= ‘\0’;

Void substringExtract (char str[], int start, int length, char result[]) {

Int i;

For ( I = 0 I < length; i++) {

Result[i] = str[start + i];

}
Result[i] = ‘\0’;

Void stringReplace(char str[], char find[], char replace[], char result[]) {

Int 10, j = 8 k, flag 8, start = -1;

While (str[i] != ‘\0’) {

K = 0;

while (str[i+k) find [k] 88 6nd[k] = '\0') (

k++;

if ( 6nd[k] ==^ - 10^ + ) {

flag = 1 ;

start = i ;

break;

i++;

while (str[i] != '\0') (

if (i == start) {

for (k = 0) replace[k] I = - (theta'; kH; j + 4) (

result[j] = replace[k];

1 + stringLength (find);

} else {

result[j++] str[i++];

}
result[j] = '\0';

int main() {

char S1[] = "Flowers";

char s2[] = "are beautiful";

char concatResult[50], substringResult[10], replaceResult[50];

printf("Length of s1: %d\n", stringLength(s1));

stringConcat(s1, s2, concatResult);

printf("Concatenated String: %s\n", concatResult);

substringExtract(s1, 1, 3, substringResult);

printf("Extracted Substring: %s\n", substringResult);

stringReplace(s2, "are", "is", replaceResult);

printf("Modified s2: %s\n", replacetesult);

return 0;

Length of S1: 7

Concatenated String: Flowersare beautiful

Extracted Substring: low

Modified S2: is beautiful


************************program17***********************

#include <stdio.h>

#define MAX 10

void displayMatrix(int matrix [MAX] [MAX], int vertices) {

int i, j;

printf("\nAdjacency Matrix:\n");

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

for (j = 0; j < vertices; j++) {

printf("%d", matrix[i][j]);

printf("\n");

void main() {

int matrix[MAX] [MAX] = {0};

int vertices, edges, i, src, dest, choice;

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

scanf("%d", &vertices);

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

scanf("%d", &edges);

printf("Enter 1 for Directed Graph or 2 for Undirected Graph: ");

scanf("%d", &choice);

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

printf("Enter edge (source destination): ");

scanf("%d %d", &src, &dest);

matrix [src-1] [dest-1] = 1;


if (choice == 2) {

matrix[dest-1][src-1] = 1;

displayMatrix(matrix, vertices);

**********************Output***********************

Enter the number of vertices: 5

Enter the number of edges: 8

Enter 1 for Directed Graph or 2 for Undirected Graph: 1

Enter edge (source destination): 12

Enter edge (source destination): 13

Enter edge (source destination): 24

Enter edge (source destination): 25

Enter edge (source destination): 34

Enter edge (source destination): 44

Enter edge (source destination): 4 1

Enter edge (source destination): 45

Adjacency Matrix:

01100

00011

00010

10011

00000
*************************program 18*************************

#include <stdio.h>

#include <stdlib.h>

#define TABLE_SIZE 11

int hashTable [TABLE_SIZE];

#define EMPTY -1

int hashFunction(int key) {

return key % TABLE_SIZE;

void insert(int key) {

int index = hashFunction(key);

int originalIndex = index;

while (hashTable[index] != EMPTY) {

index = (index + 1) % TABLE_SIZE;

if (index == originalIndex) {

printf("Hash table is full. Cannot insert %d\n", key);

return;

hashTable[index] = key;

printf("Inserted %d at index %d\n", key, index);

int search(int key) {

int index = hashFunction (key);

int originalIndex = index;

while (hashTable[index] != EMPTY) {


if (hashTable[index] == key) {

return index;

index = (index + 1) % TABLE_SIZE;

if (index == originalIndex) {

break;

return -1;

void display() {

int i;

printf("\nHash Table:\n");

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

if (hashTable[i] == EMPTY)

printf("Index %d: EMPTY\n", i);

else

printf("Index %d: %d\n", i, hashTable[i]);

printf("\n");

int main() {

int num, key, i;

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

hashTable[i] = EMPTY;

}
printf("Hash Table Size is: %d \n", TABLE_SIZE);

printf("Enter the number of elements to insert (<= %d): ", TABLE_SIZE);

scanf("%d", &num);

printf("Enter %d elements:\n", num);

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

scanf("%d", &key);

insert(key);

display();

printf("Enter a key to search: ");

scanf("%d", &key);

int result = search(key);

if (result != -1)

printf("Key %d found at index %d\n", key, result);

else

printf("Key %d not found in the hash table\n", key);

return 0;

*********************Output***********************

Enter the number of elements to insert (<= 11 ): 7

Enter 7 elements:

25 46 10 36 18 29 43

Inserted 25 at index 3

Inserted 46 at index 2

Inserted 10 at index 10

Inserted 36 at index 4
Inserted 18 at index 7

Inserted 29 at index 8

Inserted 43 at index 0

Hash Table:

Index 0: 43

Index 1: EMPTY

Index 2: 46

Index 3: 25

Index 4: 36

Index 5: EMPTY

Index 6: EMPTY

Index 7: 18

Index 8: 29

Index 9: EMPTY

Index 10: 10

Enter a key to search: 18

Key 18 found at index 7

You might also like