0% found this document useful (0 votes)
5 views

remainderdump

The document contains multiple C programming examples covering various data structures and algorithms, including arrays, stacks, trees, and search algorithms. Key functionalities include dynamic memory allocation, stack operations, binary search tree traversal, and searching techniques. Each example demonstrates specific programming concepts and practices in C.

Uploaded by

UIG
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

remainderdump

The document contains multiple C programming examples covering various data structures and algorithms, including arrays, stacks, trees, and search algorithms. Key functionalities include dynamic memory allocation, stack operations, binary search tree traversal, and searching techniques. Each example demonstrates specific programming concepts and practices in C.

Uploaded by

UIG
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Arrays-1

#include <stdio.h>

#include <stdlib.h>

int main() {

printf("Aditya Shukla | 0801EI221007 \n");

int n;

double *data;

printf("Enter the total number of elements: ");

scanf("%d", &n);

// Allocating memory for n elements

data = (double *)calloc(n, sizeof(double));

if (data == NULL) {

printf("Error!!! memory not allocated.");

exit(0);

// Storing numbers entered by the user.

for (int i = 0; i < n; ++i) {

printf("Enter number%d: ", i + 1);

scanf("%lf", data + i);

// Finding the largest number

for (int i = 1; i < n; ++i) {

if (*data < *(data + i)) {

*data = *(data + i);

}
}

printf("Largest number = %.2lf", *data);

free(data);

return 0;

Arrays3

#include <stdio.h>

#include <stdlib.h>

int main() {

int n, i;

int *arr = NULL; // Pointer to hold the array

// Ask the user for the number of elements

printf("Enter the number of elements: ");

scanf("%d", &n);

// Dynamically allocate memory for 'n' integers using malloc()

arr = (int*)malloc(n * sizeof(int));

// Check if memory allocation was successful

if (arr == NULL) {

printf("Memory allocation failed.\n");

return 1;

// Input elements into the array


printf("Enter %d elements:\n", n);

for (i = 0; i < n; i++) {

scanf("%d", &arr[i]);

// Optionally resize the array using realloc (for demonstration)

printf("Do you want to resize the array? (Enter new size or 0 to skip): ");

int new_size;

scanf("%d", &new_size);

if (new_size > 0) {

// Use realloc to resize the array

arr = (int*)realloc(arr, new_size * sizeof(int));

// Check if realloc was successful

if (arr == NULL) {

printf("Memory reallocation failed.\n");

return 1;

// If the new size is larger, input the extra elements

if (new_size > n) {

printf("Enter %d more elements:\n", new_size - n);

for (i = n; i < new_size; i++) {

scanf("%d", &arr[i]);

// Update 'n' to new size

n = new_size;

}
// Print the array in reverse order

printf("Array in reverse order:\n");

for (i = n - 1; i >= 0; i--) {

printf("%d ", arr[i]);

printf("\n");

// Free the dynamically allocated memory

free(arr);

return 0;

Arrays4

#include <stdio.h>

// Main function

int main()

int arr[100]; // Declare an array of size 100 to store integer values

int n, mm = 1, ctr = 0; // Declare variables to store array size, mm (unused), and duplicate counter

int i, j; // Declare loop counters

// Prompt the user to input the number of elements to be stored in the array

printf("Input the number of elements to be stored in the array :");

scanf("%d", &n);

// Prompt the user to input n elements into the array

printf("Input %d elements in the array :\n", n);

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

printf("element - %d : ", i);


scanf("%d", &arr[i]); // Read the input and store it in the array

// Check for duplicate elements in the array using nested loops

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

for (j = i + 1; j < n; j++)

if (arr[i] == arr[j])

ctr++; // Increment the duplicate counter if a duplicate is found

break; // Exit the inner loop as soon as a duplicate is found

// Display the total number of duplicate elements found in the array

printf("Total number of duplicate elements found in the array: %d\n", ctr);

printf("Duplicate elements are: ");

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

for(j=i+1;j<n;j++)

if(arr[i]==arr[j])

printf("%d\n",arr[i]);

getch();

return 0; // Return 0 to indicate successful execution

}
Stack13

// C program to implement a stack using singly linked list

#include <limits.h>

#include <stdio.h>

#include <stdlib.h>

// Struct representing a node in the linked list

typedef struct Node {

int data;

struct Node* next;

} Node;

Node* createNode(int new_data) {

Node* new_node = (Node*)malloc(sizeof(Node));

new_node->data = new_data;

new_node->next = NULL;

return new_node;

// Struct to implement stack using a singly linked list

typedef struct Stack {

Node* head;

} Stack;

// Constructor to initialize the stack

void initializeStack(Stack* stack) { stack->head = NULL; }

// Function to check if the stack is empty

int isEmpty(Stack* stack) {

// If head is NULL, the stack is empty


return stack->head == NULL;

// Function to push an element onto the stack

void push(Stack* stack, int new_data) {

// Create a new node with given data

Node* new_node = createNode(new_data);

// Check if memory allocation for the new node failed

if (!new_node) {

printf("\nStack Overflow");

return;

// Link the new node to the current top node

new_node->next = stack->head;

// Update the top to the new node

stack->head = new_node;

// Function to remove the top element from the stack

void pop(Stack* stack) {

// Check for stack underflow

if (isEmpty(stack)) {

printf("\nStack Underflow\n");

return;

else {
// Assign the current top to a temporary variable

Node* temp = stack->head;

// Update the top to the next node

stack->head = stack->head->next;

// Deallocate the memory of the old top node

free(temp);

// Function to return the top element of the stack

int peek(Stack* stack) {

// If stack is not empty, return the top element

if (!isEmpty(stack))

return stack->head->data;

else {

printf("\nStack is empty");

return INT_MIN;

// Driver program to test the stack implementation

int main() {

// Creating a stack

Stack stack;

initializeStack(&stack);
// Push elements onto the stack

push(&stack, 11);

push(&stack, 22);

push(&stack, 33);

push(&stack, 44);

// Print top element of the stack

printf("Top element is %d\n", peek(&stack));

// removing two elemements from the top

printf("Removing two elements...\n");

pop(&stack);

pop(&stack);

// Print top element of the stack

printf("Top element is %d\n", peek(&stack));

return 0;

Stack14

#include<stdio.h>

#include<ctype.h>

char stack[100];

int top = -1;

void push(char x)

{
stack[++top] = x;

char pop()

if(top == -1)

return -1;

else

return stack[top--];

int priority(char x)

if(x == '(')

return 0;

if(x == '+' || x == '-')

return 1;

if(x == '*' || x == '/')

return 2;

return 0;

int main()

char exp[100];

char *e, x;

printf("Enter the expression : ");

scanf("%s",exp);

printf("\n");

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());

}return 0;

Stack15

#include<stdio.h>

int stack[20];

int top = -1;


void push(int x)

stack[++top] = x;

int pop()

return stack[top--];

int main()

char exp[20];

char *e;

int n1,n2,n3,num;

printf("Enter the expression :: ");

scanf("%s",exp);

e = exp;

while(*e != '\0')

if(isdigit(*e))

num = *e - 48;

push(num);

else

n1 = pop();

n2 = pop();

switch(*e)

{
case '+':

n3 = n1 + n2;

break;

case '-':

n3 = n2 - n1;

break;

case '*':

n3 = n1 * n2;

break;

case '/':

n3 = n2 / n1;

break;

push(n3);

e++;

printf("\nThe result of expression %s = %d\n\n",exp,pop());

return 0;

}
Tree21

// Program for tree traversal inorder, postorder, preorder in Binary Tree

#include<stdio.h>

#include<stdlib.h>

// We are creating struct for the binary tree below

struct node

int data;

struct node *left, *right;

};

// newNode function for initialisation of the newly created node

struct node *newNode (int item)

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

temporary->data = item;

temporary->left = temporary->right = NULL;

return temporary;

// Here we print the postorder recursively

void postorder (struct node *root)

if (root != NULL)

postorder (root->left);

postorder (root->right);

printf ("%d ", root->data);

}
// Here we print the preorder recursively

void preorder (struct node *root)

if (root != NULL)

printf ("%d ", root->data);

preorder (root->left);

preorder (root->right);

// Here we print the inorder recursively

void inorder (struct node *root)

if (root != NULL)

inorder (root->left);

printf ("%d ", root->data);

inorder (root->right);

// Basic Program to insert new node at the correct position in BST

struct node *insert (struct node *node, int data)

/* When there no node in the tree(subtree) then create

and return new node using newNode function */

if (node == NULL)

return newNode (data);

/* If not then we recur down the tree to find correct position for insertion */
if (data < node->data)

node->left = insert (node->left, data);

else if (data > node->data)

node->right = insert (node->right, data);

return node;

int main ()

/* What our binary search tree looks like really

/\

7 14

/\/\

5 8 11 16 */

struct node *root = NULL;

root = insert (root, 9);

insert (root, 7);

insert (root, 5);

insert (root, 8);

insert (root, 14);

insert (root, 11);

insert (root, 16);

printf ("The postorder is :\n");

postorder (root);

printf ("\nThe preorder is :\n");

preorder (root);
printf ("\nThe inorder is :\n");

inorder (root);

return 0;

Tree22

#include <stdio.h>

#include <stdlib.h>

// Definition of a tree node

struct Node {

int data;

struct Node *left, *right;

};

// Function to create a new node

struct Node* createNode(int data) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = data;

newNode->left = newNode->right = NULL;

return newNode;

// Function to insert a new node into the BST

struct Node* insert(struct Node* root, int data) {

if (root == NULL) {

// If the tree is empty, return a new node

return createNode(data);

}
// Otherwise, recursively insert the data

if (data < root->data) {

root->left = insert(root->left, data);

} else if (data > root->data) {

root->right = insert(root->right, data);

return root;

// Pre-order traversal: Root -> Left -> Right

void preOrder(struct Node* root) {

if (root != NULL) {

printf("%d ", root->data);

preOrder(root->left);

preOrder(root->right);

// In-order traversal: Left -> Root -> Right

void inOrder(struct Node* root) {

if (root != NULL) {

inOrder(root->left);

printf("%d ", root->data);

inOrder(root->right);

// Post-order traversal: Left -> Right -> Root

void postOrder(struct Node* root) {


if (root != NULL) {

postOrder(root->left);

postOrder(root->right);

printf("%d ", root->data);

int main() {

struct Node* root = NULL;

int n, data, i;

// Ask user for the number of nodes

printf("Enter the number of nodes to insert in BST: ");

scanf("%d", &n);

// Insert nodes into the binary search tree

printf("Enter %d node values:\n", n);

for (i = 0; i < n; i++) {

scanf("%d", &data);

root = insert(root, data);

// Perform and display tree traversals

printf("\nPre-order Traversal: ");

preOrder(root);

printf("\nIn-order Traversal: ");

inOrder(root);

printf("\nPost-order Traversal: ");

postOrder(root);
printf("\n");

return 0;

Search23

#include <stdio.h>

int main()

int array[100], search, c, number;

printf("Enter the number of elements in array\n");

scanf("%d",&number);

printf("Enter %d numbers\n", number);

for ( c = 0 ; c < number ; c++ )

scanf("%d",&array[c]);

printf("Enter the number to search\n");

scanf("%d",&search);

for ( c = 0 ; c < number ; c++ )

if ( array[c] == search ) /* if required element found */

printf("%d is present at location %d.\n", search, c+1);

break;

if ( c == number )

printf("%d is not present in array.\n", search);

return 0;

}
Search24

//Program Name: BinarySearch.c

#include<stdio.h>

int main()

int c, first, last, middle, n, search, array[100];

printf("Enter number of elements\n");

scanf("%d",&n);

printf("Enter %d integers\n", n);

for ( c = 0 ; c < n ; c++ )

scanf("%d",&array[c]);

printf("Enter value to find\n");

scanf("%d",&search);

first = 0;

last = n - 1;

middle = (first+last)/2;

while( first <= last )

if ( array[middle] < search )

first = middle + 1;

else if ( array[middle] == search )

printf("%d found at location %d.\n", search, middle+1);

break;

else

last = middle - 1;

middle = (first + last)/2;

if ( first > last )

printf("Not found! %d is not present in the list.\n", search);


return 0;

Sort25

#include <stdio.h>

#include <conio.h>

void main()

int arr[50], num, i, j, pos, temp;

printf("Enter the number of elements in the array: ");

scanf("%d", &num);

printf("\nEnter the numbers: ");

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

scanf("%d", &arr[i]);

for(i = 0;i < (num - 1); i++)

pos = i;

for(j = i + 1; j < num; j++)

if (arr[pos] > arr[j])

pos = j;

if(pos != i)

temp = arr[i];

arr[i] = arr[pos];

arr[pos] = temp;

}
printf("\nThe array sorted in ascending order is as follows.\n");

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

printf("%d ", arr[i]);

getch();

Sort26

#include <stdio.h>

int main()

int array[100], n, c, e, swap;

printf("Enter number of elements: ");

scanf("%d", &n);

printf("Enter %d integers: ", n);

for (c = 0; c < n; c++)

scanf("%d", &array[c]);

for (c = 0 ; c < n - 1; c++)

for (e = 0 ; e < n - c - 1; e++)

if (array[e] > array[e+1]) /* For decreasing order use '<' instead of '>' */

swap = array[e];

array[e] = array[e+1];

array[e+1] = swap;

}
}

printf("Sorted list in ascending order:\n");

for (c = 0; c < n; c++)

printf("%d\n", array[c]);

return 0;

Sort27

#include <stdio.h>

int main(void)

int n, i, j, temp;

int arr[64];

printf("Enter number of elements\n");

scanf("%d", &n);

printf("Enter %d integers\n", n);

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

scanf("%d", &arr[i]);

for (i = 1; i < n; i++)

j = i;

while (j > 0 && arr[j - 1] > arr[j])

temp = arr[j];

arr[j] = arr[j - 1];


arr[j - 1] = temp;

j--;

printf("Sorted list in ascending order:\n");

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

printf("%d\n", arr[i]);

return 0;

Sort28

#include <stdio.h>

#include <stdlib.h>

// Function to merge two subarrays: arr[l..m] and arr[m+1..r]

void merge(int arr[], int l, int m, int r) {

int i, j, k;

int n1 = m - l + 1; // Size of left subarray

int n2 = r - m; // Size of right subarray

// Temporary arrays to hold the data

int *L = (int*)malloc(n1 * sizeof(int));

int *R = (int*)malloc(n2 * sizeof(int));

// Copy data to temporary arrays L[] and R[]

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

L[i] = arr[l + i];

for (j = 0; j < n2; j++)

R[j] = arr[m + 1 + j];


// Merge the two subarrays back into arr[l..r]

i = 0; // Initial index of the first subarray

j = 0; // Initial index of the second subarray

k = l; // Initial index of the merged subarray

while (i < n1 && j < n2) {

if (L[i] <= R[j]) {

arr[k] = L[i];

i++;

} else {

arr[k] = R[j];

j++;

k++;

// Copy the remaining elements of L[], if any

while (i < n1) {

arr[k] = L[i];

i++;

k++;

// Copy the remaining elements of R[], if any

while (j < n2) {

arr[k] = R[j];

j++;

k++;

}
// Free the allocated memory for temporary arrays

free(L);

free(R);

// Function to implement merge sort

void mergeSort(int arr[], int l, int r) {

if (l < r) {

// Find the middle point

int m = l + (r - l) / 2;

// Recursively sort the first and second halves

mergeSort(arr, l, m);

mergeSort(arr, m + 1, r);

// Merge the sorted halves

merge(arr, l, m, r);

int main() {

int n, i;

printf("Enter number of elements: ");

scanf("%d", &n);

int arr[n]; // Array to hold the elements

printf("Enter %d integers:\n", n);

for (i = 0; i < n; i++) {

scanf("%d", &arr[i]);

}
// Call the mergeSort function

mergeSort(arr, 0, n - 1);

// Output the sorted array

printf("Sorted array in ascending order:\n");

for (i = 0; i < n; i++) {

printf("%d ", arr[i]);

printf("\n");

return 0;

You might also like