Sie sind auf Seite 1von 93

INDEX

SNO. EXPERMENT NAME

1. implement the insertion sort algorithm


2. addition,subtraction,multiplication and division of elements of two
arrays
3. illustrate the operations on doubly linked lists
4. illustrate the implementation of circularly linked lists
5. the implementation of stacks using arrays
6. implementation of stacks using linked lists
7. implementation ofstack operations
8. implementation of queues using arrays
9. implementation of QUEUES using pointers
10. implementation of circular queues
11. check the given number is pallindrome or not
12. convert the given string lower to upper case
13. Addition,subtraction, multiplication of two matrix
14. Tree operations
15. Implementation of circular queues
16. Traversal of a binary tree
17. Implementation of linear search
18. Binary search
19. All operations for Binary search tree using pointers
20. Binary search by recursion
21. Bubble sort
22. Transpose of a Sparce matrix
23. Merge sort algorithm
24. Heap sort algorithm
25. Polynomial addition
26. Searching an element of a linked list
27. Sorting using linked list
28. Reverse a given linked list
29. Concatenate the given two linked lists
30. Warshall's algorithm
31. Creation of a tree
32. Heap sort using array
33. Hash search algorithm

_ _
ð_7___ミ 3____________________ __Š
/* Write a program in C implement the insertion sort algorithm*/

#include <stdio.h>
main()
{
int c,i,j,n,v[50];
printf("Program for insertion sort\n\n");
printf("Enter element(-0 to exit):");
scanf("%d",&n);
c=0;
while(n!=-0)
{
j=c-1;
while((n<v[j]&&(j>=0)))
{
v[j+1]=v[j];
-j;
}
v[j+1] = n;

printf("\n After insertion element %d",n);


for(i=0;i<=c; i++)
printf("%5d", v[i]);
printf("%n");
printf("Enter element(-0 to exit) :");
scanf("%d", &n);
++c;
}
printf("\n\t Final Sorted Array \n");
for(i=0;i<c;i++)
printf("%5d",v[i]);_ _
ð_7___ミ 3____________________ __Š printf("\n");
}
.pa_ _
°1____ミ 3____________________ __Š

OUTPUT:

Program for insertion sort

Enter element(-0 to exit):10

After insertion element 10 10


Enter element(-0 to exit) :20
After insertion element 20 10 20
Enter element(-0 to exit) :30
After insertion element 30 10 20 30
Enter element(-0 to exit) :40
After insertion element 40 10 20 30 40
Enter element(-0 to exit) :50
After insertion element 50 10 20 30 40 50
Enter element(-0 to exit) : -0

Final Sorted Array


10 20 30 40 50

.pa_ _
`_-___ミ 3____________________ __Š /*Write a program in C for
addition,subtraction,
_
_ ú_ú_ _
__multiplication and division of elements of two arrays*/

#include <stdio.h>
main()
{
int i, num, option;
float arr1[50], arr2[50], arr3[50];

printf("Program for operations on array \n\n");


printf("Enter size of array (1 to 50) :");
scanf("%d", &num);
printf("\nEnter %d element of 1st array\n", num);
for(i=0;i<num;i++)
scanf("%f", &arr1[i]);
printf("\nEnter %d element of 2st array\n", num);
for(i=0;i<num;i++)
scanf("%f", &arr2[i]);
do
{
printf("\n\t\tChoice Option");
printf("\n\t\t0 : Exit");
printf("\n\t\t1 : Summation");
printf("\n\t\t2 : Subraction");
printf("\n\t\t3 : Multiplication");
printf("\n\t\t4 : Division");
printf("\n\t\tEnter your option:");
scanf("%d", &option);
if((option >=1) && (option <=4))
{
if(option == 1)
{
for(i=0;i<num;i++)
arr3[i] = arr1[i] + arr2[i];
printf("\n\tSummation of two arrays");
}
if(option == 2)
{
for(i=0;i<num;i++)
arr3[i] = arr1[i] - arr2[i];
printf("\n\tSubtraction of two arrays");
}
_ _
ð_7___ミ 3____________________ __Š
if(option == 3)
{
for(i=0;i<num;i++)
arr3[i] = arr1[i] * arr2[i];
printf("Multiplication of two arrays");
}
if(option == 4)
{
for(i=0;i<num;i++)
arr3[i] = arr1[i] / arr2[i];
printf("\n\Division of two arrays");
}

printf("\n\t *** Original array 1 ***\n");


for(i=0;i<num;i++)
printf("%8.2f", arr1[i]);

printf("\n\t *** Original array 2 ***\n");


for(i=0;i<num;i++)
printf("%8.2f", arr2[i]);

printf("\n\t *** Result *** \n");


for(i=0;i<num;i++)
printf("%8.2f", arr3[i]);

}
}
while(option!=0);
}
.pa_ _
ミ_!___ミ 3____________________ __Š
OUTPUT:

Program for operations on array

Enter size of array (1 to 50) :5


Enter 5 element of 1st array
10
20
30
40
50
Enter 5 element of 2st array
11
22
33
44
55

Choice Option
0 : Exit
1 : Summation
2 : Subraction
3 : Multiplication
4 : Division
Enter your option:1

Summation of two arrays


*** Original array 1 ***
10.00 20.00 30.00 40.00 50.00
*** Original array 2 ***
11.00 22.00 33.00 44.00 55.00
*** Result ***
21.00 42.00 63.00 84.00 105.00

Choice Option
0 : Exit
1 : Summation
2 : Subraction
3 : Multiplication
4 : Division
Enter your option:3

Multiplication of two arrays


*** Original array 1 ***
10.00 20.00 30.00 40.00 50.00
*** Original array 2 ***
11.00 22.00 33.00 44.00 55.00
*** Result ***
110.00 440.00 990.00 1760.00 2750.00
.pa_ _
ð_7___ミ 3____________________ __Š
/* Write a program in C to illustrate the operations on
_
_ Â_Â_ _
__doubly linked lists,namely, create, insert, delete and display*10*/

#include <stdio.h>
#include <malloc.h>
struct node
{
int item;
struct node *right, *left;
};
void main()
{
struct node *root, *last;
int new_item, posn, quit;
char c;
struct node *create();
void insert();
void delete();
void display();

printf("Program for doubly linked list\n");


root = NULL;
quit = 0;

do
{
printf("\n\tOptions\t\tChoice");
printf("\n\tCreate\t\tC");
printf("\n\tInsert\t\tI");
printf("\n\tDelete\t\tD");
printf("\n\tExit\t\tE");
printf("\n\tEnter choice:");
do
c=getchar();
while (strchr("CcIiDdEe",c) == NULL);
switch(c)
{
case 'c':
case 'C':
root = create(&last);
printf("*** Doubly Linked list ***\n");
display(root,last);
break;
case 'i':
case 'I':
printf("\n Enter the element to be
insertd :");
scanf("%d", &new_item);
do
{
printf("\nEnter the position of
insertion:");
scanf("%d",&posn);
}_ _
ð_7___ミ 3____________________ __Š while(posn < 1);
insert(&root, &last,

new_item, posn);
printf("*** Doubly linked list ***\n");
display(root, last);
break;
case 'd':
case 'D':
delete(&root, &last);
printf("*** Doubly Linked list ***\n");
display(root, last);
break;
case 'e':
case 'E':
quit = 1;

}
}
while(!quit);
printf("\n");
}

struct node *create(last)


struct node **last;
{
struct node *root, *new;
int temp, size;
size = sizeof(struct node);
printf("Enter an integer (-9999 to end):");
scanf("%d", &temp);
*last = root = NULL;
while (temp != -9999)
{
new = (struct node *)malloc(size);
new->item = temp;
new->right = NULL;
if(root == NULL)
{
new->left = NULL;
root = new;
}
else
{
new->left = (*last);
(*last)->right = new;
}
(*last) = new;
printf("Enter an integer (-9999 to end) :");
scanf("%d", &temp);
}
if(root != NULL)
(*last) = new;
return root;
}
_ _
ð_7___ミ 3____________________ __Š void display (start,last)
struct node *start, *last;
{
printf("\n *** Forward direction ***");
while(start != NULL)
{
printf("%d->", start->item);
start = start->right;
}
printf("\n *** Backward direction ***");
while(last != NULL)
{
printf("%d ->", last->item);
last = last->left;
}
return;
}

void insert(start, last, new_item, posn)


struct node **start, **last;
int new_item, posn;
{
struct node *new_node, *temp;
int i;
if((posn == 1) || ((*start) == NULL))
{
new_node = (struct node *)malloc(sizeof(struct node));
new_node->item = new_item;
new_node->right = *start;
new_node->left = NULL;
if((*start)!= NULL)
(*start)->left = new_node;
else
(*last) = new_node;
*start = new_node;
}
else
{
temp=*start;
i=2;
while((i<posn) && (temp->right != NULL))
{
temp = temp->right;
i++;
}
new_node = (struct node *)malloc(sizeof(struct node));
new_node->item = new_item;
new_node->right = temp->right;
if(temp->right != NULL)
temp->right->left = new_node;
new_node->left = temp;
temp->right = new_node;
}

_ _
ð_7___ミ 3____________________ __Š if(new_node->right == NULL)
*last=new_node;
}

void delete (start, last)


struct node **start, **last;
{
struct node *temp, *prec;
int i,element;

printf("\n Element to be deleted:");


scanf("%d", &element);

if(*start != NULL)
{
if((*start)->item == element)
{
if((*start)->right == NULL)
*start=*last=NULL;
else
{
*start = (*start)->right;
(*start)->left = NULL;
}
}
else
{
temp=*start;
prec=NULL;
while((temp->right != NULL) &&
(temp->item != element))
{
prec=temp;
temp=temp->right;
}
if(temp->item != element)
printf("Element not found\n");
else
{
if(temp == *last)
*last=prec;
else
temp->right->left = temp->left;
prec->right = temp->right;
}
}
}
else
printf("\n Empty list ...\n");
return;
}

_ _
ð_7___ミ 3____________________ __Š OUTPUT:

Program for doubly linked list

Options Choice
Create C
Insert I
Delete D
Exit E
Enter choice:C

Enter an integer (-9999 to end):10


Enter an integer (-9999 to end) :20
Enter an integer (-9999 to end) :30
Enter an integer (-9999 to end) :-9999
*** Doubly Linked list ***

*** Forward direction ***10->20->30->


*** Backward direction ***30 ->20 ->10 ->

Options Choice
Create C
Insert I
Delete D
Exit E
Enter choice:I

Enter the element to be insertd :40


Enter the position of insertion:3

*** Doubly linked list ***

*** Forward direction ***10->20->40->30->


*** Backward direction ***30 ->40 ->20 ->10 ->

Options Choice
Create C
Insert I
Delete D
Exit E
Enter choice:D

Element to be deleted:20

*** Doubly Linked list ***

*** Forward direction ***10->40->30->


*** Backward direction ***30 ->40 ->10 ->

.pa_ _
°_3___ミ 3____________________ __Š
/*Write a program in C to illustrate the implementation of
_
_ *_*_ _
__circularly linked lists showing the function create() creates a
_
_ *_*_ _
__circularly linked list and the functions display(),insert() and
_
_ __ _
__delete() have the usual meanings.*/

#include <stdio.h>
#include <malloc.h>
struct node
{
int item;
struct node *next;
};
struct node *create(last)
struct node **last;

{
struct node *root, *new;
int temp, size;
size = sizeof(struct node);
printf("Enter an integer (-9999 to end) :");
scanf("%d",&temp);
*last = root = NULL;
while(temp != -9999)
{
new = (struct node *) malloc(size);
new->item = temp;
if(root == NULL)
root = new;
else
(*last)->next = new;

(*last) = new;
printf("Enter an integer (-9999 to end) :");
scanf("%d", &temp);
}
if(root != NULL)
new->next = root;
return root;
}

void display (start, last)


struct node *start, *last;

{
printf("\nRoot->");
if(start != NULL)
{
do
{
printf("%d->",start->item);
start = start->next;
}
_ _
ð_7___ミ 3____________________ __Š
while(last->next != start);
printf("Root\n");
}
else
printf("NULL\n");
}

void insert (start, last, new_item, posn)


struct node **start, **last;
int new_item, posn;
{
struct node *new_node, *temp;
int i;
new_node = (struct node *)malloc(sizeof(struct node));
new_node->item = new_item;
if((posn == 1) || ((*start) == NULL))
{
new_node->next = *start;
*start = new_node;
if((*last) != NULL)
(*last)->next = *start;
else
*last = *start;
}
else
{
temp=*start;
i=2;
while((i<posn) && (temp->next != (*start)))
{
temp = temp->next;
++i;
}
if(temp->next == (*start))
*last = new_node;
new_node->next = temp->next;
temp->next = new_node;
}
}

int delete (start, last, posn)


struct node **start, **last;
int posn;
{
struct node *temp;
int i, return_value = 1;

if(*start != NULL)
if(posn == 1)
{
if((*start)->next != *start)
{
*start = (*start)->next;
(*last)->next = *start;_ _
ð_7___ミ 3____________________ __Š
}
else
*start = *last = NULL;
}
else
{
temp = *start;
i=2;
while((temp->next != (*start)) && (i<posn))
{
temp = temp->next;
++i;
}
if(temp->next != *start)
{
if(temp->next == *last)
*last = temp;
temp->next = temp->next->next;
}
else
return_value = 0;
}
else
return_value = 0;
return return_value;
}

void main()
{
struct node *root, *last;
int new_item, posn, quit;
char c;

printf("Program for singly circularly linked list\n");


root = NULL;
quit = 0;
do
{
printf("\n\tOptions\t\tChoice");
printf("\n\tCreate\t\tC");
printf("\n\tInsert\t\tI");
printf("\n\tDelete\t\tD");
printf("\n\tExit\t\tE");
printf("\n\tEnte choice :");
do
c=getchar();
while(strchr("cCiIDeE", c) == NULL);
switch(c)
{
case 'c':
case 'C':
root = create(&last);
printf("*** Single Linked list ***\n");
_ _
ð_7___ミ 3____________________ __Š
display(root, last);
break;
case 'i':
case 'I':
printf("\nEnter the element to be
inserted :");
scanf("%d", &new_item);
do
{
printf("\nEnter the position of
insertion :");
scanf("%d", &posn);
}
while (posn <1);
insert(&root, &last, new_item, posn);
printf("*** Single linked list ***\n");
display (root, last);
break;
case 'd':
case 'D':
do
{
printf("Enter position of deletion :");
scanf("%d", &posn);
}
while(posn <1);
if(!delete(&root, &last, posn))
printf("Cannot delete element at
position %d\n",posn);
else
{
printf("*** Singled linked list *** ");
display(root, last);
}
break;
case 'e':
case 'E':
quit = 1;
}
}
while(!quit);
printf("\n");
}

_ _
ð_7___ミ 3____________________ __Š
OUTPUT:

Program for singly circularly linked list

Options Choice
Create C
Insert I
Delete D
Exit E
Ente choice :C
Enter an integer (-9999 to end) :10
Enter an integer (-9999 to end) :20
Enter an integer (-9999 to end) :-9999

*** Single Linked list ***

Root->10->20->Root
Options Choice
Create C
Insert I
Delete D
Exit E
Ente choice :I

Enter the element to be inserted :30

Enter the position of insertion :1

*** Single linked list ***

Root->30->10->20->Root

Options Choice
Create C
Insert I
Delete D
Exit E
Ente choice :D
Enter he position of deletion :2

*** Singled linked list ***

Root->30->20->Root

.pa_ _
p_/___ミ 3____________________ __Š

/*Write a program in C forthe implementation of stacks


_
_ d_d_ _
__using arrays*/

#include <stdio.h>
int first, check;

void main()
{
int stack[10], element, quit;
char c;
int pop();
void push();
void display();

printf("Program of Stack with array\n");


first = 1;
quit = 0;
do
{
printf("\n\tOptions\t\tChoice");
printf("\n\tPush\t\tP");
printf("\n\tPop\t\tO");
printf("\n\tExit\t\tE");
do
c=getchar();
while(strchr("PpOoEe",c) == NULL);
switch(c)
{
case 'P':
case 'p':
printf("\n Enter an element to be
pushed:");
scanf("%d",&element);
push(stack,element);
if(check)
{
printf("\n\t*** Stack ***\n");
display(stack);
if(first == 9)
printf("\n Stack overflow\n");
}

_ _
ð_7___ミ 3____________________ __Š

else
printf("\n Stack overflow...
don't push\n");
break;

case 'O':
case 'o':
element = pop(stack);
if(check)
{
printf("Popped element = %dn",element);
printf("\n\t *** Stack ****\n");
display(stack);
}
else
printf("Stack underflow...don't pop");
break;
case 'E':
case 'e':
quit = 1;
}
}
while(!quit);
printf("\n");
}

void push (stack, element)


int stack[], element;
{
if(first == 9)
check=0;
else
{
check=1;
++first;
stack[first]=element;
}
}

_ _
ð_7___ミ 3____________________ __Š

void display(stack)
int stack[];
{
int i;
if(first == -1)
printf("\n **** Empty ****\n");
else
{
for(i=first;i>=0;--i)
printf("%7d",stack[i]);
}
printf("\n");
}
int pop(stack)
int stack[];
{
int srs;
if(first == -1)
{
srs = 0;
check = 0;
}

else
{
check = 1;
srs = stack[first];
--first;
}
return srs;
}
.pa_ _
_"___ミ 3____________________ __Š OUTPUT:

Options Choice
Push P
Pop O
Exit E P

Enter an element to be pushed:10

*** Stack ***


10

Options Choice
Push P
Pop O
Exit EP

Enter an element to be pushed:20

*** Stack ***


20 10

Options Choice
Push P
Pop O
Exit EO

Popped element = 20

*** Stack ****


10

.pa_ _
ミ_!___ミ 3____________________ __Š
/*Write a program in C for implementation of stacks using
_
_ d_d_ _
__linked lists*/

#include <stdio.h>
#include <malloc.h>

struct node
{
int element;
struct node *next;
};

void main()
{
struct node *start;
int quit;
char c;
struct node *push();
struct node *pop();
void display();
printf("Program of stack with linked list\n");
start= NULL;
quit=0;

do
{
printf("\n\tOptions\t\tChoice");
printf("\n\tPush\t\tP");
printf("\n\tPop\t\tO");
printf("\n\tExit\t\tE");
printf("\n\tEnter choice:");
do
c=getchar();
while(strchr("PpOoEe",c) == NULL);
switch(c)
{
case 'P':
case 'p':
start=push(start);
printf("\n\t *** Stack ***\n");
display(start);
break;
case 'O':
case 'o':
start=pop(start);
printf("\n\t *** Stack ***\n");
display(start);
break;
case 'E':
case 'e':
quit = 1;
break;
}
}_ _
ð_7___ミ 3____________________ __Š
while(!quit);
printf("\n");
}

void display (record)


struct node *record;
{
printf("\n Root");
while(record != NULL)
{
printf("-> %d",record->element);
record = record->next;
}
printf("->NULL\n");
return;
}

struct node *push(first)


struct node *first;
{
struct node *new_node;
int new_element;
printf("Enter an element to be pushed :");
scanf("%d", &new_element);

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


new_node->element = new_element;
new_node->next = first;
first = new_node;
return(first);

struct node * pop(first)


struct node *first;

{
struct node *temp;
int element;

if(first == NULL)
printf("\n *** Empty ***\n");
else
{
printf("Popped element = %d", first->element);
temp = first->next;
free(first);
first=temp;
if(first == NULL)
printf("\n *** Empty ***\n");
}
return(first);
}
_ _
ð_7___ミ 3____________________ __Š
OUTPUT:

Program of stack with linked list

Options Choice
Push P
Pop O
Exit E
Enter choice:P
Enter an element to be pushed :10

*** Stack ***

Root-> 10->NULL
Options Choice
Push P
Pop O
Exit E
Enter choice:P
Enter an element to be pushed :20

*** Stack ***

Root-> 20-> 10->NULL

Options Choice
Push P
Pop O
Exit E
Enter choice:O

Popped element = 20

*** Stack ***

Root-> 10->NULL

.PA_ _
__(___ミ 3____________________ __Š
/* Write a program in C for implementation ofstack operations*/

#include <stdio.h>
#define MAX 50

enum boolean
{
true = 1, false = 0, negone = -1
};

enum boolean push (stack, prs, s)


int *prs;
char stack[MAX], s;
{
if (*prs ==0)
return false;
else
{
*prs = *prs - 1;
stack[*prs]=s;
}
return true;
}
enum boolean pop(stack,prs, s)
int *prs;
char stack[MAX], *s;
{
if(*prs == MAX)
return false;
else
{
*s = stack[*prs];
*prs = *prs + 1;
}
return true;
}
enum boolean peep(stack, prs, s, ch)
int prs, ch;
char stack[MAX], *s;
{
int si;

if(prs == MAX)
return false;
si = prs + ch;
if(si >= MAX || si < prs)
return negone;
*s = stack[si];
return true;
}

_ _
ð_7___ミ 3____________________ __Š
enum boolean change (stack, prs, s, ch)
int prs, ch;
char stack[MAX], s;
{
int si;

if(prs == MAX)
return false;
si = prs + ch;
if(si >= MAX || si <prs)
return negone;
stack[si] = s;
return true;
}

void diprslay(stack, prs)


int prs;
char stack[MAX];
{
int i;
for(i=0;i<prs;i++)
printf(" ");
for(i=prs;i<MAX;i++)
printf("%c", stack[i]);
if(i!=80)
printf("\n");
if(prs == MAX)
printf("\t *** Empty ***\n");
else
{
for(i=0;i<prs;i++)
printf(" ");
printf("^");
if (i!= 79)
printf("\n");
}
}
void main()
{
int prs = MAX, ch, quit;
char stack[MAX], s, c;
enum boolean flag;

printf("Program for stack operations\n");


quit = 0;

do
{
printf("\n\tOptions\t\tChoice");
printf("\n\tPush\t\tU");
printf("\n\tPop\t\tO");
printf("\n\tPeep\t\tP");
printf("\n\tEmpty\t\tM");
_ _
ð_7___ミ 3____________________ __Š
printf("\n\tChange\t\tC");
printf("\n\tExit\t\tE");
printf("\n\tEnter choice: ");
do
c=getchar();
while(strchr("UuOoPpMmCcEe",c) == NULL);
fflush (stdin);
switch(c)
{
case 'U':
case 'u':
printf("\n Enter an element to be
pushed:");
s=getc(stdin);
if(push(stack,&prs,s) == false)
printf("Stack Full\n");
diprslay(stack,prs);
break;
case 'O':
case 'o':
if(pop(stack, &prs, &s) == true)
printf("Popped element =%c\n",s);
else
printf("\n\t *** Empty ***\n");
diprslay(stack,prs);
break;
case 'P':
case 'p':
printf("\n Enter an index into stack:");
scanf("%d",&ch);
flag = peep(stack, prs, &s, ch);
if(flag == false)
printf("\n\t *** Empty *** \n");
else
if(flag == negone)
printf("Index not exist\n");
else
printf("Element at %d = %c\n",
ch,s);
diprslay(stack, prs);
break;
case 'M':
case 'm':
printf("Do you want empty stack(Y/N)?");
s=getc(stdin);
if (s == 'Y' || s == 'y')
{
prs = MAX;
printf("\n\t *** Empty ***\n");
}
diprslay(stack,prs);
break;

_ _
ð_7___ミ 3____________________ __Š

case 'C':
case 'c':
printf("\nEnter the index into stack:");
scanf ("%d",&ch);
fflush (stdin);
printf("\nEnter the value to be put:");
s=getc(stdin);
flag = change(stack, prs, s, ch);
if(flag == false)
printf("\n\t *** Empty ***\n");
else
if(flag == negone)
printf("Index not exist\n");
diprslay(stack, prs);
break;
case 'E':
case 'e':
quit = 1;
}
}
while (!quit);
printf("\n");
}
_ _
ð_7___ミ 3____________________ __Š

OUTPUT:

Program for stack operations

Options Choice
Push U
Pop O
Peep P
Empty M
Change C
Exit E
Enter choice: U

Enter an element to be pushed:10

Options Choice
Push U
Pop O
Peep P
Empty M
Change C
Exit E
Enter choice: U

Enter an element to be pushed:20


Options Choice
Push U
Pop O
Peep P
Empty M
Change C
Exit E
Enter choice: M

Do you want empty stack (Y/N) ?:Y

*** Empty ***


.pa_ _
_2___ミ 3____________________ __Š
/*Write a program in C for implementation of queues
_
_ d_d_ _
__using arrays*/

#include <stdio.h>
int first, last, check, count = 0;
void main()
{
int queue[10], element, quit;
char c;
void insert();
void delete();
void display();
printf("Program of queue with array\n");
last = first = quit = 0;
do
{
check = 1;
printf("\n\tOptions\t\tChoice");
printf("\n\tInsert\t\tI");
printf("\n\tDelete\t\tD");
printf("\n\tExit\t\tE");
printf("\n\tEnter choice :");
do
c = getchar ();
while(strchr("IiDdEe",c) == NULL);
switch(c)
{
case 'I':
case 'i':
printf("Enter an element to be inserted");
scanf("%d", &element);
insert(queue,element);
if(!check)
{
printf("*** Queue ***");
display(queue);
}
break;
case 'D':
case 'd':
delete(queue);
if((!check) && (count))
{
printf("*** Queue ***");
display(queue);
}
break;
case 'E':
case 'e':
quit = 1;
}
}
while(!quit);
printf("\n");_ _
ð_7___ミ 3____________________ __Š
void insert(queue, element)
int queue[10], element;
{
if(last<10)
{
last++;
queue[last] = element;
if(!first)
first = 1;
check = 0;
count++;
}
else
{
printf("Queue overflow ... don't insert\n");
check = 1;
}
return;
}
void display(queue)
int queue[10];
{
int c;
for(c=first;c<last+1;c++)
printf("%6d", queue[c]);
printf("\n");
return;
}
void delete(queue)
int queue[10];
{
int element;
if(first)
{
element = queue[first];
check = 0;
count--;
printf("Element deleted = %d\n", element);
if(first == last)
{
first = 0;
last = 0;
printf("\n\t *** Empty *** \n");
}
else
first++;
}
else
{
printf("Queue underflow ... don't delete\n");
check = 1;
}
return;
}_ _
ð_7___ミ 3____________________ __Š

OUTPUT:

Program of queue with array

Options Choice
Insert I
Delete D
Exit E
Enter choice :I

Enter an element to be inserte10

*** Queue ***


10

Options Choice
Insert I
Delete D
Exit E
Enter choice :I

Enter an element to be inserte20

Insert I
Delete D
Exit E
Enter choice :I

Enter an element to be inserte20

*** Queue ***


10 20

Options Choice
Insert I
Delete D
Exit E
Enter choice :D

Element deleted = 10
*** Queue ***
20
.PA_ _
` .___ミ 3____________________ __Š
/*Write a program in C for implementation of QUEUES
_
_ °_°_ _
__using pointers*/

#include <stdio.h>
#include <malloc.h>

struct node
{
int element;
struct node *next;
};

void main()
{
struct node *first = NULL, *last = NULL;
int quit,new_element;
char c;
void insert();
void delete();
void display();

printf("Program of queue with linked list\n");


quit = 0;
do
{
printf("\n\tOptions\t\tChoice");
printf("\n\tInsert\t\tI");
printf("\n\tDelete\t\tD");
printf("\n\tExit\t\tE");
printf("\n\tEnter choice :");
do
c = getchar ();
while(strchr("IiDdEe",c) == NULL);
switch(c)
{
case 'I':
case 'i':
printf("\n Enter an element to be
inserted");
scanf("%d", &new_element);
insert(&first, &last, new_element);
printf("\n\t *** Queue ***\n");
display(first);
break;
case 'D':
case 'd':
delete(&first);
printf("\n\t *** Queue *** \n");
display(first);
break;
case 'E':
case 'e':
quit = 1;
}_ _
ð_7___ミ 3____________________ __Š
}
while(!quit);
printf("\n");
}

void insert(first,last,i)
struct node **first, **last;
int i;
{
struct node *new_node;
new_node = (struct node *)malloc(sizeof(struct node));
new_node->element = i;
new_node->next = NULL;

if((*first) == NULL)
{
(*first) = new_node;
(*last) = new_node;
}
else
{
(*last)->next = new_node;
(*last) = new_node;
}
return;
}

void display(record)
struct node *record;
{
printf("\n Root");
while(record != NULL)
{
printf("-> %d", record->element);
record = record->next;
}

printf("->NULL\n");
return;
}
void delete(first)
struct node **first;
{
struct node *temp;

if((*first)!=NULL)
{
temp = *first;
(*first) = (*first)->next;
free(temp);
}
return;
}_ _
ð_7___ミ 3____________________ __Š
OUTPUT:

Program of queue with linked list

Options Choice
Insert I
Delete D
Exit E
Enter choice :I

Enter an element to be inserted10

*** Queue ***

Root-> 10->NULL

Options Choice
Insert I
Delete D
Exit E
Enter choice :I

Enter an element to be inserted20

*** Queue ***

Root-> 10-> 20->NULL

Options Choice
Insert I
Delete D
Exit E
Enter choice :D

*** Queue ***

Root-> 20->NULL

.PA_ _
__(___ミ 3____________________ __Š

/*Write a program in C for implementation of circular queues*/

#include <stdio.h>
#include <malloc.h>

struct node
{
int element;
struct node *next;
};

void main()
{
struct node *first = NULL, *last = NULL;
int quit,new_element;
char c;
void insert();
void delete();
void display();

printf("Program of circular queue\n");


quit = 0;
do
{
printf("\n\tOptions\t\tChoice");
printf("\n\tInsert\t\tI");
printf("\n\tDelete\t\tD");
printf("\n\tExit\t\tE");
printf("\n\tEnter choice :");
do
c = getchar ();
while(strchr("IiDdEe",c) == NULL);
switch(c)
{
case 'I':
case 'i':
printf("\n Enter an element to be
inserted");
scanf("%d", &new_element);
insert(&first, &last, new_element);
printf("\n\t *** Queue ***\n");
display(first);
break;

_ _
ð_7___ミ 3____________________ __Š

case 'D':
case 'd':
delete(&first,&last);
printf("\n\t *** Queue *** \n");
display(first);
break;
case 'E':
case 'e':
quit = 1;
}
}

while(!quit);
printf("\n");
}

void insert(first,last,i)
struct node **first, **last;
int i;
{
struct node *new_node;
new_node = (struct node *)malloc(sizeof(struct node));
new_node->element = i;

if((*first) == NULL)
{
(*first) = new_node;
(*last) = new_node;
}
else
{
(*last)->next = new_node;
(*last) = new_node;
}
new_node->next = *first;
return;
}

void display(record)
struct node *record;
{
struct node *temp;
temp = record;
printf("\n Root");
if(record != NULL)
{

_ _
ð_7___ミ 3____________________ __Š

do
{
printf("-> %d", record->element);
record = (record->next);
}
while(record!= temp);
printf("->Root\n");
}
else
printf("NULL\n");
return;
}

void delete(first,last)
struct node **first, **last;
{
struct node *temp;
if((*first)!= (*last))

{
temp = *first;
(*first) = (*first)->next;
(*last)->next = *first;
free(temp);
}
else
{
if((*first) != NULL)
free(*first);
(*first) = NULL;
(*last) = NULL;
}
return;
}

.pa_ _
0
+___ミ 3____________________ __Š OUTPUT:

Program of circular queue

Options Choice
Insert I
Delete D
Exit E
Enter choice :I

Enter an element to be inserted10

*** Queue ***

Root-> 10->Root

Options Choice
Insert I
Delete D
Exit E
Enter choice :I

Enter an element to be inserted20

*** Queue ***

Root-> 10-> 20->Root

Options Choice
Insert I
Delete D
Exit E
Enter choice :D

*** Queue ***

Root-> 20->Root

.pa_ _
ð_'___ミ 3____________________ __Š /*Write a program in C to test whether a given
word is
_
_ ¢_¢_ _
__palindrome or not*/

main()
{
int i, j;
char word[10];
printf("Enter a word to test for palindrome feature-->");
scanf("%s",word);
i = strlen(word);
i --;
j = 0;
while (j<= i)
{ if (word[i] != word[j])
{
printf("\nThe given word %s is not a palindrome \n", word);
goto ending;
}
i --;
j ++;
}
printf("\nThe given word %s is a palindrome \n", word);
ending : printf("\n\n");
}
.pa_ _
______ミ 3____________________ __Š OUTPUT:

Enter a word to test for palindrome feature-->HELLO

The given word HELLO is not a palindrome

.pa_ _
ð-____ミ 3____________________ __Š
_
_ „_„_ _
__/Dsp19- Writ a program in C to convert a given string of small
_
_ j
j
_
__letters into capital letters*/

#include<stdio.h>
main()
{
char str[20],I;
int i,k;
printf("Enter a word-->");
scanf("%s",str);
k=strlen(str);
printf("Before conversion the string is %s \n", str);
for (i=0;i<k;i++)
{
if(str[i]>='a'&&str[i]<='z')
str[i]=str[i]-32;
}
printf("After conversion the string is %s \n", str);
}
.pa_ _
Ð ____ミ 3____________________ __Š OUTPUT:

Enter a word-->hello
Before conversion the string is hello
After conversion the string is HELLO
.pa_ _
à.____ミ 3____________________ __Š
/*Write a program in C for addition,subtraction and
_
_ _
_
_
__multiplication of two matrices*/

# include <stdio.h>
main()
{
int i, j, k, l, m, n, o, option;
int mat1[50][50], mat2[50][50], mat3[50][50];

printf("Program for operations on matrix \n\n");


printf("\n Enter row and column of 1st matrix (1 to 50, 1 to 50) :");
scanf("%d %d", &k, &l);

printf("\nEnter %d element of 1st Matrix\n", k*l);


for(i=0;i<k;i++)
{
for(j=0;j<l;j++)
scanf("%d", &mat1[i][j]);
}
printf("\n Enter row and column of 2nd matrix (1 to 50, 1 to 50) :");
scanf("%d %d", &m, &n);
printf("\nEnter %d element of 2nd matrix \n", m*n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d", &mat2[i][j]);
}
do
{
printf("\n\t\tChoice Option");
printf("\n\t\t0 : Exit");
printf("\n\t\t1 : Summation");
printf("\n\t\t2 : Subraction");
printf("\n\t\t3 : Multiplication");
printf("\n\t\tEnter your option:");
scanf("%d", &option);
if((option >=1) && (option <=4))
{
if(option == 1)
{
if((k == m) && (l == n))
{
for(i=0;i<k;i++)
{
for(j=0;j<l;j++)
mat3[i][j] = mat1[i][j]+mat2[i][j];
}
}
else
printf("\nSummation not possible\n");
}
if(option == 2)
{
_ _
ð_7___ミ 3____________________ __Š if((k == m) && (l == n))_ _
{
for(i=0;i<k;i++)
{
for(j=0;j<l;j++)
mat3[i][j]=mat1[i][j]-mat2[i][j];
}
}
else
printf("\nSubstraction not possible\n");
}
if(option == 3)
{
if(l == m)
{
for(i=0;i<k;i++)
{
for(j=0;j<l;j++)
{
mat3[i][j] = 0;
for(o=0;o<n;o++)
mat3[i][j] = mat1[i][j] * mat2[i][j];
}
}
}
else
printf("\nMultiplication not possible\n");
}
}
printf("\n\t *** Matrix 1 ***\n");
for(i=0;i<k;i++)
{
for(j=0;j<l;j++)
printf("%d", mat1[i][j]);
printf("\n");
}
printf("\n\t *** Matrix 2 ***\n");
for(i=0;i<k;i++)
{
for(j=0;j<l;j++)
printf("%d", mat2[i][j]);
printf("\n");
}

printf("\n\t *** Result *** \n");


for(i=0;i<k;i++)
{
for(j=0;j<l;j++)
printf("%d", mat3[i][j]);
printf("\n");
}

}
while(option!=0);
}_ _
ð_7___ミ 3____________________ __Š
OUTPUT:
Program for operations on matrix
Enter row and column of 1st matrix (1 to 50, 1 to 50) :2
2
Enter 4 element of 1st Matrix
1
2
3
4

Enter row and column of 2nd matrix (1 to 50, 1 to 50) :2


2
Enter 4 element of 2nd matrix
1
0
0
1
Choice Option
0 : Exit
1 : Summation
2 : Subraction
3 : Multiplication
Enter your option:1

*** Matrix 1 ***


1 2
3 4
*** Matrix 2 ***
1 0
0 1
*** Result ***
2 2
3 5

Choice Option
0 : Exit
1 : Summation
2 : Subraction
3 : Multiplication
Enter your option:3
*** Matrix 1 ***
1 2
3 4
*** Matrix 2 ***
1 0
0 1
*** Result ***
1 2
3 4
.PA_ _
À_4___ミ 3____________________ __Š
_
_ T_T_
__/*Dsp21-Write a program in C for operation on trees*/

#include <stdio.h>
#include <malloc.h>
typedef struct tree
{
int item;
struct tree *left, *right;
}tree;

tree *insert (item,p)


int item;
tree *p;

{
if(!p)
{
p=(tree*)malloc(sizeof(tree));
p->item=item;
p->left=NULL;
p->right=NULL;
return(p);
}
if(item < p->item)
p->left = insert(item, p->left);
else
if(item > p->item)
p->right = insert(item, p->right);
return(p);
}

void output(temp, level)


tree *temp;
int level;
{
int i;

if(temp)
{
output(temp->right, level+1);
printf("\n");
for(i=0;i<level;i++)
printf(" ");
printf("%d",temp->item);

output(temp->left, level+1);
}
}

_ _
ð_7___ミ 3____________________ __Š

void depthbr(temp, depth, level)


tree *temp;
int *depth, level;
{
if(temp)
{
if(level > *depth)
*depth = level;
depthbr(temp->left, depth, level+1);
depthbr(temp->right, depth, level+1);
}
}

tree *repl(s,p)
tree *s, *p;
{
tree *data;

if(s->right != NULL)
s->right = repl(s->right, p);
else
{
data = s;
p->item = s->item;
s=s->left;
free(data);
}
return(s);
}

tree *del(p,item)
tree *p;
int item;
{
tree *s;
if(!p)
{
printf(p);
}
else
{
if(item < p->item)
p->left = del (p->left, item);
else
if(item > p->item)
p->right = del(p->right, item);

_ _
ð_7___ミ 3____________________ __Š

else
{
s=p;
if(s->right == NULL)
{
p= s->left;
free(s);
}

}
if(s->left == NULL)
{
p=s->right;
free(s);
}
else
s->left = repl(s->left,s);
}
return(p);
}
void main()
{
int item,depth,quit=0 ;
char c;
tree *temp = NULL;
printf("Program for operations of tree \n");
do
{
printf("\n\tOptions\t\t\tChoice");
printf("\n\tInsert\t\tI");
printf("\n\tDelete\t\tD");
printf("\n\tExit\t\tE");
printf("\n\tEnter choice :");

do
c=getchar();
while(strchr("IiDdEe",c) == NULL);

_ _
ð_7___ミ 3____________________ __Š

switch(c)
{
case 'I':
case 'i':
printf("Enter element :");
scanf("%d", &item);
temp = insert (item, temp);
printf("\n\t *** Tree *** \n");
output(temp,1);
depth = 0;
depthbr(temp, &depth, 0);
printf("\nDepth = %d\n", depth);
break;
case 'D':
case 'd':
printf("Enter element :");
scanf("%d", &item);
temp = del(temp, item);
printf("\n\t *** Tree ***\n");
output(temp,1);
depth = 0;
depthbr(temp, &depth, 0);
printf("\nDepth = %d\n", depth);
break;

case 'E':
case 'e':
quit = 1;
}
}
while(!quit);
printf("\n");
}

.pa_ _
0
+___ミ 3____________________ __Š OUTPUT:

Program for operations of tree

Options Choice
Insert I
Delete D
Exit E
Enter choice :I
Enter element :10

*** Tree ***


10
Depth = 0

Options Choice
Insert I
Delete D
Exit E
Enter choice :I
Enter element :20

*** Tree ***


20
10
Depth = 1

Options Choice
Insert I
Delete D
Exit E
Enter choice :D
Enter element :10

*** Tree ***


20
Depth = 0
.PA_ _
à_&___ミ 3____________________ __Š
_
_ „_„_ _
__/*Dsp22-Write a program in C for creation and traversalas of a
_
_ d_d_ _
__binary tree */

#include <stdio.h>
#include <malloc.h>

typedef struct tree


{
int item;
struct tree *left, *right;
}tree;

tree *binarytree(vector, first, last)


int *vector, first, last;
{
tree *temp;
int mid;

mid = (int) (first + last)/2;


if(temp!=(tree*)malloc(sizeof(tree)))
{
printf("No memory : Error");
return(temp);
}

temp->item = vector[mid];
if(first>=last)
{
temp->left = NULL;
temp->right = NULL;
return(temp);
}

if(first <= mid - 1)


temp->left = binarytree(vector, first, mid-1);
else
temp->left = NULL;
if(mid + 1 <= last)
temp->right = binarytree(vector, mid+1, last);
else
temp->right = NULL;
return(temp);

}
void preorder(temp)
tree *temp;
{
if(temp)
{
printf("%d", temp->item);
preorder(temp->left);
preorder(temp->right);
}
}_ _
ð_7___ミ 3____________________ __Š
void inorder(temp)
tree *temp;
{
if(temp)
{
inorder(temp->left);
printf("%d", temp->item);
inorder(temp->right);
}
}

void postorder(temp)
tree *temp;
{
if(temp)
{
postorder(temp->left);
postorder(temp->right);
printf("%",temp->item);
}
}
void triangle(temp, branch)
tree *temp;
int branch;
{
int i;
if(temp)
{
triangle(temp->right, branch+1);
printf("\n");
for(i=0;i<branch;i++)
printf(" ");

printf("%d",temp->item);
triangle(temp->left, branch+1);
}
}

void diagonal(temp, branch)


tree *temp;
int branch;
{
int i;
if(temp)
{
printf("\n");
for(i=0;i<branch;i++)
printf(" ");
printf("%d", temp->item);
diagonal(temp->left, branch+1);
diagonal(temp->right, branch+1);
}
}
_ _
ð_7___ミ 3____________________ __Š

void main()
{
int v1[50],i,j,temp,total,quit=0;
char c;
tree *bintree;
printf("Program for tree creation and traversal of binary
tree \n");
printf("Enter number of elements in vector :");
scanf("%d", &total);
for(i=0;i<total;i++)
{
printf("\n Enter element %d :",i);
scanf("%d", &v1[i]);
}

for(i=0;i<total;i++)
{
for(j=i+1;j<total;j++)
{
if(v1[i] > v1[j])
{
temp = v1[i];
v1[i] = v1[j];
v1[j] = temp;
}
}
}
printf("Data in ascending order\n");
for(i=0;i<total;i++)
{
printf("%5d", v1[i]);
}

printf("\n");

bintree = binarytree(v1,0,total-1);

do
{
printf("\n\t Options\t\t\tChoice");
printf("\n\t Triangular\t\t T");
printf("\n\t Diagonal\t\t D");
printf("\n\t Exit \t\t\t E");
printf("\n\t Enter choice :");

_ _
ð_7___ミ 3____________________ __Š

do
c=getchar();
while(strchr("TtDdEe",c) == NULL);
switch(c)
{
case 'T':
case 't':
printf("\n\t *** Binary Tree ***\n");
triangle(bintree,1);

printf("\n Preorder traversal :");


preorder(bintree);
printf("\n Inorder traversal :");
inorder(bintree);
printf("\n Postorder traversal :");
postorder(bintree);
break;

case 'D':
case 'd':
printf("\n\t *** Binary Tree ***\n");
diagonal(bintree, 1);
printf("\n Preorder traversal :");
preorder(bintree);
printf("\n Inorder traversal :");
inorder(bintree);
printf("\n Postorder traversal :");
postorder(bintree);
break;

case 'E':
case 'e':
quit = 1;
}
}
while(!quit);
printf("\n");
}

_ _
ð_7___ミ 3____________________ __Š

OUTPUT:

Program for tree creation and traversal of binary tree


Enter number of elements in vector :3

Enter element 0 :10

Enter element 1 :20

Enter element 2 :30


Data in ascending order
10 20 30
.pa_ _
#____ミ 3____________________ __Š

/*Write a program in C to illustrate the implementation of the


_
_ F_F_ _
__sequential search algorithm of an array of elements for a
_
_ V_V_ _
__given key value*/

#include <stdio.h>
main()
{
int i,j,n,v[50],yn=1;

printf("Program for linear search\n\n");


printf("Enter size of the array:");
scanf("%d", &n);

printf("\nEnter %d elements\n",n);
for(i=0;i<n;i++)
scanf("%d", &v[i]);

printf("Enter element to be searched :");


scanf("%d", &j);

printf("\n\tOriginal array \n");


for(i=0;i<n;i++)
printf("%5d", v[i]);
printf("\n");

for(i=0;i<n;i++)
{
if(v[i] == j)
{
printf("\nElement %d is at location %d\n",j,i+1);
yn=0;
break;
}
}
if(yn)
printf("\nElement %d not found in array\n",j);
}

.pa_ _
p_/___ミ 3____________________ __Š

OUTPUT:
Program for linear search
Enter size of the array:5
Enter 5 elements
10
20
30
40
50
Enter element to be searched :30
Original array
10 20 30 40 50

Element 30 is at location 3

.pa_ _
°"____ミ 3____________________ __Š

/*Dsp25 Write a program in C to illustrate the binary


_
_ ü_ü_ _
__search algorithm*/
# include <stdio.h>
main()
{
int i,j,n,v[50], yn=1, first, mid, last;
printf("Program for binary search\n\n");
printf("Enter size of the array :");
scanf("%d", &n);
printf("\n Enter %d element\n",n);
for(i=0;i<n;i++)
scanf("%d", &v[i]);
printf("Enter element to be searched :");
scanf("%d", &j);
printf("\n\t Original array \n");
for(i=0;i<n;i++)
printf("%5d", v[i]);
printf("\n");
first = 0;
last = n-1;
while(first <= last)
{
mid = (first + last)/2;
if(j<v[mid])
last = mid - 1;
else
if(j>v[mid])
first = mid + 1;
else
if(j==v[mid])
{
printf("\n Element %d is at location %d\n", j, mid+1);
yn=0;
break;
}
if(yn)
printf("\nElement %d not found in array\n", j);
}
}

.pa_ _
P
-___ミ 3____________________ __Š OUTPUT:

Program for binary search


Enter size of the array :5
Enter 5 element
10
20
30
40
50
Enter element to be searched :50
Original array
10 20 30 40 50
Element 50 is at location 5

.pa_ _
€%____ミ 3____________________ __Š

/*Dsp23A-Write a program in C to illustrate the all the concepts


_
_ 8_8_ _
__including searching for an element in a binary search tree*/

#include <stdio.h>
#include <malloc.h>

struct tree
{
int item;
struct tree *left, *right;
};

struct tree *node;


int ele, tele;

void depth(node)
struct tree *node;
{
if(node->left == NULL)
if(ele<tele)
ele=tele;
else ;
else
{
tele++;
depth(node->right);
}
tele--;
}

void preorder(node)
struct tree *node;
{
if(node != NULL)
{
printf("%i", node->item);
preorder(node->left);
preorder(node->right);
}
}

void inorder(node)
struct tree *node;
{
if(node != NULL)
{
inorder(node->left);
printf("%i", node->item);
inorder(node->right);
}
}
_ _
ð_7___ミ 3____________________ __Š

void postorder(node)
struct tree *node;
{
if(node != NULL)
{
postorder(node->left);
postorder(node->right);
printf("%i", node->item);
}
}

void disp()
{
printf("\nPreorder traversal :");
preorder(node);
printf("\nInorder traversal :");
inorder(node);
printf("\nPostorder traversal :");
postorder(node);
printf("\n");
}

void ins()
{
struct tree *temp, *prev;
int n,c,check;

do
{
printf("Enter number of elements :");
scanf("%i", &n);
fflush(stdin);
}
while(n<1);
do
{
printf("Enter elements :");
scanf("%i", &c);
fflush(stdin);
temp=node;
check=0;

if(temp == NULL)
{
node=(struct tree*)malloc(sizeof(struct tree));
temp=node;
}
else
{
while(temp!=NULL && !check)
{
prev = temp;_ _
ð_7___ミ 3____________________ __Š

if(c == temp->item)
{
printf("%i is in the tree",c);
printf("\n");
check=1;
}
else
{
if(c < temp->item)
temp=temp->left;
else
temp=temp->right;
}
}
if(!check)
{
temp = (struct tree*)malloc(sizeof(struct tree));
if(c<prev->item)
prev->left = temp;
else
prev->right = temp;
}
}
if(!check)
{
temp->item = c;
temp->left = NULL;
temp->right = NULL;
n--;
}
}
while(n > 0);
}

void search(c, srs,prs)


int c;
struct tree **srs, **prs;
{
*srs = NULL;
*prs = node;

while(*prs != NULL && (*prs)->item != c)


{
*srs = *prs;
if( c< (*prs)->item)
(*prs) = (*prs)->left;
else
(*prs) = (*prs)->right;
}
}

_ _
ð_7___ミ 3____________________ __Š

void delet0(prs, srs)


struct tree *prs, *srs;
{
struct tree *temp;

if(prs->left == NULL)
temp=prs->right;
else
temp=prs->left;
if(srs != NULL)
if(prs == srs->left)
srs->left=temp;
else
srs->right=temp;
else
node = temp;
}

void delet1(prs, srs)


struct tree *prs, *srs;
{
struct tree *suc, *srssuc;
suc = prs->right;
srssuc = prs;
while(suc->left != NULL)
{
srssuc = suc;
suc = suc->left;
}
delet0(suc, srssuc);
if(srs != NULL)
if(prs == srs->left)
srs->left = suc;
else
srs->right = suc;
else
node=suc;
suc->left = prs->left;
suc->right = prs->right;
}

void del()
{
struct tree *prs, *srs;
int n, c;
do
{
printf("Number of element (for deletion) :");
scanf("%i", &n);
fflush(stdin);
}
while(n < 1);_ _
ð_7___ミ 3____________________ __Š

do
{
printf("Enter the element :");
scanf("%i", &c);
fflush(stdin);
search(c, &srs, &prs);
if(prs == NULL)
printf("Element not found\n");
else
if(prs->left != NULL && prs->right != NULL)
delet1(prs,srs);
else
delet0(prs, srs);
n--;
}
while(n>0);
}
void main()
{
int quit=0;
char c;
node=NULL;
printf("\nProgram for operations of binary tree\n");
do
{
printf("\n\tOptions\t\t\tChoice");
printf("\n\tInsert\t\tI");
printf("\n\tDelete\t\tD");
printf("\n\tDisplay\t\tO");
printf("\n\tExit\t\tE");
printf("\n\tEnter choice :");
do
c=getchar();
while(strchr("IiDdOoEe",c) == NULL);
switch(c)
{
case 'I':
case 'i':
ins();
break;

_ _
ð_7___ミ 3____________________ __Š

case 'D':
case 'd':
del();
break;
case 'O':
case 'o':
disp();
if(node == NULL)
printf("Tree empty\n");
else
{
ele=0;
tele=0;
depth(node);
printf("Depth of tree = %i\n", ele);
}
break;
case 'E':
case 'e':
quit=1;
}
}
while(!quit);
printf("\n");
}

.pa_ _
€_ ___ミ 3____________________ __Š OUTPUT:

Program for operations of binary tree


Options Choice
Insert I
Delete D
Display O
Exit E
Enter choice :I
Enter number of elements :3
Enter elements :10
Enter elements :20
Enter elements :30

Options Choice
Insert I
Delete D
Display O
Exit E
Enter choice :O

Preorder traversal :102030


Inorder traversal :102030
Postorder traversal :302010
Depth of tree = 0
.PA_ _
0_____ミ 3____________________ __Š

_
_ 8_8_ _
__/*Dsp-26 Write a program in C to implement the Binary search
_
_ d_d_ _
__by recursion*/

#include <stdio.h>
void main()
{
int v1[50],i,j,number,pos,search,temp,mid;
void binary_search();
printf("\nBinary search by recursion\n");
printf("Enter number of elements of array (1 to 50):");
scanf("%d", &number);
printf("Enter %d element\n",number);
for(i=0;i<number;i++)
scanf("%d", &v1[i]);
printf("\n");

for(i=0;i<number-1;i++)
for(j=i+1;j<number;j++)
if(v1[i] >v1[j])
{
temp = v1[i];
v1[i] = v1[j];
v1[j] = temp;
}
printf("Enter element to be searched :");
scanf("%d", &search);

printf(" *** Original array in ascending order ***\n");


for(i=0;i<number;i++)
printf("%7d", v1[i]);
printf("\n");

pos=0;
i=1;
binary_search(v1,i,number,pos,search);
}

void binary_search(v1,min,max,pos,search)
int v1[50],min,max,pos,search;

{
int mid;
if(min >max)
pos = 0;

_ _
ð_7___ミ 3____________________ __Š

else
{
mid = (min + max)/2;
if(search<v1[mid])
{
max = mid - 1;
binary_search(v1,min,max,pos,search);
}
else

if(search>v1[mid])
{
min = mid + 1;
binary_search(v1,min,max,pos,search);
}
else
pos=mid;
if(pos>0)
printf("Your element %d is at position no %d\n",
search,pos+1);
}
}
.pa_ _
`_-___ミ 3____________________ __Š
OUTPUT:

Binary search by recursion


Enter number of elements of array (1 to 50):4
Enter 4 element
10
20
30
40

Enter element to be searched :10


*** Original array in ascending order ***
10 20 30 40
Your element 10 is at position no 1
.pa_ _
€%____ミ 3____________________ __Š
_
_
_
_ _
__/*Dsp29-Write a program in C to implement the Bubble sort algorithm*/

#include<stdio.h>
main()
{
int i, j, n, v[50], temp, last, yn=1;
printf("Program for bubble sort\n\n");
printf("Enter size of the array :");
scanf("%d", &n);
printf("\nEnter %d elements\n",n);
for(i=0; i<n; i++)
scanf("%d", &v[i]);
printf("\n\t original array \n");
for(i=0;i<n;i++)
printf("5d", v[i]);
printf("\n");
last=n-1;
for(i=0;i<n-1;i++)
{
for (j=0;j<last-i;j++)
{
if(v[j]>v[j+1])
{
temp = v[j];
v[j]=v[j+1];
v[j+1]=temp;
yn=0;
}
}
}
if(!yn)
yn=1;

/* printf("\n sorted array\n");*/


for (i=0;i<n;i++)
printf("%5d",v[i]);
printf("\n");
}
.pa_ _
__(___ミ 3____________________ __Š OUTPUT:

Program for bubble sort


Enter size of the array :5
Enter 5 elements
10
20
30
3
5
original array
10 20 30 3 5

3 5 10 20 30
.pa_ _
p&____ミ 3____________________ __Š
/*Write a program in C to give the transpose of a sparse matrix*/

#include <stdio.h>
int m,n,count=0;
main()
{
int a[10][10], i, j, b[10][10], c[10][10];
clrscr();
printf("Enter m and n\n");
scanf("%d %d", &m, &n);
printf("Enter the matrix\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
if(a[i][j] != 0)
count++;
}
if(count<m*n-count)
printf("The matrix entred is a sparse matrix\n");
else
{
printf("The matrix is not sparse\n");
exit(1);
}
sparse(a,b);
print(b,count+1,3);
transpose(b,c);
printf("the transpose of matrix is \n");
print(c,3,count+1);
printf("\n\n\n");
getch();
}

sparse(int a[][10], int b[][10])


{
int r[10], c[10], v[10], i, j, k=0, l=0, p=0;
v[k]=count;
r[k]=m;
c[k]=n;
k=1;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j] != 0)
{
v[k] = a[i][j];
r[k] = i+1;
c[k] = j+1;
k++;
}
}
}
_ _
ð_7___ミ 3____________________ __Š
k=0;l=0;m=0;
for(i=0;i<count+1;i++)
{
for(j=0;j<3;j++)
{
if(j==0)
{
b[i][j] = r[k];
k++;
}
else
if(j==1)
{
b[i][j] = c[l];
l++;
}
else
{
b[i][j] = v[p];
p++;
}
}
}
}
print(int b[][10], int x, int y)
{
int i, j;
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
printf("%5d", b[i][j]);
printf("\n");
}
}

transpose(int b[][10], int c[][10])


{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<count+1;j++)
{
c[i][j] = b[j][i];
}
}
}
.pa_ _
€_0___ミ 3____________________ __Š
OUTPUT:
Enter m and n
3
3
Enter the matrix
The matrix is not sparse
.pa_ _
_-____ミ 3____________________ __Š
/*Dsp31-Write a program in C to implement the Merge sort algorithm*/
# include <stdio.h>
main()
{
int i,j,k,l,m,n,v1[50],v2[50],v3[50];
void sort_vect();
printf("Program for merging without duplicate elements\n\n");
printf("Enter size of the array 1:");
scanf("%d", &n);

printf("\nEnter %d element \n",n);


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

sort_vect (v1, n-1);

printf("Enter size of the vector 2:");


scanf("%d", &m);

printf("\nEnter %d element \n",n);


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

sort_vect(v2,m-1);

printf("\n Sorted Vector 1\n");


for(i=0;i<n;i++)
printf("%5d", v1[i]);
printf("\n");

printf("\n Sorted Vector 2\n");


for(i=0;i<m;i++)
printf("%5d", v2[i]);
printf("\n");

i=j=k=0;

while((i<n) && (j<m))


{
if(v1[i] < v2[j])
{
v3[k]=v1[i];
i++;
k++;
}
else
if(v1[i] > v2[i])
{
v3[k] = v2[j];
j++;
k++;
}
_ _
ð_7___ミ 3____________________ __Š
else
{
v3[k] = v1[i];
i++;
j++;
k++;
}
}
if(i<n)
{
for(l=i;l<n;l++)
{
v3[k] = v1[i];
i++;
k++;
}
}
else
if(j<m)
{
for(l=j;l<m;l++)
{
v3[k] = v2[j];
j++;
k++;
}
}

printf("\n\tMerged array (without %d dupilicates)\n", (m+n)-k);


for(i=0;i<k;i++)
printf("%5d", v3[i]);
printf("\n");
return (0);
}
void sort_vect(sv,x)
int sv[50],x;
{
int i, flag, temp;
flag = 0;
while(flag == 0)
{
flag = 1;
for(i=0;i<x;i++)
{
if(sv[i] > sv[i+1])
{
temp = sv[i];
sv[i] = sv[i+1];
sv[i+1] = temp;
flag = 0;
}
}
}
}_ _
ð_7___ミ 3____________________ __Š
OUTPUT:

Program for merging without duplicate elements


Enter size of the array 1:4

Enter 4 element
10 20 30 40
Enter size of the vector 2:3

Enter 4 element
1 2 5 22

Sorted Vector 1
10 20 30 40

Sorted Vector 2
1 2 5

Merged array (without 0 dupilicates)


1 2 5 10 20 22 30 40

.PA_ _
_-____ミ 3____________________ __Š
_
_ Â_Â_ _
__/*Dsp32-Write a program in C to implement the Heap sort algorithm*/

#include <stdio.h>
main()
{
int v[50],i,n;
void crheap();
void sort();

printf("Program for heap sort\n");


printf("Enter size of the array :");
scanf("%d",&n);

printf("\nEnter %d elements\n",n);
for(i=1;i<=n;++i)
scanf("%d", &v[i]);

/*printf("\n\t *** Original Array ***\n");*/


for(i=1;i<=n;i++);
printf("%6d", v[i]);
printf("\n");

sort(v,n);

printf("\n\t *** Sorted array ***\n");


for(i=1;i<=n;i++)
printf("%6d",v[i]);
printf("\n");
return;
}

void crheap(v,n)
int v[50],n;
{
int i,j,key,q;
for(q=2;q<=n;++q)
{
i=q;
key=v[q];
j=(int)(i/2);

while((i>1) && (key >v[j]))


{
v[i]=v[j];
i=j;
j=(int)(i/2);
if(j<1);
j=1;
}
v[i] = key;
}
return;
}
_ _
ð_7___ミ 3____________________ __Š
void sort(v,n)
int v[50],n;
{
int q,temp,i,j,key;
crheap(v,n);
for(q=n;q>=2;--q)
{
temp = v[1];
v[1] = v[q];
v[q] = temp;
i=1;
key = v[1];
j=2;

if((j+1)<q)
if(v[j+1]>v[j])
j=j+1;
while((j<(q-1)) && (v[j] > key))
{
v[i] = v[j];
i=j;
j=2*i;
if((j+1)<q)
if(v[j+1]>v[j])
j=j+1;
else
if(j>n)
j=n;
}
v[i] = key;
}
return;
}
.pa_ _
°_#___ミ 3____________________ __Š OUTPUT:
Program for heap sort
Enter size of the array :5

Enter 5 elements
10 30 3 4 2
*** Sorted array ***
2 3 4 10 30
.pa_ _
_, ___ミ 3____________________ __Š
/*Dsp4-Write a program in C for implementation ofPolynomial addition*/

#include <stdio.h>
struct list
{
int coef, exp;
/* COFFICIENT AND EXPONENT OF POLYNOMIAL FUNCTION*/
struct list *next;
}*first, *second, *new, *ptr, *prev, *p, *sum, *pt;

main()
{
clrscr();
first=second=new=ptr=prev=p=sum=pt=NULL;
printf("\n Enter the values of first function:");
polyfirst();
printf("\n Enter the values of second function:");
polysecond();
printf("The Sum of first & second poly function:");
polysum();
}
polyfirst()
{
char ch='y';
while(toupper(ch) == 'Y')
{
new=(struct list *)malloc(sizeof(struct list));
printf("\n Enter the Coeff & Expo of variables of
first function");
scanf("%d%d", &new->coef, &new->exp);
new->next=NULL;
if(first == NULL)
{
first=new;
ptr=new;
}
else
{
ptr->next=new;
ptr=new;
}
printf("\n Do U want to add a variables in first
functino ");
fflush(stdin);
ch=getchar();
}
}

_ _
ð_7___ミ 3____________________ __Š
polysecond()
{
char ch ='y';
while(toupper(ch) == 'Y')
{
new=(struct list *)malloc(sizeof(struct list));
printf("\n Enter The Coeff & Expo of Variables of
second function");
scanf("%d %d", &new->coef, &new->exp);
new->next= NULL;
if(second == NULL)
{
second = new;
prev=new;
}
else
{
prev->next=new;
prev=new;
}
printf("\n Do U want to continue add variables in
second function");
fflush(stdin);
ch=getchar();
}
}
polysum()
{
new=(struct list *) malloc(sizeof(struct list));
for(ptr=first, prev=second;(ptr!=NULL) && (prev!=NULL);)
{
if(ptr->exp == prev->exp)
{
new->exp=ptr->exp;
new->coef=ptr->coef+prev->coef;
ptr=ptr->next;
goto a;
}
if(ptr->exp > prev->exp)
{
new->exp=ptr->exp;
new->coef=ptr->coef;
ptr=ptr->next;
goto a;
}
if(ptr->exp < prev->exp)
{
new->exp=prev->exp;
new->coef=prev->coef;
ptr=ptr->next;
goto a;
}
a:;
_ _
ð_7___ミ 3____________________ __Š
new->next = NULL;
if(sum==NULL)
{
sum=new;
pt=new;
}
else
if(sum!=NULL)
{
pt->next=new;
pt=new;
}
new=(struct list *)malloc(sizeof(struct list));
}
if((ptr==NULL) && (prev != NULL))
{
new->exp=prev->exp;
new->coef=prev->coef;
prev=prev->next;
goto a;
}

if((ptr!=NULL) && (prev==NULL))


{
new->exp=prev->exp;
new->coef=prev->coef;
ptr=ptr->next;
goto a;
}
for(p=sum;p;p=p->next)
{
printf("%d %d",p->coef, p->exp);
}
getch();
}

.pa_ _
ð_'___ミ 3____________________ __Š OUTPUT:

Enter the values of first function:


Enter the Coeff & Expo of variables of first function1
1
Do U want to add a variables in first functino Y
Enter the Coeff & Expo of variables of first function1
0
Do U want to add a variables in first functino Y
Enter the Coeff & Expo of variables of first function1
1
Do U want to add a variables in first functino N
Enter the values of second function:
Enter The Coeff & Expo of Variables of second function1
2
Do U want to continue add variables in second functionN
The Sum of first & second poly function:1 21 21 21 2

.PA_ _
°"____ミ 3____________________ __Š
/*Write a program in C for searching an element of a linked list*/

#include <stdio.h>
#define NULL 0
struct list_element
{
int item;
struct list_element *next;
};
typedef struct list_element node;

main()
{
node *start;
int key,choice;
node *create();
void display();
node *lookup();
node *locate(node*,int);

printf("Program for searching an element of a linked list\n");


start=create(start);
printf("\n\t *** Linked List ***\n");
display(start);
start=lookup(start);
return;
}
node*create(first)
node*first;
{
node *temp,*prev;
int info;
prev=first=NULL;
printf("\n Enter the integer (-999 to exit):");
scanf("%d", &info);
while(info != -9999)
{
temp = (node *)malloc(sizeof(node));
temp->item = info;
temp->next = NULL;
if(first == NULL)
first = temp;
else
prev->next = temp;
prev = temp;
printf("\n Enter the integer (-9999 to exit):");
scanf("%d",&info);
}
return first;
}

_ _
ð_7___ミ 3____________________ __Š
void display (first)
node *first;
{
printf("\nRoot ->");
while(first!= NULL)
{
printf("%d->",first->item);
first=first->next;
}
printf("NULL\n\n");
}

node *locate(record, target)


node *record;
int target;
{
static pos = 1;
if(record->item==target)
{
printf("\n Element at position %d\n",pos);
return(record);
}
else
if(record->item==-9999)
return(NULL);
else
{
pos++;
locate(record->next, target);
}
return;
}

node *lookup(first)
node *first;
{
node *locate();
node *tag, *temp;
int target,newitem, opt;

tag = first;
printf("Element to be searched ? ");
scanf("%d", &target);

tag = locate(first, target);


if(tag==NULL)
{
printf("\n Element not found\n");
return(first);
}
return(first);
}
_ _
ð_7___ミ 3____________________ __Š

OUTPUT:

Program for searching an element of a linked list

Enter the integer (-999 to exit):10

Enter the integer (-9999 to exit):20

Enter the integer (-9999 to exit):-9999

*** Linked List ***

Root ->10->20->NULL

Element to be searched ? 20

Element at position 2

.PA_ _
Ð ____ミ 3____________________ __Š
_
_ š
š
_
__/* program sorts a given linked list */

#define NULL 0
struct element_list
{
int info;
struct element_list *next;
};
typedef struct element_list node;

main()
{
node *start, *list, *pass;
int key, choice,temp;
node *create();
void display();

printf("Program for sorting of linked list\n");


start = create(start);

printf("\n\t *** Linked list ***\n");


display (start);
list = start;

for(list;list->next != NULL;list=list->next)
{
for(pass=list->next; pass !=NULL; pass=pass->next)
if(list->info > pass->info)
{
temp = list->info;
list->info = pass->info;
pass->info = temp;
}
}

printf("\n\t *** Sorted Linked list ***\n");


display(start);
return;
}

node *create(first)
node *first;

{
node *temp, *prev;
int element;
prev = first = NULL;
printf("\n Enter the integer (-9999 to exit) :");
scanf("%d", &element);

_ _
ð_7___ミ 3____________________ __Š

while(element != -9999)
{
temp = (node*)malloc(sizeof(node));
temp->info = element;
temp->next = NULL;
if(first == NULL)
first = temp;
else
prev->next = temp;

printf("\n Enter the integer (-9999 to exit) :");


scanf("%d", &element);
}
return first;
}

void display(first)
node *first;
{
printf("\nRoot->");
while(first != NULL)
{
printf("%d->", first->info);
first = first->next;
}
printf("NULL\n\n");
}

.pa_ _
à_&___ミ 3____________________ __Š OUTPUT:

Program for sorting of linked list

Enter the integer (-9999 to exit) :10

Enter the integer (-9999 to exit) :20

Enter the integer (-9999 to exit) :5

Enter the integer (-9999 to exit) :2

Enter the integer (-9999 to exit) :-9999

*** Linked list ***

Root->10->20->5->2->NULL

*** Sorted Linked list ***

Root->2->5->10->20->NULL

.PA_ _
_-____ミ 3____________________ __Š
_
_ *_*_ _
__/*Dsp8-Write a program in C that reverses a given linked list*/

#include<stdio.h>
#include<malloc.h>
#define NULL 0

struct element_list
{
int info;
struct element_list *next;
};
typedef struct element_list node;

main()
{
node *first, *prev, *current, *save;
int key, choice;
node *create();
void display();
printf("Program for reversing of linked list\n");
first=create(first);
printf("\n\t *** Linked list ***\n");
display (first);
printf("\n");
current = first;
prev = NULL;
while(current != NULL)
{
save = current->next;
current->next = prev;
prev = current;
current = save;
}
first = prev;
printf("\n\t *** Reversed Linked list ***\n");
display(first);
printf("\n");
}
node *create(record)
node *record;

{
node *temp, *prev;
int element;
prev = NULL;
record = NULL;
printf("\n Enter the integer (-9999 to exit) :");
scanf("%d", &element);

_ _
ð_7___ミ 3____________________ __Š

while(element != -9999)
{
temp = (node*)malloc(sizeof(node));
temp->info = element;

temp->next = NULL;
if(prev == NULL)
record = temp;
else
prev->next = temp;
prev=temp;
printf("\n Enter the integer (-9999 to exit) :");
scanf("%d", &element);
}
return (record);
}
void display(record)
node *record;
{
printf("\nRoot->");
while(record != NULL)
{
printf("%d->", record->info);
record = record->next;
}
printf("NULL\n\n");
return;
}

.pa_ _
ð_'___ミ 3____________________ __Š
OUTPUT:

Program for reversing of linked list

Enter the integer (-9999 to exit) :10

Enter the integer (-9999 to exit) :20

Enter the integer (-9999 to exit) :30

Enter the integer (-9999 to exit) :-9999

*** Linked list ***

Root->10->20->30->NULL

*** Reversed Linked list ***

Root->30->20->10->NULL
.PA_ _
Ð ____ミ 3____________________ __Š
_
_ 8_8_ _
__/*DSlab-9 Write a program in C to concatenate the two given
_
_ x_x_ _
__linked lists into one list*/

#include <stdio.h>
#include <malloc.h>
#define NULL 0

struct element_list
{
int item;
struct element_list *next;
};
typedef struct element_list node;

main()
{
node *start, *one, *two, *three;
int key, choice;
void create();
void display();
void concat();
printf("Program ot concatenate two list\n");
printf("First list\n");
one = (node *)malloc(sizeof(node));
create(one);
printf("Second list\n");
two = (node *)malloc(sizeof(node));
create(two);
printf("\n\t *** Linked list 1 ***\n");
display(one);
printf("\n\t *** Linked list 2 ***\n");
display(two);
three = (node *)malloc(sizeof(node));
concat(one,two,three);
printf("\n\t *** Concatented list ***");
display(three);
printf("\n");
}

void create(record)
node *record;

{
printf("Enter the integer (-9999 to exit) :");
scanf("%d", &record->item);
if(record->item == -9999)
record->next = NULL;
else
{
record->next = (node *)malloc(sizeof(node));
create(record->next);
}
return;
}_ _
ð_7___ミ 3____________________ __Š
void display(record)
node *record;
{
if(record->next!= NULL)
{
printf("%d->", record->item);
display(record->next);
}
return;
}
void concat(one, two, three)
node *one, *two, *three;
{
while(one->next!=NULL)
{
three->item = one->item;
one = one->next;
three->next=(node*)malloc(sizeof(node));
three=three->next;

}
while(two->next!= NULL)
{
three->item = two->item;
two= two->next;
three->next = (node *)malloc(sizeof(node));
three = three->next;
}
three->next = NULL;
return;
}
.pa_ _
ミ_!___ミ 3____________________ __Š OUTPUT:

Program ot concatenate two list


First list
Enter the integer (-9999 to exit) :10
Enter the integer (-9999 to exit) :20
Enter the integer (-9999 to exit) :30
Enter the integer (-9999 to exit) :-9999
Second list
Enter the integer (-9999 to exit) :40
Enter the integer (-9999 to exit) :50
Enter the integer (-9999 to exit) :60
Enter the integer (-9999 to exit) :-9999

*** Linked list 1 ***


10->20->30->
*** Linked list 2 ***
40->50->60->
*** Concatented list ***10->20->30->40->50->60->
.PA_ _
À!____ミ 3____________________ __Š
_
_ „_„_ _
__/*Dsp-23 Write a program in C for implementation of warshall's
_
_ r_r_ -
__algorithm*/

#include<stdio.h>
main()
{
int i, j, k, n, A[50][50],p[50][50],temp,last,yn=1;
printf("Program for transitive closure\n\n");
printf("Enter size of the graph :");
scanf("%d",&n);
printf("\nEnter %d elements\n",n);
for (i=0;i<n;i++)
for (j=0;j<n;j++)
scanf("%d",&A[i][j]);
printf("\n\t The adjecency matrix is \n");
for ( i=0; i<n; i++)
for (j=0; j<n; j++)
printf("5d", A[i][j]);
printf("\n");
for (i=0; i<n; i++)
for (j=0; j<n; j++)
p[i][j] = A[i][j];
for (k=0; k<n; k++)
for (i=0; i<n; i++)
for (j=0; j<n;j++)
p[i,j]==p[i,k]&&p[k,j];
printf("\n\t The transitive closure is\n");
for(k=0;k<n;j++);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
printf("%5d", A[i][j]);
printf("\n");
}

.pa_ _
Ð_%___ミ 3____________________ __Š OUTPUT:

Program for transitive closure


Enter size of the graph :2
Enter 4 elements
1
0
0
1
The adjecency matrix is
1 0
0 1
The transitive closure is
1 0
0 1

.PA_ _
ミ$____ミ 3____________________ __Š

_
_ ’_’_ _
__/* Dslab-20 Write a Program in C to concatenate the given
_
_ ¾_¾_ _
__two strings*/
#include<stdio.h>
main()
{
char s1[20], s2[20], s3[40];
int i,j,k;
printf("Enter the first string ==");
scanf("%s", s1);
printf("\nEnter the second string ==");
scanf("%s", s2);
i = strlen(s1);
j = strlen(s2);
for (k=0; k<i; k++)
s3[k] = s1[k];
for (k=0; k<j; k++)
s3[i+k] = s2[k];
s3[i+j] = '\0';
printf("\n\n Two strings are %s and %s \n new string is %s \n",
s1,s2,s3);
}

.pa_ _
P_____ミ 3____________________ __Š OUTPUT:

Enter the first string ==hello how are

Enter the second string == you

Two strings are hello how are and you


new string is hello how are you

.PA_ _
+
___ミ 3____________________ __Š

_
_ ’_’_ _
__/* Dsp33-Write a program in C for the creation of a tree*/

#include <stdio.h>
#include <malloc.h>

typedef struct tree


{
int item;
struct tree *left, *right;
} tree;

tree *insert (item,s)


int item;
tree *s;

{
if(!s)
{
s=(tree*)malloc(sizeof(tree));
s->item=item;
s->left=NULL;
s->right=NULL;
return(s);
}
if(item < s->item)
s->left = insert(item, s->left);
else
if(item > s->item)
s->right = insert(item, s->right);
return(s);
}

void display(ped, level)


tree *ped;
int level;
{
int i;

if(ped)
{
display(ped->right, level+1);
printf("\n");
for(i=0;i<level;i++)
printf(" ");
printf("%d",ped->item);

display(ped->left, level+1);
}
}

_ _
ð_7___ミ 3____________________ __Š

void main()
{
int item;
tree *ped = NULL;
printf("Program for tree creation\n");

while(1)
{
printf("\n Enter item (-9999 to exit) :");
scanf("%d", &item);

if(item == -9999)
break;
ped = insert(item, ped);
printf("\n\t *** Tree ***\n");
display(ped,1);
}
}
.pa_ _
ð-____ミ 3____________________ __Š OUTPUT:

Program for tree creation

Enter item (-9999 to exit) :10

*** Tree ***

10
Enter item (-9999 to exit) :20

*** Tree ***

20
10
Enter item (-9999 to exit) :30

*** Tree ***


30
20
10
Enter item (-9999 to exit) : -9999

.PA_ _
_-____ミ 3____________________ __Š
_
_
_
_ _
__/* Heap Sort */

#include<stdio.h>
#include <conio.h>
crheap(int k[20],int n)
{
int i,j,key,q;
for (q=2; q<=n; q++)
{
i=q;
key=k[q];
j=(int)(i/2);
while ( (i>1) && (key > k[j]) )
{
k[i]=k[j];
i=j;
j=(int)(i/2);
if (j < 1)
j=1;
}
k[i]=key;
}
}
heapsort(int k[20],int n)
{
int q,temp,i,j,key;
crheap(k,n);
for (q=n; q>=2; q--)
{
temp=k[1];
k[1]=k[q];
k[q]=temp;
i=1;
key=k[1];
j=2;
if ( (j+1) <q )
if ( k[j+1] > k[j])
j++;
while( (j<=(q-1) ) && (k[j] > key) )
{
k[i]=k[j];
i=j;
j=2*i;
if ( (j+1) < q)
if (k[j+1] > k[j])
j++;
else
if (j > n)
j=n;
k[i]=key;
}
}
}
_ _
ð_7___ミ 3____________________ __Š
main()
{
int k[20],i1,n;
printf("Enter the size of the vector to be sorted: ");
scanf("%d",&n);
printf("Input the given number: ");
for (i1=1; i1<=n; i1++)
scanf("%d",&k[i1]);
printf("\n");
heapsort(k,n);
printf("The srted vector is: \n");
for (i1=1; i1<=n; i1++)
printf("%4d",k[i1]);
getch();
}

OUTPUT:

Enter the size of the vector to be sorted: 5


Input the given number:
10
20
5
333
25
The srted vector is:
5
10
20
25
333

.PA_ _
_"___ミ 3____________________ __Š
_
_ H_H_ _
__/* Circular Queue */

#include<stdio.h>

struct cirqueue
{
int data;
struct cirqueue *link;
}*front=NULL,*rear=NULL,*temp;

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

void main()
{
int opt,n;

do
{
clrscr();
printf("\n\t\t##CIRCULAR QUEUE OPERATIONS##\n");
printf("\n\t\t1:INSERTION\n");
printf("\t\t2:DELETTION\n");
printf("\t\t3:ISEMPTY\n");
printf("\t\t4:DISPLAY\n");
printf("\t\t5:SIZE\n");
printf("\t\t6:EXIT\n");
printf("\n\t\tEnter your option:");
scanf("%d",&opt);
printf("\n\t\t");

switch(opt)
{
case 1:
printf("Enter element to insert:");
scanf("%d",&n);
insert(n);
break;
case 2:
if(!isempty())
printf("Deleted element is:%d",delete());
else
printf("Queue is empty");
break;
case 3:
if(isempty())
printf("Queue is empty");
else
printf("Queue is not empty");
break;
_ _
ð_7___ミ 3____________________ __Š
case 4:
if(!isempty())
display();
else
printf("Queue is empty");
break;

case 5:
printf("Size of the queue is:%d",size());
break;

case 6: exit(1);

otherwise:
printf("Invalid option");
}
getch();

}while(opt<7);
}

void insert(int m)
{
temp=(struct cirqueue*)malloc(sizeof(struct cirqueue));
temp->data=m;
temp->link=NULL;
if(front==NULL)
{
front=temp;
rear=temp;
}
else
{
rear->link=temp;
rear=temp;
}
rear->link=front;
return;
}
int delete()
{
int m;
temp=front;
if(front==rear)
front=rear=NULL;
else
{
front=front->link;
rear->link=front;
}
m=temp->data;
free(temp);
return(m);
}_ _
ð_7___ミ 3____________________ __Š
int isempty()
{
if(front==NULL)
return(1);
else
return(0);
}

void display()
{
int m;
temp=front;
printf("\n\t\tFront");
do
{
printf("<-%d",temp->data);
temp=temp->link;
}while(temp!=front);
printf("<-Rear");
return;
}

int size()
{
int m=0;
temp=front;
do
{
temp=temp->link;
m++;
}while(temp!=front);
return(m);
}

.pa_ _
À_$___ミ 3____________________ __Š
OUTPUT:

##CIRCULAR QUEUE OPERATIONS##

1:INSERTION
2:DELETTION
3:ISEMPTY
4:DISPLAY
5:SIZE
6:EXIT

Enter your option:1


Enter element to insert:10

##CIRCULAR QUEUE OPERATIONS##

1:INSERTION
2:DELETTION
3:ISEMPTY
4:DISPLAY
5:SIZE
6:EXIT

Enter your option:1

Enter element to insert:20

##CIRCULAR QUEUE OPERATIONS##

1:INSERTION
2:DELETTION
3:ISEMPTY
4:DISPLAY
5:SIZE
6:EXIT

Enter your option:4

Front<-10<-20<-Rear
.pa_ _
€_0___ミ 3____________________ __Š

/*Dsp27-Write a program in C to implement the Hash


_
_ H_H_ _
__searching algrithm*/

#define MAX_SIZE 50
#define TRUE 1
#define FALSE 0

#include<stdio.h>
main()
{
int i,j,n,v[50], yn=1, first, mid, last;
int p,f,size;
printf("Program for hash searching\n\n");
printf("Enter size of the array :");
scanf("%d", &n);
printf("\n Enter %d elements of the array\n",n);
for(i=0;i<n;i++)
scanf("%d", &v[i]);

printf("Enter element to be searched :");


scanf("%d", &j);

printf("\n\t Original array \n",j);


for(i=0;i<n;i++)
printf("%5d", v[i]);
printf("\n");
hashsearch(v,p,f,n,9999,j);
if(f)
printf("\n\t The element %d is found at %d\n",j,p);
else printf("\n\t Element %d is not found in the array \n",j);
}

hashsearch(table, position, found, tablesize, empty, key)


int position,tablesize,empty, key,found;
int table[MAX_SIZE];
{
int temp,start,active;
active= TRUE;
found=FALSE;
start=key% tablesize;
position=start;

_ _
ð_7___ミ 3____________________ __Š

if(table[start]==key)

{
active=FALSE;
found=TRUE;
temp=table[start];
}
else
{
temp=table[start];
table[start]=key;
}

while(active){
position=(position+1)%tablesize;
if (table[position]==key)
{
active=FALSE;
if(position!=start)found=TRUE;
}
else
if(table[position]==empty)active=FALSE;
}
table[start]=temp;
}

.pa_ _
€_ ___ミ 3____________________ __Š

OUTPUT:

Program for hash searching

Enter size of the array :5

Enter 5 elements of the array


10
20
30
40
50
Enter element to be searched :30

Original array
10 20 30 40 50

The element 30 is found at 3


______

Das könnte Ihnen auch gefallen