#include <stdio.h>
int main()
{
int i, low, high, mid, n, key, array[100];
printf(“Enter number of elementsn”);
scanf(“%d”,&n);
printf(“Enter %d integersn”, n);
for(i = 0; i < n; i++)
scanf(“%d”,&array[i]);
printf(“Enter value to findn”);
scanf(“%d”, &key);
low = 0;
high = n – 1;
mid = (low+high)/2;
while (low <= high) {
if(array[mid] < key)
low = mid + 1;
else if (array[mid] == key) {
printf(“%d found at location %d.n”, key, mid+1);
break;
}
else
high = mid – 1;
mid = (low + high)/2;
}
if(low > high)
printf(“Not found! %d isn’t present in the list.n”, key);
return 0;
}
Double linked list
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node* next;
struct Node* prev;
};
void insertHead(struct Node** head, int data)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = (*head);
newNode->prev = NULL;
if ((*head) != NULL)
(*head)->prev = newNode;
(*head) = newNode;
}
void insertBetween(struct Node* prev_node, int data)
{
if (prev_node == NULL) {
printf(“previous node cannot be null”);
return;
}
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = prev_node->next;
prev_node->next = newNode;
newNode->prev = prev_node;
if (newNode->next != NULL)
newNode->next->prev = newNode;
}
void inserTail(struct Node** head, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
struct Node* temp = *head;
if (*head == NULL) {
newNode->prev = NULL;
*head = newNode;
return;
}
while (temp->next != NULL)
temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
}
void deleteNode(struct Node** head, struct Node* del_node)
{
if (*head == NULL || del_node == NULL)
return;
if (*head == del_node)
*head = del_node->next;
if (del_node->next != NULL)
del_node->next->prev = del_node->prev;
if (del_node->prev != NULL)
del_node->prev->next = del_node->next;
free(del_node);
}
void displayList(struct Node* node)
{
struct Node* last;
while (node != NULL) {
printf("%d->", node->data);
last = node;
node = node->next;
}
if (node == NULL)
printf("NULL\n");
}
int main()
{
struct Node* head = NULL;
insertTail(&head, 5);
insertHead(&head, 1);
insertHead(&head, 6);
insertTail(&head, 9);
insertBetween(head, 11);
insertBetween(head->next, 15);
displayList(head);
deleteNode(&head, head->next->next->next->next->next);
displayList(head);
}
Single linked list
#include <stdio.h>
#include <stdlib.h>
// Creating a node
struct node {
int value;
struct node *next;
};
// print the linked list value
void printLinkedlist(struct node *p) {
while (p != NULL) {
printf("%d ", p->value);
p = p->next;
}
}
int main() {
// Initialize nodes
struct node *head;
struct node *one = NULL;
struct node *two = NULL;
struct node *three = NULL;
// Allocate memory
one = malloc(sizeof(struct node));
two = malloc(sizeof(struct node));
three = malloc(sizeof(struct node));
// Assign value values
one->value = 1;
two->value = 2;
three->value = 3;
// Connect nodes
one->next = two;
two->next = three;
three->next = NULL;
// printing node-value
head = one;
printLinkedlist(head);
}
Bubble sort
#include<stdio.h>
int main()
{
int a[10],i,j,temp,n;
printf("\n Enter the max no.of Elements to Sort: \n");
scanf("%d",&n);
printf("\n Enter the Elements : \n");
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
for(i=0; i<n; i++)
for(j=i+1; j<n; j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
for(i=0; i<n; i++)
{
printf("%d\t",a[i]);
}
return 0;
}
Selection sort
#include <stdio.h>
int main()
{
int array[100], n, c, d, position, t;
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]);
for (c = 0; c < (n - 1); c++) // finding minimum element (n-1) times
{
position = c;
for (d = c + 1; d < n; d++)
{
if (array[position] > array[d])
position = d;
}
if (position != c)
{
t = array[c];
array[c] = array[position];
array[position] = t;
}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
return 0;
}
Insertion sort
#include<stdio.h>
void InsertionSort(int a[], int n)
{
int j, p;
int tmp;
for(p = 1; p < n; p++)
{
tmp = a[p];
for(j = p; j > 0 && a[j-1] > tmp; j--)
a[j] = a[j-1];
a[j] = tmp;
}
}
int main()
{
int i, n, a[10];
printf("Enter the number of elements :: ");
scanf("%d",&n);
printf("Enter the elements :: ");
for(i = 0; i < n; i++)
{
scanf("%d",&a[i]);
}
InsertionSort(a,n);
printf("The sorted elements are :: ");
for(i = 0; i < n; i++)
printf("%d ",a[i]);
printf("\n");
return 0;
}
Binary tree program traversal
#include<stdio.h>
typedef struct node
{
int data;
struct node *left;
struct node *right;
} node;
node *create()
{
node *p;
int x;
printf("Enter data(-1 for no data):");
scanf("%d",&x);
if(x==-1)
return NULL;
p=(node*)malloc(sizeof(node));
p->data=x;
printf("Enter left child of %d:\n",x);
p->left=create();
printf("Enter right child of %d:\n",x);
p->right=create();
return p;
}
void preorder(node *t) //address of root node is passed in t
{
if(t!=NULL)
{
printf("\n%d",t->data); //visit the root
preorder(t->left); //preorder traversal on left subtree
preorder(t->right); //preorder traversal om right subtree
}
}
int main()
{
node *root;
root=create();
printf("\nThe preorder traversal of tree is:\n");
preorder(root);
return 0;
}
Linear search
#include<stdio.h>
int main()
{
int a[20],i,x,n;
printf("How many elements?");
scanf("%d",&n);
printf("Enter array elements:n");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
printf("nEnter element to search:");
scanf("%d",&x);
for(i=0;i<n;++i)
if(a[i]==x)
break;
if(i<n)
printf("Element found at index %d",i);
else
printf("Element not found");
return 0;
}
Matrix multiplication
#include<stdio.h>
#define ROWS 3
#define COLS 3
void matrix_multiply(int mat1[ROWS][COLS], int mat2[ROWS][COLS], int result[ROWS][COLS]) {
int i, j, k;for (i = 0; i < ROWS; i++) { for (j = 0; j < COLS; j++) { result[i][j] = 0; for (k = 0; k < COLS; k++) { result[i][j] += mat1[i][k] * mat2[k][j]; } } }
}
void print_matrix(int mat[ROWS][COLS]) {
int i, j;for (i = 0; i < ROWS; i++) { for (j = 0; j < COLS; j++) { printf("%d ", mat[i][j]); } printf("\n"); }
}
int main() {
int mat1[ROWS][COLS] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
int mat2[ROWS][COLS] = { {9, 8, 7}, {6, 5, 4}, {3, 2, 1} };
int result[ROWS][COLS];matrix_multiply(mat1, mat2, result); printf("Matrix 1:\n"); print_matrix(mat1); printf("\nMatrix 2:\n"); print_matrix(mat2); printf("\nResult:\n"); print_matrix(result); return 0;
}
Infix to postfix
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#define MAX_STACK_SIZE 100
// Stack implementation
struct Stack {
int top;
char items[MAX_STACK_SIZE];
};
void push(struct Stack* s, char item) {
if (s->top == MAX_STACK_SIZE – 1) {
printf(“Stack overflow\n”);
return;
}
s->items[++s->top] = item;
}
char pop(struct Stack* s) {
if (s->top == -1) {
printf(“Stack underflow\n”);
return ‘\0’;
}
char item = s->items[s->top];
s->top–;
return item;
}
char peek(struct Stack* s) {
return s->items[s->top];
}
int is_operator(char ch) {
if (ch == ‘+’ || ch == ‘-‘ || ch == ‘*’ || ch == ‘/’) {
return 1;
}
return 0;
}
int precedence(char ch) {
if (ch == ‘*’ || ch == ‘/’) {
return 2;
} else if (ch == ‘+’ || ch == ‘-‘) {
return 1;
} else {
return 0;
}
}
void infix_to_postfix(char* infix, char* postfix) {
int i, j;
struct Stack s;
s.top = -1;for (i = 0, j = 0; infix[i] != '\0'; i++) { if (isalnum(infix[i])) { postfix[j++] = infix[i]; } else if (is_operator(infix[i])) { while (s.top != -1 && precedence(peek(&s)) >= precedence(infix[i])) { postfix[j++] = pop(&s); } push(&s, infix[i]); } else if (infix[i] == '(') { push(&s, infix[i]); } else if (infix[i] == ')') { while (s.top != -1 && peek(&s) != '(') { postfix[j++] = pop(&s); } if (s.top == -1) { printf("Invalid expression\n"); return; } pop(&s); } } while (s.top != -1) { if (s.items[s.top] == '(') { printf("Invalid expression\n"); return; } postfix[j++] = pop(&s); } postfix[j] = '\0';
}
int main() {
char infix[100], postfix[100];printf("Enter infix expression: "); scanf("%s", infix); infix_to_postfix(infix, postfix); printf("Postfix expression: %s\n", postfix); return 0;
}
Stack operations
#include<stdio.h>
include<stdlib.h>
define MAX_SIZE 100
struct Stack {
int items[MAX_SIZE];
int top;
};
typedef struct Stack stack;
void init_stack(stack *s) {
s->top = -1;
}
int is_empty(stack *s) {
return s->top == -1;
}
int is_full(stack *s) {
return s->top == MAX_SIZE – 1;
}
void push(stack *s, int value) {
if (is_full(s)) {
printf(“Stack is full!\n”);
return;
}
s->top++;
s->items[s->top] = value;
}
int pop(stack *s) {
if (is_empty(s)) {
printf(“Stack is empty!\n”);
return -1;
}
int value = s->items[s->top];
s->top–;
return value;
}
int peek(stack *s) {
if (is_empty(s)) {
printf(“Stack is empty!\n”);
return -1;
}
return s->items[s->top];
}
int size(stack *s) {
return s->top + 1;
}
int main() {
stack s;
init_stack(&s);
push(&s, 1);
push(&s, 2);
push(&s, 3);
printf(“Current stack: “);
while (!is_empty(&s)) {
printf(“%d “, peek(&s));
pop(&s);
}
printf(“\n”);
return 0;
}