Professional Documents
Culture Documents
DS Labprint 2022 Regulation FINAL REPORT
DS Labprint 2022 Regulation FINAL REPORT
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
top=-1;
printf("\n Enter the size of STACK : ");
scanf("%d",&n);
printf("\n STACK OPERATIONS USING ARRAY");
printf("\n 1.PUSH\n 2.POP\n 3.DISPLAY\n 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("\nExiting...");
break;
}
default:
{
printf ("\n Please Enter a Valid Choice(1/2/3/4)");
}
}
}
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{
printf("\nSTACK 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 Stack is under flow");
}
else
{
printf("\nThe popped elements is %d",stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Enter Next Choice");
}
else
{
printf("\n The STACK is empty");
}
}
OUTPUT
Enter the size of STACK : 5
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:5
Enter the Choice:1
Enter a value to be pushed:6
Enter the Choice:3
The elements in STACK
6
5
3
Enter Next Choice
Enter the Choice:2
The popped elements is 6
Enter the Choice:3
The elements in STACK
5
3
Enter Next Choice
Enter the Choice:4
Exiting...
RESULT: Thus, the program for implementation of stack is executed and its output is verified.
IMPLEMENTATION OF QUEUE USING ARRAY
AIM:
ALGORITHM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define SIZE 5
int front = - 1;
int rear = - 1;
int q[SIZE];
void insert( );
void del( );
void display( );
void main()
int choice;
do
printf("\t Menu");
printf("\n 1. Insert");
printf("\n 2. Delete");
printf("\n 3. Display ");
printf("\n 4. Exit");
case1:
insert( );
display( );
break;
case2:
del( );
display( );
break;
case 3:
display( );
break;
case 4:
printf("EndofProgram...................................!!!!");
exit(0);
}}
while(choice != 4);
void insert( )
int no;
scanf("%d", &no);
q[++rear]=no;
if(front==-1)
front=0;// front=front+1;
else
}
}
void del( )
if(front == - 1)
return;
else
if(front == rear)
front = - 1; rear = -
1;
else
front = front + 1;
}}
void display( )
int i;
if( front == - 1)
printf("\nQueueisempty.......................................");
return;
printf("\t%d",q[i]);
}
OUTPUT
RESULT: Thus, the program for implementation of queue is executed and its output is verified.
ARRAY IMPLEMENTATION OF LIST ADTS
AIM:
To write a ‘C’ PROGRAM to implement the array-based circular queue.
ALGORITHM:
1. Start the program.
2. Declare the necessary variables.
3. Use a do while loop and switch statement to call the functions.
4. Insert: a) Read the value to be inserted
b) Increment rear and front if it is the first element.
c) Else increment the rear by 1.
5. Delete: a) Increment front by 1.
b) Print the deleted elements.
6. Display: a) If the both front=rear=1, print empty queue.
b) Else use for loop varying from front to rear and print the elements.
7. Stop the program.
PROGRAM:( ARRAY IMPLEMENTATION OF LISTADT)
#include<stdio.h>
#include<conio.h>#defin
e MAX 10
void create();
void insert();
void deletion();
void search();
void display();
void main()
clrscr();
g='y';
do
printf("\n1.Create\n2.Delete\n3.Search\n4.Insert\n5.Display\n6.Exit\n");
printf("\n Enter your Choice");
scanf("%d", &ch);
switch(ch)
case1:
create();
break;
case2:
deletion();
break;
case3:
search();
break;
case4:
insert();
break;
case5:
display();
break;
case6:
exit();
break;
default:
scanf("\n%c", &g);
while(g=='y'||g=='Y');
getch();
void create()
for(i=0;i<n;i++)
printf("\nEnterthe Element:",i+1);
scanf("%d",&b[i]);
void deletion()
scanf("%d", &pos);
if(pos>=n)
else
for(i=pos+1;i<n;i++)
b[i-1]=b[i];
n--;
for(i=0;i<n;i++)
printf("\t%d", b[i]);
void search()
scanf("%d",&e);
for(i=0;i<n;i++)
if(b[i]==e)
}}}
void insert()
scanf("%d", &pos);
if(pos>=n)
} else
for(i=MAX-1;i>=pos-1;i--)
b[i+1]=b[i];
scanf("%d",&p);
b[pos]=p; n+
+;
display();
voiddisplay()
printf("\n\n%d", b[i]);
}
}
OUTPUT
RESULT: Thus, the program for implementation of list ADT is executed and its output is verified.
IMPLEMENTATION OF STACK USING LINKED LIST
AIM:
To write a C program to implement stack using array.
ALGORITHM:
1. Start the program
2. According to switch case the operations are selected.
3. In case 1, data are added using PUSH operation.
4. In case 2, data are deleted from stack using POP operation.
5. In case 3, elements of array in stack are displayed using display Function.
6. End the program.
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
int data;
}*top,*new1,*first;
void main()
int wish,opt;
void create(),push(),pop(),view();
do
clrscr();
n5.Exit\n");
scanf("%d",&wish);
switch(wish)
exit(0);
scanf("%d",&opt);
}while(opt==1);
voidcreate()
intch;
top->next=NULL;
do
clrscr();
scanf("%d",&top->data);
scanf("%d",&ch);
if(ch==1)
new1->next=top;
top=new1;
first=top;
}
else
break;
}while(ch==1);
void push()
top=first;
scanf("%d",&new1->data); new1-
>next=top; top=new1;
first=top;
void pop()
clrscr(); top=first;
if(top==NULL)
{
printf("\nThe element popped out from stack is %d",top->data); top=top->next;
first=top;
}}
void view()
printf("\nStack contents\n");
while(top->next!=NULL)
printf("%d->",top->data);
top=top->next;}
printf("%d\n",top->data);
getch();
}
OUTPUT
RESULT: Thus, the program for implementation of stack is executed and its output is verified.
IMPLEMENTATION OF QUEUE ADTS USING LINKED LIST
AIM:
To write a C program for the Queue Implementation.
ALGORITHM:
1. Start the program.
2. Declare the necessary variables.
3. Use a do while loop and switch statement to call the functions.
4. Insertion:
a) Step 1: Allocate the space for the new node PTR
b) Step 2: SET PTR -> DATA = VAL
c) Step 3: IF FRONT = NULL SET FRONT = REAR = PTR SET FRONT -> NEXT = REAR -> NEXT = NULL ELSE
SET REAR -> NEXT = PTR SET REAR = PTR SET REAR -> NEXT = NULL [END OF IF]
d) Step
4: END
5. Delete: a) Step 1: IF FRONT = NULL Write " Underflow " Go to Step 5 [END OF IF]
b) Step 2: SET PTR = FRONT
c) Step 3: SET FRONT = FRONT -> NEXT
d) Step 4: FREE PTR e) Step
5: END
6. Display:
7. Stop the program
#include<conio.h>
struct node
int info;
void insert();
void delet();
void display();
nt item;
void main()
{
int ch; do
printf("\n\n1.\tEnqueue\n2.\tDequeue\n3.\tDisplay\n4.\tExit\n");
scanf("%d", &ch);
switch(ch)
case1:
insert();
break;
case2:
delet();
break;
case3:
display();
break;
case4:
exit(0);
default:
} while(1);
getch();
void insert()
scanf("%d", &item);
if(rear == NULL)
rear->info = item;
rear->link = NULL;
front = rear;
else{
>link =NULL;
}}
ptr = front;
front->link; free(ptr);
NULL)
rear = NULL;
}
}
void display()
printf("\n\n");
while(ptr != NULL)
}
}
}
OUTPUT
RESULT: Thus, the program for simulation of queue using linked list implementation is executed and its output is verified.
POLYNOMIAL MANIPULATION USING LINKED LIST
AIM:
To write a ‘C’ PROGRAM to represent a polynomial as a linked list and perform the addition of polynomial.
ALGORITHM:
1. Create Link polynomials
2. Read the coefficients and exponent values of polynomials.
3. Set the pointers P1 and P2 to both polynomials respectively to traverse them.
4. Start from first node and compare the exponent of two polynomials i)
If ( P1 -- exp = = P2 -- exp ) P3- coeff = P1 – coeff + P2 --- coeff P3 --- exp = P1 – exp
ii) If ( P1 -- exp < P2 -- exp ) P3 --- exp = P2 – exp; P3- -- coeff = P2 --- coeff Move q to next node
iii) Else P3 --- exp = P1 – exp: P3- -- coeff = P1 --- coeff iv) Move p to next node
5. Append the remaining nodes of either of the polynomials to the resultant linked list.
6. End
PROGRAM (IMPLEMENTATION OF POLYNOMIAL ADT)
#include<stdio.h>
#include<conio.h>
main()
{
int a[10], b[10], c[10],m,n,k,k1,i,j,x;
clrscr();
printf("\n\tPolynomial Addition\n"); printf("\
t===================\n");
printf("\n\tEnter the no. of terms of the polynomial:");
scanf("%d", &m);
printf("\n\tEnter the degrees andcoefficients:");
for(i=0;i<2*m;i++)
scanf("%d",&a[i]);
k1=0;
if(a[k1+1]==1)
printf("x^%d", a[k1]); else
printf("%dx^%d", a[k1+1],a[k1]); k1+=2;
while (k1<i)
{
printf("+%dx^%d", a[k1+1],a[k1]);
k1+=2;
}
printf("\n\n\n\tEnter the no. of terms of 2nd polynomial:");
scanf("%d", &n);
printf("\n\tEnter the degrees and co-efficients:");
for(j=0;j<2*n;j++)
scanf("%d", &b[j]);
printf("\n\tSecond polynomial is:");
k1=0;
if(b[k1+1]==1)
printf("x^%d", b[k1]);
else
printf("%dx^%d",b[k1+1],b[k1]);
k1+=2;
while (k1<2*n)
{
printf("+%dx^%d", b[k1+1],b[k1]);
k1+=2;
} i=0;
j=0;
k=0;
while (m>0 && n>0)
{
if (a[i]==b[j])
{ c[k+1]=a[i+1]+b[j+1];
c[k]=a[i]; m-;
n--;
i+=2;
j+=2;
}
else if (a[i]>b[j])
{ c[k+1]=a[i+1];
c[k]=a[i]; m-;
i+=2;
}
else
{ c[k+1]=b[j+1];
c[k]=b[j]; n--;
j+=2;
}
k+=2;
}
while (m>0)
{ c[k+1]=a[i+1];
c[k]=a[i];
k+=2;
i+=2;
m--;
}
while (n>0)
{ c[k+1]=b[j+1];
c[k]=b[j];
k+=2; j+=2;
n--;
}
printf("\n\n\n\n\tSum of the two polynomials is:"); k1=0;
if (c[k1+1]==1)
printf("x^%d", c[k1]); else
printf("%dx^%d", c[k1+1],c[k1]); k1+=2;
while (k1<k)
{
if (c[k1+1]==1)
printf("+x^%d", c[k1]); else
printf("+%dx^%d", c[k1+1], c[k1]); k1+=2;
}
getch();
return 0;
}
OUTPUT
RESULT: Thus, the program for implementation of polynomial addition is executed and its output is verified.
CONVERSION FROM INFIX TO POST EXPRESSION
AIM:
To write a ‘C’ PROGRAM to implement the conversion from infix to postfix expression using stack.
ALGORITHM:
Step 1: Read the expression from left to right.
Step 2: If the input symbol red is ‘(‘then push it on to the stack.
Step 3: If the input symbol read is an operand, then place it in the postfix expression.
Step 4: If the input symbol read is an operator, then, Check if precedence (stack operator) >= precedence (input
operator) If so, remove all elements from stack and place it in postfix Otherwise, push the operator being read onto
the stack.
Step 5: If the input symbol read is a closing parenthesis ‘)’ then, pop all the elements from the stack, place them in
postfix expression till ‘(’ is not popped.
Step 6: Finally print the postfix expression.
PROGRAM: (CONVERSION OF INFIX EXPRESSION TO POSTFIX)
#include<stdio.h>
char stack[20];
void push(charx)
{
stack[++top] = x;
}
char pop()
{
if(top == -1)
return -1;
else
return stack[top--];
}
int priority(char x)
{
if(x == '(')
return0;
if(x == '+' || x == '-') return1;
if(x == '*' || x == '/')
return 2;
}
main()
{
char exp[20]; char
*e, x;
printf("Enter the expression :: ");
scanf("%s",exp);
e = exp;
while(*e != '\0')
{
if(isalnum(*e))
printf("%c",*e);
else if(*e == '(') push(*e);
else if(*e == ')')
{
while((x = pop()) != '(')
printf("%c", x);
}
else
{
while(priority(stack[top]) >= priority(*e))
printf("%c",pop());
push(*e);
}
e++;
}
while(top != -1)
{
printf("%c",pop());
}
}
OUTPUT
RESULT:
Thus, the program for implementation of conversion from infix to postfix expression is executed and its output is
verified
BINARY TREE
AIM:
To write a ‘C’ PROGRAM to implement the binary tree.
ALGORITHM:
1. Start the program.
2. Get the root node and initialized the pointer.
3. Get the Values of the child nodes.
4. Call the function “Create” to construct tree.
5. According to the switch statement the Traversals was selected.
6. PROCEDURE – CREATE:
a) Use IF loop to check whether the mode is Null.
b) Create a Node using malloc function.
c) Store the value in the Node function.
d) Make the right and left Pointers to Null.
e) Else if (n>node-> data).
f) Node->right = Create (node->right, n);
g) Else if (n< node-> data)
h) Node -> left = Create (node-> left, n); i) Return the node value to main program.
7. End the process.
#include<stdio.h>
#include<stdlib.h>
struct tree {
int data;
struct tree *left; struct
tree *right;
} *root = NULL, *node = NULL, *temp = NULL;
if(leaf == 0)
{
struct tree *temp;
temp = (struct tree *)malloc(sizeof(struct tree));
temp->data = key;
temp->left = 0;
temp->right = 0;
printf("Data inserted!\n");
return temp;
}
else {
if(key < leaf->data)
leaf->left = insert(key,leaf->left);
else
leaf->right = insert(key,leaf->right);
}
return leaf;
}
if(leaf != NULL)
{
if(key == leaf->data)
{
printf("Data found!\n");
return leaf;
}
else {
if(key < leaf->data)
return search(key,leaf->left);
else
return search(key,leaf->right);
}
}
else {
}}
if(node->left)
return minvalue(node->left);
e
l
s
e
return
node
void inorder(struct tree *leaf) {
if(leaf == NULL)
return; preorder(leaf-
>left); printf("%d\n",leaf->data);
preorder(leaf->right);
}
if(leaf == NULL)
printf("Element Not Found!\n");
else
if(key <leaf->data)
leaf->left = delete(leaf->left, key); else if(key >leaf->data)
leaf->right = delete(leaf->right, key);
else {
if(leaf->right && leaf->left) {
temp = minvalue(leaf->right);
leaf->data = temp->data;
leaf->right = delete(leaf->right,temp->data);
}
else {
temp = leaf;
if(leaf->left == NULL) leaf = leaf-
>right;
else if(leaf->right == NULL) leaf = leaf-
>left;
free(temp);
} printf("Data delete successfully!\n");
}
}
int main() {
int key, choice;
while(choice != 7) {
printf("1. Insert\n2. Search\n3. Delete\n4. Display\n5. Min Value\n6.
Max Value\n7. Exit\n");
printf("Enter your choice:\n");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("\nEnter the value to insert:\n");
scanf("%d", &key);
root = insert(key, root);
break;
case 2:
printf("\nEnter the value to search:\n");
scanf("%d", &key);
search(key,root);
break;
case 3:
printf("\nEnter the value to delete:\n");
scanf("%d", &key);
delete(root,key); break;
case 4:
printf("Preorder:\n");
preorder(root);
printf("Inorder:\n");
inorder(root);
printf("Postorder:\n");
postorder(root);
break;
case 5:
if(minvalue(root) == NULL)
printf("Tree is empty!\n");
Else
printf("Minimum value is %d\n", minvalue(root)->data);
break;
case 6:
if(maxvalue(root) == NULL) printf("Tree is
empty!\n");
else
printf("Maximum value is %d\n", maxvalue(root)->data);
break;
case 7:
printf("Bye Bye!\n"); exit(0);
break;
default:
printf("Invalid choice!\n");
}
return 0;
}
OUTPUT
RESULT: Thus, the Program for implementation of binary tree is executed and its output is verified.
BINARY SEARCH TREE
AIM:
To write a ‘C’ PROGRAM to implement the binary Search tree.
ALGORITHM:
1. Start the program.
2. Get the root node and initialized the pointer.
3. Get the Values of the child nodes.
4. Call the function “Create” to construct tree.
5. According to the switch statement the Traversals was selected.
6. PROCEDURE – CREATE:
a) Use IF loop to check whether the mode is Null.
b) Create a Node using malloc function.
c) Store the value in the Node function.
d) Make the right and left Pointers to Null.
e) Else if (n>node-> data).
f) Node->right = Create (node->right, n);
g) Else if (n< node-> data)
h) Node -> left = Create (node-> left, n); i) Return the node value to main program.
7. End the process.
PROGRAM: (IMPLEMENTATION OF BINARY SEARCHTREE)
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<alloc.h>
struct tree
int data;
int element;
int ch;
do
printf("\nMain Menu\n");
printf("\n1.Create\n2.Insert\n3.Delete\n4.Find\n5.FindMin\n6.FindMax");
printf("\n7.Inorder\n8.Preorder\n9.Postorder\n10.Exit\n");
switch(ch)
{
case 1:
t=create(t,element);
inorder(t);
break;
case 2:
printf("\nEnter the data:");
scanf("%d",&element);
t=insert(t,element);
inorder(t);
break;
case 3:
printf("\nEnter the data:");
scanf("%d",&element);
t=del(t,element);
inorder(t);
break;
case 4:
printf("\nEnter the data:");
scanf("%d",&element);
temp=find(t,element);
if(temp->data==element)
printf("\nElement %d is at %d",element,temp);
else
printf("\nElement is not found");
break;
case 5:
temp=findmin(t);
printf("\nMax element=%d",temp->data);
break;
case 6:
temp=findmax(t);
printf("\nMax element=%d",temp->data);
break;
case 7:
inorder(t);
break;
case 8:
preorder(t);
break;
case 9:
postorder(t);
break;
case 10:
exit(0);
}
}
while(ch<=10);
}
struct tree * create(struct tree *t, int element)
{
t=(struct tree *)malloc(sizeof(struct tree));
t->data=element;
t->lchild=NULL; t-
>rchild=NULL;
return t;
}
struct tree * find(struct tree *t, int element)
{
if(t==NULL)
return NULL;
if(element<t->data)
return(find(t->lchild,element));
else
if(element>t->data)
return(find(t->rchild,element));
else
return t;
}
struct tree *findmin(struct tree *t)
{
if(t==NULL)
return NULL;
else
if(t->lchild==NULL)
return t;
else
} return(findmin(t->lchild));
{
while(t->rchild!=NULL) t=t->rchild;
}
return t;
}
struct tree *insert(struct tree *t,int element)
{
if(t==NULL)
{
t=(struct tree *)malloc(sizeof(struct tree)); t->data=element;
t->lchild=NULL;
t->rchild=NULL;
return t;
}
else
{ if(element<t->data)
{
t->lchild=insert(t->lchild,element);
}
else
if(element>t->data)
{
t->rchild
} =insert(t->rchild,element);
else
if(element==t->data)
}
struct tree * del(struct tree *t, int element)
{
if(t==NULL)
printf("element not found\n");
else
if(element<t->data)
t->lchild=del(t->lchild,element);
else
if(element>t->data)
t->rchild=del(t->rchild,element);
else
if(t->lchild&&t->rchild)
{
}
else
temp=findmin(t->rchild);
t->data=temp->data;
t- >rchild=del(t->rchild,t->data);
temp=t;
if(t->lchild==NULL)
t=t->rchild;
else
if(t->rchild==NULL)
t=t->lchild;
free(temp);
}
return t;
}
inorder(t->lchild);
printf("\t%d",t->data);
inorder(t->rchild);
printf("\t%d",t->data);
preorder(t->lchild);
preorder(t->rchild);
}
}
postorder(t->lchild);
postorder(t->rchild);
printf("\t%d",t->data);
}
}
OUTPUT:
RESULT: Thus, the Program for implementation of binary search tree is executed and its output is verified.
AVL TREE
AIM:
ALGORTHIM:
3. Get the position of the height through left and as well as right.
4. If the height doesn’t match with the balance factor make a single rotation. Through right side.
6. If the balance factor doesn’t match the height means go for double rotation in left as well as right.
#include<stdio.h>#include<malloc
.h>
struct node
int info;
intbalance;
};
main()
bool ht_inc;
int info ;
int choice;
struct node *root = (struct node *)malloc(sizeof(struct node));
root = NULL;
while(1)
printf("1.Insert\n");
printf("2.Display\n"); printf("3.Quit\n");
scanf("%d",&choice);
switch(choice)
case 1:
scanf("%d", &info);
else
break;
case 2:
if(root==NULL)
printf("Tree is empty\n");
continue;
printf("Tree is :\n");
n");
inorder(root);
printf("\n");
break;
case 3:
exit(1);
default:
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
}/*End of main()*/
if(ptr!=NULL)
if(info <ptr->info)
ptr=search(ptr->lchild,info);
else
return(ptr);
}/*End of search()*/
struct node *insert (int info, struct node *pptr, int *ht_inc)
node *bptr;
if(pptr==NULL)
pptr->info =info;
pptr->lchild = NULL;
pptr->rchild = NULL;
pptr->balance =0;
*ht_inc = TRUE;
return (pptr);
}
if(*ht_inc==TRUE)
switch(pptr->balance)
{
case -1: /* Right heavy */ pptr-
>balance = 0;
*ht_inc = FALSE;
break;
break;
if(aptr->balance == 1)
pptr->lchild= aptr->rchild;
aptr->balance=0;
pptr =aptr;
}
else
bptr =aptr->rchild;
aptr->rchild = bptr->lchild;
bptr->lchild =aptr;
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;
}/*End of switch */
}/*End of if */
if(*ht_inc==TRUE)
switch(pptr->balance)
*ht_inc = FALSE;
break;
case 0: /* Balanced*/
aptr = pptr->rchild;
if(aptr->balance == -1)
aptr->lchild = pptr;
pptr->balance = 0;
aptr->balance=0;
pptr = aptr;
else
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;
}/*End ofelse*/
*ht_inc =FALSE;
}/*End of switch */
}/*End of if*/
}/*End of if*/
return(pptr);
}/*End of insert()*/
{
int i;
if ( ptr!=NULL )
display(ptr->rchild, level+1);
printf("\n");
printf(" ");
printf("%d", ptr->info);
display(ptr->lchild, level+1);
}/*End of if*/
}/*End of display()*/
if(ptr!=NULL)
inorder(ptr->lchild);
printf("%d ",ptr->info);
inorder(ptr->rchild);
}
}/*End of inorder()*/
OUTPUT
RESULT: Thus, the Program for implementation of insertion in AVL tree is executed and its output is verified.
PRIORITY QUEUE USING HEAPS
AIM:
To write a ‘C’ PROGRAM to implement the priority queue using heaps.
ALGORTHIM:
1. Start the program
2. Declare the variables
3. Check the condition of for qfull() and we can insert the elements if(rear==SIZE-1) we can insert the element
4. Check the condition for qempty and delete the elements in the queue if(front==-1)||(front>rear)) we can delete the
element.
5. Check the condition for qempty and display the elements in the queue if(front==-1)||(front>rear)) we can dispaly
the element.
6. Stop the program
PROGRAM: (IMPLEMENTATION OF PRIORITY QUEUE USINGHEAPS)
#include<stdio.h>
#include<conio.h>
#include <stdlib.h>
enum{FALSE=0,TRUE=-1};
struct Node
{
struct Node *Previous;
int Data;
struct Node *Next;
}Current;
struct Node *head;
struct Node *ptr;
static int NumOfNodes;
int PriorityQueue(void);
int Maximum(void);
int Minimum(void);
void Insert(int);
int Delete(int);
void Display(void);
int Search (int);
void main()
{
int choice;
int DT;
PriorityQueue();
while(1)
{
printf("\nEnter ur Choice:");
printf("\n1.Insert\n2.Display\n3.Delete\n4.Search\n5.Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter a data to enter Queue");
scanf("%d",&DT);
Insert(DT);
break;
case 2:
Display();
break;
case 3:
{
int choice,DataDel; printf("\nEnter ur choice:");
printf("\n1.Maximum Priority queue\n2.Minimum priority Queue\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
DataDel=Maximum();
Delete(DataDel);
printf("\n%d is deleted\n",DataDel);
break;
case 2:
DataDel=Minimum(); Delete(DataDel);
printf("\n%d is deleted\n",DataDel);
break;
default:
printf("\nSorry Not a correct Choice\n");
}
}
break;
case 4:
printf("\nEnter a data to Search in Queue:");
scanf("%d",&DT);
if(Search(DT)!=FALSE)
printf("\n %d is present in queue",DT);
else
printf("\n%d is not present in queue",DT);
break;
case 5:
exit(0);
default:
printf("\nCannot process ur choice\n");
} }}
int PriorityQueue(void)
{
Current.Previous=NULL;
while(ptr->Next!=NULL)
{
if(ptr->Data>Temp)
Temp=ptr->Data;
ptr=ptr->Next;
}
if(ptr->Next==NULL && ptr->Data>Temp)
Temp=ptr->Data;
return(Temp);
}
intMinimum(void)
{
int Temp; ptr=head;
Temp=ptr->Data;
while(ptr->Next!=NULL)
{
if(ptr->Data<Temp)
Temp=ptr->Data;
ptr=ptr->Next;
}
if(ptr->Next==NULL && ptr->Data<Temp)
Temp=ptr->Data;
return(Temp);
}
void Insert(int DT)
{
{
while(ptr->Next->Next!=NULL)
{
if(ptr->Next->Data==DataDel)
{
mynode=ptr;
temp=ptr->Next;
mynode->Next=mynode->Next->Next;
mynode->Next->Previous=ptr; free(temp);
NumOfNodes--;
return(TRUE);
}
ptr=ptr->Next;
}
if(ptr->Next->Next==NULL && ptr->Next->Data==DataDel)
{
temp=ptr->Next; free(temp);
ptr->Next=NULL;
NumOfNodes--;
` return(TRUE);
}
}
return(FALSE);
}
int Search(int DataSearch)
{
ptr=head;
while(ptr->Next!=NULL)
{
if(ptr->Data==DataSearch)
return ptr->Data;
ptr=ptr->Next;
}
}
voidDisplay(void)
{
ptr=head;
printf("\nPriority Queue is as Follows:-\n"); while(ptr!
=NULL)
{
printf("\t\t%d",ptr->Data); ptr=ptr->Next;
}
}
OUTPUT
RESULT: Thus, the Program for implementation of priority queue using heaps is executed and its output is verified.
IMPLEMENTATION OF BINARY SEARCH
AIM:
To write a ‘C’ program to implement binary search.
ALGORITHM:
Step 1: Start the program
Step 2: Define function for Binary Search as
a) Sort the array in ascending order
b) Let lb=0 and ub=n-1
c) Read the data to be searched ‘X’
d) Find the mid position of the given array Mid=(lb+ub)/2
e) Compare X with a[mid] If equal then Goto step (g)
Else If X less than a[mid] then ub=mid-1
If X greater than a[mid] then lb=mid+1 f)
If lb<=ub Repeat steps (d) and (e) for the sub array lb to sub Else Goto step (g)
g) If(lb>ub) Print “Search Success” Else Print “Search Failed” h) Return
Step3: Stop the program.
PROGRAM:
#include <stdio.h>
void sequential_search(int array[], int size, int n)
{
int i;
for (i = 0; i < size; i++)
{
if (array[i] == n)
{
printf("%d found at location %d.\n", n, i+1);
break;
}
}
if (i ==size)
printf("Not found! %d is not present in the list.\n", n);
}
int main()
{
int a[200], i, j, n, size;
OUTPUT
RESULT: Thus, the C program to implement Binary Search was executed successfully and the output was verified.
MERGE SORT
AIM:
To write a ‘C’ program to implement merge sort.
ALGORITHM:
Step 1: Read the elements into the array
Step 2: Take the second element. Compare it with the first element. If the second element less than the first element
interchange them
Step 3: Take the third element compare it first and second element and insert it in the correct position by shifting the
elements in the array. So that the first, second and third elements are in sorted array
Step 4: In general take the ith element and compare it with the all the elements before it and place it in the proper
position by shifting the elements one position right.
Step 5: When the ith element is placed, the elements in the array from the 0th to the ith position will be in the sorted
order
Step 6: The above process is continued for all the elements in the array.
Step 7: Stop.
PROGRAM: (MERGE SORT)
#include<stdio.h>
#include<conio.h>
void main()
int arr[30];
int i,size;
printf("\n\t------- Mergesortingmethod------------------------------------\n\n");
scanf("%d",&size);
scanf("%d",&arr[i]);
part(arr,0,size-1);
printf("\n\t------- Mergesortedelements--------------------------------------\n\n");
printf("%d ",arr[i]);
getch();
}
void part(int arr[],int min,intmax)
intmid;
if(min<max){ mid=(min+max)/2;
part(arr,min,mid);
part(arr,mid+1,max);
merge(arr,min,mid,max);
}}
int tmp[30];
int i,j,k,m;
j=min;
m=mid+1;
if(arr[j]<=arr[m])
tmp[i]=arr[j]; j++;}
else
{ tmp[i]=arr[m];
m++;
}}
if(j>mid)
{ tmp[i]=arr[k];
i++;
}
}
Else
{
for(k=j; k<=mid; k++)
tmp[i]=arr[k];
i++;
}
}
arr[k]=tmp[k];
OUTPUT
RESULT:
Thus, the C program to implement Merge Sort was executed successfully and the output was verified.
INSERTION SORT
AIM:
To write a ‘C’ program to implement insertion sort.
ALGORITHM:
Step 1: Read the elements into the array
Step 2: Take the second element. Compare it with the first element. If the second element less than the first element
interchange them
Step 3: Take the third element compare it first and second element and insert it in the correct position by shifting the
elements in the array. So that the first, second and third elements are in sorted array
Step 4: In general, take the ith element and compare it with the all the elements before it and place it in the proper position
by shifting the elements one position right.
Step 5: When the ith element is placed, the elements in the array from the 0th to the ith position will be in the sorted order
Step 6: The above process is continued for all the elements in the array.
Step 7: Stop.
PROGRAM: (INSERTION SORT)
#include <stdio.h>
int main()
{
int arr[MAX],limit;
int i,j,temp;
j--;
}
}
printf("Array elements in Ascending Order:\n");
for(i=0; i<limit; i++)
printf("%d ",arr[i]);
printf("\n");
j--;
}
}
return 0;
}
OUTPUT
RESULT: Thus, the C program to implement Insertion Sort was executed successfully and the output was verified.
SELECTION SORT
AIM:
To write a program to implement Selection Sort.
ALGORITHM:
Step1 : Start the program.
Step2 : Set MIN to location 0
Step3 : Search the minimum element in the list
Step4 : Swap with value at location MIN
Step5 : Increment MIN to point to next element
Step6 : Repeat until list is sorted
Step7 : Stop the program.
PROGRAM: (SELECTION SORT)
#include <stdio.h>
int main()
{
int arr[MAX],limit;
int i,j,temp,position;
/*Read array*/
printf("Enter array elements: \n");
for(i=0; i<limit; i++)
{
printf("Enter element %3d: ",i+1);
scanf("%d",&arr[i]);
}
if(arr[position]>arr[j])
{
position=j;
}
if(position!=i)
{
temp=arr[i];
arr[i]=arr[position];
arr[position]=temp;
}
}
OUTPUT
RESULT: Thus, the program to implement Selection Sort was executed successfully and the output was verified.
//-------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 ;
}
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
1.Linear Probing
2.Quadratic Probing
3.Exit
Enter your option: 2
1.Linear Probing
2.Quadratic Probing
3.Exit
Enter your option: 3
*/
RESULT: Thus, the Program for implementation of linear probing hashing technique is executed and its output is
verified.
DIJKSTRA’S ALGORITHM
AIM:
To write a ‘C’ PROGRAM to implement the Dijkstra’s algorithm.
ALGORITHM
Step1: Start the program.
Step2: Read the number of vertices
Step3: Read the weight of every pair of vertices.
Step4: Get the source vertex & destination.
Step5: Construct the graph.
Step6: Start finding the start node to all other neighboring nodes.
Step7: Nearest path is selected using array until the end node is reached.
Step8: Print the shortest path.
Step9: Terminate the program.
PROGRAM: (IMPLEMENTATION OF DIJKSTRA’S ALGORITHM)
#include <stdio.h>
#include <limits.h>
#define V 9
return min_index;
}
dist[src] = 0;
for (count = 0; count < V - 1; count++)
{
int u = minDistance(dist, sptSet);
sptSet[u] = 1;
for (v = 0; v < V; v++)
int main() {
int graph[V][V] = {{0, 4, 0, 0, 0, 0, 0, 8, 0},
{4, 0, 8, 0, 0, 0, 0, 11, 0},
{0, 8, 0, 7, 0, 4, 0, 0, 2},
{0, 0, 7, 0, 9, 14, 0, 0, 0},
{0, 0, 0, 9, 0, 10, 0, 0, 0},
{0, 0, 4, 0, 10, 0, 2, 0, 0},
{0, 0, 0, 14, 0, 2, 0, 1, 6},
{8, 11, 0, 0, 0, 0, 1, 0, 7},
{0, 0, 2, 0, 0, 0, 6, 7, 0}
};
dijkstra(graph, 0);
return 0;
}
OUTPUT:
$ gcc Dijkstra.c
$ ./a.out
RESULT: Thus, the Program for implementation of Dijkstra ‘s algorithm is executed and its output is verified.
PRIM’S ALGORITM
AIM:
To write a ‘C’ PROGRAM to implement the Prim’s algorithm.
ALGORTHIM:
1. Start the program
2. Declare the variables.
3. Enter the edges and its edges in the graph.
4. Construct the graph.
5. Pick one arbitrary vertex and consider it as visited.
6. The unvisited vertices in the new edge are considered.
7. Repeat the step 6 until u cover all the edges.
8. Print the minimum spanning with the corresponding weights.
9. End the program.
int G[MAX][MAX],spanning[MAX][MAX],n;
int prims();
int main()
{
int i,j,total_cost;
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]);
total_cost=prims();
printf("\nspanning tree matrix:\n");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("%d\t",spanning[i][j]);
}
printf("\n\nTotal cost of spanning tree=%d",total_cost);
return 0;
}
int prims()
{
int cost[MAX][MAX];
int u,v,min_distance,distance[MAX],from[MAX];
int visited[MAX],no_of_edges,i,min_cost,j;
//create cost[][] matrix,spanning[][]
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];
spanning[i][j]=0;
}
//initialise visited[],distance[] and from[]
distance[0]=0;
visited[0]=1;
for(i=1;i<n;i++)
{
distance[i]=cost[0][i];
from[i]=0;
visited[i]=0;
}
min_cost=0; //cost of spanning tree
no_of_edges=n-1; //no. of edges to be added
while(no_of_edges>0)
{
//find the vertex at minimum distance from the tree
min_distance=infinity;
for(i=1;i<n;i++)
if(visited[i]==0&&distance[i]<min_distance)
{
v=i;
min_distance=distance[i];
}
u=from[v];
//insert the edge in spanning tree
spanning[u][v]=distance[v];
spanning[v][u]=distance[v];
no_of_edges--;
visited[v]=1;
//updated the distance[] array
for(i=1;i<n;i++)
if(visited[i]==0&&cost[i][v]<distance[i])
{
distance[i]=cost[i][v];
from[i]=v;
}
min_cost=min_cost+cost[u][v];
}
return(min_cost);
}
OUTPUT
Enter no. of vertices:6
Enter the adjacency matrix:
031600
305030
150564
605002
036006
004260
031000
300030
100004
000002
030000
004200
RESULT: Thus, the C program to implement Linear Search was executed successfully and the output was verified.