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

Dsa

The document contains multiple C programs demonstrating various data structures and algorithms, including linear search, binary search, sorting algorithms (bubble sort, insertion sort, merge sort, quicksort), stack operations (infix to postfix conversion, postfix evaluation), and queue operations. Each program includes user input for array elements and target values, as well as functions for sorting, searching, and managing data structures. The document serves as a comprehensive guide for implementing fundamental algorithms and data structures in C.

Uploaded by

thuruputharun
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Dsa

The document contains multiple C programs demonstrating various data structures and algorithms, including linear search, binary search, sorting algorithms (bubble sort, insertion sort, merge sort, quicksort), stack operations (infix to postfix conversion, postfix evaluation), and queue operations. Each program includes user input for array elements and target values, as well as functions for sorting, searching, and managing data structures. The document serves as a comprehensive guide for implementing fundamental algorithms and data structures in C.

Uploaded by

thuruputharun
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

DSA-01

#include <stdio.h>

int linearSearch(int arr[], int n, int target) {

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

if (arr[i] == target) {

return i;

return -1;

int main() {

int n, target;

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

scanf("%d", &n);

int arr[n];

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

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

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

printf("Enter the element to search for: ");

scanf("%d", &target);

int result = linearSearch(arr, n, target);

if (result != -1) {

printf("Element %d found at index %d\n", target, result);

} else {

printf("Element %d not found in the array\n", target);

return 0;

DSA-02
#include <stdio.h>

int main() {

int n, target,arr[100],first,last,mid,found;

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

scanf("%d", &n);

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


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

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

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

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

if (arr[j] > arr[j + 1]) {

int temp = arr[j];

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

arr[j + 1] = temp;

printf("Enter the element to search for: ");

scanf("%d", &target);

first=0;

last = n - 1;

mid=first+last/2;

found = -1;

while (first <= last) {

if (arr[mid] == target) {

found = mid;

break;

if (arr[mid] < target) {

first = mid + 1;

} else {

last = mid - 1;

if (found != -1) {

printf("Element %d found at position %d\n", target, found+1);

} else {

printf("Element %d not found in the array\n", target);

return 0;
} DSA-03(a)
#include <stdio.h>

void bubbleSort(int arr[], int n) {

int i, j, temp;

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

for (j = 0; j < n - i - 1; j++) {

if (arr[j] > arr[j + 1]) {

temp = arr[j];

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

arr[j + 1] = temp;

int main() {

int n, i;

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

scanf("%d", &n);

int arr[n];

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

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

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

bubbleSort(arr, n);

printf("Sorted array:\n");

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

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

printf("\n");

return 0;

DSA-3(b)
#include <stdio.h>

void insertionSort(int arr[], int n) {

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

key = arr[i]; // Current element to be inserted

j = i - 1;

while (j >= 0 && arr[j] > key) {

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

j = j - 1;

arr[j + 1] = key;

int main() {

int n, i;

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

scanf("%d", &n);

int arr[n];

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

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

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

insertionSort(arr, n);

printf("Sorted array:\n");

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

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

printf("\n");

return 0;

DSA-4
#include <stdio.h>

void merge(int arr[], int left, int mid, int right) {

int i, j, k;

int n1 = mid - left + 1;

int n2 = right - mid;


int L[n1], R[n2];

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

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

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

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

i = 0;

j = 0;

k = left;

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

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

arr[k] = L[i];

i++;

} else {

arr[k] = R[j];

j++;

k++;

while (i < n1) {

arr[k] = L[i];

i++;

k++;

while (j < n2) {

arr[k] = R[j];

j++;

k++;

void mergeSort(int arr[], int left, int right) {

if (left < right) {

int mid = left + (right - left) / 2;

mergeSort(arr, left, mid);

mergeSort(arr, mid + 1, right);

merge(arr, left, mid, right);

}
}

int main() {

int n, i;

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

scanf("%d", &n);

int arr[n];

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

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

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

mergeSort(arr, 0, n - 1);

printf("Sorted array:\n");

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

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

printf("\n");

return 0;

DSA-5
#include<stdio.h>

void swap(int arr[],int i, int j)

int temp = arr[i];

arr[i]=arr[j];

arr[j]=temp;

int partition(int arr[],int low , int high)

int pivot=arr[low];

int i=low;

int j=high;

while(i<j)

while(arr[i]<=pivot && i<=high-1)

i++;
}

while(arr[j]>pivot && j>=low+1)

j--;

if(i<j)

swap(arr,i,j);

swap(arr,low,j);

return j;

int quicksort(int arr[],int low,int high)

if(low<high)

int pivot=partition(arr,low,high);

quicksort(arr,low,pivot-1);

quicksort(arr,pivot+1,high);

int main()

int n,i,arr[100];

printf("Enter size:");

scanf("%d",&n);

printf("Enter elements:");

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

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

quicksort(arr,0,n-1);

printf("Sorted elements:\n");

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

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

return 0;

DSA-8
#include<stdio.h>

#include<stdlib.h>

#define MAX 5

struct stack

int items[MAX];

int top;

};

void createstack(struct stack *s)

s->top=-1;

int isfull(struct stack*s)

return s->top==MAX-1;

int isempty(struct stack*s)

return s->top==-1;

void push(struct stack*s,int value)

if(isfull(s))

printf("Stack is Full!\n");

else

(s->top)++;
s->items[s->top]=value;

printf("%d is pushed into stack\n",value);

int pop(struct stack*s)

int temp;

if(isempty(s))

printf("stack is empty!");

else

temp=s->items[s->top];

(s->top)--;

return temp;

int peek(struct stack *s)

if(isempty(s))

printf("Stack is empty!\n");

return -1;

else

return s->items[s->top];

void display(struct stack *s)

if(isempty(s))

printf("Stack is empty!\n");

}
else

printf("Stack elements:");

for(int i=0;i<=s->top;i++)

printf("%d",s->items[i]);

printf("\n");

int main()

struct stack st;

createstack(&st);

push(&st,10);

push(&st,20);

push(&st,30);

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

printf("Popped element is %d \n",pop(&st));

display(&st);

return 0;

DSA-9
#include <stdio.h>

#include <ctype.h>

#include <string.h>

#define MAX 100

char stack[MAX];

int top = -1;

void push(char ch) {

if (top == MAX - 1) {

printf("Stack Overflow\n");

return;

stack[++top] = ch;

}
char pop() {

if (top == -1) {

return '\0';

return stack[top--];

char peek() {

if (top == -1) {

return '\0';

return stack[top];

int precedence(char op) {

switch (op) {

case '+': case '-': return 1;

case '*': case '/': return 2;

case '^': return 3;

default: return 0;

void infixToPostfix(char* infix, char* postfix) {

int i = 0, j = 0;

char ch;

while ((ch = infix[i++]) != '\0') {

if (isalnum(ch)) { // If the character is an operand, add it to postfix

postfix[j++] = ch;

} else if (ch == '(') { // If '(' push to stack

push(ch);

} else if (ch == ')') { // If ')', pop and add to postfix until '(' is found

while (peek() != '(') {

postfix[j++] = pop();

pop(); // Remove '(' from stack

} else { // If an operator is encountered

while (precedence(peek()) >= precedence(ch)) {

postfix[j++] = pop();
}

push(ch);

while (top != -1) {

postfix[j++] = pop();

postfix[j] = '\0'; // Null terminate the postfix string

int main() {

char infix[MAX], postfix[MAX];

printf("Enter infix expression: ");

scanf("%s", infix);

infixToPostfix(infix, postfix);

printf("Postfix expression: %s\n", postfix);

return 0;

DSA-10
#include <stdio.h>

#include <stdlib.h>

#include <ctype.h>

#include <math.h>

#define MAX 100

double stack[MAX];

int top = -1;

void push(double value) {

if (top == MAX - 1) {

printf("Stack Overflow\n");

return;

stack[++top] = value;

double pop() {

if (top == -1) {

printf("Stack Underflow\n");

exit(1);
}

return stack[top--];

double evaluatePostfix(char* postfix) {

char ch;

double op1, op2;

for (int i = 0; postfix[i] != '\0'; i++) {

ch = postfix[i];

if (isdigit(ch)) {

push(ch - '0'); // Convert char to int

} else {

op2 = pop();

op1 = pop();

switch (ch) {

case '+': push(op1 + op2); break;

case '-': push(op1 - op2); break;

case '*': push(op1 * op2); break;

case '/': push(op1 / op2); break;

case '^': push(pow(op1, op2)); break;

default:

printf("Invalid operator: %c\n", ch);

exit(1);

return pop();

int main() {

char postfix[MAX];

printf("Enter a postfix expression (single-digit operands): ");

scanf("%s", postfix);

double result = evaluatePostfix(postfix);

printf("The result of the postfix expression is: %.2f\n", result);

return 0;

DSA-11
#include <stdio.h>

#include <stdlib.h>

#define MAX 100

typedef struct {

int items[MAX];

int front;

int rear;

} Queue;

void initializeQueue(Queue* q) {

q->front = -1;

q->rear = -1;

int isEmpty(Queue* q) {

return q->front == -1;

int isFull(Queue* q) {

return q->rear == MAX - 1;

void enqueue(Queue* q, int value) {

if (isFull(q)) {

printf("Queue is full! Cannot enqueue %d.\n", value);

return;

if (isEmpty(q)) {

q->front = 0; // Set front to 0 if it's the first element

q->rear++;

q->items[q->rear] = value;

printf("%d enqueued to the queue.\n", value);

int dequeue(Queue* q) {

if (isEmpty(q)) {

printf("Queue is empty! Cannot dequeue.\n");

return -1;

int value = q->items[q->front];


if (q->front == q->rear) {

q->front = q->rear = -1;

} else {

q->front++;

printf("%d dequeued from the queue.\n", value);

return value;

int peek(Queue* q) {

if (isEmpty(q)) {

printf("Queue is empty! No front element.\n");

return -1;

return q->items[q->front];

int main() {

Queue q;

initializeQueue(&q);

int choice, value;

while (1) {

printf("\nQueue Operations:\n");

printf("1. Enqueue\n");

printf("2. Dequeue\n");

printf("3. Peek\n");

printf("4. Check if Empty\n");

printf("5. Check if Full\n");

printf("6. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter the value to enqueue: ");

scanf("%d", &value);

enqueue(&q, value);

break;

case 2:
dequeue(&q);

break;

case 3:

value = peek(&q);

if (value != -1) {

printf("Front element: %d\n", value);

break;

case 4:

if (isEmpty(&q)) {

printf("The queue is empty.\n");

} else {

printf("The queue is not empty.\n");

break;

case 5:

if (isFull(&q)) {

printf("The queue is full.\n");

} else {

printf("The queue is not full.\n");

break;

case 6:

printf("Exiting...\n");

exit(0);

default:

printf("Invalid choice! Please try again.\n");

return 0;

DSA-12
#include <stdio.h>
#include <stdlib.h>
struct node {
int item;
struct node* left;
struct node* right;
};
void inorderTraversal(struct node* root) {
if (root == NULL) return;
inorderTraversal(root->left);
printf("%d -> ", root->item);
inorderTraversal(root->right);
}
void preorderTraversal(struct node* root) {
if (root == NULL) return;
printf("%d -> ", root->item);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
void postorderTraversal(struct node* root) {
if (root == NULL) return;
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d -> ", root->item);
}
struct node* createNode(int value) {
struct node* newNode = (struct node*)malloc(sizeof(struct node));
newNode->item = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
struct node* insertNode() {
int value;
char choice;
printf("Enter value for the node (-1 for NULL): ");
scanf("%d", &value);
if (value == -1) return NULL;
struct node* newNode = createNode(value);
printf("Do you want to insert a left child for %d? (y/n): ", value);
scanf(" %c", &choice);
if (choice == 'y' || choice == 'Y') {
newNode->left = insertNode();
}
printf("Do you want to insert a right child for %d? (y/n): ", value);
scanf(" %c", &choice);
if (choice == 'y' || choice == 'Y') {
newNode->right = insertNode();
}
return newNode;
}
int main() {
printf("Create the binary tree:\n");
struct node* root = insertNode();
int traversalChoice;
printf("\nChoose the traversal method:\n");
printf("1. Inorder\n2. Preorder\n3. Postorder\n");
printf("Enter your choice (1/2/3): ");
scanf("%d", &traversalChoice);
printf("\nTraversal result:\n");
switch (traversalChoice) {
case 1:
printf("Inorder Traversal: ");
inorderTraversal(root);
break;
case 2:
printf("Preorder Traversal: ");
preorderTraversal(root);
break;
case 3:
printf("Postorder Traversal: ");
postorderTraversal(root);
break;
default:
printf("Invalid choice.\n");
break;
}
printf("\n");
return 0;
}

DSA-13
#include<stdio.h>
#include<stdlib.h>
struct BinaryTreeNode
{
int key;
struct BinaryTreeNode *left, *right;
};
struct BinaryTreeNode* newNodeCreate(int value)
{
struct BinaryTreeNode* temp = (struct BinaryTreeNode*)malloc(sizeof(struct BinaryTreeNode));
temp->key = value;
temp->left = temp->right = NULL;
return temp;
}
struct BinaryTreeNode* searchNode(struct BinaryTreeNode* root, int target)
{
if (root == NULL || root->key == target) {
return root;
}
if (root->key < target) {
return searchNode(root->right, target);
}
return searchNode(root->left, target);
}
struct BinaryTreeNode*insertNode(struct BinaryTreeNode* node, int value)
{
if (node == NULL) {
return newNodeCreate(value);
}
if (value < node->key) {
node->left = insertNode(node->left, value);
}
else if (value > node->key) {
node->right = insertNode(node->right, value);
}
return node;
}
int main()
{
int i,j=0,search,a[50];
struct BinaryTreeNode* root = NULL;
printf("Enter elements of tree=\n");
for(i=0;i<7;i++)
scanf("%d",&a[i]);
root = insertNode(root,a[j]);
insertNode(root,a[j+1]);
insertNode(root, a[j+2]);
insertNode(root, a[j+3]);
insertNode(root, a[j+4]);
insertNode(root, a[j+5]);
insertNode(root, a[j+6]);
printf("Enter search node=\n");
scanf("%d",&search);
if (searchNode(root, search) != NULL) {
printf("%d found",search);
}
else {
printf("%d not found",search);
}
printf("\n");
return 0;
}

You might also like