DSLAB 2022 Print
DSLAB 2022 Print
#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
#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
#include <stdio.h>
#define SIZE 5
int items[SIZE];
int front = -1, rear = -1;
// 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);
}
}
int main() {
// Fails because front = -1
deQueue();
enQueue(1);
enQueue(2);
enQueue(3);
enQueue(4);
enQueue(5);
display();
deQueue();
8
display();
enQueue(7);
display();
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
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
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
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
#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
#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
1. INSERT
2. DELETE
3. DISPLAY
4. EXIT
Enter your choice: 3
Elements in queue 33 - 44
19
#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
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
#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
#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
#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
}
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
#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(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;
}
}
}
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
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
#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
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
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
#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
#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
#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
#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
#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
#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
Sorted numbers
1 5 7 9 10
55
#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
1 3 5 6 8 9 11
57
#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);
for (i=0;i<tsize;i++)
hash[i]=-1 ;
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
getch() ;
}
OUTPUT