├── bin_tree ├── bin_tree.c ├── insersion sort.c ├── linear search.c ├── linked list.c ├── queue.c ├── revwithoutswap.java └── stack implementation.c /bin_tree: -------------------------------------------------------------------------------- 1 | #include 2 | int recursiveBinarySearch(int array[], int start_index, int end_index, int element){ 3 | if (end_index >= start_index){ 4 | int middle = start_index + (end_index - start_index )/2; 5 | if (array[middle] == element) 6 | return middle; 7 | if (array[middle] > element) 8 | return recursiveBinarySearch(array, start_index, middle-1, element); 9 | return recursiveBinarySearch(array, middle+1, end_index, element); 10 | } 11 | return -1; 12 | } 13 | int main(void){ 14 | int array[] = {1, 4, 7, 9, 16, 56, 70}; 15 | int n = 7; 16 | int element = 9; 17 | int found_index = recursiveBinarySearch(array, 0, n-1, element); 18 | if(found_index == -1 ) { 19 | printf("Element not found in the array "); 20 | } 21 | else { 22 | printf("Element found at index : %d",found_index); 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /bin_tree.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | struct bin_tree { 6 | int data; 7 | struct bin_tree * right, * left; 8 | }; 9 | typedef struct bin_tree node; 10 | 11 | void insert(node ** tree, int val) 12 | { 13 | node *temp = NULL; 14 | if(!(*tree)) 15 | { 16 | temp = (node *)malloc(sizeof(node)); 17 | temp->left = temp->right = NULL; 18 | temp->data = val; 19 | *tree = temp; 20 | return; 21 | } 22 | if(val < (*tree)->data) 23 | { 24 | insert(&(*tree)->left, val); 25 | } 26 | else if(val > (*tree)->data) 27 | { 28 | insert(&(*tree)->right, val); 29 | } 30 | 31 | } 32 | void print_preorder(node * tree) 33 | { 34 | if (tree) 35 | { 36 | printf("%d\n",tree->data); 37 | print_preorder(tree->left); 38 | print_preorder(tree->right); 39 | } 40 | 41 | } 42 | void print_inorder(node * tree) 43 | { 44 | if (tree) 45 | { 46 | print_inorder(tree->left); 47 | printf("%d\n",tree->data); 48 | print_inorder(tree->right); 49 | } 50 | } 51 | void print_postorder(node * tree) 52 | { 53 | if (tree) 54 | { 55 | print_postorder(tree->left); 56 | print_postorder(tree->right); 57 | printf("%d\n",tree->data); 58 | } 59 | } 60 | void deltree(node * tree) 61 | { 62 | if (tree) 63 | { 64 | deltree(tree->left); 65 | deltree(tree->right); 66 | free(tree); 67 | } 68 | } 69 | node* search(node ** tree, int val) 70 | { 71 | if(!(*tree)) 72 | { 73 | return NULL; 74 | } 75 | 76 | if(val < (*tree)->data) { 77 | search(&((*tree)->left), val); 78 | } 79 | else if(val > (*tree)->data){ 80 | search(&((*tree)->right), val); 81 | } 82 | else if(val == (*tree)->data) { 83 | return *tree; 84 | } 85 | } 86 | void main() 87 | { 88 | node *root; 89 | node *tmp; 90 | //int i; 91 | root = NULL; 92 | insert(&root, 9); 93 | insert(&root, 4); 94 | insert(&root, 15); 95 | insert(&root, 6); 96 | insert(&root, 12); 97 | insert(&root, 17); 98 | insert(&root, 2); 99 | printf("Pre Order Display\n"); 100 | print_preorder(root); 101 | printf("In Order Display\n"); 102 | print_inorder(root); 103 | printf("Post Order Display\n"); 104 | print_postorder(root); 105 | tmp = search(&root, 4); 106 | if (tmp){ 107 | printf("Searched node=%d\n", tmp->data); 108 | } 109 | else { 110 | printf("Data Not found in tree.\n"); 111 | } 112 | deltree(root); 113 | } 114 | -------------------------------------------------------------------------------- /insersion sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int n, i, j, temp; 5 | int arr[64]; 6 | 7 | printf("Enter number of elements\n"); 8 | scanf("%d", &n); 9 | 10 | printf("Enter %d integers\n", n); 11 | for (i = 0; i < n; i++) 12 | { 13 | scanf("%d", &arr[i]); 14 | } 15 | for (i = 1 ; i <= n - 1; i++) 16 | { 17 | j = i; 18 | while ( j > 0 && arr[j-1] > arr[j]) 19 | { 20 | temp = arr[j]; 21 | arr[j] = arr[j-1]; 22 | arr[j-1] = temp; 23 | j--; 24 | } 25 | } 26 | printf("Sorted list in ascending order:\n"); 27 | for (i = 0; i <= n - 1; i++) 28 | { 29 | printf("%d\n", arr[i]); 30 | } 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /linear search.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int a[20],i,x,n; 5 | printf("How many elements?"); 6 | scanf("%d",&n); 7 | printf("Enter array elements:n"); 8 | for(i=0;i 2 | #include 3 | struct Node{ 4 | int data; 5 | struct Node *next; 6 | }; 7 | void deleteStart(struct Node** head) 8 | { 9 | struct Node* temp = *head; 10 | // If head is NULL it means Singly Linked List is empty 11 | if(*head == NULL){ 12 | printf("Impossible to delete from empty Singly Linked List"); 13 | return; 14 | } 15 | // move head to next node 16 | *head = (*head)->next; 17 | printf("Deleted: %d\n", temp->data); 18 | free(temp); 19 | } 20 | 21 | 22 | void insertStart(struct Node** head, int data){ 23 | // dynamically create memory for this newNode 24 | struct Node* newNode = (struct Node*) malloc(sizeof(struct Node)); 25 | // assign data value 26 | newNode->data = data; 27 | // change the next node of this newNode 28 | // to current head of Linked List 29 | newNode->next = *head; 30 | //re-assign head to this newNode 31 | *head = newNode; 32 | printf("Inserted %d\n",newNode->data); 33 | } 34 | void display(struct Node* node){ 35 | printf("\nLinked List: "); 36 | // as linked list will end when Node is Null 37 | while(node!=NULL){ 38 | printf("%d ",node->data); 39 | node = node->next; 40 | } 41 | printf("\n"); 42 | } 43 | int main() 44 | { 45 | struct Node* head = NULL; 46 | insertStart(&head,100); 47 | insertStart(&head,80); 48 | insertStart(&head,60); 49 | insertStart(&head,40); 50 | insertStart(&head,20); 51 | display(head); 52 | deleteStart(&head); 53 | deleteStart(&head); 54 | display(head); 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /queue.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define SIZE 5 3 | void enQueue(int); 4 | void deQueue(); 5 | void display(); 6 | int items[SIZE], front = -1, rear = -1; 7 | int main() { 8 | //deQueue is not possible on empty queue 9 | deQueue(); 10 | 11 | //enQueue 5 elements 12 | enQueue(1); 13 | enQueue(2); 14 | enQueue(3); 15 | enQueue(4); 16 | enQueue(5); 17 | 18 | // 6th element can't be added to because the queue is full 19 | enQueue(6); 20 | 21 | display(); 22 | //deQueue removes element entered first i.e. 1 23 | deQueue(); 24 | //Now we have just 4 elements 25 | display(); 26 | 27 | return 0; 28 | } 29 | 30 | void enQueue(int value) { 31 | if (rear == SIZE - 1) 32 | printf("\nQueue is Full!!"); 33 | else { 34 | if (front == -1) 35 | front = 0; 36 | rear++; 37 | items[rear] = value; 38 | printf("\nInserted -> %d", value); 39 | } 40 | } 41 | 42 | void deQueue() { 43 | if (front == -1) 44 | printf("\nQueue is Empty!!"); 45 | else { 46 | printf("\nDeleted : %d", items[front]); 47 | front++; 48 | if (front > rear) 49 | front = rear = -1; 50 | } 51 | } 52 | 53 | // Function to print the queue 54 | void display() { 55 | if (rear == -1) 56 | printf("\nQueue is Empty!!!"); 57 | else { 58 | int i; 59 | printf("\nQueue elements are:\n"); 60 | for (i = front; i <= rear; i++) 61 | printf("%d ", items[i]); 62 | } 63 | printf("\n"); 64 | } 65 | 66 | -------------------------------------------------------------------------------- /revwithoutswap.java: -------------------------------------------------------------------------------- 1 | package arrays; 2 | import java.util.*; 3 | public class revwithoutswap { 4 | public static void main(String[] args) { 5 | Scanner in = new Scanner(System.in); 6 | System.out.print("Enter the array size : "); 7 | int size =in.nextInt(); 8 | int arr[] = new int[size]; 9 | for(int i=0;i 2 | int stack[10],choice,n,top,x,i; // Declaration of variables 3 | void push(); 4 | void pop(); 5 | void display(); 6 | int main() 7 | { 8 | top = -1; // Initially there is no element in stack 9 | printf("\n Enter the size of STACK : "); 10 | scanf("%d",&n); 11 | printf("\nSTACK IMPLEMENTATION USING ARRAYS\n"); 12 | do 13 | { 14 | printf("\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\n"); 15 | printf("\nEnter the choice : "); 16 | scanf("%d",&choice); 17 | switch(choice) 18 | { 19 | case 1: 20 | { 21 | push(); 22 | break; 23 | } 24 | case 2: 25 | { 26 | pop(); 27 | break; 28 | } 29 | case 3: 30 | { 31 | display(); 32 | break; 33 | } 34 | case 4: 35 | { 36 | break; 37 | } 38 | default: 39 | { 40 | printf ("\nInvalid Choice\n"); 41 | }}} 42 | while(choice!=4); 43 | return 0; 44 | } 45 | 46 | void push() 47 | { 48 | if(top >= n - 1) 49 | { 50 | printf("\nSTACK OVERFLOW\n"); 51 | } 52 | else 53 | { 54 | printf("Enter a value to be pushed : "); 55 | scanf("%d",&x); 56 | top++; // TOP is incremented after an element is pushed 57 | stack[top] = x; // The pushed element is made as TOP 58 | }} 59 | void pop() 60 | { 61 | if(top <= -1) 62 | { 63 | printf("\nSTACK UNDERFLOW\n"); 64 | } 65 | else 66 | { 67 | printf("\nThe popped element is %d",stack[top]); 68 | top--; // Decrement TOP after a pop 69 | }} 70 | void display() 71 | { 72 | if(top >= 0) 73 | { 74 | // Print the stack 75 | printf("\nELEMENTS IN THE STACK\n\n"); 76 | for(i = top ; i >= 0 ; i--) 77 | printf("%d\t",stack[i]); 78 | } 79 | else 80 | { 81 | printf("\nEMPTY STACK\n"); 82 | }} 83 | 84 | --------------------------------------------------------------------------------