0% found this document useful (0 votes)
34 views59 pages

DSLAB 2022 Print

Uploaded by

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

DSLAB 2022 Print

Uploaded by

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

1

Ex.No:1a /* Array Implementation of Stack ADT */

#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
//clrscr();
top=-1;
printf("\n Enter the size of STACK[MAX=100]:");
scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t--------------------------------");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
do
{
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("\n\t EXIT POINT ");
2

break;
}
default:
{
printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}

}
}
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{
printf("\n\tSTACK is over flow");
}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The popped elements is %d",stack[top]);
top--;
}
}
void display()
{
3

if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
}
else
{
printf("\n The STACK is empty");
}
}

OUTPUT

Enter the size of STACK[MAX=100]:3

STACK OPERATIONS USING ARRAY


--------------------------------
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter the Choice:1
Enter a value to be pushed:3
Enter the Choice:1
Enter a value to be pushed:4
Enter the Choice:1
Enter a value to be pushed:2
Enter the Choice:3
The elements in STACK
2
4
3
Press Next Choice
Enter the Choice:2
The popped elements is 2
4

Ex. No: 1b /* Array Implementation of Queue ADT */

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

{
for(i=front; i<rear; i++)
{
printf("%d",queue[i]);
printf("\n");
}
break;
case 4:
exit(0);
default:
printf("Wrong Choice: please see the options");
}
}
}
return 0;
}

OUTPUT

Queue using Array


1.Insertion
2.Deletion
3.Display
4.Exit
Enter the Choice:1
Enter no 1:23
Enter the Choice:1
Enter no 2:21
Enter the Choice:1
Enter no 3:45
Enter the Choice:3
Queue Elements are:
23
21
45

Enter the Choice:2


Deleted Element is 23
6

Ex.No:1.c /* Implementation of Circular Queue*/

#include <stdio.h>

#define SIZE 5

int items[SIZE];
int front = -1, rear = -1;

// Check if the queue is full


int isFull() {
if ((front == rear + 1) || (front == 0 && rear == SIZE - 1)) return 1;
return 0;
}

// Check if the queue is empty


int isEmpty() {
if (front == -1) return 1;
return 0;
}

// Adding an element
void enQueue(int element) {
if (isFull())
printf("\n Queue is full!! \n");
else {
if (front == -1) front = 0;
rear = (rear + 1) % SIZE;
items[rear] = element;
printf("\n Inserted -> %d", element);
}
}
// Removing an element
int deQueue() {
int element;
if (isEmpty()) {
printf("\n Queue is empty !! \n");
return (-1);
} else {
element = items[front];
if (front == rear) {
front = -1;
rear = -1;
7

}
// Q has only one element, so we reset the
// queue after dequeing it. ?
else {
front = (front + 1) % SIZE;
}
printf("\n Deleted element -> %d \n", element);
return (element);
}
}

// Display the queue


void display() {
int i;
if (isEmpty())
printf(" \n Empty Queue\n");
else {
printf("\n Front -> %d ", front);
printf("\n Items -> ");
for (i = front; i != rear; i = (i + 1) % SIZE) {
printf("%d ", items[i]);
}
printf("%d ", items[i]);
printf("\n Rear -> %d \n", rear);
}
}

int main() {
// Fails because front = -1
deQueue();

enQueue(1);
enQueue(2);
enQueue(3);
enQueue(4);
enQueue(5);

// Fails to enqueue because front == 0 && rear == SIZE - 1


enQueue(6);

display();
deQueue();
8

display();
enQueue(7);
display();

// Fails to enqueue because front == rear + 1


enQueue(8);

return 0;
}

OUTPUT

Queue is empty !!

Inserted -> 1
Inserted -> 2
Inserted -> 3
Inserted -> 4
Inserted -> 5
Queue is full!!

Front -> 0
Items -> 1 2 3 4 5
Rear -> 4

Deleted element -> 1

Front -> 1
Items -> 2 3 4 5
Rear -> 4

Inserted -> 7
Front -> 1
Items -> 2 3 4 5 7
Rear -> 0

Queue is full!!
9

Ex.No:2 /*Singly Linked List*/


#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
int info;
struct node *link;
};
struct node *first;
void insend(int x)
{
struct node *new=(struct node*)malloc(sizeof(struct node));
struct node *save;
new->info=x;
new->link=NULL;
if(first==NULL)
{
first=new;
}
else
{
save=first;
while(save->link!=NULL)
save=save->link;
save->link=new;
}
}
void display()
{
struct node *temp=first;
if(first==NULL)
printf("no elements in linked list");
while(temp->link!=NULL)
{
printf("%d->",temp->info);
temp=temp->link;
}
printf("%d",temp->info);
}
10

void delend()
{
struct node *temp,*pred;
temp=first;
if(first->link==NULL)
{
printf("\n THE DELETED ELEMENT IS %d",first->info);
first=NULL;
}
else
{
while(temp->link!=NULL)
{
pred=temp;
temp=temp->link;
}
printf("\n THE DELETED ELEMENT IS %d",temp->info);
pred->link=NULL;
}
}
void main()
{
int item,pos,ch;
first=(struct node*) malloc(sizeof(struct node));
first=NULL;
do
{
printf("\n \n linked list");
printf("\n 1.insertion");
printf("\n 2.deletion");
printf("\n 3.display");
printf("\n 4.exit");
printf("\n enter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n enter the element to be inserted");
scanf("%d",&item);
insend(item);
11

break;
case 2:
if(first!=NULL)
delend();
else
printf("\n no elements in linked list");
break ;
case 3:
display();
break;
case 4:
printf("\n end of operation");
break;
default:
printf("\n enter only 1 to 4");
}
}
while(ch!=4);
getch();
}

OUTPUT

1. insertion
2. deletion
3. display
4. exit
enter your choice:1
enter the element to be inserted:23
1. insertion
2. deletion
3. display
4. exit
enter your choice:1
enter the element to be inserted:33
1. insertion
2. deletion
3. display
4. exit
enter your choice:1
12

enter the element to be inserted:44

1. insertion
2. deletion
3. display
4. exit
enter your choice:1
enter the element to be inserted:40
enter the position to insert:3

1. insertion
2. deletion
3. display
4. exit
enter your choice:2
the deleted element is :23

1. insertion
2. deletion
3. display
4. exit
enter your choice:2
the deleted element is :33

1. insertion
2. deletion
3. display
4. exit
enter your choice:3
40->44
13

Ex.No:3a /* Linked List Implementation Of Stack ADT */

#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *link;
};
typedef struct node NODE;
NODE *top;
void main()
{
int ch,item;
NODE *temp;
do
{
printf("\n1 PUSH");
printf("\n2 POP");
printf("\n3 DISPLAY");
printf("\n4 EXIT");
printf("\nEnter your choice:");
scanf("%d",&ch);
printf("%d",ch);
switch(ch)
{
case 1:
printf("\nEnter the item:");
scanf("%d",&item);
printf("%d",item);
if(top==NULL)
{
top =(NODE *)malloc(sizeof(NODE));
top->data=item;
top->link=NULL;
}
else
{
temp =(NODE*)malloc(sizeof(NODE));
temp->data=item;
14

temp->link=top;
top=temp;
}
break;
case 2:
if(top==NULL)
{
printf("\nN elements in stack");
}
else
{
printf("\nThe delete element is %d",top->data);
top=top->link;
}
break;
case 3:
if(top==NULL printf("\nN elements in stack");
else
{
temp=top;
printf("\nElements in stackº ");
while(temp->link!=NULL)
{
printf(¢ %ä ->",temp->data);
temp=temp->link;
}
printf( %d",temp->data);
}
break;
case 4:
printf("\nEnter the operation");
break;
default:
printf("\nEnter on list 4");
}
}while(ch!=4);
getch();
}
15

OUTPUT

1 PUSH
2 POP
3 DISPLAY
4 EXIT
Enter your choice:1
Enter the item:22
1 PUSH
2 POP
3 DISPLAY
4 EXIT
Enter your choice: 1
Enter the item: 33
1 PUSH
2 POP
3 DISPLAY
4 EXIT
Enter your choice: 1
Enter the item: 44
1 PUSH
2 POP
3 DISPLAY
4 EXIT
Enter your choice: 3
Elements in stack 44 33 22
1 PUSH
2 POP
3 DISPLAY
4 EXIT
Enter your choice: 2
The deleted elements is 44
1 PUSH
2 POP
3 DISPLAY
4 EXIT
Enter your choice: 3
Elements in stack 33 22
16

Ex.No:3b /* Linked List Implementation Of Queue ADT */

#include<stdio.h>
#include<conio.h>
strucô node
{
int data;
struct node *link;
};
typedef struct node NODE;
NODE *rear,*front;
void main()
{
int ch,item;
NODE *temp;
do
{
printf("\n1. INSERT");
printf("\n2. DELETE");
printf("\n3. DISPLAY");
printf("\n4. EXIT");
printf("\nEnter your choice:");
scanf("%d",&ch);
printf("%d",ch);
switch(ch)
{
case 1:
printf("\nEnter the item:");
scanf("%d",&item);
printf("%d",item);
if(front==NULL)
{
rear =(NODE *)malloc(sizeof(NODE));
rear->data=item;
rear->link=NULL;
front=rear;
}
else
{
temp =(NODE*)malloc(sizeof(NODE));
17

temp->data=item;
temp->link=NULL;
rear->link=temp;
rear=temp;
}
break;
case 2:
if(front==NULL)
{
printf("\nNo elements in queue");
}
else
{
printf("\nThe deleted element is %d",front->data);
front=front->link;
if(front==NULL) rear=front=NULL;
}
break;
case 3:
if(front==NULL) printf("\nNo elements in queue");
else
{
temp=front;
printf("\nElements in queue ");
while(temp!=rear)
{
printf(“ %d ->",temp->data);
temp=temp->link;
}
printf(“ %d\n",temp->data);
}
break;
case 4:
printf("\nEnd of operation");
break;
default:
printf("\nEnter only 1 to 4");
}
}
while(ch!=4);
18

getch();
}

OUTPUT

1. INSERT
2. DELETE
3. DISPLAY
4. EXIT
Enter your choice: 1
Enter the item: 22
1. INSERT
2. DELETE
3. DISPLAY
4. EXIT
Enter your choice: 1
Enter the item: 33
1. INSERT
2. DELETE
3. DISPLAY
4. EXIT
Enter your choice: 1
Enter the item: 44
1. INSERT
2. DELETE
3. DISPLAY
4. EXIT
Enter your choice: 3
Elements in queue 22 - 33 - 44

Enter your choice: 2


The deleted element is 22

1. INSERT
2. DELETE
3. DISPLAY
4. EXIT
Enter your choice: 3
Elements in queue 33 - 44
19

Ex.No: 4 /* Polynomial Addition */

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct link
{
int coeff;
int pow;
struct link *next;
};
struct link *poly1=NULL,*poly2=NULL,*poly=NULL;
void create(struct link *node)
{
char ch;
do
{
printf("\nenter coeff:");
scanf("%d",&node->coeff);
printf("\nenter pow:");
scanf("%d",&node->pow);
node->next=(struct link *)malloc(sizeof(struct link));
node=node->next;
node->next=NULL;
printf("\ncontinue(y/n):");
ch=getch();
}
while(ch=='y'||ch=='Y');
}
void show(struct link *node)
{
while(node->next!=NULL)
{
printf("%dx^%d",node->coeff,node->pow);
node=node->next;
if(node->next!=NULL)
printf("+");
}
}
void polyadd(struct link *poly1,struct link *poly2,struct link * poly)
20

{
while(poly1->next && poly2->next)
{
if(poly1->pow>poly2->pow)
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff;
poly1=poly1->next;
}
else if(poly1->pow<poly2->pow)
{
poly->pow=poly2->pow;
poly->coeff=poly2->coeff;
poly2=poly2->next;
}
else
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff+poly2->coeff;
poly1=poly1->next;
poly2=poly2->next;
}
poly->next=(struct link *)malloc(sizeof(struct link));
poly=poly->next;
poly->next=NULL;
}
while(poly1->next||poly2->next)
{
if(poly->next)
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff;
poly1=poly1->next;
}
if(poly2->next)
{
poly->pow=poly2->pow;
poly->coeff=poly2->coeff;
poly2=poly2->next;
}
21

poly->next=(struct link *)malloc(sizeof(struct link));


poly=poly->next;
poly->next=NULL;
}
}
main()
{
char ch;
clrscr();
do
{
poly1=(struct link *)malloc(sizeof(struct link));
poly2=(struct link *)malloc(sizeof(struct link));
poly=(struct link *)malloc(sizeof(struct link));
printf("\nenter 1st number:");
create(poly1);
printf("\nenter 2nd number:");
create(poly2);
printf("\n1st number:");
show(poly1);
printf("\n2nd number:");
show(poly2);
polyadd(poly1,poly2,poly);
printf("\nadded polynomial:");
show(poly);
getch();
}
while(ch=='y'||ch=='Y');
return(0);
}

OUTPUT
enter 1st number: enter coeff:5
enter coeff:2 enter pow:3
enter pow:3 continue(y/n):n
continue(y/n):n 1st number:2x^3
enter 2nd number: 2nd number:5x^3
added polynomial:7x^3
22

Ex.No:5 a /*Evaluating postfix Expression*/

#include<stdio.h>
int stack[20];
int top = -1;
void push(int x)
{
stack[++top] = x;
}
int pop()
{
return stack[top--];
}
int main()
{
char exp[20];
char *e;
int n1,n2,n3,num;
printf("Enter the expression :: ");
scanf("%s",exp);
e = exp;
while(*e != '\0')
{
if(isdigit(*e))
{
num = *e - 48;
push(num);
}
else
{
n1 = pop();
n2 = pop();
switch(*e)
{
case '+':
{
n3 = n1 + n2;
break;
}
case '-':
{
n3 = n2 - n1;
break;
}
case '*':
23

{
n3 = n1 * n2;
break;
}
case '/':
{
n3 = n2 / n1;
break;
}
}
push(n3);
}
e++;
}
printf("\nThe result of expression %s = %d\n\n",exp,pop());
return 0;
}

OUTPUT

Enter the expression :: 245+*

The result of expression 245+* = 18


24

Ex.No:5b /* Infix to Postfix Conversion */

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<math.h>
#include<ctype.h>
char pop(void);
void push(char);
int priority(char);
int top;
char s[80], result[80];
void main()
{
int len, i, j;
char a[80];
clrscr();
printf("enter the expression");
scanf("%s",&a);
len=strlen(a);
a[len]=')';
a[len+1]='\0';
push(')');
i=0;
j=0;
while(a[i])
{
if(isalpha(a[i]))
result[j++]=a[i];
else
{
if(a[i]=='(')
push('(');
else
{
if(a[i]=='+'||a[i]=='-'||a[i]=='*'||a[i]=='/')
{
if(priority(a[i])>priority(s[top]))
push(a[i]);
else
25

{
while(priority(a[i])<priority(s[top]))
result[j++]=pop();
if(priority(a[i])==priority(s[top]))
result[j++]=pop();
push(a[i]);
}
}
else
{
if(a[i]==')')
{
while(priority(a[i])<priority(s[top]))
result[j++]=pop();
pop();
}
}
}
}
i++;
}
result[j]='\0';
printf("\n postfix expression is %s",result);
getch();
}
char pop()
{
return(s[top--]);
}
void push(char ele)
{
s[++top]=ele;
}
int priority(char ch)
{
switch(ch)
{
case '+':return(4);
case '-':return(4);
case '*':return(5);
26

case '/':return(5);
case '(':return(0);
case ')':return(0);
}
return(0);
}

OUTPUT

enter the expression:a*b+c/d


postfix expression is:ab*cd/+
27

Ex.No: 6 /* Implementation of Binary Search Tree */

#include<stdio.h>
struct treenode;
typedef struct treenode *position;
typedef struct treenode *searchtree;
int find(int,searchtree);
void preorder(searchtree);
void inorder(searchtree);
void postorder(searchtree);
searchtree insert(int,searchtree);
struct treenode
{
int element;
searchtree left;
searchtree right;
};
searchtree t=NULL;
main()
{
int choice,no,result;
clrscr();
printf("\n1. Insert ");
printf("\n2. find ");
printf("\n3. preorder ");
printf("\n4. inorder ");
printf("\n5. postorder");
printf("\n6. Exit");
printf("\n Enter your choice 1/2/3/4/5/6 ");
scanf("%d",&choice);
while(choice!=6)
{
switch(choice)
{
case 1:
printf("\nEnter the element to be inserted");
scanf("%d",&no);
t=insert(no,t);
printf("\n Enter your choice 1/2/3/4/5/6 ");
scanf("%d",&choice);
28

break;
case 2:
printf("Enter the number to be searched ");
scanf("%d",&no);
result = find(no,t);
if(result == 1)
{
printf("%d is found in the tree ",no);
}
else
{
printf("%d is not found in the tree ",no);
}
printf("\n Enter your choice 1/2/3/4/5/6 ");
scanf("%d",&choice);
break;
case 3:
preorder(t);
printf("\n Enter your choice 1/2/3/4/5/6 ");
scanf("%d",&choice);
break;
case 4:
inorder(t);
printf("\n Enter your choice 1/2/3/4/5/6 ");
scanf("%d",&choice);
break;
case 5:
postorder(t);
printf("\n Enter your choice 1/2/3/4/5/6 ");
scanf("%d",&choice);
break;
default:
printf("\n Wrong Choice !! ");
printf("\n Enter your choice 1/2/3/4/5/6 ");
scanf("%d",&choice);
break;
}
}
}
29

searchtree insert(int x,searchtree t)


{
if(t==NULL)
{
t=(struct treenode *)malloc(sizeof(struct treenode));
if(t==NULL)
{
printf("Out of memory");
return NULL ;
}
else
{
t->element = x;
t->left=NULL;
t->right=NULL;
}
}
else
{
if(x<t->element)
{
t->left=insert(x,t->left);
}
else if(x>t->element)
{
t->right = insert(x,t->right);
}
return t;
}
}

int find(int x,searchtree t)


{
if(t==NULL)
{
return 0;
}
if(x<t->element)
{
return find(x,t->left);
30

}
else if(x>t->element)
{
return find(x,t->right);
}
else
{
return 1;
}
}
void preorder(searchtree t)
{
if(t!=NULL)
{
printf("\n%d",t->element);
preorder(t->left);
preorder(t->right);
}
}

void inorder(searchtree t)
{
if(t!=NULL)
{
inorder(t->left);
printf("\n%d",t->element);
inorder(t->right);
}
}

void postorder(searchtree t)
{
if(t!=NULL)
{
postorder(t->left);
postorder(t->right);
printf("\n%d",t->element);
}
}
31

OUTPUT

1. Insert
2. find
3. preorder
4. inorder
5. postorder
6. Exit
Enter your choice 1/2/3/4/5/6 1
Enter the element to be inserteä 8
Enter your choice 1/2/3/4/5/6 1
Enter the element to be inserteä 7
Enter your choice 1/2/3/4/5/¶ 1
Enter the element to be inserteä 9
Enter your choice 1/2/3/4/5/6 ²
Enter the number to be searched 8
8 is found in the tree
Enter your choice 1/2/3/4/5/¶ 2
Enter the number to be searched 1
1 is not found in the tree
Enter your choice 1/2/3/4/5/6 3
8
7
9
Enter your choice 1/2/3/4/5/¶ 4
7
8
9
Enter your choice 1/2/3/4/5/6 5
7
9
8
Enter your choice 1/2/3/4/5/¶ 6
32

Ex.No:7 /* Implementation of AVL trees */

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<alloc.h>
typedef enum { FALSE ,TRUE } bool;
struct node
{
int info;
int balance;
struct node *lchild;
struct node *rchild;
};
struct node *insert (int , struct node *, int *);
struct node* search(struct node *,int);
inorder(struct node *);
display(struct node *,int n);
main()
{
int ht_inc;
int info ;
int choice;
struct node *root = (struct node *)malloc(sizeof(struct node));
root = NULL;
clrscr();
while(1)
{
printf("\nINSERTION IN AVL TREES");
printf("\n----------------------");
printf("\n1.Insert");
printf("\n2.Display");
printf("\n3.Quit");
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter the value to be inserted : ");
scanf("%d", &info);
33

if( search(root,info) == NULL )


root = insert(info, root, &ht_inc);
else
printf("Duplicate value ignored\n");
break;
case 2:
if(root==NULL)
{
printf("Tree is empty\n");
continue;
}
printf("Tree is :\n");
display(root, 1);
printf("\n\n");
printf("Inorder Traversal is: ");
inorder(root);
printf("\n");
break;
case 3:
exit(1);
default:
printf("Wrong choice\n");
}
}
}
struct node* search(struct node *ptr,int info)
{
if(ptr!=NULL)
if(info < ptr->info)
ptr=search(ptr->lchild,info);
else if( info > ptr->info)
ptr=search(ptr->rchild,info);
return(ptr);
}/*End of search()*/
struct node *insert (int info, struct node *pptr, int *ht_inc)
{
struct node *aptr;
struct node *bptr;

if(pptr==NULL)
34

{
pptr = (struct node *) malloc(sizeof(struct node));
pptr->info = info;
pptr->lchild = NULL;
pptr->rchild = NULL;
pptr->balance = 0;
*ht_inc = TRUE;
return (pptr);
}
if(info < pptr->info)
{
pptr->lchild = insert(info, pptr->lchild, ht_inc);
if(*ht_inc==TRUE)
{
switch(pptr->balance)
{
case -1:
pptr->balance = 0;
*ht_inc = FALSE;
break;
case 0:
pptr->balance = 1;
break;
case 1:
aptr = pptr->lchild;
if(aptr->balance == 1)
{
printf("Left to Left Rotation\n");
pptr->lchild= aptr->rchild;
aptr->rchild = pptr;
pptr->balance = 0;
aptr->balance=0;
pptr = aptr;
}
else
{
printf("Left to right rotation\n");
bptr = aptr->rchild;
aptr->rchild = bptr->lchild;
bptr->lchild = aptr;
35

pptr->lchild = bptr->rchild;
bptr->rchild = pptr;
if(bptr->balance == 1 )
pptr->balance = -1;
else
pptr->balance = 0;
if(bptr->balance == -1)
aptr->balance = 1;
else
aptr->balance = 0;
bptr->balance=0;
pptr=bptr;
}
*ht_inc = FALSE;
}
}
}

if(info > pptr->info)


{
pptr->rchild = insert(info, pptr->rchild, ht_inc);
if(*ht_inc==TRUE)
{
switch(pptr->balance)
{
case 1:
pptr->balance = 0;
*ht_inc = FALSE;
break;
case 0:
pptr->balance = -1;
break;
case -1:
aptr = pptr->rchild;
if(aptr->balance == -1)
{
printf("Right to Right Rotation\n");
pptr->rchild= aptr->lchild;
aptr->lchild = pptr;
pptr->balance = 0;
36

aptr->balance=0;
pptr = aptr;
}
else
{
printf("Right to Left Rotation\n");
bptr = aptr->lchild;
aptr->lchild = bptr->rchild;
bptr->rchild = aptr;
pptr->rchild = bptr->lchild;
bptr->lchild = pptr;
if(bptr->balance == -1)
pptr->balance = 1;
else
pptr->balance = 0;
if(bptr->balance == 1)
aptr->balance = -1;
else
aptr->balance = 0;
bptr->balance=0;
pptr = bptr;
}
*ht_inc = FALSE;
}
}
}
return(pptr);
}
display(struct node *ptr,int level)
{
int i;
if ( ptr!=NULL )
{
display(ptr->rchild, level+1);
printf("\n");
for (i = 0; i < level; i++)
printf(" ");
printf("%d", ptr->info);
display(ptr->lchild, level+1);
}
37

return(0);
}
inorder(struct node *ptr)
{
if(ptr!=NULL)
{
inorder(ptr->lchild);
printf("%d ",ptr->info);
inorder(ptr->rchild);
return(0);
}

OUTPUT

INSERTION IN AVL TREES


----------------------
1.Insert
2.Display
3.Quit
Enter your choice : 1
Enter the value to be inserted : 30
INSERTION IN AVL TREES
----------------------
1.Insert
2.Display
3.Quit
Enter your choice : 1
Enter the value to be inserted º 10
INSERTION IN AVL TREES
----------------------
1.Insert
2.Display
3.Quit
Enter your choice : 1
Enter the value to be inserted : 20
Left to right rotation

INSERTION IN AVL TREES


----------------------
1.Insert
38

2.Display
3.Quit
Enter your choice : Tree is º 3
30
20
10
Inorder Traversal is: 10 20 30
INSERTION IN AVL TREES
----------------------
1.Insert
2.Display
3.Quit
Enter your choice : 3
39

Ex.No:8 /* Implementation of Heaps Using Priority Queue */

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<alloc.h>
insert();
del();
display();
struct node
{
int priority;
int info;
struct node *link;
}
*front=NULL;
void main()
{
int choice;
clrscr();
while(1)
{
printf("\n1.insert");
printf("\n2.delete");
printf("\n3.display");
printf("\n4.quit");
printf("\nenter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
40

exit(0);
default:
printf("wtong choice\n");
}
}
}
insert()
{
struct node *tmp,*q;
int added_item,item_priority;
tmp=(struct node *)malloc(sizeof(struct node));
printf("input the item value to be added in the queue:");
scanf("%d",&added_item);
printf("\nenter its priority:");
scanf("%d",&item_priority);
tmp->info=added_item;
tmp->priority=item_priority;
if(front==NULL||item_priority<front->priority)
{
tmp->link=front;
front=tmp;
}
else
{
q=front;
while(q->link!=NULL&&q->link->priority<=item_priority)
q=q->link;
tmp->link=q->link;
q->link=tmp;
}
return(0);
}
del()
{
struct node *tmp;
if(front==NULL)
printf("\nqueue underflow");
else
{
tmp=front;
41

printf("deleted item is %d\n",tmp->info);


front=front->link;
free(tmp);
}
return(0);
}
display()
{
struct node *ptr;
ptr=front;
if(front==NULL)
printf("queue is empty\n");
else
{
printf("queue is:\n");
printf("priority item\n");
while(ptr!=NULL)
{
printf("%5d %5d\n",ptr->priority,ptr->info);
ptr=ptr->link;
}
}
return(0);
}

OUTPUT

1.insert
2.delete
3.display
4.quit
enter your choice: 1
input the item value to be added in the queue: 10
enter its priority: 1
1.insert
2.delete
3.display
4.quit
enter your choice: 1
input the item value to be added in the queue: 20
42

enter its priority: 2


1.insert
2.delete
3.display
4.quit
enter your choice: 2
deleted item is 10

1.insert
2.delete
3.display
4.quit
enter your choice: 3
queue is
priority item
2 20

1.insert
2.delete
3.display
4.quit
43

Ex:No 9 /*Shortest Path Algorithm- Dijkstra*/

#include<stdio.h>
#include<conio.h>
#define INFINITY 9999
#define MAX 10
void dijkstra(int G[MAX][MAX],int n,int startnode);
int main()
{
int G[MAX][MAX],i,j,n,u;
printf("Enter no. of vertices:");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
printf("\nEnter the starting node:");
scanf("%d",&u);
dijkstra(G,n,u);
return 0;
}
void dijkstra(int G[MAX][MAX],int n,int startnode)
{
int cost[MAX][MAX],distance[MAX],pred[MAX];
int visited[MAX],count,mindistance,nextnode,i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(G[i][j]==0)
cost[i][j]=INFINITY;
else
cost[i][j]=G[i][j];
for(i=0;i<n;i++)
{
distance[i]=cost[startnode][i];
pred[i]=startnode;
visited[i]=0;
}
distance[startnode]=0;
visited[startnode]=1;
count=1;
while(count<n-1)
{
mindistance=INFINITY;
for(i=0;i<n;i++)
if(distance[i]<mindistance&&!visited[i])
44

{
mindistance=distance[i];
nextnode=i;
}
visited[nextnode]=1;
for(i=0;i<n;i++)
if(!visited[i])
if(mindistance+cost[nextnode][i]<distance[i])
{
distance[i]=mindistance+cost[nextnode][i];
pred[i]=nextnode;
}
count++;
}
for(i=0;i<n;i++)
if(i!=startnode)
{
printf("\nDistance of node%d=%d",i,distance[i]);
printf("\nPath=%d",i);
j=i;
do
{
j=pred[j];
printf("<-%d",j);
}
while(j!=startnode);
}
}

OUTPUT

Enter no. of vertices:4


Enter the adjacency matrix:
1234
5642
5671
1231
Enter the starting node:1
Distance of node0=3
Path=0<-3<-1
Distance of node2=4
Path=2<-1
Distance of node3=2
Path=3<-1
45

Ex.No: 10 /*Minimum Spanning Tree-Prim’s algorithm*/

#include<stdio.h>
#include<conio.h>
int a[10][10],n;
void cost()
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i!=j)
{
printf("\nenter the value of edge(%d,%d)[noedge- 999]:",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
}
}
void main()
{
int lowcost[10],closest[10],i,j,min,k;
clrscr();
printf("\nminimum spanning tree");
printf("\nenter the no of vertices");
scanf("%d",&n);
cost();
printf("\nedges in spanning tree:");
for(i=1;i<n;i++)
{
lowcost[i]=a[0][i];
closest[i]=0;
}
lowcost[0]=999;
for(i=1;i<n;i++)
{
min=lowcost[i];
k=i;
for(j=2;j>n;j++)
46

{
if(lowcost[j]<min)
{
min=lowcost[j];
k=j;
}
}
printf("\n(%d,%d)",k+1,closest[k]+1);
lowcost[k]=9999;
for(j=1;j<n;j++)
{
if((a[k][j]<lowcost[j])&&lowcost[j]<9999)
{
lowcost[j]=a[k][j];
closest[j]=k;
}
}
}
getch();
}

OUTPUT

minimum spanning tree


enter the no of vertices :4
enter the value of edge(1,2)[noedge -999]: 2
enter the value of edge(1,3)[noedge -999]: 1
enter the value of edge(1,4)[noedge -999]: 999
enter the value of edge(2,1)[noedge -999]: 2
enter the value of edge(2,3)[noedge -999]: 999
enter the value of edge(2,4)[noedge -999]: 1
enter the value of edge(3,1)[noedge -999]: 1
enter the value of edge(3,2)[noedge -999]: 999
enter the value of edge(3,4)[noedge -999]: 5
enter the value of edge(4,1)[noedge -999]: 999
enter the value of edge(4,2)[noedge -999]: 1
enter the value of edge(4,3)[noedge -999]: 5
edges in spanning tree:
(2,1) (3,1) (4,2) (2,1) (3,1) (4,2)
47

Ex.No: 11 a /*Linear search*/

#include <stdio.h>
#define MAX 20
int intArray[MAX] = {1,2,3,4,6,7,9,11,12,14,15,16,17,19,33,34,43,45,55,66};
void printline(int count)
{
int i;
for(i = 0;i <count-1;i++)
{
printf("=");
}
printf("=\n");
}
int find(int data)
{
int comparisons = 0;
int index = -1;
int i;
for(i = 0;i<MAX;i++) {
comparisons++;
if(data == intArray[i]) {
index = i;
break;
}
}
printf("Total comparisons made: %d", comparisons);
return index;
}
void display()
{
int i;
printf("[");
for(i = 0;i<MAX;i++) {
printf("%d ",intArray[i]);
}
printf("]\n");
}
void main() {
printf("Input Array: ");
48

display();
printline(50);
int location = find(55);
if(location != -1)
printf("\nElement found at location: %d" ,(location+1));
else
printf("Element not found.");
}

OUTPUT
Input Array: [1 2 3 4 6 7 9 11 12 14 15 16 17 19 33 34 43 45 55 66]
==================================================
Total comparisons made: 19
Element found at location: 19
49

Ex.No.11 b /* Binary search */

#include<stdio.h>
#define MAX 10
int list[MAX] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44 };
int find(int data) {
int lo = 0;
int hi = MAX - 1;
int mid = -1;
int comparisons = 1;
int index = -1;
while(lo <= hi) {
printf("\nComparison %d \n" , comparisons ) ;
printf("lo : %d, list[%d] = %d\n", lo, lo, list[lo]);
printf("hi : %d, list[%d] = %d\n", hi, hi, list[hi]);
comparisons++;
mid = lo + (((double)(hi - lo) / (list[hi] - list[lo])) * (data - list[lo]));
printf("mid = %d\n",mid);
if(list[mid] == data) {
index = mid;
break;
} else {
if(list[mid] < data) {
lo = mid + 1;
} else {
hi = mid - 1;
}
}
}
printf("\nTotal comparisons made: %d", --comparisons);
return index;
}
int main() {
int location = find(33);
if(location != -1)
printf("\nElement found at location: %d" ,(location+1));
else
printf("Element not found.");
return 0;
}
50

OUTPUT

Comparison 1
lo : 0, list[0] = 10
hi : 9, list[9] = 44
mid = 6

Total comparisons made: 1


Element found at location: 7
51

Ex.No:12 a /* Insertion sort */

#include<stdio.h>
#include<conio.h>
main()
{
int i,n;
int a[20];
void insort(int a[],int n);
clrscr();
printf("Enter the total number of elements:");
scanf("%d",&n);
printf("Enter the numbers:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
insort(a,n);
printf("Sorted numbers");
for(i=0;i<n;i++)
{
printf("\n%d",a[i]);
}
getch();
}
void insort(int a[],int n)
{
int i,j,temp;
for(i=0;i<n;i++)
{
temp=a[i];
for(j=i;a[j-1]>temp&&j>0;j--)
{
a[j]=a[j-1];
}
a[j]=temp;
}
}
52

OUTPUT:
Enter the total number of elements: 4
Enter the numbers:
10
-9
0
-3
Sorted numbers
-9
-3
0
10
53

Ex.No:12 b /* Selection sort*/

#include<stdio.h>
#include<conio.h>
main()
{
int i,n,a[30];
void selsort(int a[],int n);
clrscr();
printf("Enter the total no. of elements :");
scanf("%d",&n);
printf("Enter the numbers :\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
selsort(a,n);
printf("Sorted numbers\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
getch();
}
void selsort(int a[],int n)
{
int min,i,j,pos;
for(i=0;i<n-1;i++)
{
min=a[i];
pos=i;
for(j=i+1;j<n;j++)
{
if(a[j]<min)
{
min=a[j];
pos=j;
}
}
a[pos]=a[i];
54

a[i]=min;
}
}

OUTPUT

Enter the total number of elements: 5


Enter the numbers: 1 7 5 10 9

Sorted numbers
1 5 7 9 10
55

Ex.No:13 /*Merge sort*/

#include<stdio.h>
#include<conio.h>
void mergesort(int a[],int,int);
void merge(int [],int,int,int);
void main()
{
int a[20],i,n;
clrscr();
printf("Enter the number of elements");
scanf("%d",&n);
printf("Enter the elements");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
mergesort(a,0,n-1);
printf("Data After Merge Sort");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
getch();
}
void mergesort(int a[],int lb,int ub)
{
int mid;
if(lb<ub)
{
mid=(lb+ub)/2;
mergesort(a,lb,mid);
mergesort(a,mid+1,ub);
merge(a,lb,mid+1,ub);
}
}
void merge(int a[],int lb,int mid,int ub)
{
int k,p1,p2,p3,b[20];
p1=lb;
p3=lb;
56

p2=mid;
while((p1<mid)&&(p2<=ub))
{
if(a[p1]<=a[p2])
b[p3++]=a[p1++];
else
b[p3++]=a[p2++];
}
while(p1<mid)
{
b[p3++]=a[p1++];
}
while(p2<=ub)
{
b[p3++]=a[p2++];
}
for(k=lb;k<p3;k++)
{
a[k]=b[k];
}
}

OUTPUT

Enter the number of elements7

Enter the elements6 5 9 11 3 1 8 4

Data After Merge Sort

1 3 5 6 8 9 11
57

Ex.No:14 /*Implementation of Open Addressing (Linear Probing and Quadratic


Probing)*/

#include <stdio.h>
#include <conio.h>
int tsize;
int hasht(int key)
{
int i ;
i = key%tsize ;
return i;
}
//-------LINEAR PROBING-------
int rehashl(int key)
{
int i ;
i = (key+1)%tsize ;
return i ;
}
//-------QUADRATIC PROBING-------
int rehashq(int key, int j)
{
int i ;
i = (key+(j*j))%tsize ;
return i ;
}

void main()
{
int key,arr[20],hash[20],i,n,s,op,j,k ;
clrscr() ;
printf ("Enter the size of the hash table: ");
scanf ("%d",&tsize);

printf ("\nEnter the number of elements: ");


scanf ("%d",&n);

for (i=0;i<tsize;i++)
hash[i]=-1 ;

printf ("Enter Elements: ");


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

do
{
printf("\n\n1.Linear Probing\n2.Quadratic Probing \n3.Exit \nEnter your option: ");
scanf("%d",&op);
switch(op)
{
case 1:
for (i=0;i<tsize;i++)
hash[i]=-1 ;

for(k=0;k<n;k++)
{
key=arr[k] ;
i = hasht(key);
while (hash[i]!=-1)
{
i = rehashl(i);
}
hash[i]=key ;
}
printf("\nThe elements in the array are: ");
for (i=0;i<tsize;i++)
{
printf("\n Element at position %d: %d",i,hash[i]);
}
break ;

case 2:
for (i=0;i<tsize;i++)
hash[i]=-1 ;

for(k=0;k<n;k++)
{
j=1;
key=arr[k] ;
i = hasht(key);
while (hash[i]!=-1)
{
i = rehashq(i,j);
j++ ;
}
hash[i]=key ;
}
59

printf("\nThe elements in the array are: ");


for (i=0;i<tsize;i++)
{
printf("\n Element at position %d: %d",i,hash[i]);
}
break ;
}
}while(op!=3);

getch() ;
}

OUTPUT

You might also like