C Programming Codes
Name – Aditya Agarwal
Registration No. 23BCB7086
VIT-AP University
1. C program to get "n" elements and printing the elements in
the reverse order.
#include <stdio.h>
int main() {
int n, i;
// Asking the user for the number of elements
printf("Enter the number of elements: ");
scanf("%d", &n);
// Declare an array to store the elements
int arr[n];
// Get the elements from the user
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Print the elements in reverse order
printf("Elements in reverse order:\n");
for(i = n - 1; i >= 0; i--) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
2. Assume the array in any order:
a) sort the array in descending order
b) check whether the array consist of distinct elements only
#include <stdio.h>
int main() {
// Declare variables
int arr[100], n, i, j, temp, distinct = 1;
// Input the number of elements
printf("Enter the number of elements: ");
scanf("%d", &n);
// Input the elements
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Sort the array in descending order
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (arr[i] < arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
// Check if array consists of distinct elements only
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (arr[i] == arr[j]) {
distinct = 0;
break;
}
}
if (distinct == 0) {
break;
}
}
// Print sorted array in descending order
printf("Sorted array in descending order:\n");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
// Print whether array consists of distinct elements only
if (distinct == 1) {
printf("The array consists of distinct elements only.\
n");
} else {
printf("The array does not consist of distinct
elements only.\n");
}
return 0;
}
4. Write a C program to count the number of nodes in a linked
list
#include <stdio.h>
#include <stdlib.h>
// Define the structure of a node
struct Node {
int data;
struct Node* next;
};
int main() {
// Create some nodes for demonstration
struct Node* head = NULL;
struct Node* second = NULL;
struct Node* third = NULL;
// Allocate memory for nodes
head = (struct Node*)malloc(sizeof(struct Node));
second = (struct Node*)malloc(sizeof(struct Node));
third = (struct Node*)malloc(sizeof(struct Node));
// Assign data and next pointers
head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = NULL;
// Traverse the list to count nodes
struct Node* current = head;
int count = 0;
while (current != NULL) {
count++;
current = current->next;
}
// Print the count
printf("Number of nodes in the linked list: %d\n",
count);
// Free memory allocated for nodes
free(head);
free(second);
free(third);
return 0;
}
5. Create a Linked List in C and reverse the linked list
#include <stdio.h>
#include <stdlib.h>
// Define the structure of a node
struct Node {
int data;
struct Node* next;
};
// Function to reverse the linked list
struct Node* reverseLinkedList(struct Node* head) {
struct Node *prev = NULL, *current = head, *next =
NULL;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
return prev;
}
int main() {
// Create some nodes for demonstration
struct Node* head = NULL;
struct Node* second = NULL;
struct Node* third = NULL;
// Allocate memory for nodes
head = (struct Node*)malloc(sizeof(struct Node));
second = (struct Node*)malloc(sizeof(struct Node));
third = (struct Node*)malloc(sizeof(struct Node));
// Assign data and next pointers
head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = NULL;
// Print original linked list
printf("Original linked list:\n");
struct Node* current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
// Reverse the linked list
head = reverseLinkedList(head);
// Print reversed linked list
printf("Reversed linked list:\n");
current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
// Free memory allocated for nodes
free(head);
free(second);
free(third);
return 0;
}
6. Write a C program to create a Binary Search Tree and do
traversals
#include <stdio.h>
#include <stdlib.h>
// Define the structure of a node
struct Node {
int data;
struct Node* left;
struct Node* right;
};
// Function to create a new node
struct Node* createNode(int value) {
struct Node* newNode = (struct
Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
// Function to insert a new node into the binary search
tree
struct Node* insertNode(struct Node* root, int value) {
if (root == NULL) {
return createNode(value);
}
if (value < root->data) {
root->left = insertNode(root->left, value);
} else if (value > root->data) {
root->right = insertNode(root->right, value);
}
return root;
}
// In-order traversal
void inorderTraversal(struct Node* root) {
if (root != NULL) {
inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->right);
}
}
// Pre-order traversal
void preorderTraversal(struct Node* root) {
if (root != NULL) {
printf("%d ", root->data);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
}
// Post-order traversal
void postorderTraversal(struct Node* root) {
if (root != NULL) {
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ", root->data);
}
}
int main() {
struct Node* root = NULL;
// Insert elements into the binary search tree
root = insertNode(root, 8);
insertNode(root, 3);
insertNode(root, 10);
insertNode(root, 1);
insertNode(root, 6);
insertNode(root, 14);
insertNode(root, 4);
insertNode(root, 7);
insertNode(root, 13);
// Perform traversals
printf("In-order traversal: ");
inorderTraversal(root);
printf("\n");
printf("Pre-order traversal: ");
preorderTraversal(root);
printf("\n");
printf("Post-order traversal: ");
postorderTraversal(root);
printf("\n");
// Free memory allocated for nodes
// (This step is usually not necessary in real-world
applications)
free(root);
return 0;
}
7. Deletion in a Binary search tree
#include <stdio.h>
#include <stdlib.h>
// Define the structure of a node
struct Node {
int data;
struct Node* left;
struct Node* right;
};
// Function to create a new node
struct Node* createNode(int value) {
struct Node* newNode = (struct
Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
// Function to insert a new node into the binary search
tree
struct Node* insertNode(struct Node* root, int value) {
if (root == NULL) {
return createNode(value);
}
if (value < root->data) {
root->left = insertNode(root->left, value);
} else if (value > root->data) {
root->right = insertNode(root->right, value);
}
return root;
}
// Function to find the minimum value node in a tree
rooted at 'node'
struct Node* minValueNode(struct Node* node) {
struct Node* current = node;
while (current && current->left != NULL) {
current = current->left;
}
return current;
}
// Function to delete a node with a given key from the
binary search tree
struct Node* deleteNode(struct Node* root, int key) {
if (root == NULL) {
return root;
}
// If the key to be deleted is smaller than the root's key,
then it lies in the left subtree
if (key < root->data) {
root->left = deleteNode(root->left, key);
}
// If the key to be deleted is greater than the root's key,
then it lies in the right subtree
else if (key > root->data) {
root->right = deleteNode(root->right, key);
}
// If the key is the same as the root's key, then this is
the node to be deleted
else {
// Node with only one child or no child
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;
}
// Node with two children: Get the inorder successor
(smallest in the right subtree)
struct Node* temp = minValueNode(root->right);
// Copy the inorder successor's content to this node
root->data = temp->data;
// Delete the inorder successor
root->right = deleteNode(root->right, temp->data);
}
return root;
}
// Function to perform in-order traversal of the binary
search tree
void inorderTraversal(struct Node* root) {
if (root != NULL) {
inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->right);
}
}
int main() {
struct Node* root = NULL;
// Insert elements into the binary search tree
root = insertNode(root, 50);
insertNode(root, 30);
insertNode(root, 20);
insertNode(root, 40);
insertNode(root, 70);
insertNode(root, 60);
insertNode(root, 80);
printf("Binary Search Tree before deletion: ");
inorderTraversal(root);
printf("\n");
// Delete node with key 20 (leaf node)
root = deleteNode(root, 20);
printf("Binary Search Tree after deletion of node with
key 20: ");
inorderTraversal(root);
printf("\n");
// Delete node with key 30 (node with one child)
root = deleteNode(root, 30);
printf("Binary Search Tree after deletion of node with
key 30: ");
inorderTraversal(root);
printf("\n");
// Delete node with key 50 (node with two children)
root = deleteNode(root, 50);
printf("Binary Search Tree after deletion of node with
key 50: ");
inorderTraversal(root);
printf("\n");
// Free memory allocated for nodes
// (This step is usually not necessary in real-world
applications)
free(root);
return 0;
}
8. Insertion in a Binary search tree
#include <stdio.h>
#include <stdlib.h>
// Define the structure of a node
struct Node {
int data;
struct Node* left;
struct Node* right;
};
// Function to create a new node
struct Node* createNode(int value) {
struct Node* newNode = (struct
Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
// Function to insert a new node into the binary search
tree
struct Node* insertNode(struct Node* root, int value) {
if (root == NULL) {
return createNode(value);
}
if (value < root->data) {
root->left = insertNode(root->left, value);
} else if (value > root->data) {
root->right = insertNode(root->right, value);
}
return root;
}
// Function to perform in-order traversal of the binary
search tree
void inorderTraversal(struct Node* root) {
if (root != NULL) {
inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->right);
}
}
int main() {
struct Node* root = NULL;
// Insert elements into the binary search tree
root = insertNode(root, 50);
insertNode(root, 30);
insertNode(root, 20);
insertNode(root, 40);
insertNode(root, 70);
insertNode(root, 60);
insertNode(root, 80);
// Print the binary search tree using in-order traversal
printf("Binary Search Tree: ");
inorderTraversal(root);
printf("\n");
// Free memory allocated for nodes
// (This step is usually not necessary in real-world
applications)
free(root);
return 0;
}
9. Bubble sort in C
#include <stdio.h>
int main()
{
int n;
printf("Enter the size of the Element: ");
scanf("%d",&n);
int arr[n];
printf("Enter the Elements: ");
for (int i=0;i<n;i++){
scanf("%d",&arr[i]);
}
//Bubble shor
for (int i=0;i<n-1;i++){
//Flag to check if any swap accured in the current
pass
int flag=0;
for (int j=0;j<n-1-i;j++){
if(arr[j]>arr[j+1]){
//swap if the current element is grater than the
next one
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
//set flaag to 1 indicating a swap accurred
flag = 1;
}
}
//If no swapping is occurred in this pass, the array is
already sorted hence we can break the loop
if(flag==0)
break;
}
//Printing the sorted array
printf("Sorted array: ");
for (int i=0;i<n;i++){
printf("%d ",arr[i]);
}
printf("\n");
return 0;
}
10. Insertion sort in c
#include <stdio.h>
int main()
{
int n;
printf("Enter the size of the array: ");
scanf("%d",&n);
int arr[n];
printf("Enter the Elements of the Array: ");
for (int i=0;i<n;i++){
scanf("%d",&arr[i]);
}
//Loop to traverse the unsorted part of the Array
for (int i=1;i<n;i++){
int temp = arr[i];
int j=i-1;
while(j>=0&&arr[j]>temp){
arr[j+1] = arr[j]; //Shift elements to teh right
j--;
}
arr[j+1] = temp; //insert the current element at the
correct position
}
//print the sorted Array
printf("Sorted Array: ");
for (int i=0;i<n;i++){
printf("%d ",arr[i]);
}
return 0;
}
11. Selection sort in C
#include <stdio.h>
int main ()
{
int n;
printf("Enter the size of the array: ");
scanf("%d",&n);
int arr[n];
printf("Enter the Elements of the array: ");
for (int i=0;i<n;i++){
scanf("%d",&arr[i]);
}
//Selection sort
for (int i=0;i<n-1;i++){
int min = i;
for (int j=i+1;j<n;j++){
if (arr[j]<arr[min]){
min = j;
}
}
//swapping if needed
if (min!=i){
int temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
}
//Printing the sorted array
printf("Sorted Array: ");
for (int i=0;i<n;i++){
printf("%d ",arr[i]);
}
return 0;
}