Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 49

Program No.

-11
Write a program to implement the fibonacci series.
#include <stdio.h>
int main() {
int i, n;
// initialize first and second terms
int t1 = 0, t2 = 1;
// initialize the next term (3rd term)
int nextTerm = t1 + t2;
// get no. of terms from user
printf("Enter the number of terms: ");
scanf("%d", &n);

// print the first two terms t1 and t2


printf("Fibonacci Series: %d, %d, ", t1, t2);

// print 3rd to nth terms


for (i = 3; i <= n; ++i)
{ printf("%d, ",
nextTerm); t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
return 0;

}
OUTPUT:-
Program No.-12
Write a program to implement the tail and head recursion.
//Head recursion
#include <stdio.h>
void fun(int n)
{
if (n > 0) {
fun(n - 1);
printf("%d ", n);
}
}
// Driver code
int main()
{
int x = 3;
fun(x);
return 0;
}

//Tail Recursion
#include <stdio.h>
// Recursion function
void fun(int n)
{
if (n > 0)
{ printf("%d ", n);
fun(n - 1);
}
}
int main()
{
int x = 3;
fun(x);
return 0;
}

OUTPUT:-
Tail Recursion

Head recursion
Program No.-13
Write a program to calculate the factorial of the number using recursion
and iteration.
Using

Iteration
#include<stdio.h>
int main()
{
int i,fact=1,number;
printf("Enter a number: ");
scanf("%d",&number);

for(i=1;i<=number;i++)
{ fact=fact*i;
}
printf("Factorial of %d is: %d",number,fact);
return 0;

Using Recursion

#include<stdio.h>
long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
void main()
{
int number;
long fact;
printf("Enter a number: ");
scanf("%d", &number);

fact = factorial(number);
printf("Factorial of %d is %ld\n", number, fact);
return 0;
}
OUTPUT:-
Using Iteration

Using Recursion
Program No.-14
Write a program to implement circular queue operations using array.
#include <stdio.h>
# define max 6
int queue[max];
int front=-1;
int rear=-1;
// function to insert an element in a circular queue
void enqueue(int element)
{
if(front==-1 && rear==-1)
{
front=0;
rear=0;
queue[rear]=element;
}
else if((rear+1)%max==front)
{
printf("Queue is overflow..");
}
else
{
rear=(rear+1)%max;
queue[rear]=element;
}
}
// function to delete the element from the queue
int dequeue()
{
if((front==-1) && (rear==-1))
{
printf("\nQueue is underflow..");
}
else if(front==rear)
{
printf("\nThe dequeued element is %d", queue[front]);
front=-1;
rear=-1;
}
else
{
printf("\nThe dequeued element is %d", queue[front]);
front=(front+1)%max;
}
}
// function to display the elements of a queue
void display()
{
int i=front;
if(front==-1 && rear==-1)
{
printf("\n Queue is empty..");
}
else
{
printf("\nElements in a Queue are :");
while(i<=rear)
{
printf("%d,", queue[i]);
i=(i+1)%max;
}
}
}
int main()
{
int choice=1,x;
while(choice<4 && choice!=0)
{
printf("\n Press 1: Insert an element");
printf("\nPress 2: Delete an element");
printf("\nPress 3: Display the element");
printf("\nEnter your choice");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter the element which is to be inserted");
scanf("%d", &x);
enqueue(x);
break;
case 2:
dequeue();
break;
case 3:
display();
}}
return 0;
}
OUTPUT:-
Program No.-15
Write a program to implement circular queue operations using linked list.
#include <stdio.h>
struct node
{
int data;
struct node *next;
};
struct node *front=-1;
struct node *rear=-1;
// function to insert the element in the Queue
void enqueue(int x)
{
struct node *newnode;
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=x;
newnode->next=0;
if(rear==-1) // checking whether the Queue is empty or not.
{
front=rear=newnode;
rear->next=front;
}
else
{
rear->next=newnode;
rear=newnode;
rear->next=front;
}
}
void dequeue()
{
struct node *temp;
temp=front;
if((front==-1)&&(rear==-1))
{
printf("\nQueue is empty");
}
else if(front==rear)
{
front=rear=-1;
free(temp);
}
else
{
front=front->next;
rear->next=front;
free(temp);
}
}
int peek()
{
if((front==-1) &&(rear==-1))
{
printf("\nQueue is empty");
}
else
{
printf("\nThe front element is %d", front->data);
}
}
void display()
{
struct node *temp;
temp=front;
printf("\n The elements in a Queue are : ");
if((front==-1) && (rear==-1))
{
printf("Queue is empty");
}

else
{
while(temp->next!=front)
{
printf("%d,", temp->data);
temp=temp->next;
}
printf("%d", temp->data);
}
}
void main()
{
enqueue(34);
enqueue(10);
enqueue(23);
display();
dequeue();
peek();
}
OUTPUT:-
Program No.-16
Write a program to implement bubble sort.
#include<stdio.h>
void print(int a[], int n) //function to print array elements
{
int i;
for(i = 0; i < n; i++)
{
printf("%d ",a[i]);
}
}
void bubble(int a[], int n) // function to implement bubble sort
{
int i, j, temp;
for(i = 0; i < n; i++)
{
for(j = i+1; j < n; j++)
{
if(a[j] < a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
void main ()
{
int i, j,temp;
int a[5] = { 10, 35, 32, 13, 26};
int n = sizeof(a)/sizeof(a[0]);
printf("Before sorting array elements are - \n");
print(a, n);
bubble(a, n);
printf("\nAfter sorting array elements are - \n");
print(a, n);
}
OUTPUT:-
Program No.-17
Write a program to implement quick sort.

#include <stdio.h>
int partition (int a[], int start, int end)
{
int pivot = a[end]; // pivot element
int i = (start - 1);
for (int j = start; j <= end - 1; j++)
{
// If current element is smaller than the pivot
if (a[j] < pivot)
{
i++; // increment index of smaller element
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
int t = a[i+1];
a[i+1] = a[end];
a[end] = t;
return (i + 1);
}
{
if (start < end)
{
int p = partition(a, start, end); //p is the partitioning index
quick(a, start, p - 1);
quick(a, p + 1, end);
}
}
void printArr(int a[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", a[i]);
}
int main()
{
int a[] = { 24, 9, 29, 14, 19, 27 };
int n = sizeof(a) / sizeof(a[0]);
printf("Before sorting array elements are - \n");
printArr(a, n);
quick(a, 0, n - 1);
printf("\nAfter sorting array elements are - \n");
printArr(a, n);
return 0; }
OUTPUT:-
Program No.-18
Write a program to implement selection sort.
#include <stdio.h>
void selection(int arr[], int n)
{
int i, j, small;
for (i = 0; i < n-1; i++)
{
small = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[small])
small = j;
int temp = arr[small];
arr[small] = arr[i];
arr[i] = temp;
}
}

void printArr(int a[], int n) /* function to print the array */


{
int i;
for (i = 0; i < n; i++)
printf("%d ", a[i]);
}
int main()
{
int a[] = { 12, 31, 25, 8, 32, 17 };
int n = sizeof(a) / sizeof(a[0]);
printf("Before sorting array elements are - \n");
printArr(a, n);
selection(a, n);
printf("\nAfter sorting array elements are - \n");
printArr(a, n);
return 0;
}
OUTPUT:-
Program No. 19
Objective: Write a program in C to implement Tower of Hanoi.
Code:
#include <stdio.h>
#include <conio.h>
void hanoi(char,char,char,int);
void main()
{
int num;
clrscr();
printf("\nENTER NUMBER OF DISKS: ");
scanf("%d",&num);
printf("\nTOWER OF HANOI FOR %d NUMBER OF DISKS:\n", num);
hanoi('A','B','C',num);
getch();
}
void hanoi(char from,char to,char other,int n)
{
if(n<=0)
printf("\nILLEGAL NUMBER OF DISKS");
if(n==1)
printf("\nMOVE DISK FROM %c TO %c",from,other);
if(n>1)
{
hanoi(from,other,to,n-1);
hanoi(from,to,other,1);
hanoi(to,from,other,n-1);
}
}
Output:
Program No. 20

Objective: Write a C program to implement operations on single linked list.


#include <stdio.h>
struct node{
int val;
struct node *next;
};
void print_list(struct node *head)
{
printf("H->");

while(head)
{
printf("%d->", head->val);
head = head->next;
}

printf("|||\n");
}

void insert_front(struct node **head, int value)


{
struct node * new_node = NULL;

new_node = (struct node *)malloc(sizeof(struct node));

if (new_node == NULL)
{
printf("Failed to insert element. Out of memory");
}

new_node->val = value;

new_node->next = *head;

*head = new_node;
}

void main()
{
int count = 0, i, val;
struct node * head = NULL;

printf("Enter number of elements: ");


scanf("%d", &count);

for (i = 0; i < count; i++)


{
printf("Enter %dth element: ", i);
scanf("%d", &val);
insert_front(&head, val);
}

printf("Linked List: ");


print_list(head);
}
Output:
Program No. 21
Objective: Write a program in C to implement operations on circular linked list.
#include <stdio.h>

struct node
{ int data;
int key;

struct node *next;


};

struct node *head = NULL;


struct node *current = NULL;

bool isEmpty() {
return head == NULL;
}

int length() {
int length = 0;

if(head == NULL)
{ return 0;
}

current = head->next;

while(current != head) {
length++;
current = current->next;
}

return length;
}

void insertFirst(int key, int data) {

struct node *link = (struct node*) malloc(sizeof(struct node));


link->key = key;
link->data = data;

if (isEmpty())
{ head = link;
head->next = head;
} else {
link->next = head;

//point first to new first node


head = link;
}
}

struct node * deleteFirst() {

struct node *tempLink = head;

if(head->next == head) {
head = NULL;
return tempLink;
}

head = head->next;

return tempLink;
}

void printList() {

struct node *ptr = head;


printf("\n[ ");

if(head != NULL)

{ while(ptr->next != ptr)

{
printf("(%d,%d) ",ptr->key,ptr->data);
ptr = ptr->next;
}
}

printf(" ]");
}

void main() {
insertFirst(1,10);
insertFirst(2,20);
insertFirst(3,30);
insertFirst(4,1);
insertFirst(5,40);
insertFirst(6,56);

printf("Original List: ");


printList();

while(!isEmpty()) {
struct node *temp = deleteFirst();
printf("\nDeleted value:"); printf("(%d,
%d) ",temp->key,temp->data);
}

printf("\nList after deleting all items: ");


printList();
}

Output:
Program No. 22
Objective: Write a program in C to implement operations on double linked list.
#include <stdio.h>
struct node {
int data;
int key;
struct node *next;
struct node *prev;
};
struct node *head = NULL;
struct node *last = NULL;
struct node *current = NULL;
bool isEmpty() {
return head == NULL;
}
int length() {
int length = 0;
struct node *current;
for(current = head; current != NULL; current = current->next){
length++;
}
return length;
}
void displayForward()
{ struct node *ptr =
head; printf("\n[ ");
while(ptr != NULL) {
printf("(%d,%d) ",ptr->key,ptr->data);
ptr = ptr->next;
}
printf(" ]");
}
void displayBackward()
{ struct node *ptr =
last; printf("\n[ ");
while(ptr != NULL) {
printf("(%d,%d) ",ptr->key,ptr->data);
ptr = ptr ->prev;
}
}
void insertFirst(int key, int data) {
struct node *link = (struct node*) malloc(sizeof(struct node));
link->key = key;
link->data = data;
if(isEmpty())
{ last = link;
} else {
head->prev = link;
}
link->next = head;
head = link;
}
void insertLast(int key, int data) {
struct node *link = (struct node*) malloc(sizeof(struct node));
link->key = key;
link->data = data;
if(isEmpty()) {
last = link;
} else {
last->next = link;
link->prev = last;
}
last = link;
}
struct node* deleteFirst()
{ struct node *tempLink =
head; if(head->next == NULL){
last = NULL;
} else {
head->next->prev = NULL;
}
head = head->next;
return tempLink;
}
struct node* deleteLast()
{ struct node *tempLink =
last; if(head->next == NULL) {
head = NULL;
} else {
last->prev->next = NULL;
}
last = last->prev;
return tempLink;
}
struct node* delete(int key)
{ struct node* current = head;
struct node* previous = NULL;
if(head == NULL) {
return NULL;
}
while(current->key != key) {
if(current->next == NULL)
{ return NULL;
} else {
previous = current;
current = current->next;
}
}
if(current == head)
{ head = head->next;
} else {
current->prev->next = current->next;
}
if(current == last)
{ last = current-
>prev;
} else {
current->next->prev = current->prev;
}
return current;
}
bool insertAfter(int key, int newKey, int data)
{ struct node *current = head;
if(head == NULL)
{ return false;
}
while(current->key != key)
{ if(current->next == NULL)
{
return false;
} else {
current = current->next;
}
}
struct node *newLink = (struct node*) malloc(sizeof(struct node));
newLink->key = newKey;
newLink->data = data;
if(current == last) {
newLink->next = NULL;
last = newLink;
} else {
newLink->next = current->next;
current->next->prev = newLink;
}
newLink->prev = current;
current->next = newLink;
return true;
}
void main() {
insertFirst(1,10);
insertFirst(2,20);
insertFirst(3,30);
insertFirst(4,1);
insertFirst(5,40);
insertFirst(6,56);
printf("\nList (First to Last): ");
displayForward();
printf("\n");
printf("\nList (Last to first): ");
displayBackward();
printf("\nList , after deleting first record: ");
deleteFirst();
displayForward();
printf("\nList , after deleting last record: ");
deleteLast();
displayForward();
printf("\nList , insert after key(4) : ");
insertAfter(4,7, 13);
displayForward();
printf("\nList , after delete key(4) : ");
delete(4);
displayForward();
}

Output:
Program No. 23
Objective: Write a program in C to implement binary tree using linked list.
#include <stdio.h>
#include <malloc.h>
struct node {
struct node * left;
char data;
struct node * right;
};
struct node *constructTree( int );
void inorder(struct node *);
char array[ ] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', '\0', '\0', 'H' };
int leftcount[ ] = { 1, 3, 5, -1, 9, -1, -1, -1, -1, -1 };
int rightcount[ ] = { 2, 4, 6, -1, -1, -1, -1, -1, -1, -1 };
void main() {
struct node
*root;
root = constructTree( 0 );
printf("In-order Traversal: \n");
inorder(root);
}
struct node * constructTree( int index )
{ struct node *temp = NULL;
if (index != -1) {
temp = (struct node *)malloc( sizeof ( struct node ) );
temp->left = constructTree( leftcount[index] );
temp->data = array[index];
temp->right = constructTree( rightcount[index] );
}
return temp;
}
void inorder( struct node *root )
{ if (root != NULL) {
inorder(root->left);
printf("%c\t", root->data);
inorder(root->right);
}
}

Output:
Output

In-order Traversal:
D B H E A F C G
Program No. 24
Objective: Write a program in C to implement binary search tree using
linked list.
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *right_child;
struct node *left_child;
};
struct node* new_node(int x)
{ struct node *temp;
temp = malloc(sizeof(struct node));
temp -> data = x;
temp -> left_child = NULL;
temp -> right_child = NULL;
return temp;
}
struct node* search(struct node * root, int x)
{ if (root == NULL || root -> data == x)
return root;
else if (x > root -> data)
return search(root -> right_child, x);
else
return search(root -> left_child, x);
}
struct node* insert(struct node * root, int x) {
if (root == NULL)
return new_node(x);
else if (x > root -> data)
root -> right_child = insert(root -> right_child, x);
else
root -> left_child = insert(root -> left_child, x);
return root;
}
struct node* find_minimum(struct node * root)
{ if (root == NULL)
return NULL;
else if (root -> left_child != NULL)
return find_minimum(root -> left_child);
return root;
}
struct node* delete(struct node * root, int x) {
if (root == NULL)
return NULL;
if (x > root -> data)
root -> right_child = delete(root -> right_child, x);
else if (x < root -> data)
root -> left_child = delete(root -> left_child, x);
else {
if (root -> left_child == NULL && root -> right_child == NULL) {
free(root);
return NULL;
}
else if (root -> left_child == NULL || root -> right_child == NULL)
{ struct node *temp;
if (root -> left_child == NULL)
temp = root -> right_child;
else
temp = root -> left_child;
free(root);
return temp;
}
else {
struct node *temp = find_minimum(root -> right_child);
root -> data = temp -> data;
root -> right_child = delete(root -> right_child, temp -> data);
}
}
return root;
}
void inorder(struct node *root) {
if (root != NULL) {
inorder(root -> left_child);
printf(" %d ", root -> data);
inorder(root -> right_child);
}
}
int main() {
struct node *root;
root = new_node(20);
insert(root, 5);
insert(root, 1);
insert(root, 15);
insert(root, 9);
insert(root, 7);
insert(root, 12);
insert(root, 30);
insert(root, 25);
insert(root, 40);
insert(root, 45);
insert(root, 42);
inorder(root);
printf("\n");
root = delete(root, 1);
root = delete(root, 40);
root = delete(root, 45);
root = delete(root, 9);
inorder(root); printf("\
n");
return 0;
}

Output:
Program No. 25
Objective: Write a program in C to implement Tower of Hanoi.
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *leftChild;
struct node *rightChild;};
struct node *root = NULL;
void insert(int data) {
struct node *tempNode = (struct node*) malloc(sizeof(struct node));
struct node *current;
struct node *parent;
tempNode->data = data;
tempNode->leftChild = NULL;
tempNode->rightChild = NULL;

if(root == NULL)
{ root =
tempNode;
} else {
current = root;
parent = NULL;

while(1) {
parent = current;
if(data < parent->data) {
current = current->leftChild;
if(current == NULL) {
parent->leftChild = tempNode;
return;
}
}
else {
current = current->rightChild;
if(current == NULL) {
parent->rightChild = tempNode;
return;
}
}
}
}}
struct node* search(int data) {
struct node *current = root;
printf("Visiting elements: ");

while(current->data != data)
{ if(current != NULL)
printf("%d ",current->data);
if(current->data > data) {
current = current->leftChild;
}
else {
current = current->rightChild;
}
if(current == NULL)
{ return NULL;
}
}
return current;}
void pre_order_traversal(struct node* root)
{ if(root != NULL) {
printf("%d ",root->data);
pre_order_traversal(root->leftChild);
pre_order_traversal(root->rightChild);
}}
void inorder_traversal(struct node* root) {
if(root != NULL) {
inorder_traversal(root->leftChild);
printf("%d ",root->data);
inorder_traversal(root->rightChild);
}}
void post_order_traversal(struct node* root)
{ if(root != NULL) {
post_order_traversal(root->leftChild);
post_order_traversal(root->rightChild);
printf("%d ", root->data);
}}
int main()
{ int i;
int array[7] = { 27, 14, 35, 10, 19, 31, 42 };

for(i = 0; i < 7; i++)


insert(array[i]);

i = 31;
struct node * temp = search(i);

if(temp != NULL) {
printf("[%d] Element found.", temp->data);
printf("\n");
}else {
printf("[ x ] Element not found (%d).\n", i);
}
i = 15;
temp = search(i);

if(temp != NULL) {
printf("[%d] Element found.", temp->data);
printf("\n");
}else {
printf("[ x ] Element not found (%d).\n", i);
}

printf("\nPreorder traversal: ");


pre_order_traversal(root);

printf("\nInorder traversal: ");


inorder_traversal(root);

printf("\nPost order traversal: ");


post_order_traversal(root);

return 0;}

Output:
Visiting elements: 27 35 [31) Element found.
Visiting elements: 27 14 19 [ x] Element not found (15).

Preorder traversal: 27 14 10 19 35 31 42
Inorder traversal: 10 14 19 27 31 35 42
Post order traversal: 10 19 14 31 42 35 27
Program No. 26
Objective: Write a program in C to implement BFS using linked list.
#include<stdio.h>
#include<conio.h>
int a[20][20],q[20],visited[20],n,i,j,f=0,r=-1;
void bfs(int v) {
for (i=1;i<=n;i++) if(a[v]
[i] && !visited[i]) q[+
+r]=i;
if(f<=r) {
visited[q[f]]=1;
bfs(q[f++]);
}
}
void main()
{ int v;
printf("\n Enter the number of vertices:");
scanf("%d",&n);
for (i=1;i<=n;i++) {
q[i]=0;
visited[i]=0;
}
printf("\n Enter graph data in matrix form:\n");
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
scanf("%d",&a[i][j]);
printf("\n Enter the starting vertex:");
scanf("%d",&v);
bfs(v);
printf("\n The node which are reachable are:\n");
for (i=1;i<=n;i++)
if(visited[i]) printf("%d\
t",i); else
printf("\n Bfs is not possible");
getch();
}

Output:
Output

Enter the number of vertices:2


Enter graph data in matrix form:
1
5
8
6
Enter the starting vertex:2
The node which are reachable are:
1 2
Program No. 27
Objective: Write a program in C to implement DFS using linked list.
#include<stdio.h>
#include<conio.h>
int a[20][20],reach[20],n;
void dfs(int v)
{ int i;
reach[v]=1;
for (i=1;i<=n;i++) if(a[v]
[i] && !reach[i]) {
printf("\n %d->%d",v,i);
dfs(i);
}
}
void main() {
int i,j,count=0;
printf("\n Enter number of vertices:");
scanf("%d",&n);
for (i=1;i<=n;i++) {
reach[i]=0;
for (j=1;j<=n;j++) a[i]
[j]=0;
}
printf("\n Enter the adjacency matrix:\n");
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
scanf("%d",&a[i][j]);
dfs(1); printf("\
n");
for (i=1;i<=n;i++) {
if(reach[i])
count++;
}
if(count==n)
printf("\n Graph is connected");
else
printf("\n Graph is not connected");
getch();
}
Output:

You might also like