Dsa
Dsa
#include <stdio.h>
if (arr[i] == target) {
return i;
return -1;
int main() {
int n, target;
scanf("%d", &n);
int arr[n];
scanf("%d", &arr[i]);
scanf("%d", &target);
if (result != -1) {
} else {
return 0;
DSA-02
#include <stdio.h>
int main() {
int n, target,arr[100],first,last,mid,found;
scanf("%d", &n);
scanf("%d", &arr[i]);
arr[j + 1] = temp;
scanf("%d", &target);
first=0;
last = n - 1;
mid=first+last/2;
found = -1;
if (arr[mid] == target) {
found = mid;
break;
first = mid + 1;
} else {
last = mid - 1;
if (found != -1) {
} else {
return 0;
} DSA-03(a)
#include <stdio.h>
int i, j, temp;
temp = arr[j];
arr[j + 1] = temp;
int main() {
int n, i;
scanf("%d", &n);
int arr[n];
scanf("%d", &arr[i]);
bubbleSort(arr, n);
printf("Sorted array:\n");
printf("\n");
return 0;
DSA-3(b)
#include <stdio.h>
int i, key, j;
for (i = 1; i < n; i++) {
j = i - 1;
arr[j + 1] = arr[j];
j = j - 1;
arr[j + 1] = key;
int main() {
int n, i;
scanf("%d", &n);
int arr[n];
scanf("%d", &arr[i]);
insertionSort(arr, n);
printf("Sorted array:\n");
printf("\n");
return 0;
DSA-4
#include <stdio.h>
int i, j, k;
i = 0;
j = 0;
k = left;
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
k++;
arr[k] = L[i];
i++;
k++;
arr[k] = R[j];
j++;
k++;
}
}
int main() {
int n, i;
scanf("%d", &n);
int arr[n];
scanf("%d", &arr[i]);
mergeSort(arr, 0, n - 1);
printf("Sorted array:\n");
printf("\n");
return 0;
DSA-5
#include<stdio.h>
arr[i]=arr[j];
arr[j]=temp;
int pivot=arr[low];
int i=low;
int j=high;
while(i<j)
i++;
}
j--;
if(i<j)
swap(arr,i,j);
swap(arr,low,j);
return j;
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;
};
s->top=-1;
return s->top==MAX-1;
return s->top==-1;
if(isfull(s))
printf("Stack is Full!\n");
else
(s->top)++;
s->items[s->top]=value;
int temp;
if(isempty(s))
printf("stack is empty!");
else
temp=s->items[s->top];
(s->top)--;
return temp;
if(isempty(s))
printf("Stack is empty!\n");
return -1;
else
return s->items[s->top];
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()
createstack(&st);
push(&st,10);
push(&st,20);
push(&st,30);
display(&st);
return 0;
DSA-9
#include <stdio.h>
#include <ctype.h>
#include <string.h>
char stack[MAX];
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];
switch (op) {
default: return 0;
int i = 0, j = 0;
char ch;
postfix[j++] = ch;
push(ch);
} else if (ch == ')') { // If ')', pop and add to postfix until '(' is found
postfix[j++] = pop();
postfix[j++] = pop();
}
push(ch);
postfix[j++] = pop();
int main() {
scanf("%s", infix);
infixToPostfix(infix, postfix);
return 0;
DSA-10
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
double stack[MAX];
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--];
char ch;
ch = postfix[i];
if (isdigit(ch)) {
} else {
op2 = pop();
op1 = pop();
switch (ch) {
default:
exit(1);
return pop();
int main() {
char postfix[MAX];
scanf("%s", postfix);
return 0;
DSA-11
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int items[MAX];
int front;
int rear;
} Queue;
void initializeQueue(Queue* q) {
q->front = -1;
q->rear = -1;
int isEmpty(Queue* q) {
int isFull(Queue* q) {
if (isFull(q)) {
return;
if (isEmpty(q)) {
q->rear++;
q->items[q->rear] = value;
int dequeue(Queue* q) {
if (isEmpty(q)) {
return -1;
} else {
q->front++;
return value;
int peek(Queue* q) {
if (isEmpty(q)) {
return -1;
return q->items[q->front];
int main() {
Queue q;
initializeQueue(&q);
while (1) {
printf("\nQueue Operations:\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Peek\n");
printf("6. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
enqueue(&q, value);
break;
case 2:
dequeue(&q);
break;
case 3:
value = peek(&q);
if (value != -1) {
break;
case 4:
if (isEmpty(&q)) {
} else {
break;
case 5:
if (isFull(&q)) {
} else {
break;
case 6:
printf("Exiting...\n");
exit(0);
default:
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;
}