DS Record
DS Record
Department of
...............................................................................................................................
Page
S.No Date Title Marks Signature
No.
s
Implementation of Polynomial
4 32
Manipulation Using Linked List
Page
S.No Date Title Marks Signature
No.
s
Implementation of Dijkstra’s
9 69
Algorithm
AIM
To write a C program to perform array implementation of stack.
ALGORITHM
PROGRAM
#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[MAX=100]:");
scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
do
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("\n\t EXIT POINT ");
break;
}
default:
{
printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}
}
}
while(choice!=4);
return 0;
2|Page
}
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()
{
if(top>=0)
{
3|Page
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
}
else
{
4|Page
OUTPUT
RESULT
Thus the array implementation of stack program was executed successfully.
5|Page
Ex.No : 1b
ARRAY IMPLEMENTATION OF QUEUE
Date :
AIM
To write a C program to perform array implementation of queue.
ALGORITHM
PROGRAM
#include <stdio.h>
#include<stdlib.h>
#define MAX 50
int queue_array[MAX];
int rear = - 1, front = - 1;
void insert(void);
void delete(void);
void display(void);
void main()
{
int choice;
while (1)
{
6|Page
printf("3.Display all elements of queue \n");
printf("4.Quit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice \n");
}}}
void insert()
{
int add_item;
if (rear == MAX - 1)
printf("Queue Overflow \n");
else
{
if (front == - 1)
7|Page
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
}
void delete()
{
if (front == - 1 || front > rear)
{printf("Queue Underflow \n");
}
else
{
printf("Element deleted from queue is : %d\n", queue_array[front]);
front = front + 1;
}
} /*End of delete() */
void display()
{
int i;
if (front == - 1)
printf("Queue is empty \n");
else
{
printf("Queue is : \n");
for (i = front; i<= rear; i++)
printf("%d ", queue_array[i]);
printf("\n");
}
}
8|Page
OUTPUT
RESULT
Thus the program for array implementation of Queue was executed successfully.
9|Page
Ex.No : 1c
ARRAY IMPLEMENTATION OF CIRCULAR QUEUE
Date :
AIM
To write a C program to perform array implementation of Circular queue
ALGORITHM
PROGRAM
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define size 5
int main()
{
int arr[size],Rear=-1,Front=0,temp=0,ch,n,i,x;
for(;;)
{
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter Choice: ");
10 | P a g e
scanf("%d",&ch);
switch(ch)
{
case 1:
if(temp==size)
{
printf("Queue is full");
getch(); // pause the loop to see the message
}
else
{
printf("Enter a number ");
scanf("%d",&n);
Rear=(Rear+1)%size;
arr[Rear]=n;
temp=temp+1;
}
break;
case 2:
if(temp==0)
{
printf("Queue is empty");
11 | P a g e
break;
case 3:
if(temp==0)
{
printf("Queue is empty");
}
getch(); // pause the loop to see the numbers
}
break;
case 4:
exit(0);
break;
default:
printf("Wrong Choice");
12 | P a g e
OUTPUT
RESULT
Thus the program for array implementation of circular queue was executed successfully.
13 | P a g e
Ex.No : 02
IMPLEMENTATION OF SINGLY LINKED LIST
Date :
AIM
To write a C program to implement Singly Linked List.
ALGORITHM
1. Start the program.
2. Create a list with the menus 1) Insertion 2) Deletion 3) Display and 4) Quit in the list.
Read the option from the user. Also define the structure nodes and member functions.
3. For insertion find the right position in the list and insert the element.
4. Make necessary pointer changes after insertion.
5. For deletion find the item in the list and delete the element. If the list is empty, print an error
message as “List is Empty”.
6. Make necessary pointer changes after deletion.
7. Display the contents of the list.
.
PROGRAM
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *link;
};
struct node *start=NULL;
struct node *temp,*prev, *nod;
void insert(struct node *);
void delet();
void display();
int main(void)
{
int opt;
14 | P a g e
clrscr();
do
{
printf("\n 1.Insert element into a singly linked list\n");
printf("\n 2.Delete element from a singly linked list\n");
printf("\n 3.Display list\n");
printf("\n 4.Quit\n");
printf("\nEnter option: ");
scanf("%d",&opt);
switch(opt)
{
case 1:
delet();
break;
case 3:
display();
break;
case 4:
exit(1);
}
} while(1);
}
void insert(struct node *n)
{
int item;
printf("\nEnter the element that has to be inserted:");
15 | P a g e
scanf("%d",&item);
n->data=item;
if(start==NULL)
{
start= n;
n->link =0;
}
else
{
temp = start;
while (temp != 0)
{
if (temp->data < item)
{
prev = temp;
temp = temp->link;
}
else break;
}
if (temp == start)
{
n->link = temp;
start = n;
}
else
{
prev->link = n;
n->link = temp;
}
}
}
16 | P a g e
void delet()
{
int item;
if(start==NULL)
printf("\n List is empty! No deletion possible! \n");
else
{
temp = start;
while (temp != 0)
{
if (temp->data == item) break;
else
{
prev = temp;
temp = temp->link;
}
}
if (temp == start)
{
start = temp->link;
free(temp);
}
else
{
if (temp == 0)
{
17 | P a g e
prev->link = temp->link;
free(temp);
}
}
}
}
void display()
{
if(start == NULL)
printf("\n List is empty! \n");
else
{
printf("contents of the List: \n");
temp=start;
while(temp->link !=0)
{
printf("\n %d ",temp->data);
temp=temp->link;
}
printf("\n %d ",temp->data);
}
}
18 | P a g e
OUTPUT
RESULT
Thus the program for implementation of Singly Linked list was executed successfully.
19 | P a g e
Ex.No : 3a
LINKED LIST IMPLEMENTATION OF STACK
Date :
AIM
To write a C program to perform Linked List implementation of stack.
ALGORITHM
PROGRAM
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *ptr;
}*top,*top1,*temp;
int topelement();
void push(int data);
void pop();
void empty();
void display();
20 | P a g e
void destroy();
void stack_count();
void create();
int count = 0;
void main()
{
int no, ch, e;
printf("\n 1 - Push");
printf("\n 2 - Pop");
printf("\n 3 - Top");
printf("\n 4 - Empty");
printf("\n 5 - Exit");
printf("\n 6 - Dipslay");
printf("\n 7 - Stack Count");
printf("\n 8 - Destroy stack");
create();
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter data : ");
scanf("%d", &no);
push(no);
break;
case 2:
pop();
break;
case 3:
21 | P a g e
if (top == NULL)
printf("No elements in stack");
else
{
e = topelement();
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:
stack_count();
break;
case 8:
destroy();
break;
default :
}
}
void create()
{
top = NULL;
22 | P a g e
}
void stack_count()
{
if (top == NULL)
{
top =(struct node *)malloc(1*sizeof(struct node));
top->ptr = NULL;
top->info = data;
}
else
{
temp->info = data;
top = temp;
}
count++;
}
void display()
{
top1 = top;
if (top1 == NULL)
{
printf("Stack is empty");
return;
}
while (top1 != NULL)
23 | P a g e
{
printf("%d ", top1->info);
top1 = top1->ptr;
}
}
void pop()
{
top1 = top;
if (top1 == NULL)
{
else
top1 = top1->ptr;
printf("\n Popped value : %d", top->info);
free(top);
top = top1;
count--;
}
int topelement()
{
return(top->info);
}
void empty()
{
if (top == NULL)
printf("\n Stack is empty");
else
24 | P a g e
void destroy()
{
top1 = top;
while (top1 != NULL)
{
top1 = top->ptr;
free(top);
top = top1;
top1 = top1->ptr;
}
free(top1);
top = NULL;
printf("\n All stack elements destroyed");
count = 0;
25 | P a g e
OUTPUT
RESULT
Thus the program for Linked List implementation of Stack was executed successfully.
26 | P a g e
Ex.No : 3b
LINKED LIST IMPLEMENTATION OF LINEAR QUEUE ADT
Date :
AIM
To write a C program to implement Queue ADT by using linked list
ALGORITHM
3. Define two Node pointers 'front' and 'rear' and set both to NULL.
4. Implement the main method by displaying Menu of list of operations and make suitable v
function calls in the main method to perform user selected operation.
• Create a newNode with given value and set 'newNode ^ next' to NULL.
• If it is Not Empty then, set rear ^ next = newNode and rear = newNode.
• If it is Not Empty then, define a Node pointer 'temp' and set it to 'front'.
• If it is Not Empty then, define a Node pointer 'temp' and initialize with front.
27 | P a g e
• Display 'temp ^ data --->' and move it to the next node. Repeat the same until 'temp'
reaches to 'rear' (temp ^ next != NULL).
PROGRAM
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
{
rear->next = ptr;
rear = ptr;
}
printf("Node is Inserted\n\n");
}
int dequeue()
28 | P a g e
{
if (front == NULL)
{
printf("\nUnderflow\n");
return -1;
}
else
{
struct node *temp = front;
int temp_data = front->data;
front = front->next;
free(temp);
return temp_data;
}
}
void display() {
struct node *temp;
{
printf("The queue is \n");
temp = front;
while (temp)
{
printf("%d--->", temp->data);
temp = temp->next;
}
printf("NULL\n\n");
29 | P a g e
}
}
int main()
{
int choice, value;
printf("\nImplementation of Queue using Linked List\n");
while (choice != 4) {
printf("1.Enqueue\n2.Dequeue\n3.Display\n4.Exit\n");
printf("\nEnter your choice : ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("\nEnter the value to insert: ");
scanf("%d", &value);
enqueue(value);
break;
case 2:
printf("\nWrong Choice\n");
}
}
return 0;
}
30 | P a g e
OUTPUT
Implementation of Queue using Linked List
1. Enqueue
2. Dequeue
3. Display
4. Exit
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 2
Popped element is: 32
RESULT
Thus the C program to implement Queue ADT by using linked list is executed successfully
and the output is verified.
31 | P a g e
Ex.No : 04 IMPLEMENTATION OF POLYNOMIAL MANIPULATION
Date : USING LINKED LIST
AIM
To write a program in C language for polynomial addition using Linked List
ALGORITHM
3. Give the coefficients and the powers for the two polynomials.
PROGRAM
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int coeff;
int pow;
32 | P a g e
*poly = temp;
do
{
printf("\n Coefficient: ");
scanf("%d", &coeff);
printf("\n Exponent: ");
scanf("%d", &exp);
temp->coeff = coeff;
temp->pow = exp;
temp-> next = NULL;
printf("\nHave more terms? 1 for y and 0 for no: ");
scanf("%d", &cont);
if(cont)
{
temp->next = (struct Node*)malloc(sizeof(struct Node));
temp = temp->next;
temp->next = NULL;
}
}while(cont);
}
void displayPolynomial(struct Node* poly)
{
}
}
33 | P a g e
void addPolynomials(struct Node** result, struct Node* first, struct Node* second)
{
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->next = NULL;
*result = temp;
}
else if(first->pow < second->pow)
{
temp->coeff = second->coeff;
temp->pow = second->pow;
second = second->next;
}
else
{
temp->coeff = first->coeff + second->coeff;
temp->pow = first->pow;
first = first->next;
second = second->next;
}
if(first && second)
{
34 | P a g e
}
}
while(first || second)
}
else if(second)
{
temp->coeff = second->coeff;
temp->pow = second->pow;
second = second->next;
}
}
}
int main()
{
struct Node* first = NULL;
struct Node* second = NULL;
struct Node* result = NULL;
35 | P a g e
readPolynomial(&second);
displayPolynomial(second);
addPolynomials(&result, first, second);
displayPolynomial(result);
return 0;
}
36 | P a g e
OUTPUT
RESULT
Thus, the program for polynomial addition using linked list was written and executed
successfully
37 | P a g e
Ex.No : 5a
EVALUATING POSTFIX EXPRESSIONS
Date :
AIM
To write a program in C languages for evaluating postfix expressions
ALGORITHM
PROGRAM
#include <stdio.h>
#include <ctype.h>
#define MAXSTACK 100
}
else
{
38 | P a g e
top = top + 1;
stack[top] = item;
}
}
int pop()
{
int item;
if (top < 0)
{
printf("stack under flow");
}
else
{
item = stack[top];
top = top - 1;
return item;
}
}
void EvalPostfix(char postfix[])
{
int i;
char ch;
int val;
int A, B;
for (i = 0; postfix[i] != ')'; i++) {
ch = postfix[i];
if (isdigit(ch)) {
push(ch - '0');
}
else if (ch == '+' || ch == '-' || ch == '*' || ch == '/')
{
39 | P a g e
A = pop();
B = pop();
switch (ch) /* ch is an operator */
{
case '*':
val = B * A;
break;
case '/':
val = B / A;
break;
case '+':
val = B + A;
break;
case '-':
val = B - A;
break;
}
push(val);
}
}
printf(" \n Result of expression evaluation : %d \n", pop());
}
int main()
{
int i;
char postfix[POSTFIXSIZE];
printf(" \nEnter postfix expression,\npress right parenthesis ')' for end expression : ");
for (i = 0; i <= POSTFIXSIZE - 1; i++)
{
scanf("%c", &postfix[i]);
if (postfix[i] == ')')
40 | P a g e
{
break;
}
EvalPostfix(postfix);
return 0;
}
41 | P a g e
OUTPUT
RESULT
Thus, the program for evaluating postfix expressions was written and executed successfully.
42 | P a g e
Ex.No : 5b
IMPLEMENTATION OF INFIX TO POSTFIX CONVERSION
Date :
AIM
To write a program for the conversion of infix to postfix using Stack in C language
ALGORITHM
• If the read character is an operand, then add the operand to the postfix string.
• If the stack is not empty and precedence of the top of the stack operator is
higher than the read operator, then pop the operator from stack and add this
operator to the postfix string. Else push the operator onto stack.
4. Repeat STEP 2 till all characters are processed from the input string.
5. If stack is not empty, then pop the operator from stack and add this operator to the postfix
string.
6. Repeat STEP 4 till all the operators are popped from the stack.
7. Display the result of given infix expression or resultant postfix expression stored in a string
called postfix.
PROGRAM
#include<stdio.h>
#include<string.h>
#define operand(x) (x>='a' && x<='z' || x>='A' && x<='Z' || x>='0' && x<='9')
char infix[30],postfix[30],stack[30];
int top,i=0;
void init()
43 | P a g e
top=-1;
void push(char x)
stack[++top]=x;
char pop()
return(stack[top--]);
int isp(char x)
int y;
y=(x=='('?0:x=='^'?4:x=='*'?2:x=='/'?2:x=='+'?1:x=='-'?1:x==')'?6:-1);
return y;
int icp(char x)
int y;
y=(x=='('?4:x=='^'?4:x=='*'?2:x=='/'?2:x=='+'?1:x=='-'?1:x==')'?6:-1);
return y;
void infixtopostfix()
int j,n=0;
char x,y;
stack[++top]='\0';
44 | P a g e
if (operand(x))
postfix[n++]=x;
else
if (x==')')
while ((y=pop())!='(')
postfix[n++]=y;
else
while (isp(stack[top])>=icp(x))
postfix[n++]=pop();
push(x);
while (top>=0)
postfix[n++]=pop();
int main()
init();
clrscr();
scanf("%s",infix);
infixtopostfix();
getch();
return 0;
45 | P a g e
OUTPUT
RESULT
Thus, the program for infix to postfix conversion was executed successfully
46 | P a g e
Ex.No : 6
IMPLEMENTATION OF BINARY SEARCH TREE
Date :
AIM
PROGRAM
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
struct node *root = NULL;
struct node *create_node(int);
void insert(int);
struct node *delete (struct node *, int);
int search(int);
47 | P a g e
int main()
{
int userChoice;
printf("\n1. Insert");
printf("\n2. Delete");
printf("\n3. Search");
printf("\n4. Get Larger Node Data");
printf("\n5. Get smaller Node data");
printf("\n6. Exit");
printf("\n\nEnter Your Choice: ");
scanf("%d", &userChoice);
printf("\n");
switch(userChoice)
{
case 1:
data = get_data();
insert(data);
break;
case 2:
data = get_data();
root = delete(root, data);
break;
case 3:
data = get_data();
48 | P a g e
if (search(data) == 1)
{
printf("\nData was found!\n");
}
else
{
printf("\nData does not found!\n");
}
break;
case 4:
result = largest_node(root);
if (result != NULL)
{
result = smallest_node(root);
if (result != NULL)
{
printf("\nSmallest Data: %d\n", result->data);
}
break;
case 6:
printf("\n\nProgram was terminated\n");
break;
default:
printf("\n\tInvalid Choice\n");
break;
}
printf("\n \nDo you want to continue? ");
49 | P a g e
fflush(stdin);
scanf(" %C", &userActive);
}
return 0;
}
struct node *create_node(int data)
{
new_node->data = data;
new_node->left = NULL;
new_node->right = NULL;
return new_node;
}
void insert(int data)
{
struct node *new_node = create_node(data);
if (new_node != NULL)
{
if (root == NULL)
{
root = new_node;
printf("\n* node having data %d was inserted\n", data);
return;
}
50 | P a g e
while (temp != NULL)
{
prev = temp;
if (data > temp->data)
{
temp = temp->right;
}
else
{
temp = temp->left;
}
}
if (data > prev->data)
{
prev->right = new_node;
}
else
{
prev->left = new_node;
}
printf("\n data %d was inserted\n", data);
}
}
struct node *delete (struct node *root, int key)
{
if (root == NULL)
{
return root;
}
if (key < root->data)
51 | P a g e
{
root->left = delete (root->left, key);
}
else
{
if (root->left == NULL)
{
struct node *temp = root->right;
free(root);
return temp;
}
else if (root->right == NULL)
{
struct node *temp = root->left;
free(root);
return temp;
}
struct node *temp = smallest_node(root->right);
root->data = temp->data;
root->right = delete (root->right, temp->data);
}
return root;
}
int search(int key)
{
struct node *temp = root;
52 | P a g e
{
if (key == temp->data)
{
return 1;
}
else if (key > temp->data)
{
temp = temp->right;
}
else
{
temp = temp->left;
}
}
return 0;
}
struct node *smallest_node(struct node *root)
{
struct node *curr = root;
53 | P a g e
}
return curr;
}
int get_data()
{
int data;
printf("\nEnter Data: ");
scanf("%d", &data);
return data;
54 | P a g e
OUTPUT
55 | P a g e
RESULT
Thus the C programs to implement Binary search tree was executed successfully and
output is verified.
56 | P a g e
Ex.No : 07
IMPLEMENTATION OF AVL TREE
Date :
AIM
To write a C program to implement AVL trees
ALGORITHM
• If balanceFactor > 1, it means the height of the left subtree is greater than that of the
right subtree. So, do a right rotation or left-right rotation
• If newNodeKey < leftChildKey do right rotation.
• Else, do left-right rotation
5. If balanceFactor < -1, it means the height of the right subtree is greater than that of the
left subtree. So, do right rotation or right-left rotation
57 | P a g e
PROGRAM
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *left, *right;
int ht;
} node;
node *insert(node *, int);
node *Delete(node *, int);
void preorder(node *);
void inorder(node *);
int height(node *);
node *rotateright(node *);
node *rotateleft(node *);
node *RR(node *);
printf("\n5)Quit:");
58 | P a g e
printf("\n\nEnter Your Choice:");
scanf("%d", &op);
switch (op) {
case 1:
printf("\nEnter no. of elements:");
scanf("%d", &n);
printf("\nEnter tree data:");
root = NULL;
for (i = 0; i < n; i++) {
scanf("%d", &x);
root = insert(root, x);
}
break;
case 2:
printf("\nEnter a data:");
scanf("%d", &x);
root = insert(root, x);
break;
case 3:
printf("\nEnter a data:");
scanf("%d", &x);
root = Delete(root, x);
break;
case 4:
printf("\nPreorder sequence:\n");
preorder(root);
printf("\n\nInorder sequence:\n");
inorder(root);
printf("\n");
break;
}
59 | P a g e
}
while (op != 5);
return 0;
}
T = (node*) malloc(sizeof(node));
T->data = x;
T->left = NULL;
T->right = NULL;
}
} else if (x < T->data) {
T->left = insert(T->left, x);
if (BF(T) == 2) {
if (x < T->left->data)
T = LL(T);
else
T = LR(T);
}
}
T->ht = height(T);
return (T);
60 | P a g e
}
node * Delete(node *T, int x) {
node * p;
if (T == NULL) {
return NULL;
} else
if (x > T->data) // insert in right subtree
{
T->right = Delete(T->right, x);
if (BF(T) == 2) {
if (BF(T->left) >= 0)
T = LL(T);
else
T = LR(T);
}
} else if (x < T->data) {
if (BF(T->right) <= 0)
T = RR(T);
else
T = RL(T);
}
} else {
61 | P a g e
if (BF(T) == 2)//Rebalance during windup
{
if (BF(T->left) >= 0)
T = LL(T);
else
T = LR(T);
}
} else
return (T->left);
}
T->ht = height(T);
return (T);
}
else
rh = 1 + T->right->ht;
if (lh > rh)
return (lh);
return (rh);
}
62 | P a g e
y = x->left;
x->left = y->right;
y->right = x;
x->ht = height(x);
y->ht = height(y);
return (y);
}
node * rotateleft(node *x) {
node * y;
y = x->right;
x->right = y->left;
y->left = x;
x->ht = height(x);
y->ht = height(y);
return (y);
}
node * RR(node *T) {
T = rotateleft(T);
return (T);
}
}
node * LR(node *T) {
T->left = rotateleft(T->left);
T = rotateright(T);
return (T);
}
63 | P a g e
T = rotateleft(T);
return (T);
}
int BF(node *T) {
int lh, rh;
if (T == NULL)
return (0);
if (T->left == NULL)
lh = 0;
else
lh = 1 + T->left->ht;
if (T->right == NULL)
rh = 0;
else
rh = 1 + T->right->ht;
}
}
void inorder(node *T) {
if (T != NULL) {
inorder(T->left);
printf("%d(Bf=%d) ", T->data, BF(T));
inorder(T->right);
}
}
64 | P a g e
OUTPUT
RESULT
Thus, the program for AVL tree was written and executed successfully.
65 | P a g e
Ex.No : 08
IMPLEMENTATION OF HEAPS USING PRIORITY QUEUES
Date :
AIM
To write a C program to implement heap data structure
ALGORITHM
PROGRAM
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define MAX 10
int arr[MAX];
int i,n;
void insert(int num)
{
if(n<MAX)
{
arr[n]=num;
n++;
}
else
printf("\n Array is full");
}
void makeheap()
{
for(i=1;i<n;i++)
{
66 | P a g e
int val=arr[i];
int j=i;
int f=(j-1)/2;
while(j>0&&arr[f]<val)//creatinag a MAX heap
{
arr[j]=arr[f];
j=f;
f=(j-1)/2;
}
arr[j]=val;
}
}
void display()
{
printf("/n");
for(i=0;i<n;i++)
printf("%d",arr[i]);
}
int main()
{
insert(14);
insert(12);
insert(9);
insert(8);
insert(7);
insert(10);
insert(18);
printf("\n The Elements are .....");
display();
makeheap();
printf("\n Heapified");
display();
return 0;
}
67 | P a g e
OUTPUT
RESULT
Thus the C program to implement heap was executed successfully and output is
verified.
68 | P a g e
Ex.No : 09
IMPLEMENTATION OF DIJKSTRA’S ALGORITHM
Date :
AIM
To write a C program to implement Dijkstra’s Algorithm
ALGORITHM
1. Create cost matrix C[ ][ ] from adjacency matrix adj[ ][ ]. C[i][j] is the cost of going from
vertex i to vertex j. If there is no edge between vertices i and j then C[i][j] is infinity.
4. Create the distance matrix, by storing the cost of vertices from vertex no. 0 to n-1 from the
source vertex 0.
6. Choose a vertex w, such that distance[w] is minimum and visited[w] is 0. Mark visited[w]
as 1.
8. Only, the vertices not marked as 1 in array visited[ ] should be considered for recalculation
of distance. i.e. for each vertex v
69 | P a g e
PROGRAM
#include<stdio.h>
#include<conio.h>
#define MAX 10
int main()
clrscr();
int G[MAX][MAX],i,j,n,u;
scanf("%d",&n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
scanf("%d",&u);
dijkstra(G,n,u);
getch();
return 0;
int cost[MAX][MAX],distance[MAX],pred[MAX];
int visited[MAX],count,mindistance,nextnode,i,j;
70 | P a g e
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])
mindistance=distance[i];
nextnode=i;
71 | P a g e
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);
72 | P a g e
OUTPUT
RESULT
Thus the C program to implement Dijkstra’s Algorithm was executed successfully and
output is verified.
73 | P a g e
Ex.No : 10
IMPLEMENTATION OF PRIM’S ALGORITHM
Date :
AIM
To write a C program to implement Prim’s Algorithm
ALGORITHM
PROGRAM
#include<stdio.h>
#include<conio.h>
int n, cost[10][10];
void prim()
temp = cost[0][0];
temp = cost[i][j];
startVertex = i;
74 | P a g e
endVertex = j;
tree[0][0] = startVertex;
tree[0][1] = endVertex;
tree[0][2] = temp;
minimumCost = temp;
nr[i] = startVertex;
else
nr[i] = endVertex;
nr[startVertex] = 100;
nr[endVertex] = 100;
temp = 99;
temp = cost[j][nr[j]];
tree[i][0] = k;
tree[i][1] = nr[k];
tree[i][2] = cost[k][nr[k]];
nr[k] = 100;
75 | P a g e
for (j = 0; j < n; j++) {
nr[j] = k;
temp = 99;
printf("%d", tree[i][j]);
printf("\n");
void main() {
int i, j;
scanf("%d", &n);
scanf("%d", &cost[i][j]);
printf("%d\t", cost[i][j]);
76 | P a g e
}
printf("\n");
prim();
getch();
OUTPUT
RESULT
Thus the C program to implement Prim’s Algorithm was executed successfully
and output is verified.
77 | P a g e
Ex.No : 11a
IMPLEMENTATION OF LINEAR SEARCH
Date :
AIM
To write a C program to perform Linear Search
ALGORITHM
6. Repeat steps 3 and 4 until the search element is compared with the last element in the list.
7. If the last element in the list is also doesn't match, then display "Element not
found!!!" and terminate the function
PROGRAM
#include <stdio.h>
int main()
{
78 | P a g e
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if (c == n)
printf("%d isn't present in the array.\n", search);
return 0;
}
79 | P a g e
OUTPUT
RESULT
Thus the program to perform linear search operation was executed successfully.
.
80 | P a g e
Ex.No : 11b
IMPLEMENTATION OF BINARY SEARCH
Date :
AIM
To write a C program to perform Binary Search operation
ALGORITHM
6. If both are not matching, then check whether the search element is smaller or larger
than middle element.
7. If the search element is smaller than middle element, then repeat steps 2, 3, 4 and 5
for the left sublist of the middle element.
8. If the search element is larger than middle element, then repeat steps 2, 3, 4 and 5 for
the right sublist of the middle element.
9. Repeat the same process until we find the search element in the list or until sublist
contains only one element.
10. If that element also doesn't match with the search element, then display "Element not
found in the list!!!" and terminate the function.
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,n,item,flag=0,low,high,mid;
clrscr();
printf("Enter the size of an array: ");
81 | P a g e
scanf("%d",&n);
printf("Enter the elements in ascending order: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
{
mid=(low+high)/2;
if(item==a[mid])
{
flag=1;
break;
}
else if(item<a[mid])
high=mid-1;
else
low=mid+1;
}
if(flag==0)
printf("%d is not found",item);
else
82 | P a g e
OUTPUT
RESULT
Thus the program to perform binary search operation was executed successfully.
.
83 | P a g e
Ex.No : 12a
IMPLEMENTATION OF INSERTION SORT
Date :
AIM
To write a C program to implement Insertion sort.
ALGORITHM
1. It is efficient for smaller data sets, but very inefficient for larger lists.
2. Insertion Sort is adaptive, that means it reduces its total number of steps if a
partially sorted array is provided as input, making it efficient.
3. It is better than Selection Sort and Bubble Sort algorithms.
4. Its space complexity is less. Like bubble Sort, insertion sort also requires a single
additional memory space.
5. It is a stable sorting technique, as it does not change the relative order of elements
which are equal
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int A[10],n,i;
84 | P a g e
getch();
}
void Insert_sort(int A[10],int n)
int i,j,temp;
for(i=1;i<=n-1;i++)
{
temp=A[i];
j=i-1;
while((j>=0)&&(A[j]>temp))
{
A[j+1]=A[j];
j=j-1;
}
A[j+1]=temp;
}
85 | P a g e
OUTPUT
RESULT
Thus the program to perform insertion sort operation was executed successfully.
86 | P a g e
Ex.No : 12b
IMPLEMENTATION OF SELECTION SORT
Date :
AIM
To write a C program to implement selection sort.
ALGORITHM
1. Starting from the first element, we search the smallest element in the array, and
the first position in the subarray, with the second smallest element.
4. This is repeated, until the array is completely sorted.
PROGRAM
#include<stdio.h>
#include<conio.h>
int n;
void main()
{
int i,A[10];
87 | P a g e
getch();
}
void selection(int A[10])
int i,j,Min,temp;
for(i=0;i<=n-2;i++)
{
Min=i;
for(j=i+1;j<=n-1;j++)
{
if(A[j]<A[Min])
Min=j;
}
temp=A[i];
A[i]=A[Min];
A[Min]=temp;
}
printf("\n the sorted list is. .. \n");
for(i=0;i<n;i++)
printf("%d",A[i]);
}
88 | P a g e
OUTPUT
RESULT
Thus the program to perform selection sort operation was executed successfully.
89 | P a g e
Ex.No : 13
IMPLEMENTATION OF MERGE SORT
Date :
AIM
To write a C program to implement merge sort.
ALGORITHM
2. Divide the input array into two parts in the middle. Call it as left part and right part.
4. The above process is continued for all the elements in the array.
PROGRAM
#include<stdio.h>
int main()
int a[30],n,i;
printf("Enter no of elements:");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(a,0,n-1);
for(i=0;i<n;i++)
printf("%d ",a[i]);
90 | P a g e
return 0;
int mid;
if(i<j)
mid=(i+j)/2;
int i,j,k;
k=0;
if(a[i]<a[j])
temp[k++]=a[i++];
else
temp[k++]=a[j++];
91 | P a g e
temp[k++]=a[i++];
temp[k++]=a[j++];
for(i=i1,j=0;i<=j2;i++,j++)
a[i]=temp[j];
92 | P a g e
OUTPUT
RESULT:
Thus the above c program for sorting numbers using Merge sort was executed and the
output is verified successfully.
93 | P a g e
Ex.No : 14
IMPLEMENTATION OF OPEN ADDRESSING
Date :
AIM
To write a C program to implement open addressing linear probing and Quadratic
probing
ALGORITHM
4. The element is stored in the hash table where it can be quickly retrieved using hashed
key. hash = hashfunc(key), index = hash % array_size
PROGRAM
#include <stdio.h>
#include <conio.h>
int tsize;
int hasht(int key)
{
int i ;
i = key%tsize ;
return i;
}
int rehashl(int key)
{
int i ;
i = (key+1)%tsize ;
return i ;
}
94 | P a g e
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 ;
printf ("Enter the size of the hash table: ");
scanf ("%d",&tsize);
}
do
{
95 | P a g e
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++)
96 | P a g e
{
printf("\n Element at position %d: %d",i,hash[i]);
}
break ;
}
}while(op!=3);
getch() ;
}
97 | P a g e
OUTPUT
RESULT:
Thus the above c program for implementing linear and Quadratic probing was executed
successfully.
98 | P a g e