Program - All Units
Program - All Units
UNIT - 1
#include <stdio.h>
#include <stdlib.h>
// Creation of node for linked list
struct node
{
int data; //Data of the node
struct node *next; //Address of the next node
}*newnode;
// Creating function for various operations
void createNodeList(int n); // function to create the list
void displayList(); // function to display the list
int main()
{
int n;
printf("\n\n Linked List : To create and display Singly Linked List :\n");
printf("-------------------------------------------------------------\n");
printf(" Input the number of nodes : ");
scanf("%d", &n);
createNodeList(n); // Calling a Function to create a list
printf("\n Data entered in the list : \n");
displayList(); // Calling a function to display the created list
return 0;
Page | 1
}
void createNodeList(int n)
{
struct node *fnNode, *tmp;
int data, i;
newnode = (struct node *)malloc(sizeof(struct node));
if(newnode == NULL) //check whether the fnnode is NULL and if so no memory allocation
{
printf(" Memory can not be allocated.");
}
else
{
// reads data for the node through keyboard
printf(" Input data for node 1 : ");
scanf("%d", &data);
newnode->data = data;
newnode->next = NULL; // links the address field to NULL
tmp = newnode;
// Creating n nodes and adding to linked list
for(i=2; i<=n; i++)
{
fnNode = (struct node *)malloc(sizeof(struct node));
if(fnNode == NULL)
{
printf(" Memory can not be allocated.");
break;
}
Page | 2
else
{
printf(" Input data for node %d : ", i);
scanf(" %d", &data);
Page | 3
}
}
}
Ouput
Page | 4
int n;
printf("\n\n Linked List : To create and display Double Linked List :\n");
printf("-------------------------------------------------------------\n");
create(); // Calling a Function to create a list
printf("\n Data entered in the list : \n");
display(); // Calling a function to display the created list
return 0;
}
void create()
{
head = 0;
int choice,data;
while(choice)
{
newnode = (struct node*)malloc(sizeof(struct node));
printf("enter data");
scanf("%d",&data);
newnode->data = data;
newnode->next = 0;
newnode->prev = 0;
if(head==0)
{
head = temp = newnode;
}
else
{
Page | 5
temp->next = newnode;
newnode->prev = temp;
}
printf("do you want to continue- enter 1 to continue");
scanf("%d",&choice);
}
//free(temp);
}
void display()
{
temp = head;
while(temp!=NULL)
{
printf("%d",temp->data);
temp = temp->next;
}
}
Ouput
Page | 6
1.c. Circular Linked List
// implementation of Circular linked list
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node * next;
}*head,*newnode,*temp;
void create();
void display();
int main()
{
//int n;
printf("\n\n Linked List : To create and display Circular Linked List :\n");
printf("-------------------------------------------------------------\n");
create(); // Calling a Function to create a list
printf("\n Data entered in the list : \n");
display(); // Calling a function to display the created list
return 0;
}
void create()
{
head=0;
int choice;
while(choice)
Page | 7
{
newnode = (struct node *)malloc(sizeof(struct node));
printf(" Enter data : ");
scanf("%d", &newnode->data);
newnode->next=0;
if(head==0)
{
head=temp=newnode;
}
else
{
temp->next=newnode;
temp=newnode;
temp->next=head;
}
printf("Enter 1 to continue");
scanf("%d", &choice);
}
}
void display()
{
temp = head;
while(temp->next!=head)
{
printf("%d",temp->data);
temp = temp->next;
Page | 8
}
}
Ouput
Page | 9
printf("-------------------------------------------------------------\n");
create(); // Calling a Function to create a list
printf("\n Data entered in the list : \n");
display(); // Calling a function to display the created list
return 0;
}
void create()
{
head = 0;
int choice,data;
while(choice)
{
newnode = (struct node*)malloc(sizeof(struct node));
printf("enter data");
scanf("%d",&data);
newnode->data = data;
newnode->next = 0;
newnode->prev = 0;
if(head==0)
{
head = temp = newnode;
}
else
{
temp->next = newnode;
newnode->prev = temp;
Page | 10
}
printf("do you want to continue- enter 1 to continue");
scanf("%d",&choice);
}
//free(temp);
}
void display()
{
temp = head;
while(temp->next!=head)
{
printf("%d",temp->data);
temp = temp->next;
}
printf("\t");
printf("%d",temp->data);
}
Ouput
Page | 11
INSERT AN ELEMENT IN LINKED LIST
#include <stdio.h>
#include <stdlib.h>
// Creation of node for linked list
struct node
{
int data; //Data of the node
struct node *next; //Address of the next node
}*newnode;
// Creating function for various operations
void createNodeList(int n); // function to create the list
void NodeInsertatEnd(int data); // function to insert at end
void NodeInsertatBegin(int data); // function to insert at beginning
void insertNodeAtMiddle(int data, int pos); // function to insert in the middle
void displayList(); // function to display the list
int main()
{
int n,data,pos;
printf("\n\n Linked List : To create and display Singly Linked List :\n");
printf("-------------------------------------------------------------\n");
printf(" Input the number of nodes : ");
scanf("%d", &n);
createNodeList(n); // Calling a Function to create a list
printf("\n Data entered in the list : \n");
displayList(); // Calling a function to display the created list
printf(" Input the number to be inserted : ");
scanf("%d", &data);
NodeInsertatEnd(data); // Calling a function to insert at END
printf("\n Data entered in the list after insert: \n");
displayList(); // Calling a function to display the created list after insert
printf(" Input the number to be inserted : ");
scanf("%d", &data);
NodeInsertatBegin(data); // Calling a function to insert at BEGINING
printf("\n Data entered in the list after insert: \n");
displayList(); // Calling a function to display the created list after insert
printf(" Input the number to be inserted : ");
scanf("%d%d", &data,&pos);
insertNodeAtMiddle(data,pos); // Calling a function to insert at given POSITION
Page | 12
printf("\n Data entered in the list after insert: \n");
displayList(); // Calling a function to display the created list after insert
return 0;
}
void createNodeList(int n)
{
struct node *fnNode, *tmp;
int data, i;
newnode = (struct node *)malloc(sizeof(struct node));
if(newnode == NULL) //check whether the fnnode is NULL and if so no memory allocation
{
printf(" Memory can not be allocated.");
}
else
{
// reads data for the node through keyboard
printf(" Input data for node 1 : ");
scanf("%d", &data);
newnode->data = data;
newnode->next = NULL; // links the address field to NULL
tmp = newnode;
// Creating n nodes and adding to linked list
for(i=2; i<=n; i++)
{
fnNode = (struct node *)malloc(sizeof(struct node));
if(fnNode == NULL)
{
printf(" Memory can not be allocated.");
break;
}
else
{
printf(" Input data for node %d : ", i);
scanf(" %d", &data);
Page | 13
}
void NodeInsertatEnd(int data)
{
struct node *fnNode, *tmp;
fnNode = (struct node*)malloc(sizeof(struct node));
if(fnNode == NULL)
{
printf(" Memory can not be allocated.");
}
else
{
printf(" Input data for insert : ");
scanf("%d", &data);
fnNode->data = data; //Links the data part
fnNode->next = NULL;
tmp = newnode;
while(tmp->next != NULL)
tmp = tmp->next;
tmp->next = fnNode; //Links the address part
}
}
Page | 14
void insertNodeAtMiddle(int data, int pos)
{
int i;
struct node *fnNode, *tmp;
fnNode = (struct node*)malloc(sizeof(struct node));
if(fnNode == NULL)
{
printf(" Memory can not be allocated.");
}
else
{
printf(" Input data for insert : ");
scanf("%d", &data);
fnNode->data = data; //Links the data part
fnNode->next = NULL;
tmp = newnode;
for(i=2; i<=pos-1; i++)
{
tmp = tmp->next;
if(tmp == NULL)
break;
}
if(tmp != NULL)
{
fnNode->next = tmp->next; //Links the address part of new node
tmp->next = fnNode;
}
else
{
printf(" Insert is not possible to the given position.\n");
}
}
}
void displayList()
{
struct node *tmp;
if(newnode == NULL)
{
printf(" List is empty.");
}
Page | 15
else
{
tmp = newnode;
while(tmp != NULL)
{
printf(" Data = %d\n", tmp->data); // prints the data of current node
tmp = tmp->next; // increments the position of current node
}
}
}
#include <stdio.h>
#include <stdlib.h>
// Creation of node for linked list
struct node
{
int data; //Data of the node
struct node *next; //Address of the next node
}*newnode;
// Creating function for various operations
void createNodeList(int n); // function to create the list
void NodeInsertatEnd(int data); // function to insert at end
void NodeInsertatBegin(int data); // function to insert at beginning
void insertNodeAtMiddle(int data, int pos); // function to insert in the middle
void displayList(); // function to display the list
int main()
{
int n,data,pos;
printf("\n\n Linked List : To create and display Singly Linked List :\n");
printf("-------------------------------------------------------------\n");
printf(" Input the number of nodes : ");
scanf("%d", &n);
createNodeList(n); // Calling a Function to create a list
printf("\n Data entered in the list : \n");
displayList(); // Calling a function to display the created list
printf(" Input the number to be inserted : ");
scanf("%d", &data);
NodeInsertatEnd(data); // Calling a function to insert at END
Page | 16
printf("\n Data entered in the list after insert: \n");
displayList(); // Calling a function to display the created list after insert
printf(" Input the number to be inserted : ");
scanf("%d", &data);
NodeInsertatBegin(data); // Calling a function to insert at BEGINING
printf("\n Data entered in the list after insert: \n");
displayList(); // Calling a function to display the created list after insert
printf(" Input the number to be inserted : ");
scanf("%d%d", &data,&pos);
insertNodeAtMiddle(data,pos); // Calling a function to insert at given POSITION
printf("\n Data entered in the list after insert: \n");
displayList(); // Calling a function to display the created list after insert
return 0;
}
void createNodeList(int n)
{
struct node *fnNode, *tmp;
int data, i;
newnode = (struct node *)malloc(sizeof(struct node));
if(newnode == NULL) //check whether the fnnode is NULL and if so no memory allocation
{
printf(" Memory can not be allocated.");
}
else
{
// reads data for the node through keyboard
printf(" Input data for node 1 : ");
scanf("%d", &data);
newnode->data = data;
newnode->next = head; // links the address field to head
tmp = newnode;
// Creating n nodes and adding to linked list
for(i=2; i<=n; i++)
{
fnNode = (struct node *)malloc(sizeof(struct node));
if(fnNode == NULL)
{
printf(" Memory can not be allocated.");
break;
}
else
{
Page | 17
printf(" Input data for node %d : ", i);
scanf(" %d", &data);
Page | 18
{
printf(" Input data for insert : ");
scanf("%d", &data);
fnNode->data = data; //Links the data part
fnNode->next = newnode; //Links the address part
newnode = fnNode; //Makes stnode as first node
}
}
void insertNodeAtMiddle(int data, int pos)
{
int i;
struct node *fnNode, *tmp;
fnNode = (struct node*)malloc(sizeof(struct node));
if(fnNode == NULL)
{
printf(" Memory can not be allocated.");
}
else
{
printf(" Input data for insert : ");
scanf("%d", &data);
fnNode->data = data; //Links the data part
fnNode->next = NULL;
tmp = newnode;
for(i=2; i<=pos-1; i++)
{
tmp = tmp->next;
if(tmp == NULL)
break;
}
if(tmp != NULL)
{
fnNode->next = tmp->next; //Links the address part of new node
tmp->next = fnNode;
}
else
{
printf(" Insert is not possible to the given position.\n");
}
}
}
Page | 19
void displayList()
{
struct node *tmp;
if(newnode == NULL)
{
printf(" List is empty.");
}
else
{
tmp = newnode;
while(tmp->next != head)
{
printf(" Data = %d\n", tmp->data); // prints the data of current node
tmp = tmp->next; // increments the position of current node
}
}
}
2.C. Double Linked List and 2.D. Circularly Double Linked List
Insert at beginning
// point previous of the first node (now first node is the second node) to
newNode
Page | 20
if ((*head) != NULL)
(*head)->prev = newNode;
// head points to newNode
(*head) = newNode;
}
Insert at a given position
Page | 21
newNode->prev = prev_node;
Insert at End
Page | 22
// if the linked list is not empty, traverse to the end of the linked list
while (temp->next != NULL)
temp = temp->next;
#include <stdio.h>
#include <stdlib.h>
// Creation of node for linked list
struct node
{
int data; //Data of the node
struct node *next; //Address of the next node
}*newnode, *tmp,*head,*prevnode;
// Creating function for various operations
void createNodeList(int n); // function to create the list
void deleteend(); // function to delete at end
void deletegivenpos(); // function to delete in the middle
void displayList(); // function to display the list
int main()
{
int n,data,pos;
printf("\n\n Linked List : To create and display Singly Linked List :\n");
printf("-------------------------------------------------------------\n");
Page | 23
printf(" Input the number of nodes : ");
scanf("%d", &n);
createNodeList(n); // Calling a Function to create a list
printf("\n Data entered in the list : \n");
displayList(); // Calling a function to display the created list
//printf(" Input the number to be deleted : ");
//scanf("%d", &data);
deleteend(); // Calling a function to delete at END
printf("\n Data entered in the list after delete: \n");
displayList(); // Calling a function to display the created list after delete
deletegivenpos(); // Calling a function to delete at given POSITION
printf("\n Data entered in the list after delete: \n");
displayList(); // Calling a function to display the created list after delete
return 0;
}
void createNodeList(int n)
{
struct node *fnNode, *tmp;
int data, i;
newnode = (struct node *)malloc(sizeof(struct node));
if(newnode == NULL) //check whether the fnnode is NULL and if so no memory
allocation
{
printf(" Memory can not be allocated.");
}
else
{
// reads data for the node through keyboard
printf(" Input data for node 1 : ");
scanf("%d", &data);
newnode->data = data;
newnode->next = head; // links the address field to head
tmp = newnode;
// Creating n nodes and adding to linked list
for(i=2; i<=n; i++)
{
fnNode = (struct node *)malloc(sizeof(struct node));
if(fnNode == NULL)
{
printf(" Memory can not be allocated.");
break;
Page | 24
}
else
{
printf(" Input data for node %d : ", i);
scanf(" %d", &data);
void deleteend()
{
struct node*prevnode;
tmp = head;
while(tmp->next!=0)
{
prevnode = tmp;
tmp = tmp->next;
if(tmp == head)
{
tmp = 0;
free(tmp);
}
else
{
prevnode->next = 0;
free(tmp);
}
}
}
void deletegivenpos()
{
struct node*nextnode;
int pos,i=1;
tmp = head;
printf("enter the position");
Page | 25
scanf("%d",&pos);
while(i<pos-1)
{
tmp = tmp->next;
i++;
}
nextnode = tmp->next;
tmp->next = nextnode->next;
free(nextnode);
}
void displayList()
{
struct node *tmp;
if(newnode == NULL)
{
printf(" List is empty.");
}
else
{
tmp = newnode;
while(tmp->next != 0)
{
printf(" Data = %d\n", tmp->data); // prints the data of current node
tmp = tmp->next; // increments the position of current node
}
}
//printf("\t");
printf(" Data = %d\n", tmp->data);
}
#include <stdio.h>
#include <stdlib.h>
// Creation of node for linked list
struct node
{
int data; //Data of the node
struct node *next; //Address of the next node
}*newnode, *tmp,*head,*prevnode;
// Creating function for various operations
void createNodeList(int n); // function to create the list
Page | 26
void deleteend(); // function to delete at end
void deletegivenpos(); // function to delete in the middle
void displayList(); // function to display the list
int main()
{
int n,data,pos;
printf("\n\n Linked List : To create and display Singly Linked List :\n");
printf("-------------------------------------------------------------\n");
printf(" Input the number of nodes : ");
scanf("%d", &n);
createNodeList(n); // Calling a Function to create a list
printf("\n Data entered in the list : \n");
displayList(); // Calling a function to display the created list
//printf(" Input the number to be deleted : ");
//scanf("%d", &data);
deleteend(); // Calling a function to delete at END
printf("\n Data entered in the list after delete: \n");
displayList(); // Calling a function to display the created list after delete
deletegivenpos(); // Calling a function to delete at given POSITION
printf("\n Data entered in the list after delete: \n");
displayList(); // Calling a function to display the created list after delete
return 0;
}
void createNodeList(int n)
{
struct node *fnNode, *tmp;
int data, i;
newnode = (struct node *)malloc(sizeof(struct node));
if(newnode == NULL) //check whether the fnnode is NULL and if so no memory
allocation
{
printf(" Memory can not be allocated.");
}
else
{
// reads data for the node through keyboard
printf(" Input data for node 1 : ");
scanf("%d", &data);
newnode->data = data;
newnode->next = head; // links the address field to head
Page | 27
tmp = newnode;
// Creating n nodes and adding to linked list
for(i=2; i<=n; i++)
{
fnNode = (struct node *)malloc(sizeof(struct node));
if(fnNode == NULL)
{
printf(" Memory can not be allocated.");
break;
}
else
{
printf(" Input data for node %d : ", i);
scanf(" %d", &data);
void deleteend()
{
struct node*prevnode;
tmp = head;
while(tmp->next!=0)
{
prevnode = tmp;
tmp = tmp->next;
if(tmp == head)
{
tmp = 0;
free(tmp);
}
else
{
prevnode->next = 0;
free(tmp);
Page | 28
}
}
}
void deletegivenpos()
{
struct node*nextnode;
int pos,i=1;
tmp = head;
printf("enter the position");
scanf("%d",&pos);
while(i<pos-1)
{
tmp = tmp->next;
i++;
}
nextnode = tmp->next;
tmp->next = nextnode->next;
free(nextnode);
}
void displayList()
{
struct node *tmp;
if(newnode == NULL)
{
printf(" List is empty.");
}
else
{
tmp = newnode;
while(tmp->next != head)
{
printf(" Data = %d\n", tmp->data); // prints the data of current node
tmp = tmp->next; // increments the position of current node
}
}
//printf("\t");
printf(" Data = %d\n", tmp->data);
}
Page | 29
3.C. Double Linked List and 3.D. Circularly Double Linked List
Page | 30
// C program to implement
// the above approach
#include <stdio.h>
#include <stdlib.h>
Page | 31
/* Change next only if node to be deleted
is NOT the last node */
if (del->next != NULL)
del->next->prev = del->prev;
// UTILITY FUNCTIONS
/* Function to insert a node at the
beginning of the Doubly Linked List */
void push(struct Node** head_ref,
int new_data)
{
// Allocate node
struct Node* new_node =
(struct Node*)malloc(sizeof(struct Node));
Page | 32
new_node->data = new_data;
Page | 33
// Start with the empty list
struct Node* head = NULL;
/* Let us create the doubly
linked list 10<->8<->4<->2 */
push(&head, 2);
push(&head, 4);
push(&head, 8);
push(&head, 10);
printf(
"Original Linked list ");
printList(head);
/* Delete nodes from the doubly
linked list */
// Delete first node
deleteNode(&head, head);
// Delete middle node
deleteNode(&head, head->next);
// Delete last node
deleteNode(&head, head->next);
/* Modified linked list will be
NULL<-8->NULL */
printf(
"Modified Linked list ");
printList(head);
getchar();
}
Page | 34
UNIT 2
IMPLEMENTATION OF STACKS USING ARRAYS
#include<stdio.h>
#include<stdlib.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:
{
Page | 35
printf("\n\t EXIT POINT ");
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()
{
Page | 36
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");
}
}
Page | 37
UNIT 3
IMPLEMENTATION OF QUEUES USING ARRAYS
SEARCHING TECHNIQUES
LINEAR SERACH
#include <stdio.h>
int main()
{
int array[100], search, c, n;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d integer(s)\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter a number to search\n");
scanf("%d", &search);
for (c = 0; c < n; c++)
{
if (array[c] == search) /* If required element is found */
{
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;
}
BINARY SEARCH
#include <stdio.h>
int iterativeBinarySearch(int array[], int start_index, int end_index, int element){
while (start_index<= end_index){
int middle = start_index + (end_index- start_index )/2;
if (array[middle] == element)
Page | 39
return middle;
if (array[middle] < element)
start_index = middle + 1;
else
end_index = middle - 1;
}
return -1;
}
int main(void){
int array[] = {1, 4, 7, 9, 16, 56, 70};
int n = 7;
int element = 16;
int found_index = iterativeBinarySearch(array, 0, n-1, element);
if(found_index == -1 ) {
printf("Element not found in the array ");
}
else {
printf("Element found at index : %d",found_index);
}
return 0;
}
SORTING TECHNIQUES
a) Bubble sort:
// C program for implementation of Bubble sort
#include <stdio.h>
Page | 40
// Last i elements are already in place
for (j = 0; j < n-i-1; j++)
if (arr[j] >arr[j+1])
swap(&arr[j], &arr[j+1]);
}
Page | 41
void printArray(int arr[], int n)
{
int i;
for (i = 0; i< n; i++)
printf("%d ", arr[i]);
printf("\n");
}
insertionSort(arr, n);
printArray(arr, n);
return 0;
}
-----------------------------------------------------------------------------------------------------------
c) Selection Sort:
// C program for implementation of selection sort
#include <stdio.h>
Page | 42
void printArray(int arr[], int size)
{
int i;
for (i=0; i< size; i++)
printf("%d ", arr[i]);
printf("\n");
}
if (largest != i)
{
temp = arr[i];
arr[i]= arr[largest];
arr[largest] = temp;
heapify(arr, size, largest);
}
}
Page | 43
{
temp = arr[0];
arr[0]= arr[i];
arr[i] = temp;
heapify(arr, i, 0);
}
}
void main()
{
int arr[] = {1, 10, 2, 3, 4, 1, 2, 100,23, 2};
int i;
int size = sizeof(arr)/sizeof(arr[0]);
heapSort(arr, size);
Page | 44
printf("Enter total no. of elements : ");
scanf("%d", &num);
printf("\nEnter %d numbers: ", num);
int main()
{
int list[50];
int size, i;
return 0;
}
void quicksort(int list[], int low, int high)
{
int pivot, i, j, temp;
if (low < high)
{
Page | 45
pivot = low;
i = low;
j = high;
while (i< j)
{
while (list[i] <= list[pivot] &&i<= high)
{
i++;
}
while (list[j] > list[pivot] && j >= low)
{
j--;
}
if (i< j)
{
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
temp = list[j];
list[j] = list[pivot];
list[pivot] = temp;
quicksort(list, low, j - 1);
quicksort(list, j + 1, high);
}
}
g) Merge sort:
#include <stdio.h>
#define max 10
int a[11] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44, 0 };
int b[10];
void merging(int low, int mid, int high) {
int l1, l2, i;
for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) {
if(a[l1] <= a[l2])
b[i] = a[l1++];
else
b[i] = a[l2++];
}
while(l1 <= mid)
b[i++] = a[l1++];
Page | 46
if(low < high) {
mid = (low + high) / 2;
sort(low, mid);
sort(mid+1, high);
merging(low, mid, high);
} else {
return;
}
}
int main() {
int i;
Page | 47
UNIT 4
BINARY SEARCH TREE
Page | 48
else if (value > node->key) {
node->right = insertNode(node->right, value);
}
return node;
}
Page | 49
int x)
{
if (root == NULL)
return NULL;
if (x > root->key) {
root->right = delete (root->right, x);
}
else if (x < root->key) {
root->left = delete (root->left, x);
}
else {
if (root->left == NULL && root->right == NULL) {
free(root);
return NULL;
}
else if (root->left == NULL
|| root->right == NULL) {
struct BinaryTreeNode* temp;
if (root->left == NULL) {
temp = root->right;
}
else {
temp = root->left;
}
free(root);
return temp;
}
else {
struct BinaryTreeNode* temp
= findMin(root->right);
root->key = temp->key;
root->right = delete (root->right, temp->key);
}
}
return root;
}
int main()
{
// Initialize the root node
struct BinaryTreeNode* root = NULL;
Page | 50
insertNode(root, 80);
printf("\n");
return 0;
}
AVL TREE
C program to insert a node in AVL tree
#include<stdio.h>
#include<stdlib.h>
Page | 51
{
int key;
struct Node *left;
struct Node *right;
int height;
};
/* Helper function that allocates a new node with the given key and
NULL left and right pointers. */
struct Node* newNode(int key)
Page | 52
{
struct Node* node = (struct Node*)
malloc(sizeof(struct Node));
node->key = key;
node->left = NULL;
node->right = NULL;
node->height = 1; // new node is initially added at leaf
return(node);
}
// Perform rotation
x->right = y;
y->left = T2;
// Update heights
y->height = max(height(y->left), height(y->right))+1;
x->height = max(height(x->left), height(x->right))+1;
Page | 53
}
// Perform rotation
y->left = x;
x->right = T2;
// Update heights
x->height = max(height(x->left), height(x->right))+1;
y->height = max(height(y->left), height(y->right))+1;
Page | 54
}
Page | 55
// there are 4 cases
Page | 56
// A utility function to print preorder traversal
// of the tree.
// The function also prints height of every node
void preOrder(struct Node *root)
{
if(root != NULL)
{
printf("%d ", root->key);
preOrder(root->left);
preOrder(root->right);
}
}
Page | 57
/* The constructed AVL Tree would be
30
/ \
20 40
/ \ \
10 25 50
*/
Page | 58
UNIT 5
Shortest Path Algorithm
// C program for Dijkstra's single source shortest path
// algorithm. The program is for adjacency matrix
// representation of the graph
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
return min_index;
}
Page | 59
{
int dist[V]; // The output array. dist[i] will hold the
// shortest
// distance from src to i
Page | 60
// print the constructed distance array
printSolution(dist);
}
// driver's code
int main()
{
/* Let us create the example graph discussed above */
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, 14, 10, 0, 2, 0, 0 },
{ 0, 0, 0, 0, 0, 2, 0, 1, 6 },
{ 8, 11, 0, 0, 0, 0, 1, 0, 7 },
{ 0, 0, 2, 0, 0, 0, 6, 7, 0 } };
// Function call
dijkstra(graph, 0);
return 0;
}
Page | 61