├── 6. Recursion ├── 8_find GCD of two numbers.c ├── 7_print the prime factors.c ├── 14_find the factorial of a number by tail recursive method.c ├── 11_solve Tower of Hanoi problem using recursion.c ├── 6_raise a floating point number to a positive integer.c ├── 3_display and find out the sum of series.c ├── 12_Strings and recursion.c ├── 5_convert a positive decimal number to Binary, Octal or Hexadecimal.c ├── 1_factorial of a number by recursive method.c ├── 2_display numbers from 1 to n and their sum.c ├── 9_generate fibonacci series.c ├── 15_search an element through for binary search.c ├── 10_tests whether a number is divisible by 11 and 9 or not.c └── 4_display integer as sequence of digits and find sum of its digits.c ├── 10. Sorting ├── 4. Insertion Sort.c ├── 2. Bubble Sort.c ├── 1. Selection Sort.c ├── 3. Improvements of Bubble Sort.c ├── 6. Shell Sort Using Knuth Increments.c ├── 5. Shell Sort.c ├── 17. Sorting Records Using Bubble Sort.c ├── 19. Sorting by Address Using Bubble Sort.c ├── 12. Quick Sort.c ├── 7. Merging two sorted arrays into a third sorted array.c ├── 9. Merge sort through recursion.c ├── 16. Address Calculation Sort.c ├── 14. Heap Sort.c ├── 10. Merge Sort Iterative.c ├── 15. Radix Sort.c ├── 13. Binary Tree Sort.c └── 11. Merge Sort For Linked List.c ├── 11. Searching ├── 1_Linear Search or Sequential Search.c ├── 2_Sequential Search Sentinel.c ├── 3_Sequential Search in Sorted Array.c ├── 4_Binary Search.c └── 5_Recursive Binary Search.c ├── 4. Stack ├── 3_reversing a string using stack.c ├── 6_Simple Stack Implementation.cpp ├── 9_Check a string is palindrome or not using stack.c ├── 10_Check valid parenthesis using stack.c ├── 4_check nesting of parentheses using stack.c ├── 8_Reverse stack using recursion.c ├── 7_Stack using linked list.c ├── 1_Stack Using Array.c ├── 5_conversion of infix to postfix and evaluation of postfix.c └── 2_Stack Using Linked List.c ├── 7. Binary Tree ├── 2_Binary Tree - Implementation.c ├── 3_Insertion in a binary tree in level order.cpp ├── 6_Level Order Tree Traversal Using Function.c ├── 4_ree Traversals (Inorder, Preorder and Postorder).c ├── 5_Level Order Tree Traversal Using Queue.c ├── 1_Binary Tree with All Operation.c ├── 7_construct a binary tree from inorder and preorder.C └── 8_constructing a binary tree from inorder and postorder.C ├── 5. Queue ├── 7_simple queue implementation.cpp ├── 1_queue using array.c ├── 2_queue using linked list.c ├── 3_queue using circular linked list.c ├── 6_priority queue using linked list.c ├── 4_circular queue.c └── 5_deque using circular array.c ├── 1. Singly Linked List ├── 1 Linked List Implementation.c ├── 5 finding pointer to last node in a linked list.c ├── 11 insert element at the end.c ├── 6 finding pointer to second last node in a linked list.c ├── 2 traverse a linked list.c ├── 4 counting nodes in a linked list.c ├── 22 sorted linked list.C ├── 9 finding pointer to a node at a particular position.c ├── 3 searching in a linked list.c ├── 7 finding pointer to a node with particular information.c ├── 21 concatenate two single linked lists.c ├── 20 reversing a single linked list.c ├── 8 finding pointer to predecessor of a node with particular info.c ├── 15 insert a new node at a given position.c ├── 24 merging two sorted single linked lists.c ├── 19 deletion at the end.c ├── 17 deletion of the only node.c ├── 16 deletion of first node or only node.c ├── 18 deletion between the list nodes.c ├── 14 insert a new node before a given node.c ├── 10 insertion in a linked list at the beginning.c ├── 10. insert a new node in a linked list at end of the list.c ├── 25 polynomial addition and multiplication using linked list.c ├── 13 insertion a new node after a given node.c └── 12 insert node at the middle of Singly Linked List.c ├── 8. Binary Search Tree └── 3. Insertion in a Binary Search Tree Using Recursion.c ├── 9. Heap ├── 2_Build Heap.c └── 1_Heap.c ├── 3. Circular Linked List └── 2_Concatenate two circular linked lists.c └── 12. Hashing ├── P9_7 Quadrtic probing.C ├── P9_6 Linear probing.C ├── P9_9 Separate chaining.c └── P9_8 Double hashing.c /6. Recursion/8_find GCD of two numbers.c: -------------------------------------------------------------------------------- 1 | /*Program to find GCD of two numbers*/ 2 | 3 | #include 4 | int GCD(int a, int b); 5 | int gcd(int a, int b); 6 | 7 | main() 8 | { 9 | 10 | int a, b; 11 | printf("Enter a and b : \n"); 12 | scanf("%d%d",&a, &b); 13 | printf("%d\n",GCD(a,b)); 14 | printf("%d\n",gcd(a,b)); 15 | }/*End of main()*/ 16 | 17 | /*Recursive*/ 18 | int GCD(int a, int b) 19 | { 20 | if(b==0) 21 | return a; 22 | return GCD(b, a%b); 23 | }/*End of GCD()*/ 24 | 25 | /*Iterative*/ 26 | int gcd(int a, int b) 27 | { 28 | int rem; 29 | while(b != 0) 30 | { 31 | rem = a%b; 32 | a = b; 33 | b = rem; 34 | } 35 | return a; 36 | }/*End of gcd()*/ 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /10. Sorting/4. Insertion Sort.c: -------------------------------------------------------------------------------- 1 | /* 2 | File Name : Insertion Sort.c 3 | Purpose : To explain Insertion Sort 4 | Date : 19 October 2018 5 | Author : Bipul Islam 6 | */ 7 | #include 8 | #define MAX 100 9 | main() 10 | { 11 | int a[MAX], i, j, n, temp; 12 | printf("Enter the number of elements: "); 13 | scanf("%d", &n); 14 | 15 | for(i = 0; i < n; i++) 16 | { 17 | printf("Enter element %d: ",i+1); 18 | scanf("%d", &a[i]); 19 | } 20 | 21 | for(i = 1; i=0 && a[j]>temp; j--) 26 | a[j+1] = a[j]; 27 | 28 | a[j+1] = temp; 29 | } 30 | 31 | printf("Sorted Array is : \n"); 32 | for(i = 0; i 4 | void PFactors( int num); 5 | void IPFactors( int n); 6 | 7 | main( ) 8 | { 9 | int num; 10 | printf("Enter a number : "); 11 | scanf("%d", &num); 12 | PFactors(num); printf("\n"); 13 | IPFactors(num); printf("\n"); 14 | }/*End of main()*/ 15 | 16 | void PFactors( int num) 17 | { 18 | int i = 2; 19 | if( num == 1 ) 20 | return; 21 | while( num%i != 0 ) 22 | i++; 23 | printf("%d ", i); 24 | PFactors(num/i); 25 | }/*End of PFactors()*/ 26 | 27 | /*Iterative*/ 28 | void IPFactors( int num) 29 | { 30 | int i; 31 | for( i = 2; num!=1; i++) 32 | while( num%i == 0 ) 33 | { 34 | printf("%d ", i); 35 | num = num/i; 36 | } 37 | }/*End of IPFactors()*/ 38 | 39 | -------------------------------------------------------------------------------- /10. Sorting/2. Bubble Sort.c: -------------------------------------------------------------------------------- 1 | /* 2 | File Name : Bubble Sort.c 3 | Purpose : To explain Bubble Sort 4 | Date : 19 October 2018 5 | Author : Bipul Islam 6 | */ 7 | #include 8 | #define MAX 100 9 | main() 10 | { 11 | int a[MAX], i, j, n, temp, x; 12 | printf("Enter the number of elements: "); 13 | scanf("%d", &n); 14 | 15 | for(i = 0; i < n; i++) 16 | { 17 | printf("Enter element %d: ",i+1); 18 | scanf("%d", &a[i]); 19 | } 20 | 21 | for(x = n-2; x>=0; x--) 22 | { 23 | for(j=0; j<=n; j++) 24 | { 25 | if(a[j] > a[j+1]) 26 | { 27 | temp = a[j]; 28 | a[j] = a[j+1]; 29 | a[j+1] = temp; 30 | } 31 | } 32 | } 33 | printf("Sorted Array is : \n"); 34 | for(i = 0; i 4 | long TailRecursiveFact(int n); 5 | long TRfact(int n, int result); 6 | main( ) 7 | { 8 | int num; 9 | printf("Enter a number : "); 10 | scanf("%d", &num); 11 | if(num<0) 12 | printf("No factorial for negative number\n"); 13 | printf("Factorial of %d is %ld\n", num, TailRecursiveFact(num) ); 14 | } 15 | 16 | /*Tail recursive*/ 17 | long TRfact(int n, int result) 18 | { 19 | if( n==0) 20 | return result; 21 | return TRfact(n-1, n*result); 22 | }/*End of TRFact()*/ 23 | 24 | /*Helper function for tail recursive function*/ 25 | long TailRecursiveFact(int n) 26 | { 27 | return TRfact(n, 1); 28 | }/*End of TailRecursiveFact()*/ 29 | 30 | 31 | -------------------------------------------------------------------------------- /6. Recursion/11_solve Tower of Hanoi problem using recursion.c: -------------------------------------------------------------------------------- 1 | /*Program to solve Tower of Hanoi problem using recursion*/ 2 | 3 | #include 4 | void tofh(int ndisk, char source, char temp, char dest); 5 | 6 | main( ) 7 | { 8 | char source = 'A', temp = 'B', dest = 'C'; 9 | int ndisk; 10 | printf("Enter the number of disks : "); 11 | scanf("%d", &ndisk ); 12 | printf("Sequence is :\n"); 13 | tofh(ndisk, source, temp, dest); 14 | }/*End of main()*/ 15 | 16 | void tofh(int ndisk, char source, char temp, char dest) 17 | { 18 | if(ndisk==1) 19 | { 20 | printf("Move Disk %d from %c-->%c\n", ndisk, source, dest); 21 | return; 22 | } 23 | tofh(ndisk-1, source, dest, temp); 24 | printf("Move Disk %d from %c-->%c\n", ndisk, source, dest); 25 | tofh(ndisk-1, temp, source, dest); 26 | 27 | }/*End of tofh( )*/ 28 | 29 | -------------------------------------------------------------------------------- /10. Sorting/1. Selection Sort.c: -------------------------------------------------------------------------------- 1 | /* 2 | File Name : Selection Sort.c 3 | Purpose : To explain Selection Sort 4 | Date : 19 October 2018 5 | Author : Bipul Islam 6 | */ 7 | #include 8 | #define MAX 100 9 | main() 10 | { 11 | int a[MAX], i, j, n, temp, minIndex; 12 | printf("Enter the number of elements: "); 13 | scanf("%d", &n); 14 | 15 | for(i = 0; i < n; i++) 16 | { 17 | printf("Enter element %d: ",i+1); 18 | scanf("%d", &a[i]); 19 | } 20 | 21 | for(i = 0; i 3 | float power(float a , int n); 4 | float Ipower(float a , int n); 5 | main( ) 6 | { 7 | float a, p; 8 | int n; 9 | printf("Enter a and n : "); 10 | scanf("%f %d", &a, &n); 11 | p = power(a, n); 12 | printf("%f raised to power %d is %f\n", a, n, p); 13 | p = Ipower(a, n); 14 | printf("%f raised to power %d is %f\n", a, n, p); 15 | }/*End of main()*/ 16 | 17 | /*Recursive*/ 18 | float power(float a , int n) 19 | { 20 | if(n == 0) 21 | return(1); 22 | else 23 | return(a * power(a,n-1)); 24 | }/*End of power()*/ 25 | 26 | /*Iterative*/ 27 | float Ipower(float a , int n) 28 | { 29 | int i; 30 | float result=1; 31 | for(i=1; i<=n; i++) 32 | result = result * a; 33 | return result; 34 | }/*End of Ipower()*/ 35 | -------------------------------------------------------------------------------- /6. Recursion/3_display and find out the sum of series.c: -------------------------------------------------------------------------------- 1 | /*Program to display and find out the sum of series*/ 2 | /* Series : 1 + 2 + 3 + 4 + 5 +....... */ 3 | 4 | #include 5 | int series(int n); 6 | int rseries(int n); 7 | 8 | main( ) 9 | { 10 | int n; 11 | printf("Enter number of terms : "); 12 | scanf("%d", &n); 13 | 14 | printf("\b\b = %d\n", series(n)); /* \b to erase last +sign */ 15 | printf("\b\b = %d\n\n\n", rseries(n)); 16 | }/*End of main()*/ 17 | 18 | /*Iterative function*/ 19 | int series(int n) 20 | { 21 | int i, sum=0; 22 | for(i=1; i<=n; i++) 23 | { 24 | printf("%d + ", i); 25 | sum+=i; 26 | } 27 | return sum; 28 | }/*End of series()*/ 29 | 30 | /*Recursive function*/ 31 | int rseries(int n) 32 | { 33 | int sum; 34 | if(n == 0) 35 | return 0; 36 | sum = (n + rseries(n-1)); 37 | printf("%d + ",n); 38 | return sum; 39 | }/*End of rseries()*/ 40 | 41 | -------------------------------------------------------------------------------- /6. Recursion/12_Strings and recursion.c: -------------------------------------------------------------------------------- 1 | /*Strings and recursion*/ 2 | 3 | #include 4 | #include 5 | void display(char *str); 6 | void Rdisplay(char *str); 7 | int length(char *str); 8 | 9 | main( ) 10 | { 11 | char str[100]; 12 | printf("Enter a string : "); 13 | gets(str); 14 | display( str ); 15 | printf("\n"); 16 | Rdisplay(str); 17 | printf("\n"); 18 | printf("%d\n",length(str)); 19 | }/*End of main()*/ 20 | 21 | void display(char *str ) 22 | { 23 | if(*str == '\0') 24 | return; 25 | putchar(*str ); 26 | display(str+1); 27 | }/*End of display()*/ 28 | 29 | void Rdisplay(char *str ) 30 | { 31 | if(*str == '\0') 32 | return; 33 | Rdisplay(str+1); 34 | putchar(*str ); 35 | }/*End of Rdisplay()*/ 36 | 37 | int length(char *str ) 38 | { 39 | if(*str == '\0') 40 | return 0; 41 | return (1 + length(str+1)); 42 | }/*End of length()*/ 43 | 44 | 45 | -------------------------------------------------------------------------------- /10. Sorting/3. Improvements of Bubble Sort.c: -------------------------------------------------------------------------------- 1 | /* 2 | File Name : Improvements of Bubble Sort.c 3 | Purpose : To explain Bubble Sort 4 | Date : 19 October 2018 5 | Author : Bipul Islam 6 | */ 7 | #include 8 | #define MAX 100 9 | main() 10 | { 11 | int a[MAX], i, j, n, temp, x, swaps; 12 | printf("Enter the number of elements: "); 13 | scanf("%d", &n); 14 | 15 | for(i = 0; i < n; i++) 16 | { 17 | printf("Enter element %d: ",i+1); 18 | scanf("%d", &a[i]); 19 | } 20 | 21 | for(x = n-2; x>=0; x--) 22 | { 23 | swaps = 0; 24 | for(j=0; j<=n; j++) 25 | { 26 | if(a[j] > a[j+1]) 27 | { 28 | temp = a[j]; 29 | a[j] = a[j+1]; 30 | a[j+1] = temp; 31 | swaps++; 32 | } 33 | } 34 | if(swaps == 0) 35 | break; 36 | } 37 | printf("Sorted Array is : \n"); 38 | for(i = 0; i 4 | #define SIZE 100 5 | int search(int a[], int n, int searchValue); 6 | 7 | main() 8 | { 9 | int i, n, searchValue, a[SIZE], index; 10 | 11 | printf("Enter the number of elements : "); 12 | scanf("%d", &n); 13 | 14 | printf("Enter the elements: "); 15 | for(i = 0; i 3 | void convert(int, int); 4 | main() 5 | { 6 | int num; 7 | printf("Enter a positive decimal number : "); 8 | scanf("%d", &num); 9 | convert(num, 2); ///to Binary 10 | printf("\n"); 11 | convert(num, 8); ///to Octal 12 | printf("\n"); 13 | convert(num, 16); ///to Hexadecimal 14 | printf("\n"); 15 | }/*End of main()*/ 16 | 17 | void convert (int num, int base) 18 | { 19 | int rem = num%base; 20 | 21 | if(num==0) 22 | return; 23 | convert(num/base, base); 24 | 25 | if(rem < 10) 26 | printf("%d", rem); 27 | else 28 | printf("%c", rem-10+'A' ); 29 | }/*End of convert()*/ 30 | 31 | void toBinary(int n) 32 | { 33 | if(n == 0) 34 | return; 35 | 36 | toBinary(n/2); 37 | printf("%d\n", n%2); 38 | } 39 | 40 | -------------------------------------------------------------------------------- /6. Recursion/1_factorial of a number by recursive method.c: -------------------------------------------------------------------------------- 1 | /*Program to find the factorial of a number by recursive method*/ 2 | 3 | #include 4 | long int fact(int n); 5 | long int Ifact(int n); 6 | 7 | main( ) 8 | { 9 | int num; 10 | printf("Enter a number : "); 11 | scanf("%d", &num); 12 | if(num<0) 13 | printf("No factorial for negative number\n"); 14 | else 15 | printf("Factorial of %d is %ld\n", num, fact(num) ); 16 | 17 | if(num<0) 18 | printf("No factorial for negative number\n"); 19 | else 20 | printf("Factorial of %d is %ld\n", num, Ifact(num) ); 21 | }/*End of main()*/ 22 | 23 | /*Recursive*/ 24 | long int fact(int n) 25 | { 26 | if(n == 0) 27 | return(1); 28 | return(n * fact(n-1)); 29 | }/*End of fact()*/ 30 | 31 | /*Iterative*/ 32 | long int Ifact(int n) 33 | { 34 | long fact=1; 35 | while(n>0) 36 | { 37 | fact = fact*n; 38 | n--; 39 | } 40 | return fact; 41 | }/*End of ifact()*/ 42 | 43 | -------------------------------------------------------------------------------- /11. Searching/2_Sequential Search Sentinel.c: -------------------------------------------------------------------------------- 1 | /*Linear Search in an array using sentinel*/ 2 | 3 | #include 4 | #define SIZE 100 5 | int search(int a[], int n, int searchValue); 6 | 7 | main() 8 | { 9 | int i, n, searchValue, a[SIZE], index; 10 | 11 | printf("Enter the number of elements : "); 12 | scanf("%d", &n); 13 | 14 | printf("Enter the elements: "); 15 | for(i = 0; i 9 | #define MAX 100 10 | main() 11 | { 12 | int arr[MAX],i,j,k,n,incr; 13 | 14 | printf("Enter the number of elements : "); 15 | scanf("%d",&n); 16 | 17 | for(i=0;i=1) 28 | { 29 | for(i=incr; i=0 && k 4 | int summation(int n); 5 | void display1(int n); 6 | void display2(int n); 7 | 8 | main( ) 9 | { 10 | int n; 11 | printf("Enter number of terms : "); 12 | scanf("%d", &n); 13 | 14 | display1(n); 15 | printf("\n"); 16 | 17 | display2(n); 18 | printf("\n"); 19 | 20 | printf("sum = %d\n", summation(n)); 21 | }/*End of main()*/ 22 | int summation( int n) 23 | { 24 | if(n==0) 25 | return 0; 26 | return ( n + summation(n-1) ); 27 | }/*End of summation()*/ 28 | 29 | /*displays in reverse order*/ 30 | void display1(int n) 31 | { 32 | if( n==0 ) 33 | return; 34 | printf("%d ",n); 35 | display1(n-1); 36 | }/*End of display1()*/ 37 | 38 | /*displays in ascending order*/ 39 | void display2(int n) 40 | { 41 | if( n==0 ) 42 | return; 43 | display2(n-1); 44 | printf("%d ",n); 45 | }/*End of display2()*/ 46 | -------------------------------------------------------------------------------- /11. Searching/3_Sequential Search in Sorted Array.c: -------------------------------------------------------------------------------- 1 | /*Linear Search in Sorted Array*/ 2 | 3 | #include 4 | #define SIZE 100 5 | int search(int a[], int n, int searchValue); 6 | 7 | main() 8 | { 9 | int i, n, searchValue, a[SIZE], index; 10 | 11 | printf("Enter the number of elements : "); 12 | scanf("%d", &n); 13 | 14 | printf("Enter the elements: "); 15 | for(i = 0; i= searchValue) 37 | break; 38 | } 39 | 40 | if(a[i] == searchValue) 41 | return i; 42 | else 43 | return -1; 44 | } 45 | 46 | -------------------------------------------------------------------------------- /10. Sorting/5. Shell Sort.c: -------------------------------------------------------------------------------- 1 | /* 2 | File Name : Shell Sort.c 3 | Purpose : To explain Shell Sort 4 | Date : 19 October 2018 5 | Author : Bipul Islam 6 | */ 7 | #include 8 | #define MAX 100 9 | 10 | main() 11 | { 12 | 13 | int arr[MAX],i,j,k,n,incr; 14 | 15 | printf("Enter the number of elements : "); 16 | scanf("%d",&n); 17 | for(i=0;i=1) 28 | { 29 | for(i=incr; i=0 && k 3 | #include 4 | #include 5 | 6 | #define MAX 20 7 | 8 | int top = -1; 9 | char stack[MAX]; 10 | char pop(); 11 | void push(char); 12 | 13 | main() 14 | { 15 | char str[20]; 16 | unsigned int i; 17 | printf("Enter the string : " ); 18 | gets(str); 19 | /*Push characters of the string str on the stack */ 20 | for(i=0;i 4 | #define SIZE 100 5 | int search(int a[], int n, int searchValue); 6 | 7 | main() 8 | { 9 | int i, n, searchValue, a[SIZE], index; 10 | 11 | printf("Enter the number of elements : "); 12 | scanf("%d", &n); 13 | 14 | printf("Enter the elements (in sorted order): "); 15 | for(i = 0; i a[mid]) 39 | first = mid + 1; 40 | else if (searchValue < a[mid]) 41 | last = mid - 1; 42 | else 43 | return mid; 44 | } 45 | return -1; 46 | } 47 | 48 | 49 | -------------------------------------------------------------------------------- /4. Stack/6_Simple Stack Implementation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define stackSize 3 3 | 4 | int myStack[stackSize], top = -1; 5 | 6 | void push(int x) 7 | { 8 | if(isFull()) 9 | { 10 | printf("Stack Overflow\n"); 11 | return; 12 | } 13 | ///else 14 | top = top + 1; 15 | a[top] = x; 16 | } 17 | 18 | void peek() 19 | { 20 | if(top<0) 21 | printf("Stack Underflow!\n"); 22 | else 23 | printf("%d\n", myStack[top]); 24 | } 25 | 26 | void pop() 27 | { 28 | if(top<0) 29 | printf("Stack underflow!\n"); 30 | else 31 | { 32 | printf("Popped %d, from Stack\n", myStack[top]); 33 | top--; 34 | } 35 | 36 | } 37 | 38 | void displayStack() 39 | { 40 | printf("\nPrint the full stack from TOP to BOTTOM:\n"); 41 | for(int i = top; i>=0; i--) 42 | printf("%d\n",myStack[i]); 43 | } 44 | 45 | int main() 46 | { 47 | 48 | push(5); 49 | push(90); 50 | push(12); 51 | 52 | displayStack(); 53 | 54 | pop(); 55 | 56 | displayStack(); 57 | 58 | printf("\nTop value of this Stack: "); 59 | peek(); 60 | 61 | 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /6. Recursion/9_generate fibonacci series.c: -------------------------------------------------------------------------------- 1 | /*Program to generate fibonacci series*/ 2 | #include 3 | 4 | int fib(int n); 5 | int TailRecursiveFib(int n); 6 | int TRfib(int n, int next, int result); 7 | int Ifib(int n); 8 | 9 | main( ) 10 | { 11 | int nterms, i; 12 | printf("Enter number of terms : "); 13 | scanf("%d", &nterms); 14 | 15 | for(i=0; i 3 | #define SIZE 100 4 | int binary_search(int arr[],int item, int low, int high); 5 | main() 6 | { 7 | int arr[SIZE],i, item, n; 8 | printf("Enter the number of elements : "); 9 | scanf("%d",&n); 10 | printf("Enter elements of the array(in sorted order) : \n"); 11 | for(i=0; i arr[mid]) 30 | return binary_search(arr,item,mid+1,up); /*Search in right portion, tail recursive call */ 31 | else if(item < arr[mid]) 32 | return binary_search(arr,item,low,mid-1); /*Search in left portion, tail recursive call */ 33 | else 34 | return mid; /*found*/ 35 | }/*End of binary_search()*/ 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /7. Binary Tree/2_Binary Tree - Implementation.c: -------------------------------------------------------------------------------- 1 | #include 2 | struct node 3 | { 4 | int data; 5 | struct node *left; 6 | struct node *right; 7 | }; 8 | 9 | /* newNode() allocates a new node with the given data and NULL left and 10 | right pointers. */ 11 | struct node* newNode(int data) 12 | { 13 | // Allocate memory for new node 14 | struct node* node = (struct node*)malloc(sizeof(struct node)); 15 | 16 | // Assign data to this node 17 | node->data = data; 18 | 19 | // Initialize left and right children as NULL 20 | node->left = NULL; 21 | node->right = NULL; 22 | return(node); 23 | } 24 | 25 | 26 | int main() 27 | { 28 | /*create root*/ 29 | struct node *root = newNode(1); 30 | /* following is the tree after above statement 31 | 32 | 1 33 | / \ 34 | NULL NULL 35 | */ 36 | 37 | 38 | root->left = newNode(2); 39 | root->right = newNode(3); 40 | /* 2 and 3 become left and right children of 1 41 | 1 42 | / \ 43 | 2 3 44 | / \ / \ 45 | NULL NULL NULL NULL 46 | */ 47 | 48 | 49 | root->left->left = newNode(4); 50 | /* 4 becomes left child of 2 51 | 1 52 | / \ 53 | 2 3 54 | / \ / \ 55 | 4 NULL NULL NULL 56 | / \ 57 | NULL NULL 58 | */ 59 | 60 | getchar(); 61 | return 0; 62 | } 63 | 64 | -------------------------------------------------------------------------------- /5. Queue/7_simple queue implementation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define queueSize 100 3 | 4 | int myQueue[queueSize], front = 0, rear = -1; 5 | 6 | 7 | void enqueue(int value) 8 | { 9 | if(rear==queueSize) 10 | printf("Queue is full! Cannot insert any Data!\n"); 11 | else 12 | myQueue[++rear] = value; 13 | } 14 | 15 | void dequeue() 16 | { 17 | if(front==queueSize) 18 | printf("Queue is empty!\n"); 19 | else 20 | printf("Dequeued value: %d\n", myQueue[front++]); 21 | } 22 | 23 | void display() 24 | { 25 | int i; 26 | 27 | for(i = front; i<=rear; i++) 28 | printf("%d\n", myQueue[i]); 29 | } 30 | 31 | int main() 32 | { 33 | for(int i = 10; i<=100; i=i+10) 34 | { 35 | enqueue(i); //push 10 elements in queue 36 | } 37 | 38 | //Display the queue 39 | printf("Full Queue is:\n"); 40 | display(); 41 | puts(""); 42 | 43 | dequeue(); 44 | dequeue(); 45 | 46 | //Display the updated queue 47 | printf("\nUpdated Queue is:\n"); 48 | display(); 49 | puts(""); 50 | 51 | //Enqueue another value 52 | enqueue(999); 53 | 54 | //Display the updated queue 55 | printf("\nUpdated Queue is:\n"); 56 | display(); 57 | puts(""); 58 | 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /11. Searching/5_Recursive Binary Search.c: -------------------------------------------------------------------------------- 1 | /*Recursive Binary Search in an Array*/ 2 | 3 | #include 4 | #define SIZE 100 5 | int search(int a[], int n, int searchValue); 6 | int searchR(int a[], int first, int last, int searchValue); 7 | 8 | main() 9 | { 10 | int i, n, searchValue, a[SIZE], index; 11 | 12 | printf("Enter the number of elements : "); 13 | scanf("%d", &n); 14 | 15 | printf("Enter the elements (in sorted order): "); 16 | for(i = 0; i last) 41 | return -1; 42 | 43 | mid = (first+last)/2; 44 | 45 | if(searchValue > a[mid]) 46 | return searchR(a, mid+1, last, searchValue); 47 | else if(searchValue < a[mid]) 48 | return searchR(a, first, mid-1, searchValue); 49 | else 50 | return mid; 51 | } 52 | 53 | 54 | -------------------------------------------------------------------------------- /10. Sorting/17. Sorting Records Using Bubble Sort.c: -------------------------------------------------------------------------------- 1 | /*P8.14 Program of sorting records using bubble sort algorithm*/ 2 | #include 3 | #define MAX 100 4 | 5 | struct record 6 | { 7 | char name[20]; 8 | int age; 9 | int salary; 10 | }; 11 | 12 | main() 13 | { 14 | struct record arr[MAX], temp; 15 | int i,j,n, xchanges; 16 | 17 | printf("Enter the number of records : "); 18 | scanf("%d",&n); 19 | 20 | for( i=0; i arr[j+1].age ) 38 | { 39 | temp = arr[j]; 40 | arr[j] = arr[j+1]; 41 | arr[j+1] = temp ; 42 | xchanges++; 43 | } 44 | } 45 | if( xchanges == 0 ) 46 | break; 47 | } 48 | 49 | printf("List of Records Sorted on age \n"); 50 | for( i=0; i 4 | int divisibleBy9(long int x); 5 | int divisibleBy11(long int x); 6 | 7 | main( ) 8 | { 9 | long int num; 10 | printf("Enter the number to be tested : "); 11 | scanf("%ld", &num); 12 | 13 | if(divisibleBy9(num)) 14 | printf("The number is divisible by 9\n"); 15 | else 16 | printf("The number is not divisible by 9\n"); 17 | 18 | if(divisibleBy11(num)) 19 | printf("The number is divisible by 11\n"); 20 | else 21 | printf("The number is not divisible by 11\n"); 22 | }/*End of main()*/ 23 | 24 | int divisibleBy9( long int n ) 25 | { 26 | int sumofDigits; 27 | if(n==9) 28 | return 1; 29 | if(n<9) 30 | return 0; 31 | sumofDigits=0; 32 | while(n>0) 33 | { 34 | sumofDigits += n%10; 35 | n/=10; 36 | } 37 | divisibleBy9(sumofDigits); 38 | }/*End of divisibleBy9()*/ 39 | 40 | int divisibleBy11( long int n ) 41 | { 42 | int s1=0, s2=0,diff; 43 | 44 | if(n == 0) 45 | return 1; 46 | if(n < 10) 47 | return 0; 48 | 49 | while(n>0) 50 | { 51 | s1 += n%10; 52 | n /=10; 53 | s2 += n%10; 54 | n /= 10; 55 | } 56 | diff = s1>s2 ? (s1-s2) : (s2-s1); 57 | divisibleBy11(diff); 58 | }/*End of divisibleBy11()*/ 59 | 60 | -------------------------------------------------------------------------------- /10. Sorting/19. Sorting by Address Using Bubble Sort.c: -------------------------------------------------------------------------------- 1 | /*P8.16 Program of sorting by address using bubble sort algorithm*/ 2 | #include 3 | #define MAX 100 4 | 5 | struct record 6 | { 7 | char name[20]; 8 | int age; 9 | int salary; 10 | }; 11 | 12 | main() 13 | { 14 | struct record arr[MAX], *ptr[MAX], *temp; 15 | int i,j,n, xchanges; 16 | 17 | printf("Enter the number of elements : "); 18 | scanf("%d",&n); 19 | 20 | for(i=0; iage > ptr[j+1]->age) 39 | { 40 | temp = ptr[j]; 41 | ptr[j] = ptr[j+1]; 42 | ptr[j+1] = temp ; 43 | xchanges++; 44 | } 45 | } 46 | if(xchanges==0) 47 | break; 48 | } 49 | 50 | printf("List of Records Sorted on age \n"); 51 | for(i=0; iname); 54 | printf("%d\t\t", ptr[i]->age); 55 | printf("%d\n", ptr[i]->salary); 56 | } 57 | printf("\n"); 58 | }/*End of main()*/ 59 | 60 | 61 | 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /1. Singly Linked List/1 Linked List Implementation.c: -------------------------------------------------------------------------------- 1 | #inlcude 2 | #include 3 | struct node 4 | { 5 | int info; 6 | struct node *link; 7 | }; 8 | 9 | void displaylist(struct node *start) ///displayList() function 10 | { 11 | struct node *p; 12 | if(start==NULL) 13 | { 14 | printf("List is empty.\n"); 15 | return; 16 | } 17 | printf("List is : "); 18 | p = start; 19 | 20 | while(p!=NULL) 21 | { 22 | printf("%d ", p->info); 23 | p = p->link; 24 | } 25 | printf("\n"); 26 | } ///End of displaylist() 27 | 28 | void countNodes(struct node *start) ///countNodes() function 29 | { 30 | int n=0; 31 | struct node *p=start; 32 | while(p!=NULL) 33 | { 34 | n++; 35 | p=p->link; 36 | } 37 | printf("Number of nodes in list: %d\n", n); 38 | } ///End of countNodes() 39 | 40 | void search(struct node *start, int x) ///search() function 41 | { 42 | struct node *p 43 | int position =1; 44 | p=start; 45 | while(p!=NULL) 46 | { 47 | if(p->info == x) 48 | break; 49 | else 50 | position++; 51 | 52 | p=p->link; 53 | } 54 | if(p==NULL) 55 | printf("%d is not found in list.\n",x); 56 | else 57 | printf("%d is found at position %d\n", x, position); 58 | } ///End of search() 59 | -------------------------------------------------------------------------------- /6. Recursion/4_display integer as sequence of digits and find sum of its digits.c: -------------------------------------------------------------------------------- 1 | /*Program to display integer as sequence of digits and find sum of its digits*/ 2 | /*sum(n) = sum(n without lsd) + lsd of n 3 | sum(n) = sum(n/10) + n%10 4 | here, lsd means Least Significant Digit */ 5 | 6 | #include 7 | void display(long int n); 8 | void Rdisplay(long int n); 9 | int sumdigits( long int n); 10 | 11 | main( ) 12 | { 13 | long int num; 14 | printf("Enter number : "); 15 | scanf("%ld", &num); 16 | printf("%d\n",sumdigits(num)); 17 | printf("\n"); 18 | display(num); 19 | printf("\n"); 20 | Rdisplay(num); 21 | printf("\n"); 22 | }/*End of main()*/ 23 | 24 | /*Finds the sum of digits of an integer*/ 25 | int sumdigits(long int n) 26 | { 27 | if( n/10 == 0 ) /* if n is a single digit number*/ 28 | return n; 29 | return n%10 + sumdigits(n/10); 30 | }/*End of sumdigits()*/ 31 | 32 | /*Displays the digits of an integer*/ 33 | void display(long int n) 34 | { 35 | if( n/10==0 ) 36 | { 37 | printf("%d",n); 38 | return; 39 | } 40 | display(n/10); 41 | printf("%d",n%10); 42 | }/*End of display()*/ 43 | 44 | /*Displays the digits of an integer in reverse order*/ 45 | void Rdisplay(long int n) 46 | { 47 | if(n/10==0) 48 | { 49 | printf("%d",n); 50 | return; 51 | } 52 | printf("%d",n%10); 53 | Rdisplay(n/10); 54 | }/*End of Rdisplay()*/ 55 | 56 | 57 | -------------------------------------------------------------------------------- /10. Sorting/12. Quick Sort.c: -------------------------------------------------------------------------------- 1 | /*P8.9 Program of sorting using quick sort */ 2 | /* 3 | File Name : Quick Sort.c 4 | Purpose : To explain Quick Sort 5 | Date : 19 October 2018 6 | Author : Bipul Islam 7 | */ 8 | #include 9 | #define MAX 100 10 | 11 | void quick(int arr[],int low,int up); 12 | int partition(int arr[], int low, int up); 13 | 14 | main() 15 | { 16 | int array[MAX],n,i; 17 | printf("Enter the number of elements : "); 18 | scanf("%d",&n); 19 | 20 | for(i=0;i=up) 41 | return; 42 | pivloc = partition(arr,low,up); 43 | quick(arr,low,pivloc-1); /*process left sublist*/ 44 | quick(arr,pivloc+1,up); /*process right sublist*/ 45 | }/*End of quick()*/ 46 | 47 | int partition(int arr[], int low, int up) 48 | { 49 | int temp,i,j,pivot; 50 | 51 | i=low+1; 52 | j=up; 53 | pivot=arr[low]; 54 | 55 | while(i <= j) 56 | { 57 | while( arr[i] < pivot && i pivot ) 61 | j--; 62 | 63 | if(i < j) 64 | { 65 | temp=arr[i]; 66 | arr[i]=arr[j]; 67 | arr[j]=temp; 68 | i++; 69 | j--; 70 | } 71 | else 72 | i++; 73 | } 74 | arr[low]=arr[j]; 75 | arr[j]=pivot; 76 | 77 | return j; 78 | }/*End of partition()*/ 79 | 80 | 81 | 82 | 83 | -------------------------------------------------------------------------------- /8. Binary Search Tree/3. Insertion in a Binary Search Tree Using Recursion.c: -------------------------------------------------------------------------------- 1 | // C program to demonstrate insert operation in binary search tree 2 | #include 3 | #include 4 | 5 | struct node 6 | { 7 | int key; 8 | struct node *left, *right; 9 | }; 10 | 11 | // A utility function to create a new BST node 12 | struct node *newNode(int item) 13 | { 14 | struct node *temp = (struct node *)malloc(sizeof(struct node)); 15 | temp->key = item; 16 | temp->left = temp->right = NULL; 17 | return temp; 18 | } 19 | 20 | // A utility function to do inorder traversal of BST 21 | void inorder(struct node *root) 22 | { 23 | if (root != NULL) 24 | { 25 | inorder(root->left); 26 | printf("%d \n", root->key); 27 | inorder(root->right); 28 | } 29 | } 30 | 31 | /* A utility function to insert a new node with given key in BST */ 32 | struct node* insert(struct node* node, int key) 33 | { 34 | /* If the tree is empty, return a new node */ 35 | if (node == NULL) return newNode(key); 36 | 37 | /* Otherwise, recur down the tree */ 38 | if (key < node->key) 39 | node->left = insert(node->left, key); 40 | else if (key > node->key) 41 | node->right = insert(node->right, key); 42 | 43 | /* return the (unchanged) node pointer */ 44 | return node; 45 | } 46 | 47 | // Driver Program to test above functions 48 | int main() 49 | { 50 | /* Let us create following BST 51 | 50 52 | / \ 53 | 30 70 54 | / \ / \ 55 | 20 40 60 80 */ 56 | struct node *root = NULL; 57 | root = insert(root, 50); 58 | insert(root, 30); 59 | insert(root, 20); 60 | insert(root, 40); 61 | insert(root, 70); 62 | insert(root, 60); 63 | insert(root, 80); 64 | 65 | // print inoder traversal of the BST 66 | inorder(root); 67 | 68 | return 0; 69 | } 70 | 71 | -------------------------------------------------------------------------------- /4. Stack/9_Check a string is palindrome or not using stack.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define MAX 100 6 | #define TRUE 1 7 | #define FALSE 0 8 | int a[MAX]; 9 | int top; 10 | 11 | void initialize() { 12 | top = -1; 13 | } 14 | 15 | int isFull() { 16 | if(top == MAX-1) 17 | return TRUE; 18 | else 19 | return FALSE; 20 | } 21 | 22 | int isEmpty() { 23 | if(top == -1) 24 | return TRUE; 25 | else 26 | return FALSE; 27 | } 28 | 29 | void push(int x) { 30 | if(isFull()) 31 | { 32 | printf("Stack Overflow\n"); 33 | return; 34 | } 35 | ///else 36 | top = top + 1; 37 | a[top] = x; 38 | } 39 | int pop() { 40 | int x; 41 | if(isEmpty()) 42 | { 43 | printf("Stack Underflow\n"); 44 | exit(1); 45 | } 46 | ///else 47 | x = a[top]; 48 | top = top - 1; 49 | return x; 50 | } 51 | 52 | int main() { 53 | char inputStr[100], c; 54 | int i, length; 55 | initialize(); 56 | printf("Enter a string\n"); 57 | gets(inputStr); 58 | length = strlen(inputStr); 59 | ///Push all characters of input String to Stack 60 | for(i = 0; i < length; i++){ 61 | push(inputStr[i]); 62 | } 63 | ///Popping characters from stack returns the characters of input string 64 | ///in reverse order. We will then compare it with corresponding 65 | ///characters of input string. If we found a mismatch the input 66 | ///string is not a palindrome string 67 | for(i = 0; i < length; i++){ 68 | if(pop() != inputStr[i]) { 69 | printf("Not a Palindrome String\n"); 70 | return 0; 71 | } 72 | } 73 | 74 | printf("Palindrome String\n"); 75 | return 0; 76 | } 77 | -------------------------------------------------------------------------------- /10. Sorting/7. Merging two sorted arrays into a third sorted array.c: -------------------------------------------------------------------------------- 1 | /* P8.5 Program of merging two sorted arrays into a third sorted array*/ 2 | /* 3 | File Name : Merge Sort.c 4 | Purpose : To explain Merge Sort 5 | Date : 19 October 2018 6 | Author : Bipul Islam 7 | */ 8 | #include 9 | #define MAX 100 10 | 11 | void merge(int arr1[], int arr2[], int arr3[], int n1, int n2); 12 | main() 13 | { 14 | int arr1[MAX],arr2[MAX],arr3[2*MAX],n1,n2,i; 15 | 16 | printf("Enter the number of elements in array 1 : "); 17 | scanf("%d",&n1); 18 | printf("Enter all the elements in sorted order :\n"); 19 | for(i=0;i 2 | #include 3 | 4 | #define MAX 100 5 | int a[MAX]; 6 | int top; 7 | 8 | void initialize() 9 | { 10 | top = -1; 11 | } 12 | int size() 13 | { 14 | return top+1; 15 | } 16 | int isEmpty() 17 | { 18 | if(top == -1) 19 | return 1; 20 | else 21 | return 0; 22 | } 23 | int isFull() 24 | { 25 | if(top == MAX-1) 26 | return 1; 27 | else 28 | return 0; 29 | } 30 | void push(int x) 31 | { 32 | if(isFull()) 33 | { 34 | printf("Stack Overflow\n"); 35 | return; 36 | } 37 | ///else 38 | top = top + 1; 39 | a[top] = x; 40 | } 41 | int pop() 42 | { 43 | int x; 44 | if(isEmpty()) 45 | { 46 | printf("Stack Underflow\n"); 47 | exit(1); 48 | } 49 | ///else 50 | x = a[top]; 51 | top = top - 1; 52 | return x; 53 | } 54 | int main() { 55 | char str[100], c; 56 | int i, length; 57 | initialize(); 58 | printf("Enter a string of parenthesis: \n"); 59 | gets(str); 60 | length = strlen(str); 61 | /* 62 | # for '{' : we push '{' in stack 63 | # for '}' : we pop a character from stack. 64 | For every '}' there must be one '{' earlier. 65 | This will ensure that: 66 | ** There are equal number of '{' and '}' characters in string. 67 | ** For every '{' there is a '}' in input string later. 68 | */ 69 | for(i = 0; i < length; i++){ 70 | if(str[i] == '{') 71 | push(str[i]); 72 | else if(str[i] == '}') 73 | pop(); 74 | else { 75 | printf("Error : Invalid Character !! \n"); 76 | return 0; 77 | } 78 | } 79 | 80 | if(isEmpty()) 81 | printf("Valid Paranthesis Expression\n"); 82 | else 83 | printf("InValid Paranthesis Expression\n"); 84 | 85 | return 0; 86 | } 87 | -------------------------------------------------------------------------------- /4. Stack/4_check nesting of parentheses using stack.c: -------------------------------------------------------------------------------- 1 | /*Program to check nesting of parentheses using stack*/ 2 | #include 3 | #define MAX 30 4 | int top=-1; 5 | int stack[MAX]; 6 | 7 | void push(char); 8 | char pop(); 9 | int match(char a,char b); 10 | main() 11 | { 12 | char exp[MAX]; 13 | int valid; 14 | printf("Enter an algebraic expression : "); 15 | gets(exp); 16 | valid=check(exp); 17 | if(valid==1) 18 | printf("Valid expression\n"); 19 | else 20 | printf("Invalid expression\n"); 21 | } 22 | int check(char exp[] ) 23 | { 24 | int i; 25 | char temp; 26 | for(i=0;i 9 | #define MAX 100 10 | 11 | void merge_sort( int arr[], int low, int up ); 12 | void merge( int arr[], int temp[], int low1, int up1, int low2, int up2 ); 13 | void copy(int arr[], int temp[], int low, int up ); 14 | 15 | main() 16 | { 17 | int i, n, arr[MAX]; 18 | 19 | printf("Enter the number of elements : "); 20 | scanf("%d",&n); 21 | 22 | for(i=0; i 12 | #include 13 | using namespace std; 14 | /* A binary tree node has key, pointer to left child 15 | and a pointer to right child */ 16 | struct Node { 17 | int key; 18 | struct Node* left, *right; 19 | }; 20 | 21 | /* function to create a new node of tree and r 22 | eturns pointer */ 23 | struct Node* newNode(int key) 24 | { 25 | struct Node* temp = new Node; 26 | temp->key = key; 27 | temp->left = temp->right = NULL; 28 | return temp; 29 | }; 30 | 31 | /* Inorder traversal of a binary tree*/ 32 | void inorder(struct Node* temp) 33 | { 34 | if (!temp) 35 | return; 36 | 37 | inorder(temp->left); 38 | cout << temp->key << " "; 39 | inorder(temp->right); 40 | } 41 | 42 | /*function to insert element in binary tree */ 43 | void insert(struct Node* temp, int key) 44 | { 45 | queue q; 46 | q.push(temp); 47 | 48 | // Do level order traversal until we find 49 | // an empty place. 50 | while (!q.empty()) { 51 | struct Node* temp = q.front(); 52 | q.pop(); 53 | 54 | if (!temp->left) { 55 | temp->left = newNode(key); 56 | break; 57 | } else 58 | q.push(temp->left); 59 | 60 | if (!temp->right) { 61 | temp->right = newNode(key); 62 | break; 63 | } else 64 | q.push(temp->right); 65 | } 66 | } 67 | 68 | // Driver code 69 | int main() 70 | { 71 | struct Node* root = newNode(10); 72 | root->left = newNode(11); 73 | root->left->left = newNode(7); 74 | root->right = newNode(9); 75 | root->right->left = newNode(15); 76 | root->right->right = newNode(8); 77 | 78 | cout << "Inorder traversal before insertion:"; 79 | inorder(root); 80 | 81 | int key = 12; 82 | insert(root, key); 83 | 84 | cout << endl; 85 | cout << "Inorder traversal after insertion:"; 86 | inorder(root); 87 | 88 | return 0; 89 | } 90 | 91 | -------------------------------------------------------------------------------- /7. Binary Tree/6_Level Order Tree Traversal Using Function.c: -------------------------------------------------------------------------------- 1 | // Recursive C program for level order traversal of Binary Tree 2 | #include 3 | #include 4 | 5 | /* A binary tree node has data, pointer to left child 6 | and a pointer to right child */ 7 | struct node 8 | { 9 | int data; 10 | struct node* left, *right; 11 | }; 12 | 13 | /* Function protoypes */ 14 | void printGivenLevel(struct node* root, int level); 15 | int height(struct node* node); 16 | struct node* newNode(int data); 17 | 18 | /* Function to print level order traversal a tree*/ 19 | void printLevelOrder(struct node* root) 20 | { 21 | int h = height(root); 22 | int i; 23 | for (i=1; i<=h; i++) 24 | printGivenLevel(root, i); 25 | } 26 | 27 | /* Print nodes at a given level */ 28 | void printGivenLevel(struct node* root, int level) 29 | { 30 | if (root == NULL) 31 | return; 32 | if (level == 1) 33 | printf("%d ", root->data); 34 | else if (level > 1) 35 | { 36 | printGivenLevel(root->left, level-1); 37 | printGivenLevel(root->right, level-1); 38 | } 39 | } 40 | 41 | /* Compute the "height" of a tree -- the number of 42 | nodes along the longest path from the root node 43 | down to the farthest leaf node.*/ 44 | int height(struct node* node) 45 | { 46 | if (node==NULL) 47 | return 0; 48 | else 49 | { 50 | /* compute the height of each subtree */ 51 | int lheight = height(node->left); 52 | int rheight = height(node->right); 53 | 54 | /* use the larger one */ 55 | if (lheight > rheight) 56 | return(lheight+1); 57 | else return(rheight+1); 58 | } 59 | } 60 | 61 | /* Helper function that allocates a new node with the 62 | given data and NULL left and right pointers. */ 63 | struct node* newNode(int data) 64 | { 65 | struct node* node = (struct node*) 66 | malloc(sizeof(struct node)); 67 | node->data = data; 68 | node->left = NULL; 69 | node->right = NULL; 70 | 71 | return(node); 72 | } 73 | 74 | /* Driver program to test above functions*/ 75 | int main() 76 | { 77 | struct node *root = newNode(1); 78 | root->left = newNode(2); 79 | root->right = newNode(3); 80 | root->left->left = newNode(4); 81 | root->left->right = newNode(5); 82 | 83 | printf("Level Order traversal of binary tree is \n"); 84 | printLevelOrder(root); 85 | 86 | return 0; 87 | } 88 | 89 | -------------------------------------------------------------------------------- /7. Binary Tree/4_ree Traversals (Inorder, Preorder and Postorder).c: -------------------------------------------------------------------------------- 1 | // C program for different tree traversals 2 | #include 3 | #include 4 | 5 | /* A binary tree node has data, pointer to left child 6 | and a pointer to right child */ 7 | struct node 8 | { 9 | int data; 10 | struct node* left; 11 | struct node* right; 12 | }; 13 | 14 | /* Helper function that allocates a new node with the 15 | given data and NULL left and right pointers. */ 16 | struct node* newNode(int data) 17 | { 18 | struct node* node = (struct node*)malloc(sizeof(struct node)); 19 | node->data = data; 20 | node->left = NULL; 21 | node->right = NULL; 22 | 23 | return(node); 24 | } 25 | 26 | /* Given a binary tree, print its nodes according to the 27 | "bottom-up" postorder traversal. */ 28 | void printPostorder(struct node* node) 29 | { 30 | if (node == NULL) 31 | return; 32 | 33 | // first recur on left subtree 34 | printPostorder(node->left); 35 | 36 | // then recur on right subtree 37 | printPostorder(node->right); 38 | 39 | // now deal with the node 40 | printf("%d ", node->data); 41 | } 42 | 43 | /* Given a binary tree, print its nodes in inorder*/ 44 | void printInorder(struct node* node) 45 | { 46 | if (node == NULL) 47 | return; 48 | 49 | /* first recur on left child */ 50 | printInorder(node->left); 51 | 52 | /* then print the data of node */ 53 | printf("%d ", node->data); 54 | 55 | /* now recur on right child */ 56 | printInorder(node->right); 57 | } 58 | 59 | /* Given a binary tree, print its nodes in preorder*/ 60 | void printPreorder(struct node* node) 61 | { 62 | if (node == NULL) 63 | return; 64 | 65 | /* first print data of node */ 66 | printf("%d ", node->data); 67 | 68 | /* then recur on left sutree */ 69 | printPreorder(node->left); 70 | 71 | /* now recur on right subtree */ 72 | printPreorder(node->right); 73 | } 74 | 75 | /* Driver program to test above functions*/ 76 | int main() 77 | { 78 | struct node *root = newNode(1); 79 | root->left = newNode(2); 80 | root->right = newNode(3); 81 | root->left->left = newNode(4); 82 | root->left->right = newNode(5); 83 | 84 | printf("\nPreorder traversal of binary tree is \n"); 85 | printPreorder(root); 86 | 87 | printf("\nInorder traversal of binary tree is \n"); 88 | printInorder(root); 89 | 90 | printf("\nPostorder traversal of binary tree is \n"); 91 | printPostorder(root); 92 | 93 | getchar(); 94 | return 0; 95 | } 96 | 97 | -------------------------------------------------------------------------------- /1. Singly Linked List/5 finding pointer to last node in a linked list.c: -------------------------------------------------------------------------------- 1 | ///Traverse a linked list 2 | #include 3 | #include 4 | 5 | struct node { ///struct of a new node 6 | int data; ///data 7 | struct node *next; ///address 8 | } *head; 9 | 10 | void createlist(int n); ///function to create list 11 | void traverselist(); ///function to display list 12 | 13 | int main() 14 | { 15 | int n; 16 | printf("Enter the total number of nodes: "); 17 | scanf("%d",&n); 18 | createlist(n); 19 | 20 | traverselist(); 21 | 22 | return 0; 23 | } 24 | ///create a list of n nodes 25 | void createlist( int n) 26 | { 27 | struct node *newnode, *temp; 28 | int data , i; 29 | head=(struct node *)malloc(sizeof(struct node)); 30 | ///allocate memory for head node 31 | if(head==NULL) 32 | printf("Unable to allocatae memory!!!\n"); 33 | else{ 34 | ///Input data of node from the user 35 | printf("Enter the data of node 1: "); 36 | scanf("%d",&data); 37 | 38 | head->data=data; ///link the data fieled with data 39 | head->next=NULL; ///linked the address fieled to NULL 40 | 41 | temp=head; 42 | ///create n nodes and add to the linked list 43 | for(i=2; i<=n; i++) 44 | { 45 | newnode=(struct node *)malloc(sizeof(struct node)); 46 | ///allocate memory for newnode 47 | if(newnode==NULL) 48 | { 49 | printf("Unable to allocate memory!!\n"); 50 | break; 51 | } 52 | else 53 | { 54 | printf("Enter the data of node %d: ",i); 55 | scanf("%d",&data); 56 | 57 | newnode->data=data; ///link the data fieled of newnode with data 58 | newnode->next=NULL; ///link the address field of newnode with NULL 59 | 60 | temp->next=newnode; 61 | temp=temp->next; 62 | 63 | } 64 | } 65 | } 66 | } 67 | ///Display list 68 | void traverselist() 69 | { 70 | struct node *temp; 71 | if(head==NULL) 72 | printf("List is empty!!!\n"); 73 | else 74 | { 75 | temp=head; 76 | while(temp->next!=NULL) 77 | { 78 | temp=temp->next; ///move to the next node 79 | } 80 | printf("Pointer of last node is %d\n",temp->data); 81 | } 82 | } 83 | 84 | -------------------------------------------------------------------------------- /1. Singly Linked List/11 insert element at the end.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct node { 4 | int info; 5 | struct node *link; 6 | 7 | }; 8 | struct node *createList(struct node *start); 9 | void displayList(struct node *start); 10 | struct node *insertInBeginning(struct node *start, int data); 11 | void insertAtEnd(struct node *start, int data); 12 | 13 | 14 | int main() 15 | { 16 | struct node *start=NULL; 17 | int choice, data, x, k; 18 | start=createList(start); 19 | displayList(start); ///display lsit 20 | printf("Enter a element to insert in before the list or in an empty list: "); 21 | scanf("%d",&data); 22 | ///call function to insert data in beginning or in an empty list` 23 | start=insertInBeginning(start, data); 24 | displayList(start); 25 | 26 | } 27 | struct node *createList(struct node *start) 28 | { 29 | int i, n, data; 30 | printf("Enter the no of nodes: "); 31 | scanf("%d",&n); 32 | if(n==0) 33 | return start; 34 | 35 | printf("Enter the first elemlent to be inserted: "); 36 | scanf("%d",&data); 37 | start=insertInBeginning(start, data); 38 | for(i=2; i<=n; i++) 39 | { 40 | printf("Enter the next element to be inserted: "); 41 | scanf("%d",&data); 42 | insertAtEnd(start, data); 43 | } 44 | return start; 45 | }; 46 | struct node *insertInBeginning(struct node *start, int data) 47 | { 48 | struct node *temp; 49 | temp=(struct node * )malloc(sizeof(struct node)); 50 | 51 | temp->info=data; 52 | temp->link=start; 53 | start=temp; 54 | 55 | return start; 56 | }; 57 | void insertAtEnd(struct node *start, int data) 58 | { 59 | struct node *temp, *p; 60 | temp=(struct node * )malloc(sizeof(struct node)); 61 | 62 | temp->info=data; 63 | 64 | p=start; 65 | 66 | while(p->link!=NULL) 67 | { 68 | p=p->link; 69 | } 70 | p->link=temp; 71 | temp->link=NULL; 72 | } 73 | void displayList(struct node *start) 74 | { 75 | struct node *p; 76 | if(start==NULL) 77 | { 78 | printf("List is empty!!!\n"); 79 | return; 80 | } 81 | printf("List is : "); 82 | ///Code for traverse list 83 | ///Traverse list means visiting each nodes exactly ones 84 | p=start; 85 | while(p!=NULL) 86 | { 87 | printf("%d ", p->info); 88 | p=p->link; 89 | } 90 | printf("\n"); 91 | } 92 | 93 | 94 | -------------------------------------------------------------------------------- /9. Heap/2_Build Heap.c: -------------------------------------------------------------------------------- 1 | /* 2 | File Name: Build Heap 3 | Purpose : Building a heap from an array 4 | Date : 11th November 2018 5 | Author : Bipul Islam 6 | */ 7 | #include 8 | #define LARGE_VALUE 99999 ///All values in heap should be less than this value 9 | 10 | void restoreUp(int a[], int loc); 11 | void restoreDown(int a[], int i, int size); 12 | void display(int arr[],int hsize); 13 | void buildHeap_TopDown(int a[], int size); 14 | void buildHeap_BottomUp(int a[], int size); 15 | 16 | main( ) 17 | { 18 | int a1[] = {LARGE_VALUE, 20,33, 15, 6, 40, 60, 45, 16, 75, 10, 80, 12}; 19 | int a2[] = {LARGE_VALUE, 20,33, 15, 6, 40, 60, 45, 16, 75, 10, 80, 12}; 20 | 21 | int n = 12; 22 | 23 | buildHeap_TopDown(a1, n); 24 | buildHeap_BottomUp(a2, n); 25 | 26 | display(a1, n); 27 | display(a2, n); 28 | } 29 | 30 | void buildHeap_TopDown(int a[], int size) 31 | { 32 | int i; 33 | for(i=2; i<=size; i++) 34 | restoreUp(a,i); 35 | } 36 | 37 | void buildHeap_BottomUp(int a[], int size) 38 | { 39 | int i; 40 | for(i=size/2; i>=1; i--) 41 | restoreDown(a,i,size); 42 | } 43 | 44 | void restoreUp(int a[], int i) 45 | { 46 | int k = a[i]; 47 | int iparent = i/2; 48 | 49 | ///while( iparent>=1 && arr[iparent] < num ) if MAX_VAL not in arr[0] 50 | while( a[iparent] < k ) 51 | { 52 | a[i]=a[iparent]; 53 | i = iparent; 54 | iparent = i/2; 55 | } 56 | a[i] = k; 57 | } 58 | 59 | void restoreDown(int a[], int i, int hsize ) 60 | { 61 | int lchild=2*i, rchild=lchild+1; 62 | 63 | int num=a[i]; 64 | 65 | while( rchild <= hsize ) 66 | { 67 | if( num>=a[lchild] && num>=a[rchild] ) 68 | { 69 | a[i] = num; 70 | return; 71 | } 72 | else if(a[lchild] > a[rchild]) 73 | { 74 | a[i] = a[lchild]; 75 | i = lchild; 76 | } 77 | else 78 | { 79 | a[i] = a[rchild]; 80 | i = rchild; 81 | } 82 | lchild = 2 * i; 83 | rchild = lchild + 1; 84 | } 85 | ///Ifnumber of nodes is even 86 | if(lchild == hsize && num < a[lchild] ) 87 | { 88 | a[i]=a[lchild]; 89 | i = lchild; 90 | } 91 | a[i]=num; 92 | } 93 | 94 | void display(int a[],int hsize) 95 | { 96 | int i; 97 | if(hsize==0) 98 | { 99 | printf("Heap is empty\n"); 100 | return; 101 | } 102 | for(i=1;i<=hsize;i++) 103 | printf("%d ",a[i]); 104 | printf("\n"); 105 | 106 | printf("Number of elements = %d\n",hsize); 107 | } 108 | 109 | -------------------------------------------------------------------------------- /1. Singly Linked List/6 finding pointer to second last node in a linked list.c: -------------------------------------------------------------------------------- 1 | ///Traverse a linked list 2 | #include 3 | #include 4 | 5 | struct node { ///struct of a new node 6 | int data; ///data 7 | struct node *next; ///address 8 | } *head; 9 | 10 | void createlist(int n); ///function to create list 11 | void traverselist(); ///function to display list 12 | 13 | int main() 14 | { 15 | int n; 16 | printf("Enter the total number of nodes: "); 17 | scanf("%d",&n); 18 | createlist(n); 19 | 20 | traverselist(); 21 | 22 | return 0; 23 | } 24 | ///create a list of n nodes 25 | void createlist( int n) 26 | { 27 | struct node *newnode, *temp; 28 | int data , i; 29 | head=(struct node *)malloc(sizeof(struct node)); 30 | ///allocate memory for head node 31 | if(head==NULL) 32 | printf("Unable to allocatae memory!!!\n"); 33 | else{ 34 | ///Input data of node from the user 35 | printf("Enter the data of node 1: "); 36 | scanf("%d",&data); 37 | 38 | head->data=data; ///link the data fieled with data 39 | head->next=NULL; ///linked the address fieled to NULL 40 | 41 | temp=head; 42 | ///create n nodes and add to the linked list 43 | for(i=2; i<=n; i++) 44 | { 45 | newnode=(struct node *)malloc(sizeof(struct node)); 46 | ///allocate memory for newnode 47 | if(newnode==NULL) 48 | { 49 | printf("Unable to allocate memory!!\n"); 50 | break; 51 | } 52 | else 53 | { 54 | printf("Enter the data of node %d: ",i); 55 | scanf("%d",&data); 56 | 57 | newnode->data=data; ///link the data fieled of newnode with data 58 | newnode->next=NULL; ///link the address field of newnode with NULL 59 | 60 | temp->next=newnode; 61 | temp=temp->next; 62 | 63 | } 64 | } 65 | } 66 | } 67 | ///Display list 68 | void traverselist() 69 | { 70 | struct node *temp; 71 | if(head==NULL) 72 | printf("List is empty!!!\n"); 73 | else 74 | { 75 | temp=head; 76 | while(temp->next->next!=NULL) 77 | { 78 | temp=temp->next; ///move to the next node 79 | } 80 | printf("Pointer of second last node is %d\n",temp->data); 81 | } 82 | } 83 | 84 | 85 | -------------------------------------------------------------------------------- /10. Sorting/16. Address Calculation Sort.c: -------------------------------------------------------------------------------- 1 | /*P8.13 Program of sorting using address calculation sort*/ 2 | 3 | #include 4 | #include 5 | #define MAX 100 6 | 7 | struct node 8 | { 9 | int info ; 10 | struct node *link; 11 | }; 12 | 13 | struct node *head[5]; 14 | int n,arr[MAX]; 15 | int large; 16 | 17 | void addr_sort(); 18 | void insert(int num,int addr); 19 | int hash_fn(int number); 20 | 21 | main() 22 | { 23 | int i; 24 | printf("Enter the number of elements in the list : "); 25 | scanf("%d", &n); 26 | for(i=0; i large) 35 | large = arr[i]; 36 | } 37 | 38 | addr_sort(); 39 | 40 | printf("Sorted list is :\n"); 41 | for(i=0;i ",i); 65 | p=head[i]; 66 | while(p!=NULL) 67 | { 68 | printf("%d ",p->info); 69 | p=p->link; 70 | } 71 | printf("\n"); 72 | } 73 | printf("\n"); 74 | /*Taking the elements of linked lists in array*/ 75 | i=0; 76 | for(k=0;k<=5;k++) 77 | { 78 | p=head[k]; 79 | while(p!=NULL) 80 | { 81 | arr[i++]=p->info; 82 | p=p->link; 83 | } 84 | } 85 | }/*End of addr_sort()*/ 86 | 87 | /*Inserts the number in sorted linked list*/ 88 | void insert(int num,int addr) 89 | { 90 | struct node *q,*tmp; 91 | tmp= malloc(sizeof(struct node)); 92 | tmp->info=num; 93 | /*list empty or item to be added in beginning */ 94 | if(head[addr] == NULL || num < head[addr]->info) 95 | { 96 | tmp->link=head[addr]; 97 | head[addr]=tmp; 98 | return; 99 | } 100 | else 101 | { 102 | q=head[addr]; 103 | while(q->link != NULL && q->link->info < num) 104 | q=q->link; 105 | tmp->link=q->link; 106 | q->link=tmp; 107 | } 108 | }/*End of insert()*/ 109 | 110 | int hash_fn(int number) 111 | { 112 | int addr; 113 | float tmp; 114 | tmp=(float)number/large; 115 | addr=tmp*5; 116 | return(addr); 117 | }/*End of hash_fn()*/ 118 | 119 | -------------------------------------------------------------------------------- /10. Sorting/14. Heap Sort.c: -------------------------------------------------------------------------------- 1 | /* P8.11 Program of sorting through heapsort*/ 2 | /* 3 | File Name : Heap Sort.c 4 | Purpose : To explain Heap Sort 5 | Date : 19 October 2018 6 | Author : Bipul Islam 7 | */ 8 | #include 9 | #define MAX 100 10 | 11 | void heap_sort(int arr[], int size); 12 | void buildHeap(int arr[], int size); 13 | int del_root(int arr[], int *size); 14 | void restoreDown(int arr[], int i, int size ); 15 | void display(int arr[], int n); 16 | 17 | main() 18 | { 19 | int i; 20 | int arr[MAX],n; 21 | 22 | printf("Enter number of elements : "); 23 | scanf("%d",&n); 24 | 25 | for(i=1;i<=n;i++) 26 | { 27 | printf("Enter element %d : ",i); 28 | scanf("%d",&arr[i]); 29 | } 30 | 31 | printf("Entered list is :\n"); 32 | display(arr,n); 33 | 34 | heap_sort(arr,n); 35 | 36 | printf("Sorted list is :\n"); 37 | display(arr,n); 38 | 39 | }/*End of main()*/ 40 | 41 | void heap_sort(int arr[], int size) 42 | { 43 | int max; 44 | buildHeap(arr, size); 45 | printf("Heap is : "); 46 | display(arr,size); 47 | 48 | while(size>1) 49 | { 50 | max = del_root(arr,&size); 51 | arr[size+1]=max; 52 | } 53 | }/*End of heap_sort*/ 54 | 55 | void buildHeap(int arr[], int size) 56 | { 57 | int i; 58 | for(i=size/2; i>=1; i--) 59 | restoreDown(arr,i,size); 60 | }/*End of buildHeap()*/ 61 | 62 | int del_root(int arr[], int *size) 63 | { 64 | int max = arr[1]; 65 | arr[1] = arr[*size]; 66 | (*size)--; 67 | restoreDown(arr,1,*size); 68 | return max; 69 | }/*End of del_root()*/ 70 | 71 | void restoreDown(int arr[], int i, int size ) 72 | { 73 | int left=2*i, right=left+1; 74 | 75 | int num=arr[i]; 76 | 77 | while(right<=size) 78 | { 79 | if( num>=arr[left] && num>=arr[right] ) 80 | { 81 | arr[i] = num; 82 | return; 83 | } 84 | else if(arr[left] > arr[right]) 85 | { 86 | arr[i] = arr[left]; 87 | i = left; 88 | } 89 | else 90 | { 91 | arr[i] = arr[right]; 92 | i = right; 93 | } 94 | left = 2 * i; 95 | right = left + 1; 96 | } 97 | 98 | if(left == size && num < arr[left] ) /*when right == size+1*/ 99 | { 100 | arr[i]=arr[left]; 101 | i = left; 102 | } 103 | arr[i]=num; 104 | }/*End of restoreDown()*/ 105 | 106 | void display(int arr[], int n) 107 | { 108 | int i; 109 | for(i=1;i<=n;i++) 110 | printf("%d ",arr[i]); 111 | printf("\n"); 112 | }/*End of display()*/ 113 | 114 | 115 | -------------------------------------------------------------------------------- /5. Queue/1_queue using array.c: -------------------------------------------------------------------------------- 1 | /*Program of queue using array*/ 2 | #include 3 | #include 4 | #define MAX 10 5 | 6 | int queue_arr[MAX]; 7 | int rear=-1; 8 | int front=-1; 9 | 10 | void insert(int item); 11 | int del(); 12 | int peek(); 13 | void display(); 14 | int isFull(); 15 | int isEmpty(); 16 | 17 | main() 18 | { 19 | int choice,item; 20 | while(1) 21 | { 22 | printf("1.Insert\n"); 23 | printf("2.Delete\n"); 24 | printf("3.Display element at the front\n"); 25 | printf("4.Display all elements of the queue\n"); 26 | printf("5.Quit\n"); 27 | printf("Enter your choice : "); 28 | scanf("%d",&choice); 29 | 30 | switch(choice) 31 | { 32 | case 1: 33 | printf("Input the element for adding in queue : "); 34 | scanf("%d",&item); 35 | insert(item); 36 | break; 37 | case 2: 38 | item=del(); 39 | printf("Deleted element is %d\n",item); 40 | break; 41 | case 3: 42 | printf("Element at the front is %d\n",peek()); 43 | break; 44 | case 4: 45 | display(); 46 | break; 47 | case 5: 48 | exit(1); 49 | default: 50 | printf("Wrong choice\n"); 51 | }/*End of switch*/ 52 | }/*End of while*/ 53 | }/*End of main()*/ 54 | 55 | void insert(int item) 56 | { 57 | if( isFull() ) 58 | { 59 | printf("Queue Overflow\n"); 60 | return; 61 | } 62 | if( front == -1 ) 63 | front=0; 64 | rear=rear+1; 65 | queue_arr[rear]=item ; 66 | }/*End of insert()*/ 67 | 68 | int del() 69 | { 70 | int item; 71 | if( isEmpty() ) 72 | { 73 | printf("Queue Underflow\n"); 74 | exit(1); 75 | } 76 | item=queue_arr[front]; 77 | front=front+1; 78 | return item; 79 | }/*End of del()*/ 80 | 81 | int peek() 82 | { 83 | if( isEmpty() ) 84 | { 85 | printf("Queue Underflow\n"); 86 | exit(1); 87 | } 88 | return queue_arr[front]; 89 | }/*End of peek()*/ 90 | 91 | int isEmpty() 92 | { 93 | if( front==-1 || front==rear+1 ) 94 | return 1; 95 | else 96 | return 0; 97 | }/*End of isEmpty()*/ 98 | 99 | int isFull() 100 | { 101 | if( rear==MAX-1 ) 102 | return 1; 103 | else 104 | return 0; 105 | }/*End of isFull()*/ 106 | 107 | void display() 108 | { 109 | int i; 110 | if ( isEmpty() ) 111 | { 112 | printf("Queue is empty\n"); 113 | return; 114 | } 115 | printf("Queue is :\n\n"); 116 | for(i=front;i<=rear;i++) 117 | printf("%d ",queue_arr[i]); 118 | printf("\n\n"); 119 | }/*End of display() */ 120 | -------------------------------------------------------------------------------- /1. Singly Linked List/2 traverse a linked list.c: -------------------------------------------------------------------------------- 1 | ///Traverse a linked list 2 | #include 3 | #include 4 | 5 | struct node { ///struct of a new node 6 | int data; ///data 7 | struct node *next; ///address 8 | } *head; 9 | 10 | void createlist(int n); ///function to create list 11 | void traverselist(); ///function to display list 12 | 13 | int main() 14 | { 15 | int n; 16 | printf("Enter the total number of nodes: "); 17 | scanf("%d",&n); 18 | createlist(n); 19 | printf("Data in the list: \n"); 20 | traverselist(); 21 | 22 | return 0; 23 | } 24 | ///create a list of n nodes 25 | void createlist( int n) 26 | { 27 | struct node *newnode, *temp; 28 | int data , i; 29 | head=(struct node *)malloc(sizeof(struct node)); 30 | ///allocate memory for head node 31 | if(head==NULL) 32 | printf("Unable to allocatae memory!!!\n"); 33 | else{ 34 | ///Input data of node from the user 35 | printf("Enter the data of node 1: "); 36 | scanf("%d",&data); 37 | 38 | head->data=data; ///link the data fieled with data 39 | head->next=NULL; ///linked the address fieled to NULL 40 | 41 | temp=head; 42 | ///create n nodes and add to the linked list 43 | for(i=2; i<=n; i++) 44 | { 45 | newnode=(struct node *)malloc(sizeof(struct node)); 46 | ///allocate memory for newnode 47 | if(newnode==NULL) 48 | { 49 | printf("Unable to allocate memory!!\n"); 50 | break; 51 | } 52 | else 53 | { 54 | printf("Enter the data of node %d: ",i); 55 | scanf("%d",&data); 56 | 57 | newnode->data=data; ///link the data fieled of newnode with data 58 | newnode->next=NULL; ///link the address field of newnode with NULL 59 | 60 | temp->next=newnode; 61 | temp=temp->next; 62 | 63 | } 64 | } 65 | } 66 | } 67 | ///Display list 68 | void traverselist() 69 | { 70 | struct node *temp; 71 | if(head==NULL) 72 | printf("List is empty!!!\n"); 73 | else 74 | { 75 | temp=head; 76 | while(temp!=NULL) 77 | { 78 | ///print the data of current node 79 | printf("Data = %d\n",temp->data); 80 | temp=temp->next; ///move to the next node 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /7. Binary Tree/5_Level Order Tree Traversal Using Queue.c: -------------------------------------------------------------------------------- 1 | // Iterative Queue based C program to do level order traversal 2 | // of Binary Tree 3 | #include 4 | #include 5 | #define MAX_Q_SIZE 500 6 | 7 | /* A binary tree node has data, pointer to left child 8 | and a pointer to right child */ 9 | struct node 10 | { 11 | int data; 12 | struct node* left; 13 | struct node* right; 14 | }; 15 | 16 | /* frunction prototypes */ 17 | struct node** createQueue(int *, int *); 18 | void enQueue(struct node **, int *, struct node *); 19 | struct node *deQueue(struct node **, int *); 20 | 21 | /* Given a binary tree, print its nodes in level order 22 | using array for implementing queue */ 23 | void printLevelOrder(struct node* root) 24 | { 25 | int rear, front; 26 | struct node **queue = createQueue(&front, &rear); 27 | struct node *temp_node = root; 28 | 29 | while (temp_node) 30 | { 31 | printf("%d ", temp_node->data); 32 | 33 | /*Enqueue left child */ 34 | if (temp_node->left) 35 | enQueue(queue, &rear, temp_node->left); 36 | 37 | /*Enqueue right child */ 38 | if (temp_node->right) 39 | enQueue(queue, &rear, temp_node->right); 40 | 41 | /*Dequeue node and make it temp_node*/ 42 | temp_node = deQueue(queue, &front); 43 | } 44 | } 45 | 46 | /*UTILITY FUNCTIONS*/ 47 | struct node** createQueue(int *front, int *rear) 48 | { 49 | struct node **queue = 50 | (struct node **)malloc(sizeof(struct node*)*MAX_Q_SIZE); 51 | 52 | *front = *rear = 0; 53 | return queue; 54 | } 55 | 56 | void enQueue(struct node **queue, int *rear, struct node *new_node) 57 | { 58 | queue[*rear] = new_node; 59 | (*rear)++; 60 | } 61 | 62 | struct node *deQueue(struct node **queue, int *front) 63 | { 64 | (*front)++; 65 | return queue[*front - 1]; 66 | } 67 | 68 | /* Helper function that allocates a new node with the 69 | given data and NULL left and right pointers. */ 70 | struct node* newNode(int data) 71 | { 72 | struct node* node = (struct node*) 73 | malloc(sizeof(struct node)); 74 | node->data = data; 75 | node->left = NULL; 76 | node->right = NULL; 77 | 78 | return(node); 79 | } 80 | 81 | /* Driver program to test above functions*/ 82 | int main() 83 | { 84 | struct node *root = newNode(1); 85 | root->left = newNode(2); 86 | root->right = newNode(3); 87 | root->left->left = newNode(4); 88 | root->left->right = newNode(5); 89 | 90 | printf("Level Order traversal of binary tree is \n"); 91 | printLevelOrder(root); 92 | 93 | return 0; 94 | } 95 | 96 | -------------------------------------------------------------------------------- /10. Sorting/10. Merge Sort Iterative.c: -------------------------------------------------------------------------------- 1 | /* P8.7 Program of sorting using merge sort without recursion*/ 2 | /* 3 | File Name : Merge Sort.c 4 | Purpose : To explain Merge Sort Through Iterative Method 5 | Date : 19 October 2018 6 | Author : Bipul Islam 7 | */ 8 | #include 9 | #define MAX 100 10 | 11 | void merge_sort(int arr[], int n); 12 | void merge_pass(int arr[], int temp[], int size, int n); 13 | void merge(int arr[], int temp[],int low1, int up1, int low2, int up2 ); 14 | void copy(int arr[], int temp[], int n); 15 | 16 | main( ) 17 | { 18 | int arr[MAX],i,n; 19 | 20 | printf("Enter the number of elements : "); 21 | scanf("%d",&n); 22 | for(i=0;i= n )/*if length of last sublist is less than size*/ 57 | up2 = n-1; 58 | merge(arr,temp,low1,up1,low2,up2); 59 | low1=up2+1; /*Take next two sublists for merging*/ 60 | } 61 | for(i=low1;i<=n-1;i++) 62 | temp[i]=arr[i]; /*If any sublist is left*/ 63 | copy(arr, temp, n); 64 | } 65 | 66 | void merge(int arr[], int temp[],int low1, int up1, int low2, int up2 ) 67 | { 68 | int i=low1; 69 | int j=low2; 70 | int k=low1; 71 | 72 | while(i<=up1 && j<=up2 ) 73 | { 74 | if( arr[i] <= arr[j] ) 75 | temp[k++]=arr[i++]; 76 | else 77 | temp[k++]=arr[j++]; 78 | } 79 | while(i<=up1) 80 | temp[k++]=arr[i++]; 81 | while(j<=up2) 82 | temp[k++]=arr[j++]; 83 | } 84 | void copy(int arr[], int temp[], int n) 85 | { 86 | int i; 87 | for(i=0;i 2 | #define MAX 100 3 | #define TRUE 1 4 | #define FALSE 0 5 | int a[MAX]; 6 | int top; 7 | 8 | void initialize() { 9 | top = -1; 10 | } 11 | 12 | int isFull() { 13 | if(top == MAX-1) 14 | return TRUE; 15 | else 16 | return FALSE; 17 | } 18 | 19 | int isEmpty() { 20 | if(top == -1) 21 | return TRUE; 22 | else 23 | return FALSE; 24 | } 25 | 26 | void push(int x) { 27 | if(isFull()) 28 | { 29 | printf("Stack Overflow\n"); 30 | return; 31 | } 32 | ///else 33 | top = top + 1; 34 | a[top] = x; 35 | } 36 | int pop() { 37 | int x; 38 | if(isEmpty()) 39 | { 40 | printf("Stack Underflow\n"); 41 | exit(1); 42 | } 43 | ///else 44 | x = a[top]; 45 | top = top - 1; 46 | return x; 47 | } 48 | 49 | void display() 50 | { 51 | int i; 52 | printf("top = %d\n", top); 53 | if(isEmpty()) 54 | { 55 | printf("Stack is Empty\n"); 56 | } 57 | ///else 58 | printf("Stack is : \n\n"); 59 | for(i = top; i>=0; i--) 60 | printf(" %d\n", a[i]); 61 | printf("\n"); 62 | } 63 | void insertAtBottom(int item) { 64 | if (isEmpty()) { 65 | push(item); 66 | } else { 67 | 68 | /* Store the top most element of stack in top variable and 69 | recursively call insertAtBottom for rest of the stack */ 70 | int top = pop(); 71 | insertAtBottom(item); 72 | 73 | /* Once the item is inserted at the bottom, push the 74 | top element back to stack */ 75 | push(top); 76 | } 77 | } 78 | 79 | void reverse() { 80 | if (!isEmpty()) { 81 | /* keep on popping top element of stack in 82 | every recursive call till stack is empty */ 83 | int top = pop(); 84 | reverse(); 85 | 86 | /* Now, instead of inserting element back on top 87 | of stack, we will insert it at the bottom of stack */ 88 | insertAtBottom(top); 89 | } 90 | } 91 | 92 | int main() { 93 | ///Initializing top index of stack */ 94 | initialize(); 95 | ///Adding elements in stack 96 | push(1); 97 | push(2); 98 | push(3); 99 | push(4); 100 | push(5); 101 | printf("Original Stack\n"); 102 | display(); 103 | reverse(); 104 | printf("\nReversed Stack\n"); 105 | display(); 106 | return 0; 107 | } 108 | -------------------------------------------------------------------------------- /1. Singly Linked List/4 counting nodes in a linked list.c: -------------------------------------------------------------------------------- 1 | ///Traverse a linked list 2 | #include 3 | #include 4 | 5 | struct node { ///struct of a new node 6 | int data; ///data 7 | struct node *next; ///address 8 | } *head; 9 | 10 | void createlist(int n); ///function to create list 11 | void traverselist(); ///function to display list 12 | 13 | int main() 14 | { 15 | int n; 16 | printf("Enter the total number of nodes: "); 17 | scanf("%d",&n); 18 | createlist(n); 19 | printf("Data in the list: \n"); 20 | traverselist(); 21 | 22 | return 0; 23 | } 24 | ///create a list of n nodes 25 | void createlist( int n) 26 | { 27 | struct node *newnode, *temp; 28 | int data , i; 29 | head=(struct node *)malloc(sizeof(struct node)); 30 | ///allocate memory for head node 31 | if(head==NULL) 32 | printf("Unable to allocatae memory!!!\n"); 33 | else{ 34 | ///Input data of node from the user 35 | printf("Enter the data of node 1: "); 36 | scanf("%d",&data); 37 | 38 | head->data=data; ///link the data fieled with data 39 | head->next=NULL; ///linked the address fieled to NULL 40 | 41 | temp=head; 42 | ///create n nodes and add to the linked list 43 | for(i=2; i<=n; i++) 44 | { 45 | newnode=(struct node *)malloc(sizeof(struct node)); 46 | ///allocate memory for newnode 47 | if(newnode==NULL) 48 | { 49 | printf("Unable to allocate memory!!\n"); 50 | break; 51 | } 52 | else 53 | { 54 | printf("Enter the data of node %d: ",i); 55 | scanf("%d",&data); 56 | 57 | newnode->data=data; ///link the data fieled of newnode with data 58 | newnode->next=NULL; ///link the address field of newnode with NULL 59 | 60 | temp->next=newnode; 61 | temp=temp->next; 62 | 63 | } 64 | } 65 | } 66 | } 67 | ///Display list 68 | void traverselist() 69 | { 70 | struct node *temp; 71 | if(head==NULL) 72 | printf("List is empty!!!\n"); 73 | else 74 | { 75 | temp=head; 76 | int count=0; 77 | while(temp!=NULL) 78 | { 79 | ///print the data of current node 80 | printf("Data = %d\n",temp->data); 81 | temp=temp->next; ///move to the next node 82 | count++; ///counting nodes 83 | } 84 | printf("Number of nodes: %d\n",count); 85 | } 86 | } 87 | 88 | -------------------------------------------------------------------------------- /5. Queue/2_queue using linked list.c: -------------------------------------------------------------------------------- 1 | /*Program of queue using linked list*/ 2 | #include 3 | #include 4 | 5 | struct node 6 | { 7 | int info; 8 | struct node *link; 9 | }*front=NULL,*rear=NULL; 10 | 11 | void insert(int item); 12 | int del(); 13 | int peek(); 14 | int isEmpty(); 15 | void display(); 16 | 17 | main() 18 | { 19 | int choice,item; 20 | while(1) 21 | { 22 | printf("1.Insert\n"); 23 | printf("2.Delete\n"); 24 | printf("3.Display the element at the front\n"); 25 | printf("4.Display all elements of the queue\n"); 26 | printf("5.Quit\n"); 27 | printf("Enter your choice : "); 28 | scanf("%d", &choice); 29 | 30 | switch(choice) 31 | { 32 | case 1: 33 | printf("Input the element for adding in queue : "); 34 | scanf("%d",&item); 35 | insert(item); 36 | break; 37 | case 2: 38 | printf("Deleted element is %d\n",del()); 39 | break; 40 | case 3: 41 | printf("Element at the front of the queue is %d\n", peek() ); 42 | break; 43 | case 4: 44 | display(); 45 | break; 46 | case 5: 47 | exit(1); 48 | default : 49 | printf("Wrong choice\n"); 50 | }/*End of switch*/ 51 | }/*End of while*/ 52 | }/*End of main()*/ 53 | 54 | void insert(int item) 55 | { 56 | struct node *tmp; 57 | tmp=(struct node *)malloc(sizeof(struct node)); 58 | if(tmp==NULL) 59 | { 60 | printf("Memory not available\n"); 61 | return; 62 | } 63 | tmp->info = item; 64 | tmp->link=NULL; 65 | if(front==NULL) /*If Queue is empty*/ 66 | front=tmp; 67 | else 68 | rear->link=tmp; 69 | rear=tmp; 70 | }/*End of insert()*/ 71 | 72 | int del() 73 | { 74 | struct node *tmp; 75 | int item; 76 | if( isEmpty( ) ) 77 | { 78 | printf("Queue Underflow\n"); 79 | exit(1); 80 | } 81 | tmp=front; 82 | item=tmp->info; 83 | front=front->link; 84 | free(tmp); 85 | return item; 86 | }/*End of del()*/ 87 | 88 | int peek() 89 | { 90 | if( isEmpty( ) ) 91 | { 92 | printf("Queue Underflow\n"); 93 | exit(1); 94 | } 95 | return front->info; 96 | }/*End of peek()*/ 97 | 98 | int isEmpty() 99 | { 100 | if(front==NULL) 101 | return 1; 102 | else 103 | return 0; 104 | 105 | }/*End of isEmpty()*/ 106 | 107 | void display() 108 | { 109 | struct node *ptr; 110 | ptr=front; 111 | if(isEmpty()) 112 | { 113 | printf("Queue is empty\n"); 114 | return; 115 | } 116 | printf("Queue elements :\n\n"); 117 | while(ptr!=NULL) 118 | { 119 | printf("%d ",ptr->info); 120 | ptr=ptr->link; 121 | } 122 | printf("\n\n"); 123 | }/*End of display()*/ 124 | -------------------------------------------------------------------------------- /1. Singly Linked List/22 sorted linked list.C: -------------------------------------------------------------------------------- 1 | /*Program of sorted linked list*/ 2 | 3 | #include 4 | #include 5 | 6 | struct node 7 | { 8 | int info; 9 | struct node *link; 10 | }; 11 | struct node *insert_s(struct node *start,int data); 12 | void search(struct node *start,int data); 13 | void display(struct node *start); 14 | main() 15 | { 16 | int choice,data; 17 | struct node *start=NULL; 18 | while(1) 19 | { 20 | printf("1.Insert\n"); 21 | printf("2.Display\n"); 22 | printf("3.Search\n"); 23 | printf("4.Exit\n"); 24 | printf("Enter your choice : "); 25 | scanf("%d",&choice); 26 | switch(choice) 27 | { 28 | case 1: 29 | printf("Enter the element to be inserted : "); 30 | scanf("%d",&data); 31 | start=insert_s(start,data); 32 | break; 33 | case 2: 34 | display(start); 35 | break; 36 | case 3: 37 | printf("Enter the element to be searched : "); 38 | scanf("%d",&data); 39 | search(start,data); 40 | break; 41 | case 4: 42 | exit(1); 43 | default: 44 | printf("Wrong choice\n"); 45 | }/*End of switch*/ 46 | }/*End of while*/ 47 | } /*end of main */ 48 | 49 | struct node *insert_s(struct node *start,int data) 50 | { 51 | struct node *p,*tmp; 52 | tmp=(struct node *)malloc(sizeof(struct node)); 53 | tmp->info=data; 54 | /*list empty or new node to be added before first node*/ 55 | if(start==NULL || datainfo) 56 | { 57 | tmp->link=start; 58 | start=tmp; 59 | return start; 60 | } 61 | else 62 | { 63 | p=start; 64 | while(p->link!=NULL && p->link->info < data) 65 | p=p->link; 66 | tmp->link=p->link; 67 | p->link=tmp; 68 | } 69 | return start; 70 | }/*End of insert()*/ 71 | void search(struct node *start,int data) 72 | { 73 | struct node *p; 74 | int pos; 75 | 76 | if(start==NULL || data < start->info) 77 | { 78 | printf("%d not found in list\n",data); 79 | return; 80 | } 81 | p=start; 82 | pos=1; 83 | while(p!=NULL && p->info<=data) 84 | { 85 | if(p->info==data) 86 | { 87 | printf("%d found at position %d\n",data,pos); 88 | return; 89 | } 90 | p=p->link; 91 | pos++; 92 | } 93 | printf("%d not found in list\n",data); 94 | }/*End of search()*/ 95 | 96 | void display(struct node *start) 97 | { 98 | struct node *q; 99 | if(start==NULL) 100 | { 101 | printf("List is empty\n"); 102 | return; 103 | } 104 | q=start; 105 | printf("List is :\n"); 106 | while(q!=NULL) 107 | { 108 | printf("%d ",q->info); 109 | q=q->link; 110 | } 111 | printf("\n"); 112 | }/*End of display() */ 113 | 114 | -------------------------------------------------------------------------------- /1. Singly Linked List/9 finding pointer to a node at a particular position.c: -------------------------------------------------------------------------------- 1 | ///Traverse a linked list 2 | #include 3 | #include 4 | 5 | struct node { ///struct of a new node 6 | int data; ///data 7 | struct node *next; ///address 8 | } *head; 9 | 10 | void createlist(int n); ///function to create list 11 | void traverselist(); ///function to display list 12 | 13 | int main() 14 | { 15 | int n; 16 | printf("Enter the total number of nodes: "); 17 | scanf("%d",&n); 18 | createlist(n); 19 | int k=3; 20 | traverselist(k); 21 | 22 | return 0; 23 | } 24 | ///create a list of n nodes 25 | void createlist( int n) 26 | { 27 | struct node *newnode, *temp; 28 | int data , i; 29 | head=(struct node *)malloc(sizeof(struct node)); 30 | ///allocate memory for head node 31 | if(head==NULL) 32 | printf("Unable to allocatae memory!!!\n"); 33 | else{ 34 | ///Input data of node from the user 35 | printf("Enter the data of node 1: "); 36 | scanf("%d",&data); 37 | 38 | head->data=data; ///link the data fieled with data 39 | head->next=NULL; ///linked the address fieled to NULL 40 | 41 | temp=head; 42 | ///create n nodes and add to the linked list 43 | for(i=2; i<=n; i++) 44 | { 45 | newnode=(struct node *)malloc(sizeof(struct node)); 46 | ///allocate memory for newnode 47 | if(newnode==NULL) 48 | { 49 | printf("Unable to allocate memory!!\n"); 50 | break; 51 | } 52 | else 53 | { 54 | printf("Enter the data of node %d: ",i); 55 | scanf("%d",&data); 56 | 57 | newnode->data=data; ///link the data fieled of newnode with data 58 | newnode->next=NULL; ///link the address field of newnode with NULL 59 | 60 | temp->next=newnode; 61 | temp=temp->next; 62 | 63 | } 64 | } 65 | } 66 | } 67 | ///Display list 68 | void traverselist(int k) 69 | { 70 | struct node *temp; 71 | 72 | if(head==NULL) 73 | printf("List is empty!!!\n"); 74 | else 75 | { 76 | temp=head; 77 | int position=1; 78 | int i; 79 | for(i=1; inext; ///move to the next node 82 | } 83 | if(temp==NULL) 84 | printf("Value of position %d is not found in the list.\n", k); 85 | else 86 | printf("Value of position %d is %d.\n",k,temp->data); 87 | } 88 | } 89 | ///same as searching 90 | -------------------------------------------------------------------------------- /3. Circular Linked List/2_Concatenate two circular linked lists.c: -------------------------------------------------------------------------------- 1 | /* P3.9 Program to concatenate two circular linked lists*/ 2 | #include 3 | #include 4 | 5 | struct node 6 | { 7 | int info; 8 | struct node *link; 9 | }; 10 | 11 | struct node *create_list(struct node *last); 12 | void display(struct node *last); 13 | struct node *addtoempty(struct node *last,int data ); 14 | struct node *addatend(struct node *last,int data); 15 | struct node *concat(struct node *last1,struct node *last2); 16 | 17 | main( ) 18 | { 19 | struct node *last1=NULL,*last2=NULL; 20 | last1=create_list(last1); 21 | last2=create_list(last2); 22 | printf("First list is : "); 23 | display(last1); 24 | printf("Second list is : "); 25 | display(last2); 26 | last1=concat(last1, last2); 27 | printf("Concatenated list is : "); 28 | display(last1); 29 | }/*End of main( )*/ 30 | 31 | struct node *concat( struct node *last1,struct node *last2) 32 | { 33 | struct node *ptr; 34 | if(last1==NULL) 35 | { 36 | last1=last2; 37 | return last1; 38 | } 39 | if(last2==NULL ) 40 | return last1; 41 | ptr=last1->link; 42 | last1->link=last2->link; 43 | last2->link=ptr; 44 | last1=last2; 45 | return last1; 46 | } 47 | struct node *create_list(struct node *last) 48 | { 49 | int i,n; 50 | int data; 51 | printf("Enter the number of nodes : "); 52 | scanf("%d",&n); 53 | last=NULL; 54 | if(n==0) 55 | return last; 56 | printf("Enter the element to be inserted : "); 57 | scanf("%d",&data); 58 | last=addtoempty(last,data); 59 | 60 | for(i=2;i<=n;i++) 61 | { 62 | printf("Enter the element to be inserted : "); 63 | scanf("%d",&data); 64 | last=addatend(last,data); 65 | } 66 | return last; 67 | } 68 | 69 | void display(struct node *last) 70 | { 71 | struct node *p; 72 | if(last==NULL) 73 | { 74 | printf("List is empty\n"); 75 | return; 76 | } 77 | p=last->link; /*p points to first node*/ 78 | do 79 | { 80 | printf("%d ", p->info); 81 | p=p->link; 82 | }while(p!=last->link); 83 | printf("\n"); 84 | }/*End of display( )*/ 85 | 86 | struct node *addtoempty(struct node *last,int data) 87 | { 88 | struct node *tmp; 89 | tmp = (struct node *)malloc(sizeof(struct node)); 90 | tmp->info = data; 91 | last = tmp; 92 | last->link = last; 93 | return last; 94 | }/*End of addtoempty( )*/ 95 | 96 | struct node *addatend(struct node *last,int data) 97 | { 98 | struct node *tmp; 99 | tmp = (struct node *)malloc(sizeof(struct node)); 100 | tmp->info = data; 101 | tmp->link = last->link; 102 | last->link = tmp; 103 | last = tmp; 104 | return last; 105 | }/*End of addatend( )*/ 106 | 107 | -------------------------------------------------------------------------------- /1. Singly Linked List/3 searching in a linked list.c: -------------------------------------------------------------------------------- 1 | ///Traverse a linked list 2 | #include 3 | #include 4 | 5 | struct node { ///struct of a new node 6 | int data; ///data 7 | struct node *next; ///address 8 | } *head; 9 | 10 | void createlist(int n); ///function to create list 11 | void search(); ///function to search in the linked list 12 | 13 | int main() 14 | { 15 | int n, key; 16 | printf("Enter the total number of nodes: "); 17 | scanf("%d",&n); 18 | createlist(n); 19 | printf("Enter a value to search: \n"); 20 | scanf("%d",&key); 21 | search(key); 22 | 23 | return 0; 24 | } 25 | ///create a list of n nodes 26 | void createlist( int n) 27 | { 28 | struct node *newnode, *temp; 29 | int data , i; 30 | head=(struct node *)malloc(sizeof(struct node)); 31 | ///allocate memory for head node 32 | if(head==NULL) 33 | printf("Unable to allocatae memory!!!\n"); 34 | else{ 35 | ///Input data of node from the user 36 | printf("Enter the data of node 1: "); 37 | scanf("%d",&data); 38 | 39 | head->data=data; ///link the data fieled with data 40 | head->next=NULL; ///linked the address fieled to NULL 41 | 42 | temp=head; 43 | ///create n nodes and add to the linked list 44 | for(i=2; i<=n; i++) 45 | { 46 | newnode=(struct node *)malloc(sizeof(struct node)); 47 | ///allocate memory for newnode 48 | if(newnode==NULL) 49 | { 50 | printf("Unable to allocate memory!!\n"); 51 | break; 52 | } 53 | else 54 | { 55 | printf("Enter the data of node %d: ",i); 56 | scanf("%d",&data); 57 | 58 | newnode->data=data; ///link the data fieled of newnode with data 59 | newnode->next=NULL; ///link the address field of newnode with NULL 60 | 61 | temp->next=newnode; 62 | temp=temp->next; 63 | 64 | } 65 | } 66 | } 67 | } 68 | ///Search in a linked list 69 | void search(int key) 70 | { 71 | struct node *temp; 72 | if(head==NULL) 73 | printf("List is empty!!!\n"); 74 | else 75 | { 76 | temp=head; 77 | int position=1; 78 | while(temp!=NULL) 79 | { 80 | if(temp->data==key) 81 | break; 82 | position++; 83 | 84 | temp=temp->next; ///move to the next node 85 | } 86 | if(temp==NULL) 87 | printf("%d is not found in the list.\n", key); 88 | else 89 | printf("%d is at postion %d.\n",key, position); 90 | } 91 | } 92 | 93 | -------------------------------------------------------------------------------- /1. Singly Linked List/7 finding pointer to a node with particular information.c: -------------------------------------------------------------------------------- 1 | ///Traverse a linked list 2 | #include 3 | #include 4 | 5 | struct node { ///struct of a new node 6 | int data; ///data 7 | struct node *next; ///address 8 | } *head; 9 | 10 | void createlist(int n); ///function to create list 11 | void traverselist(); ///function to display list 12 | 13 | int main() 14 | { 15 | int n; 16 | printf("Enter the total number of nodes: "); 17 | scanf("%d",&n); 18 | createlist(n); 19 | int x=3; 20 | traverselist(x); 21 | 22 | return 0; 23 | } 24 | ///create a list of n nodes 25 | void createlist( int n) 26 | { 27 | struct node *newnode, *temp; 28 | int data , i; 29 | head=(struct node *)malloc(sizeof(struct node)); 30 | ///allocate memory for head node 31 | if(head==NULL) 32 | printf("Unable to allocatae memory!!!\n"); 33 | else{ 34 | ///Input data of node from the user 35 | printf("Enter the data of node 1: "); 36 | scanf("%d",&data); 37 | 38 | head->data=data; ///link the data fieled with data 39 | head->next=NULL; ///linked the address fieled to NULL 40 | 41 | temp=head; 42 | ///create n nodes and add to the linked list 43 | for(i=2; i<=n; i++) 44 | { 45 | newnode=(struct node *)malloc(sizeof(struct node)); 46 | ///allocate memory for newnode 47 | if(newnode==NULL) 48 | { 49 | printf("Unable to allocate memory!!\n"); 50 | break; 51 | } 52 | else 53 | { 54 | printf("Enter the data of node %d: ",i); 55 | scanf("%d",&data); 56 | 57 | newnode->data=data; ///link the data fieled of newnode with data 58 | newnode->next=NULL; ///link the address field of newnode with NULL 59 | 60 | temp->next=newnode; 61 | temp=temp->next; 62 | 63 | } 64 | } 65 | } 66 | } 67 | ///Display list 68 | void traverselist(int x) 69 | { 70 | struct node *temp; 71 | 72 | if(head==NULL) 73 | printf("List is empty!!!\n"); 74 | else 75 | { 76 | temp=head; 77 | int position=1; 78 | while(temp!=NULL) 79 | { 80 | if(temp->data==x) 81 | break; 82 | position++; 83 | 84 | temp=temp->next; ///move to the next node 85 | } 86 | if(temp==NULL) 87 | printf("%d is not found in the list.\n", x); 88 | else 89 | printf("%d is at postion %d.\n",x, position); 90 | } 91 | } 92 | ///same as searching 93 | 94 | 95 | 96 | 97 | -------------------------------------------------------------------------------- /1. Singly Linked List/21 concatenate two single linked lists.c: -------------------------------------------------------------------------------- 1 | /*To show concatenation in single linked list*/ 2 | 3 | #include 4 | #include 5 | 6 | struct node 7 | { 8 | int info; 9 | struct node *link; 10 | }; 11 | struct node *createList(struct node *); 12 | struct node *concatenate( struct node *start1,struct node *start2); 13 | struct node *insertInBeginning(struct node *start, int data); 14 | struct node *insertAtEnd(struct node *start,int data); 15 | void display(struct node *start); 16 | 17 | main() 18 | { 19 | struct node *start1=NULL,*start2=NULL; 20 | start1=createList(start1); 21 | start2=createList(start2); 22 | printf("First list is : "); 23 | display(start1); 24 | printf("Second list is : "); 25 | display(start2); 26 | start1=concatenate(start1, start2); 27 | printf("Concatenated list is : "); 28 | display(start1); 29 | }/*End of main()*/ 30 | 31 | struct node *concatenate( struct node *start1,struct node *start2) 32 | { 33 | struct node *ptr; 34 | if(start1==NULL) 35 | { 36 | start1=start2; 37 | return start1; 38 | } 39 | if(start2==NULL) 40 | return start1; 41 | ptr=start1; 42 | while(ptr->link!=NULL) 43 | ptr=ptr->link; 44 | ptr->link=start2; 45 | return start1; 46 | } 47 | struct node *createList(struct node *start) 48 | { 49 | int i,n,data; 50 | printf("Enter the number of nodes : "); 51 | scanf("%d",&n); 52 | start=NULL; 53 | if(n==0) 54 | return start; 55 | 56 | printf("Enter the element to be inserted : "); 57 | scanf("%d",&data); 58 | start=insertInBeginning(start,data); 59 | 60 | for(i=2;i<=n;i++) 61 | { 62 | printf("Enter the element to be inserted : "); 63 | scanf("%d",&data); 64 | start=insertAtEnd(start,data); 65 | } 66 | return start; 67 | }/*End of createList()*/ 68 | 69 | void display(struct node *start) 70 | { 71 | struct node *p; 72 | if(start==NULL) 73 | { 74 | printf("List is empty\n"); 75 | return; 76 | } 77 | p=start; 78 | while(p!=NULL) 79 | { 80 | printf("%d ", p->info); 81 | p=p->link; 82 | } 83 | printf("\n"); 84 | }/*End of display() */ 85 | 86 | struct node *insertInBeginning(struct node *start,int data) 87 | { 88 | struct node *tmp; 89 | tmp=(struct node *)malloc(sizeof(struct node)); 90 | tmp->info=data; 91 | tmp->link=start; 92 | start=tmp; 93 | return start; 94 | }/*End of insertInBeginning()*/ 95 | 96 | struct node *insertAtEnd(struct node *start, int data) 97 | { 98 | struct node *p,*tmp; 99 | tmp= (struct node *)malloc(sizeof(struct node)); 100 | tmp->info=data; 101 | p=start; 102 | while(p->link!=NULL) 103 | p=p->link; 104 | p->link=tmp; 105 | tmp->link=NULL; 106 | return start; 107 | }/*End of insertAtEnd()*/ 108 | -------------------------------------------------------------------------------- /5. Queue/3_queue using circular linked list.c: -------------------------------------------------------------------------------- 1 | /*Program of queue using circular linked list*/ 2 | 3 | #include 4 | #include 5 | 6 | struct node 7 | { 8 | int info; 9 | struct node *link; 10 | }*rear=NULL; 11 | 12 | void insert(int item); 13 | int del(); 14 | void display(); 15 | int isEmpty(); 16 | int peek(); 17 | 18 | main() 19 | { 20 | int choice,item; 21 | while(1) 22 | { 23 | printf("1.Insert\n"); 24 | printf("2.Delete\n"); 25 | printf("3.Peek\n"); 26 | printf("4.Display\n"); 27 | printf("5.Quit\n"); 28 | printf("Enter your choice : "); 29 | scanf("%d",&choice); 30 | 31 | switch(choice) 32 | { 33 | case 1: 34 | printf("Enter the element for insertion : "); 35 | scanf("%d",&item); 36 | insert(item); 37 | break; 38 | case 2: 39 | printf("Deleted element is %d\n",del()); 40 | break; 41 | case 3: 42 | printf("Item at the front of queue is %d\n",peek()); 43 | break; 44 | case 4: 45 | display(); 46 | break; 47 | case 5: 48 | exit(1); 49 | default: 50 | printf("Wrong choice\n"); 51 | }/*End of switch*/ 52 | }/*End of while*/ 53 | }/*End of main()*/ 54 | 55 | void insert(int item) 56 | { 57 | struct node *tmp; 58 | tmp=(struct node *)malloc(sizeof(struct node)); 59 | tmp->info=item; 60 | if(tmp==NULL) 61 | { 62 | printf("Memory not available\n"); 63 | return; 64 | } 65 | 66 | if( isEmpty() ) /*If queue is empty */ 67 | { 68 | rear=tmp; 69 | tmp->link=rear; 70 | } 71 | else 72 | { 73 | tmp->link=rear->link; 74 | rear->link=tmp; 75 | rear=tmp; 76 | } 77 | }/*End of insert()*/ 78 | 79 | del() 80 | { 81 | int item; 82 | struct node *tmp; 83 | if( isEmpty() ) 84 | { 85 | printf("Queue underflow\n"); 86 | exit(1); 87 | } 88 | if(rear->link==rear) /*If only one element*/ 89 | { 90 | tmp=rear; 91 | rear=NULL; 92 | } 93 | else 94 | { 95 | tmp=rear->link; 96 | rear->link=rear->link->link; 97 | } 98 | item=tmp->info; 99 | free(tmp); 100 | return item; 101 | }/*End of del()*/ 102 | 103 | int peek() 104 | { 105 | if( isEmpty() ) 106 | { 107 | printf("Queue underflow\n"); 108 | exit(1); 109 | } 110 | return rear->link->info; 111 | }/* End of peek() */ 112 | 113 | int isEmpty() 114 | { 115 | if( rear == NULL ) 116 | return 1; 117 | else 118 | return 0; 119 | }/*End of isEmpty()*/ 120 | 121 | 122 | void display() 123 | { 124 | struct node *p; 125 | if(isEmpty()) 126 | { 127 | printf("Queue is empty\n"); 128 | return; 129 | } 130 | printf("Queue is :\n"); 131 | p=rear->link; 132 | do 133 | { 134 | printf("%d ",p->info); 135 | p=p->link; 136 | }while(p!=rear->link); 137 | printf("\n"); 138 | }/*End of display()*/ 139 | 140 | -------------------------------------------------------------------------------- /5. Queue/6_priority queue using linked list.c: -------------------------------------------------------------------------------- 1 | /*Program of priority queue using linked list*/ 2 | #include 3 | #include 4 | 5 | struct node 6 | { 7 | int priority; 8 | int info; 9 | struct node *link; 10 | }*front=NULL; 11 | 12 | void insert(int item, int item_priority); 13 | int del(); 14 | void display(); 15 | int isEmpty(); 16 | 17 | main() 18 | { 19 | int choice,item,item_priority; 20 | while(1) 21 | { 22 | printf("1.Insert\n"); 23 | printf("2.Delete\n"); 24 | printf("3.Display\n"); 25 | printf("4.Quit\n"); 26 | printf("Enter your choice : "); 27 | scanf("%d", &choice); 28 | 29 | switch(choice) 30 | { 31 | case 1: 32 | printf("Input the item to be added in the queue : "); 33 | scanf("%d",&item); 34 | printf("Enter its priority : "); 35 | scanf("%d",&item_priority); 36 | insert(item, item_priority); 37 | break; 38 | case 2: 39 | printf("Deleted item is %d\n",del()); 40 | break; 41 | case 3: 42 | display(); 43 | break; 44 | case 4: 45 | exit(1); 46 | default : 47 | printf("Wrong choice\n"); 48 | }/*End of switch*/ 49 | }/*End of while*/ 50 | }/*End of main()*/ 51 | 52 | void insert(int item,int item_priority) 53 | { 54 | struct node *tmp,*p; 55 | 56 | tmp=(struct node *)malloc(sizeof(struct node)); 57 | if(tmp==NULL) 58 | { 59 | printf("Memory not available\n"); 60 | return; 61 | } 62 | tmp->info=item; 63 | tmp->priority=item_priority; 64 | /*Queue is empty or item to be added has priority more than first element*/ 65 | if( isEmpty() || item_priority < front->priority ) 66 | { 67 | tmp->link=front; 68 | front=tmp; 69 | } 70 | else 71 | { 72 | p = front; 73 | while( p->link!=NULL && p->link->priority<=item_priority ) 74 | p=p->link; 75 | tmp->link=p->link; 76 | p->link=tmp; 77 | } 78 | }/*End of insert()*/ 79 | 80 | int del() 81 | { 82 | struct node *tmp; 83 | int item; 84 | if( isEmpty() ) 85 | { 86 | printf("Queue Underflow\n"); 87 | exit(1); 88 | } 89 | else 90 | { 91 | tmp=front; 92 | item=tmp->info; 93 | front=front->link; 94 | free(tmp); 95 | } 96 | return item; 97 | }/*End of del()*/ 98 | 99 | int isEmpty() 100 | { 101 | if( front == NULL ) 102 | return 1; 103 | else 104 | return 0; 105 | 106 | }/*End of isEmpty()*/ 107 | 108 | 109 | void display() 110 | { 111 | struct node *ptr; 112 | ptr=front; 113 | if( isEmpty() ) 114 | printf("Queue is empty\n"); 115 | else 116 | { printf("Queue is :\n"); 117 | printf("Priority Item\n"); 118 | while(ptr!=NULL) 119 | { 120 | printf("%5d %5d\n",ptr->priority,ptr->info); 121 | ptr=ptr->link; 122 | } 123 | } 124 | }/*End of display() */ 125 | 126 | 127 | 128 | 129 | -------------------------------------------------------------------------------- /4. Stack/7_Stack using linked list.c: -------------------------------------------------------------------------------- 1 | /* 2 | C Program to Implement a Stack using Linked List 3 | 4 | */ 5 | #include 6 | #include 7 | 8 | struct node { 9 | int data; 10 | struct node *next; 11 | }*top; 12 | 13 | /* 14 | Initialize an empty stack 15 | */ 16 | void initialize() { 17 | top = NULL; 18 | } 19 | 20 | /* 21 | Checks if Stack is empty or not 22 | */ 23 | int isEmpty() { 24 | if (top == NULL) 25 | return 1; 26 | else 27 | return 0; 28 | } 29 | 30 | /* 31 | Returns the top element of Stack 32 | */ 33 | int peek() { 34 | return top->data; 35 | } 36 | 37 | /* Count stack elements */ 38 | int getStackSize(struct node *head){ 39 | /* Input Validation */ 40 | if (head == NULL) { 41 | printf("Error : Invalid stack pointer !!!\n"); 42 | return; 43 | } 44 | 45 | int length = 0; 46 | while(head != NULL){ 47 | head = head->next; 48 | length++; 49 | } 50 | return length; 51 | } 52 | 53 | /* 54 | Push an Element in Stack 55 | */ 56 | void push(int num) { 57 | struct node *temp; 58 | temp =(struct node *)malloc(1*sizeof(struct node)); 59 | temp->data = num; 60 | 61 | if (top == NULL) { 62 | top = temp; 63 | top->next = NULL; 64 | } else { 65 | temp->next = top; 66 | top = temp; 67 | } 68 | } 69 | 70 | /* 71 | Pop Operation: Removes Top Element of the Stack 72 | */ 73 | void pop() { 74 | struct node *temp; 75 | if (isEmpty(top)) { 76 | printf("\nStack is Empty\n"); 77 | return; 78 | } else { 79 | temp = top; 80 | top = top->next; 81 | printf("Removed Element : %d\n", temp->data); 82 | free(temp); 83 | } 84 | } 85 | 86 | /* 87 | Prints the linked list representation of a stack 88 | */ 89 | void printStack(struct node *nodePtr) { 90 | while (nodePtr != NULL) { 91 | printf("%d", nodePtr->data); 92 | nodePtr = nodePtr->next; 93 | if(nodePtr != NULL) 94 | printf("-->"); 95 | } 96 | printf("\n"); 97 | } 98 | 99 | void main() { 100 | /* Initialize Stack */ 101 | initialize(); 102 | /* Push Elements in stack */ 103 | push(1); 104 | push(2); 105 | push(3); 106 | push(4); 107 | /* Prints Size of Stack */ 108 | printf("Stack Size : %d\n", getStackSize(top)); 109 | /* Printing top element of Stack */ 110 | printf("\nTop Element : %d\n", peek()); 111 | /* Printing Stack */ 112 | printf("Stack as linked List\n"); 113 | printStack(top); 114 | /* Removing elements from stack */ 115 | pop(); 116 | pop(); 117 | pop(); 118 | pop(); 119 | pop(); 120 | printStack(top); 121 | 122 | return; 123 | } 124 | -------------------------------------------------------------------------------- /1. Singly Linked List/20 reversing a single linked list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct node { 4 | int info; 5 | struct node *link; 6 | 7 | }; 8 | struct node *createList(struct node *start); 9 | void displayList(struct node *start); 10 | struct node *insertInBeginning(struct node *start, int data); 11 | void insertAtEnd(struct node *start, int data); 12 | struct node *reverseList(struct node *start); 13 | 14 | int main() 15 | { 16 | struct node *start=NULL; 17 | int choice, data, x, k; 18 | start=createList(start); 19 | displayList(start); ///display lsit 20 | 21 | ///call function to reverse list 22 | start=reverseList(start); 23 | printf("Reverse "); 24 | displayList(start); 25 | 26 | } 27 | struct node *createList(struct node *start) 28 | { 29 | int i, n, data; 30 | printf("Enter the no of nodes: "); 31 | scanf("%d",&n); 32 | if(n==0) 33 | return start; 34 | 35 | printf("Enter the first elemlent to be inserted: "); 36 | scanf("%d",&data); 37 | start=insertInBeginning(start, data); 38 | for(i=2; i<=n; i++) 39 | { 40 | printf("Enter the next element to be inserted: "); 41 | scanf("%d",&data); 42 | insertAtEnd(start, data); 43 | } 44 | return start; 45 | }; 46 | struct node *insertInBeginning(struct node *start, int data) 47 | { 48 | struct node *temp; 49 | temp=(struct node * )malloc(sizeof(struct node)); 50 | 51 | temp->info=data; 52 | temp->link=start; 53 | start=temp; 54 | 55 | return start; 56 | }; 57 | void insertAtEnd(struct node *start, int data) 58 | { 59 | struct node *temp, *p; 60 | temp=(struct node * )malloc(sizeof(struct node)); 61 | 62 | temp->info=data; 63 | 64 | p=start; 65 | 66 | while(p->link!=NULL) 67 | { 68 | p=p->link; 69 | } 70 | p->link=temp; 71 | temp->link=NULL; 72 | } 73 | void displayList(struct node *start) 74 | { 75 | struct node *p; 76 | if(start==NULL) 77 | { 78 | printf("List is empty!!!\n"); 79 | return; 80 | } 81 | printf("List is : "); 82 | ///Code for traverse list 83 | ///Traverse list means visiting each nodes exactly ones 84 | p=start; 85 | while(p!=NULL) 86 | { 87 | printf("%d ", p->info); 88 | p=p->link; 89 | } 90 | printf("\n"); 91 | } 92 | struct node *reverseList(struct node *start) 93 | { 94 | struct node *prev, *ptr, *next; 95 | prev=NULL; 96 | ptr=start; 97 | while(ptr!=NULL) 98 | { 99 | next=ptr->link; 100 | ptr->link=prev; 101 | prev=ptr; 102 | ptr=next; 103 | } 104 | start=prev; 105 | 106 | return start; 107 | }; 108 | -------------------------------------------------------------------------------- /1. Singly Linked List/8 finding pointer to predecessor of a node with particular info.c: -------------------------------------------------------------------------------- 1 | ///Traverse a linked list 2 | #include 3 | #include 4 | 5 | struct node { ///struct of a new node 6 | int data; ///data 7 | struct node *next; ///address 8 | } *head; 9 | 10 | void createlist(int n); ///function to create list 11 | void traverselist(); ///function to display list 12 | 13 | int main() 14 | { 15 | int n; 16 | printf("Enter the total number of nodes: "); 17 | scanf("%d",&n); 18 | createlist(n); 19 | int x=3; 20 | traverselist(x); 21 | 22 | return 0; 23 | } 24 | ///create a list of n nodes 25 | void createlist( int n) 26 | { 27 | struct node *newnode, *temp; 28 | int data , i; 29 | head=(struct node *)malloc(sizeof(struct node)); 30 | ///allocate memory for head node 31 | if(head==NULL) 32 | printf("Unable to allocatae memory!!!\n"); 33 | else{ 34 | ///Input data of node from the user 35 | printf("Enter the data of node 1: "); 36 | scanf("%d",&data); 37 | 38 | head->data=data; ///link the data fieled with data 39 | head->next=NULL; ///linked the address fieled to NULL 40 | 41 | temp=head; 42 | ///create n nodes and add to the linked list 43 | for(i=2; i<=n; i++) 44 | { 45 | newnode=(struct node *)malloc(sizeof(struct node)); 46 | ///allocate memory for newnode 47 | if(newnode==NULL) 48 | { 49 | printf("Unable to allocate memory!!\n"); 50 | break; 51 | } 52 | else 53 | { 54 | printf("Enter the data of node %d: ",i); 55 | scanf("%d",&data); 56 | 57 | newnode->data=data; ///link the data fieled of newnode with data 58 | newnode->next=NULL; ///link the address field of newnode with NULL 59 | 60 | temp->next=newnode; 61 | temp=temp->next; 62 | 63 | } 64 | } 65 | } 66 | } 67 | ///Display list 68 | void traverselist(int x) 69 | { 70 | struct node *temp; 71 | 72 | if(head==NULL) 73 | printf("List is empty!!!\n"); 74 | else 75 | { 76 | temp=head; 77 | int position=1; 78 | while(temp!=NULL) 79 | { 80 | if(temp->next->data==x) 81 | ///predecessor of temp->next->data is temp->next 82 | break; 83 | position++; 84 | 85 | temp=temp->next; ///move to the next node 86 | } 87 | if(temp==NULL) 88 | printf("Predecessor of %d is not found in the list.\n", x); 89 | else 90 | printf("Predecessor of %d is %d at postion %d.\n",x,temp->data, position); 91 | } 92 | } 93 | ///same as searching 94 | 95 | 96 | 97 | 98 | 99 | -------------------------------------------------------------------------------- /5. Queue/4_circular queue.c: -------------------------------------------------------------------------------- 1 | /*Program of circular queue*/ 2 | #include 3 | #include 4 | #define MAX 10 5 | 6 | int cqueue_arr[MAX]; 7 | int front=-1; 8 | int rear=-1; 9 | 10 | void display( ); 11 | void insert(item); 12 | int del(); 13 | int peek(); 14 | int isEmpty(); 15 | int isFull(); 16 | 17 | main() 18 | { 19 | int choice,item; 20 | while(1) 21 | { 22 | printf("1.Insert\n"); 23 | printf("2.Delete\n"); 24 | printf("3.Peek\n"); 25 | printf("4.Display\n"); 26 | printf("5.Quit\n"); 27 | printf("Enter your choice : "); 28 | scanf("%d",&choice); 29 | 30 | switch(choice) 31 | { 32 | case 1 : 33 | printf("Input the element for insertion : "); 34 | scanf("%d",&item); 35 | insert(item); 36 | break; 37 | case 2 : 38 | printf("Element deleted is : %d\n",del()); 39 | break; 40 | case 3: 41 | printf("Element at the front is : %d\n",peek()); 42 | break; 43 | case 4: 44 | display(); 45 | break; 46 | case 5: 47 | exit(1); 48 | default: 49 | printf("Wrong choice\n"); 50 | }/*End of switch*/ 51 | }/*End of while */ 52 | }/*End of main()*/ 53 | 54 | void insert(int item) 55 | { 56 | if( isFull() ) 57 | { 58 | printf("Queue Overflow\n"); 59 | return; 60 | } 61 | if(front == -1 ) 62 | front=0; 63 | 64 | if(rear==MAX-1)/*rear is at last position of queue*/ 65 | rear=0; 66 | else 67 | rear=rear+1; 68 | cqueue_arr[rear]=item ; 69 | }/*End of insert()*/ 70 | 71 | int del() 72 | { 73 | int item; 74 | if( isEmpty() ) 75 | { 76 | printf("Queue Underflow\n"); 77 | exit(1); 78 | } 79 | item=cqueue_arr[front]; 80 | if(front==rear) /* queue has only one element */ 81 | { 82 | front=-1; 83 | rear=-1; 84 | } 85 | else if(front==MAX-1) 86 | front=0; 87 | else 88 | front=front+1; 89 | return item; 90 | }/*End of del() */ 91 | 92 | int isEmpty() 93 | { 94 | if(front==-1) 95 | return 1; 96 | else 97 | return 0; 98 | }/*End of isEmpty()*/ 99 | 100 | int isFull() 101 | { 102 | if((front==0 && rear==MAX-1) || (front==rear+1)) 103 | return 1; 104 | else 105 | return 0; 106 | }/*End of isFull()*/ 107 | 108 | int peek() 109 | { 110 | if( isEmpty() ) 111 | { 112 | printf("Queue Underflow\n"); 113 | exit(1); 114 | } 115 | return cqueue_arr[front]; 116 | }/*End of peek()*/ 117 | 118 | void display() 119 | { 120 | int i; 121 | if(isEmpty()) 122 | { 123 | printf("Queue is empty\n"); 124 | return; 125 | } 126 | printf("Queue elements :\n"); 127 | i=front; 128 | if( front<=rear ) 129 | { 130 | while(i<=rear) 131 | printf("%d ",cqueue_arr[i++]); 132 | } 133 | else 134 | { 135 | while(i<=MAX-1) 136 | printf("%d ",cqueue_arr[i++]); 137 | i=0; 138 | while(i<=rear) 139 | printf("%d ",cqueue_arr[i++]); 140 | } 141 | printf("\n"); 142 | }/*End of display() */ 143 | 144 | -------------------------------------------------------------------------------- /4. Stack/1_Stack Using Array.c: -------------------------------------------------------------------------------- 1 | ///Purpose: To implement stack using array 2 | ///Author : Bipul Islam, EEE, HSTU-5200 3 | #include 4 | #include 5 | #define MAX 50 6 | int a[MAX]; 7 | int top; 8 | void initialize(); 9 | int isEmpty(); 10 | int isFull(); 11 | int size(); 12 | int peek(); 13 | void push(int x); 14 | int pop(); 15 | void display(); 16 | 17 | main() 18 | { 19 | int choice, x; 20 | initialize(); 21 | while(1) 22 | { 23 | printf("1. Push an element on the top.\n"); 24 | printf("2. Pop an element from the top.\n"); 25 | printf("3. Display the top element.\n"); 26 | printf("4. Display all stack elements.\n"); 27 | printf("5. Display size of the stack.\n"); 28 | printf("6. Quit.\n"); 29 | printf("Enter your choice : "); 30 | scanf("%d", &choice); 31 | 32 | if(choice == 6) 33 | break; 34 | switch(choice) 35 | { 36 | case 1: 37 | printf("Enter the element to be pushed : "); 38 | scanf("%d", &x); 39 | push(x); 40 | break; 41 | case 2: 42 | x = pop(); 43 | printf("Popped element is : %d\n",x); 44 | break; 45 | case 3: 46 | printf("Element at the top is : %d\n", peek()); 47 | break; 48 | case 4: 49 | display(); 50 | break; 51 | case 5: 52 | printf("Size of stack = %d\n", size()); 53 | break; 54 | default: 55 | printf("Wrong Choice!!\n"); 56 | break; 57 | } 58 | printf("\n"); 59 | } 60 | } 61 | void initialize() 62 | { 63 | top = -1; 64 | } 65 | int size() 66 | { 67 | return top+1; 68 | } 69 | int isEmpty() 70 | { 71 | if(top == -1) 72 | return 1; 73 | else 74 | return 0; 75 | } 76 | int isFull() 77 | { 78 | if(top == MAX-1) 79 | return 1; 80 | else 81 | return 0; 82 | } 83 | void push(int x) 84 | { 85 | if(isFull()) 86 | { 87 | printf("Stack Overflow\n"); 88 | return; 89 | } 90 | ///else 91 | top = top + 1; 92 | a[top] = x; 93 | } 94 | int pop() 95 | { 96 | int x; 97 | if(isEmpty()) 98 | { 99 | printf("Stack Underflow\n"); 100 | exit(1); 101 | } 102 | ///else 103 | x = a[top]; 104 | top = top - 1; 105 | return x; 106 | } 107 | int peek() ///returns the top value 108 | { 109 | if(isEmpty()) 110 | { 111 | printf("Stack Underflow\n"); 112 | exit(1); 113 | } 114 | ///else 115 | return a[top]; 116 | } 117 | void display() 118 | { 119 | int i; 120 | printf("top = %d\n", top); 121 | if(isEmpty()) 122 | { 123 | printf("Stack is Empty\n"); 124 | } 125 | ///else 126 | printf("Stack is : \n\n"); 127 | for(i = top; i>=0; i--) 128 | printf(" %d\n", a[i]); 129 | printf("\n"); 130 | } 131 | -------------------------------------------------------------------------------- /10. Sorting/15. Radix Sort.c: -------------------------------------------------------------------------------- 1 | /*P8.12 Program of sorting using radix sort*/ 2 | /* 3 | File Name : Radix Sort.c 4 | Purpose : To explain Radix Sort 5 | Date : 19 October 2018 6 | Author : Bipul Islam 7 | */ 8 | #include 9 | #include 10 | 11 | struct node 12 | { 13 | int info ; 14 | struct node *link; 15 | }*start=NULL; 16 | 17 | void radix_sort(); 18 | int large_dig(); 19 | int digit(int number, int k); 20 | 21 | main() 22 | { 23 | struct node *tmp,*q; 24 | int i,n,item; 25 | 26 | printf("Enter the number of elements in the list : "); 27 | scanf("%d", &n); 28 | 29 | for(i=0;iinfo=item; 37 | tmp->link=NULL; 38 | 39 | if(start==NULL) /*Inserting first element */ 40 | start=tmp; 41 | else 42 | { 43 | q=start; 44 | while(q->link!=NULL) 45 | q=q->link; 46 | q->link=tmp; 47 | } 48 | }/*End of for*/ 49 | 50 | radix_sort(); 51 | printf("Sorted list is :\n"); 52 | 53 | q=start; 54 | while( q !=NULL) 55 | { 56 | printf("%d ", q->info); 57 | q = q->link; 58 | } 59 | printf("\n"); 60 | 61 | }/*End of main()*/ 62 | 63 | void radix_sort() 64 | { 65 | int i,k,dig,least_sig,most_sig; 66 | struct node *p, *rear[10], *front[10]; 67 | 68 | least_sig=1; 69 | most_sig=large_dig(start); 70 | 71 | for(k=least_sig; k<=most_sig; k++) 72 | { 73 | /*Make all the queues empty at the beginning of each pass*/ 74 | for(i=0; i<=9 ; i++) 75 | { 76 | rear[i] = NULL; 77 | front[i] = NULL ; 78 | } 79 | 80 | for( p=start; p!=NULL; p=p->link ) 81 | { 82 | /*Find kth digit in the number*/ 83 | dig = digit(p->info, k); 84 | 85 | /*Add the number to queue of dig*/ 86 | if(front[dig] == NULL) 87 | front[dig] = p ; 88 | else 89 | rear[dig]->link = p; 90 | rear[dig] = p; 91 | } 92 | 93 | /*Join all the queues to form the new linked list*/ 94 | 95 | i=0; 96 | while(front[i] == NULL) 97 | i++; 98 | start = front[i]; 99 | while(i<9) 100 | { 101 | if(rear[i+1]!=NULL) 102 | rear[i]->link=front[i+1]; 103 | else 104 | rear[i+1]=rear[i]; 105 | i++; 106 | } 107 | rear[9]->link=NULL; 108 | } 109 | }/*End of radix_sort*/ 110 | 111 | /*This function finds number of digits in the largest element of the list */ 112 | int large_dig() 113 | { 114 | struct node *p=start; 115 | int large=0,ndig=0; 116 | 117 | /*Find largest element*/ 118 | while(p != NULL) 119 | { 120 | if(p ->info > large) 121 | large = p->info; 122 | p = p->link ; 123 | } 124 | /*Find number of digits in largest element*/ 125 | while(large != 0) 126 | { 127 | ndig++; 128 | large = large/10 ; 129 | } 130 | return(ndig); 131 | } /*End of large_dig()*/ 132 | 133 | /*This function returns kth digit of a number*/ 134 | int digit(int number, int k) 135 | { 136 | int digit, i ; 137 | for(i=1; i<=k; i++) 138 | { 139 | digit = number % 10 ; 140 | number = number /10 ; 141 | } 142 | return(digit); 143 | }/*End of digit()*/ 144 | 145 | 146 | -------------------------------------------------------------------------------- /10. Sorting/13. Binary Tree Sort.c: -------------------------------------------------------------------------------- 1 | /*P8.10 Binary tree Sort*/ 2 | /* 3 | File Name : Binary Tree Sort.c 4 | Purpose : To explain Binary Tree Sort 5 | Date : 19 October 2018 6 | Author : Bipul Islam 7 | */ 8 | #include 9 | #include 10 | #define MAX 100 11 | 12 | struct node 13 | { 14 | struct node *lchild; 15 | int info; 16 | struct node *rchild; 17 | }; 18 | 19 | struct node *stack[MAX]; 20 | int top=-1; 21 | void push_stack(struct node *item); 22 | struct node *pop_stack(); 23 | int stack_empty(); 24 | 25 | struct node *insert(struct node *ptr, int item ); 26 | void inorder(struct node *ptr, int arr[]); 27 | struct node *Destroy(struct node *ptr); 28 | 29 | main() 30 | { 31 | struct node *root=NULL; 32 | int arr[MAX], n, i; 33 | 34 | printf("Enter the number of elements : "); 35 | scanf("%d",&n); 36 | 37 | for(i=0; iinfo) 66 | ptr = ptr->lchild; 67 | else 68 | ptr = ptr->rchild; 69 | } 70 | 71 | tmp=(struct node *)malloc(sizeof(struct node)); 72 | tmp->info=ikey; 73 | tmp->lchild=NULL; 74 | tmp->rchild=NULL; 75 | 76 | if(par==NULL) 77 | root=tmp; 78 | else if(ikey < par->info) 79 | par->lchild=tmp; 80 | else 81 | par->rchild=tmp; 82 | 83 | return root; 84 | }/*End of insert( )*/ 85 | 86 | void inorder(struct node *root, int arr[]) 87 | { 88 | struct node *ptr=root; 89 | int i=0; 90 | if( ptr==NULL ) 91 | { 92 | printf("Tree is empty\n"); 93 | return; 94 | } 95 | while(1) 96 | { 97 | while(ptr->lchild!=NULL ) 98 | { 99 | push_stack(ptr); 100 | ptr = ptr->lchild; 101 | } 102 | 103 | while( ptr->rchild==NULL ) 104 | { 105 | arr[i++]=ptr->info; 106 | if(stack_empty()) 107 | return; 108 | ptr = pop_stack(); 109 | } 110 | arr[i++]=ptr->info; 111 | ptr = ptr->rchild; 112 | } 113 | printf("\n"); 114 | }/*End of inorder( )*/ 115 | 116 | /*Delete all nodes of the tree*/ 117 | struct node *Destroy(struct node *ptr) 118 | { 119 | if(ptr!=NULL) 120 | { 121 | Destroy(ptr->lchild); 122 | Destroy(ptr->rchild); 123 | free(ptr); 124 | } 125 | return NULL; 126 | }/*End of Destroy()*/ 127 | 128 | /*Functions for implementation of stack*/ 129 | void push_stack(struct node *item) 130 | { 131 | if(top==(MAX-1)) 132 | { 133 | printf("Stack Overflow\n"); 134 | return; 135 | } 136 | stack[++top]=item; 137 | }/*End of push_stack()*/ 138 | 139 | struct node *pop_stack() 140 | { 141 | struct node *item; 142 | if(top==-1) 143 | { 144 | printf("Stack Underflow\n"); 145 | exit(1); 146 | } 147 | item=stack[top--]; 148 | return item; 149 | }/*End of pop_stack()*/ 150 | 151 | int stack_empty() 152 | { 153 | if(top==-1) 154 | return 1; 155 | else 156 | return 0; 157 | } /*End of stack_empty*/ 158 | 159 | 160 | -------------------------------------------------------------------------------- /1. Singly Linked List/15 insert a new node at a given position.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct node { 4 | int info; 5 | struct node *link; 6 | 7 | }; 8 | struct node *createList(struct node *start); 9 | void displayList(struct node *start); 10 | struct node *insertInBeginning(struct node *start, int data); 11 | void insertAtEnd(struct node *start, int data); 12 | struct node *insertAtPosition(struct node *start, int data, int k); 13 | 14 | 15 | 16 | 17 | int main() 18 | { 19 | struct node *start=NULL; 20 | int choice, data, x, k; 21 | start=createList(start); 22 | displayList(start); ///display lsit 23 | printf("Enter a element to insert data at a given position: "); 24 | scanf("%d",&data); 25 | printf("Enter the position which to insert: "); 26 | scanf("%d",&k); 27 | 28 | ///call function to insert at a given position 29 | start=insertAtPosition(start, data, k); 30 | displayList(start); 31 | 32 | } 33 | struct node *createList(struct node *start) 34 | { 35 | int i, n, data; 36 | printf("Enter the no of nodes: "); 37 | scanf("%d",&n); 38 | if(n==0) 39 | return start; 40 | 41 | printf("Enter the first elemlent to be inserted: "); 42 | scanf("%d",&data); 43 | start=insertInBeginning(start, data); 44 | for(i=2; i<=n; i++) 45 | { 46 | printf("Enter the next element to be inserted: "); 47 | scanf("%d",&data); 48 | insertAtEnd(start, data); 49 | } 50 | return start; 51 | }; 52 | struct node *insertInBeginning(struct node *start, int data) 53 | { 54 | struct node *temp; 55 | temp=(struct node * )malloc(sizeof(struct node)); 56 | 57 | temp->info=data; 58 | temp->link=start; 59 | start=temp; 60 | 61 | return start; 62 | }; 63 | void insertAtEnd(struct node *start, int data) 64 | { 65 | struct node *temp, *p; 66 | temp=(struct node * )malloc(sizeof(struct node)); 67 | 68 | temp->info=data; 69 | 70 | p=start; 71 | 72 | while(p->link!=NULL) 73 | { 74 | p=p->link; 75 | } 76 | p->link=temp; 77 | temp->link=NULL; 78 | } 79 | void displayList(struct node *start) 80 | { 81 | struct node *p; 82 | if(start==NULL) 83 | { 84 | printf("List is empty!!!\n"); 85 | return; 86 | } 87 | printf("List is : "); 88 | ///Code for traverse list 89 | ///Traverse list means visiting each nodes exactly ones 90 | p=start; 91 | while(p!=NULL) 92 | { 93 | printf("%d ", p->info); 94 | p=p->link; 95 | } 96 | printf("\n"); 97 | } 98 | struct node *insertAtPosition(struct node *start, int data, int k) 99 | { 100 | struct node *temp, *p; 101 | int i; 102 | if(k==1) 103 | { 104 | temp=(struct node *)malloc(sizeof(struct node)); 105 | temp->info=data; 106 | temp->link=start; 107 | start=temp; 108 | 109 | return start; 110 | } 111 | p=start; 112 | ///find a pointer to k-1 node 113 | for(i=1; ilink; 115 | if(p==NULL) 116 | printf("You can insert only upto %dth position\n", i); 117 | else 118 | { 119 | temp=(struct node *)malloc(sizeof(struct node)); 120 | temp->info=data; 121 | temp->link=p->link; 122 | p->link=temp; 123 | } 124 | return start; 125 | } 126 | 127 | -------------------------------------------------------------------------------- /9. Heap/1_Heap.c: -------------------------------------------------------------------------------- 1 | /* 2 | File Name: Heap 3 | Purpose : To explain heap tree 4 | Date : 11th November 2018 5 | Author : Bipul Islam 6 | */ 7 | #include 8 | #define LARGE_VALUE 99999 ///All values in heap should be less than this value 9 | 10 | void insert(int num, int arr[], int *p_hsize); 11 | int deleteRoot(int arr[], int *p_hsize); 12 | void restoreUp(int a[], int loc); 13 | void restoreDown(int a[], int i, int hsize) 14 | void display(int arr[],int hsize); 15 | 16 | main( ) 17 | { 18 | int a[100]; ///array used to represent heap 19 | int n=0; ////Number of nodes in the heap 20 | int choice, value; 21 | 22 | arr[0]= LARGE_VALUE; ///Sentinel: All keys in heap should be less than this value 23 | 24 | while(1) 25 | { 26 | printf("1.Insert\n"); 27 | printf("2.Delete root\n"); 28 | printf("3.Display\n"); 29 | printf("4.Exit\n"); 30 | printf("Enter your choice : "); 31 | scanf("%d",&choice); 32 | switch(choice) 33 | { 34 | case 1: 35 | printf("Enter the number to be inserted : "); 36 | scanf("%d",&value); 37 | insert(value, a,&n); 38 | break; 39 | case 2: 40 | if(n==0) 41 | printf("Heap is empty \n"); 42 | else 43 | { 44 | value = deleteRoot(a, &n); 45 | printf("Maximum element is %d\n", value); 46 | } 47 | break; 48 | case 3: 49 | display(a, n); 50 | break; 51 | case 4: 52 | printf("Enter size of the array "); 53 | scanf("%d",&hsize); 54 | printf("Enter array : "); 55 | for(i=1;i<=hsize;i++) 56 | scanf("%d",&arr[i]); 57 | buildHeap(arr,hsize); 58 | break; 59 | case 5: 60 | exit(1); 61 | default: 62 | printf("Wrong choice\n"); 63 | } 64 | } 65 | } 66 | 67 | void insert(int value, int a[], int *pn) 68 | { 69 | (*pn)++; ///Increase the heap size by 1 70 | a[*pn]=value; 71 | restoreUp(a, *pn); 72 | } 73 | 74 | void restoreUp(int a[], int i) 75 | { 76 | int k = a[i]; 77 | int iparent = i/2; 78 | 79 | ///while( iparent>=1 && arr[iparent] < num ) if MAX_VAL not in a[0] 80 | while( a[iparent] < k ) 81 | { 82 | a[i]=arr[iparent]; 83 | i = iparent; 84 | iparent = i/2; 85 | } 86 | a[i] = k; 87 | } 88 | 89 | int deleteRoot(int a[], int *pn) 90 | { 91 | int maxValue = a[1]; ///Save the element present at the root 92 | a[1] = a[*pn]; ///Place the last element in the root 93 | (*pn)--; ///Decrease the heap size by 1 94 | restoreDown(a,1,*pn); 95 | return maxValue; 96 | } 97 | 98 | void restoreDown(int a[], int i, int hsize) 99 | { 100 | int lchild=2*i, rchild=lchild+1; 101 | 102 | int num=a[i]; 103 | 104 | while( rchild <= hsize) 105 | { 106 | if( num>=a[lchild] && num>=a[rchild] ) 107 | { 108 | a[i] = num; 109 | return; 110 | } 111 | else if(a[lchild] > a[rchild]) 112 | { 113 | a[i] = a[lchild]; 114 | i = lchild; 115 | } 116 | else 117 | { 118 | a[i] = a[rchild]; 119 | i = rchild; 120 | } 121 | lchild = 2 * i; 122 | rchild = lchild + 1; 123 | } 124 | ///If number of nodes is even 125 | if(lchild == hsize && num < a[lchild] ) 126 | { 127 | a[i]=a[lchild]; 128 | i = lchild; 129 | } 130 | a[i]=num; 131 | } 132 | 133 | void display(int a[],int hsize) 134 | { 135 | int i; 136 | if(hsize==0) 137 | { 138 | printf("Heap is empty\n"); 139 | return; 140 | } 141 | for(i=1;i<=hsize;i++) 142 | printf("%d ",a[i]); 143 | printf("\n"); 144 | 145 | printf("Number of elements = %d\n",hsize); 146 | } -------------------------------------------------------------------------------- /1. Singly Linked List/24 merging two sorted single linked lists.c: -------------------------------------------------------------------------------- 1 | /*Program of merging two sorted single linked lists*/ 2 | 3 | #include 4 | #include 5 | 6 | struct node 7 | { 8 | int info; 9 | struct node *link; 10 | }; 11 | 12 | struct node *create(struct node *start); 13 | struct node *insert_s(struct node *start,int data); 14 | struct node *insert(struct node *start,int data); 15 | void display(struct node *start ); 16 | void merge(struct node *p1,struct node *p2); 17 | main() 18 | { 19 | struct node *start1=NULL,*start2=NULL; 20 | start1=create(start1); 21 | start2=create(start2); 22 | 23 | printf("List1 : "); 24 | display(start1); 25 | printf("List2 : "); 26 | display(start2); 27 | merge(start1, start2); 28 | }/*End of main()*/ 29 | 30 | void merge(struct node *p1,struct node *p2) 31 | { 32 | struct node *start3; 33 | start3=NULL; 34 | 35 | while(p1!=NULL && p2!=NULL) 36 | { 37 | if(p1->info < p2->info) 38 | { 39 | start3=insert(start3,p1->info); 40 | p1=p1->link; 41 | } 42 | else if(p2->info < p1->info) 43 | { 44 | start3=insert(start3,p2->info); 45 | p2=p2->link; 46 | } 47 | else if(p1->info==p2->info) 48 | { 49 | start3=insert(start3,p1->info); 50 | p1=p1->link; 51 | p2=p2->link; 52 | } 53 | } 54 | /*If second list has finished and elements left in first list*/ 55 | while(p1!=NULL) 56 | { 57 | start3=insert(start3,p1->info); 58 | p1=p1->link; 59 | } 60 | /*If first list has finished and elements left in second list*/ 61 | while(p2!=NULL) 62 | { 63 | start3=insert(start3,p2->info); 64 | p2=p2->link; 65 | } 66 | printf("Merged list is : "); 67 | display(start3); 68 | } 69 | 70 | struct node *create(struct node *start ) 71 | { 72 | int i,n,data; 73 | printf("Enter the number of nodes : "); 74 | scanf("%d",&n); 75 | start=NULL; 76 | for(i=1;i<=n;i++) 77 | { 78 | printf("Enter the element to be inserted : "); 79 | scanf("%d",&data); 80 | start=insert_s(start, data); 81 | } 82 | return start; 83 | }/*End of create_slist()*/ 84 | 85 | struct node *insert_s(struct node *start,int data) 86 | { 87 | struct node *p,*tmp; 88 | tmp=(struct node *)malloc(sizeof(struct node)); 89 | tmp->info=data; 90 | /*list empty or data to be added in beginning */ 91 | if(start==NULL || datainfo) 92 | { 93 | tmp->link=start; 94 | start=tmp; 95 | return start; 96 | } 97 | else 98 | { 99 | p=start; 100 | while(p->link!=NULL && p->link->info < data) 101 | p=p->link; 102 | tmp->link=p->link; 103 | p->link=tmp; 104 | } 105 | return start; 106 | }/*End of insert_s()*/ 107 | 108 | struct node *insert(struct node *start,int data) 109 | { 110 | struct node *p,*tmp; 111 | tmp=(struct node *)malloc(sizeof(struct node)); 112 | tmp->info=data; 113 | /*If list is empty*/ 114 | if(start==NULL) 115 | { 116 | tmp->link=start; 117 | start=tmp; 118 | return start; 119 | } 120 | else /*Insert at the end of the list*/ 121 | { 122 | p=start; 123 | while(p->link!=NULL) 124 | p=p->link; 125 | tmp->link=p->link; 126 | p->link=tmp; 127 | } 128 | return start; 129 | }/*End of insert()*/ 130 | 131 | void display(struct node *start) 132 | { 133 | struct node *p; 134 | if(start==NULL) 135 | { 136 | printf("List is empty\n"); 137 | return; 138 | } 139 | p=start; 140 | while(p!=NULL) 141 | { 142 | printf("%d ",p->info); 143 | p=p->link; 144 | } 145 | printf("\n"); 146 | }/*End of display()*/ 147 | -------------------------------------------------------------------------------- /4. Stack/5_conversion of infix to postfix and evaluation of postfix.c: -------------------------------------------------------------------------------- 1 | /*Program for conversion of infix to postfix and evaluation of postfix. 2 | It will evaluate only single digit numbers*/ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #define BLANK ' ' 10 | #define TAB '\t' 11 | #define MAX 50 12 | 13 | void push(long int symbol); 14 | long int pop(); 15 | void infix_to_postfix(); 16 | long int eval_post(); 17 | int priority(char symbol); 18 | int isEmpty(); 19 | int white_space(); 20 | 21 | char infix[MAX], postfix[MAX]; 22 | long int stack[MAX]; 23 | int top; 24 | 25 | main() 26 | { 27 | long int value; 28 | top=-1; 29 | printf("Enter infix : "); 30 | gets(infix); 31 | infix_to_postfix(); 32 | printf("Postfix : %s\n",postfix); 33 | value=eval_post(); 34 | printf("Value of expression : %ld\n",value); 35 | }/*End of main()*/ 36 | 37 | void infix_to_postfix() 38 | { 39 | unsigned int i,p=0; 40 | char next; 41 | char symbol; 42 | for(i=0;i= priority(symbol) ) 63 | postfix[p++]=pop(); 64 | push(symbol); 65 | break; 66 | default: /*if an operand comes*/ 67 | postfix[p++]=symbol; 68 | } 69 | } 70 | } 71 | while(!isEmpty( )) 72 | postfix[p++]=pop(); 73 | postfix[p]='\0'; /*End postfix with'\0' to make it a string*/ 74 | }/*End of infix_to_postfix()*/ 75 | 76 | /*This function returns the priority of the operator*/ 77 | int priority(char symbol) 78 | { 79 | switch(symbol) 80 | { 81 | case '(': 82 | return 0; 83 | case '+': 84 | case '-': 85 | return 1; 86 | case '*': 87 | case '/': 88 | case '%': 89 | return 2; 90 | case '^': 91 | return 3; 92 | default : 93 | return 0; 94 | } 95 | }/*End of priority()*/ 96 | 97 | void push(long int symbol) 98 | { 99 | if(top>MAX) 100 | { 101 | printf("Stack overflow\n"); 102 | exit(1); 103 | } 104 | stack[++top]=symbol; 105 | }/*End of push()*/ 106 | 107 | long int pop() 108 | { 109 | if( isEmpty() ) 110 | { 111 | printf("Stack underflow\n"); 112 | exit(1); 113 | } 114 | return (stack[top--]); 115 | }/*End of pop()*/ 116 | int isEmpty() 117 | { 118 | if(top==-1) 119 | return 1; 120 | else 121 | return 0; 122 | }/*End of isEmpty()*/ 123 | 124 | int white_space(char symbol) 125 | { 126 | if( symbol == BLANK || symbol == TAB ) 127 | return 1; 128 | else 129 | return 0; 130 | }/*End of white_space()*/ 131 | 132 | long int eval_post() 133 | { 134 | long int a,b,temp,result; 135 | unsigned int i; 136 | 137 | for(i=0;i='0') 140 | push(postfix[i]-'0'); 141 | else 142 | { 143 | a=pop(); 144 | b=pop(); 145 | switch(postfix[i]) 146 | { 147 | case '+': 148 | temp=b+a; break; 149 | case '-': 150 | temp=b-a;break; 151 | case '*': 152 | temp=b*a;break; 153 | case '/': 154 | temp=b/a;break; 155 | case '%': 156 | temp=b%a;break; 157 | case '^': 158 | temp=pow(b,a); 159 | } 160 | push(temp); 161 | } 162 | } 163 | result=pop(); 164 | return result; 165 | }/*End of eval_post */ 166 | -------------------------------------------------------------------------------- /1. Singly Linked List/19 deletion at the end.c: -------------------------------------------------------------------------------- 1 | /** Deletion in a single linked list 2 | 1. Deletion of the first node 3 | 2. Deletion of the only node 4 | 3. Deletion between the list 5 | 4. Deletion at the end 6 | NB: all codes are here 7 | **/ 8 | #include 9 | #include 10 | struct node { 11 | int info; 12 | struct node *link; 13 | 14 | }; 15 | struct node *createList(struct node *start); 16 | void displayList(struct node *start); 17 | struct node *insertInBeginning(struct node *start, int data); 18 | void insertAtEnd(struct node *start, int data); 19 | struct node *deleteNode(struct node *start, int x); 20 | 21 | int main() 22 | { 23 | struct node *start=NULL; 24 | int choice, data, x, k; 25 | start=createList(start); 26 | displayList(start); ///display lsit 27 | printf("Enter a element to be deleted: "); 28 | scanf("%d",&x); 29 | 30 | ///call function to delete node 31 | start=deleteNode(start, x); 32 | displayList(start); 33 | 34 | } 35 | struct node *createList(struct node *start) 36 | { 37 | int i, n, data; 38 | printf("Enter the no of nodes: "); 39 | scanf("%d",&n); 40 | if(n==0) 41 | return start; 42 | 43 | printf("Enter the first elemlent to be inserted: "); 44 | scanf("%d",&data); 45 | start=insertInBeginning(start, data); 46 | for(i=2; i<=n; i++) 47 | { 48 | printf("Enter the next element to be inserted: "); 49 | scanf("%d",&data); 50 | insertAtEnd(start, data); 51 | } 52 | return start; 53 | }; 54 | struct node *insertInBeginning(struct node *start, int data) 55 | { 56 | struct node *temp; 57 | temp=(struct node * )malloc(sizeof(struct node)); 58 | 59 | temp->info=data; 60 | temp->link=start; 61 | start=temp; 62 | 63 | return start; 64 | }; 65 | void insertAtEnd(struct node *start, int data) 66 | { 67 | struct node *temp, *p; 68 | temp=(struct node * )malloc(sizeof(struct node)); 69 | 70 | temp->info=data; 71 | 72 | p=start; 73 | 74 | while(p->link!=NULL) 75 | { 76 | p=p->link; 77 | } 78 | p->link=temp; 79 | temp->link=NULL; 80 | } 81 | void displayList(struct node *start) 82 | { 83 | struct node *p; 84 | if(start==NULL) 85 | { 86 | printf("List is empty!!!\n"); 87 | return; 88 | } 89 | printf("List is : "); 90 | ///Code for traverse list 91 | ///Traverse list means visiting each nodes exactly ones 92 | p=start; 93 | while(p!=NULL) 94 | { 95 | printf("%d ", p->info); 96 | p=p->link; 97 | } 98 | printf("\n"); 99 | } 100 | struct node *deleteNode(struct node *start, int x) 101 | { 102 | struct node *temp, *p; 103 | if(start==NULL) 104 | { 105 | printf("List is empty.\n"); 106 | return start; 107 | } 108 | ///Deletion of first node 109 | if(start->info==x) 110 | { 111 | temp=start; 112 | start=start->link; 113 | free(temp); 114 | 115 | return start; 116 | } 117 | ///Deletion in between or at the end 118 | p=start; 119 | while(p->link!=NULL) 120 | { 121 | if(p->link->info==x) 122 | break; 123 | p=p->link; 124 | } 125 | if(p->link==NULL) 126 | printf("Element %d is not in the list!!\n", x); 127 | else 128 | { 129 | temp=p->link; 130 | p->link=temp->link; 131 | free(temp); 132 | 133 | return start; 134 | } 135 | 136 | }; 137 | 138 | -------------------------------------------------------------------------------- /1. Singly Linked List/17 deletion of the only node.c: -------------------------------------------------------------------------------- 1 | /** Deletion in a single linked list 2 | 1. Deletion of the first node 3 | 2. Deletion of the only node 4 | 3. Deletion between the list 5 | 4. Deletion at the end 6 | NB: all codes are here 7 | **/ 8 | #include 9 | #include 10 | struct node { 11 | int info; 12 | struct node *link; 13 | 14 | }; 15 | struct node *createList(struct node *start); 16 | void displayList(struct node *start); 17 | struct node *insertInBeginning(struct node *start, int data); 18 | void insertAtEnd(struct node *start, int data); 19 | struct node *deleteNode(struct node *start, int x); 20 | 21 | int main() 22 | { 23 | struct node *start=NULL; 24 | int choice, data, x, k; 25 | start=createList(start); 26 | displayList(start); ///display lsit 27 | printf("Enter a element to be deleted: "); 28 | scanf("%d",&x); 29 | 30 | ///call function to delete node 31 | start=deleteNode(start, x); 32 | displayList(start); 33 | 34 | } 35 | struct node *createList(struct node *start) 36 | { 37 | int i, n, data; 38 | printf("Enter the no of nodes: "); 39 | scanf("%d",&n); 40 | if(n==0) 41 | return start; 42 | 43 | printf("Enter the first elemlent to be inserted: "); 44 | scanf("%d",&data); 45 | start=insertInBeginning(start, data); 46 | for(i=2; i<=n; i++) 47 | { 48 | printf("Enter the next element to be inserted: "); 49 | scanf("%d",&data); 50 | insertAtEnd(start, data); 51 | } 52 | return start; 53 | }; 54 | struct node *insertInBeginning(struct node *start, int data) 55 | { 56 | struct node *temp; 57 | temp=(struct node * )malloc(sizeof(struct node)); 58 | 59 | temp->info=data; 60 | temp->link=start; 61 | start=temp; 62 | 63 | return start; 64 | }; 65 | void insertAtEnd(struct node *start, int data) 66 | { 67 | struct node *temp, *p; 68 | temp=(struct node * )malloc(sizeof(struct node)); 69 | 70 | temp->info=data; 71 | 72 | p=start; 73 | 74 | while(p->link!=NULL) 75 | { 76 | p=p->link; 77 | } 78 | p->link=temp; 79 | temp->link=NULL; 80 | } 81 | void displayList(struct node *start) 82 | { 83 | struct node *p; 84 | if(start==NULL) 85 | { 86 | printf("List is empty!!!\n"); 87 | return; 88 | } 89 | printf("List is : "); 90 | ///Code for traverse list 91 | ///Traverse list means visiting each nodes exactly ones 92 | p=start; 93 | while(p!=NULL) 94 | { 95 | printf("%d ", p->info); 96 | p=p->link; 97 | } 98 | printf("\n"); 99 | } 100 | struct node *deleteNode(struct node *start, int x) 101 | { 102 | struct node *temp, *p; 103 | if(start==NULL) 104 | { 105 | printf("List is empty.\n"); 106 | return start; 107 | } 108 | ///Deletion of first node 109 | if(start->info==x) 110 | { 111 | temp=start; 112 | start=start->link; 113 | free(temp); 114 | 115 | return start; 116 | } 117 | ///Deletion in between or at the end 118 | p=start; 119 | while(p->link!=NULL) 120 | { 121 | if(p->link->info==x) 122 | break; 123 | p=p->link; 124 | } 125 | if(p->link==NULL) 126 | printf("Element %d is not in the list!!\n", x); 127 | else 128 | { 129 | temp=p->link; 130 | p->link=temp->link; 131 | free(temp); 132 | 133 | return start; 134 | } 135 | 136 | }; 137 | 138 | -------------------------------------------------------------------------------- /1. Singly Linked List/16 deletion of first node or only node.c: -------------------------------------------------------------------------------- 1 | /** Deletion in a single linked list 2 | 1. Deletion of the first node 3 | 2. Deletion of the only node 4 | 3. Deletion between the list 5 | 4. Deletion at the end 6 | NB: all codes are here 7 | **/ 8 | #include 9 | #include 10 | struct node { 11 | int info; 12 | struct node *link; 13 | 14 | }; 15 | struct node *createList(struct node *start); 16 | void displayList(struct node *start); 17 | struct node *insertInBeginning(struct node *start, int data); 18 | void insertAtEnd(struct node *start, int data); 19 | struct node *deleteNode(struct node *start, int x); 20 | 21 | int main() 22 | { 23 | struct node *start=NULL; 24 | int choice, data, x, k; 25 | start=createList(start); 26 | displayList(start); ///display lsit 27 | printf("Enter a element to be deleted: "); 28 | scanf("%d",&x); 29 | 30 | ///call function to delete node 31 | start=deleteNode(start, x); 32 | displayList(start); 33 | 34 | } 35 | struct node *createList(struct node *start) 36 | { 37 | int i, n, data; 38 | printf("Enter the no of nodes: "); 39 | scanf("%d",&n); 40 | if(n==0) 41 | return start; 42 | 43 | printf("Enter the first elemlent to be inserted: "); 44 | scanf("%d",&data); 45 | start=insertInBeginning(start, data); 46 | for(i=2; i<=n; i++) 47 | { 48 | printf("Enter the next element to be inserted: "); 49 | scanf("%d",&data); 50 | insertAtEnd(start, data); 51 | } 52 | return start; 53 | }; 54 | struct node *insertInBeginning(struct node *start, int data) 55 | { 56 | struct node *temp; 57 | temp=(struct node * )malloc(sizeof(struct node)); 58 | 59 | temp->info=data; 60 | temp->link=start; 61 | start=temp; 62 | 63 | return start; 64 | }; 65 | void insertAtEnd(struct node *start, int data) 66 | { 67 | struct node *temp, *p; 68 | temp=(struct node * )malloc(sizeof(struct node)); 69 | 70 | temp->info=data; 71 | 72 | p=start; 73 | 74 | while(p->link!=NULL) 75 | { 76 | p=p->link; 77 | } 78 | p->link=temp; 79 | temp->link=NULL; 80 | } 81 | void displayList(struct node *start) 82 | { 83 | struct node *p; 84 | if(start==NULL) 85 | { 86 | printf("List is empty!!!\n"); 87 | return; 88 | } 89 | printf("List is : "); 90 | ///Code for traverse list 91 | ///Traverse list means visiting each nodes exactly ones 92 | p=start; 93 | while(p!=NULL) 94 | { 95 | printf("%d ", p->info); 96 | p=p->link; 97 | } 98 | printf("\n"); 99 | } 100 | struct node *deleteNode(struct node *start, int x) 101 | { 102 | struct node *temp, *p; 103 | if(start==NULL) 104 | { 105 | printf("List is empty.\n"); 106 | return start; 107 | } 108 | ///Deletion of first node 109 | if(start->info==x) 110 | { 111 | temp=start; 112 | start=start->link; 113 | free(temp); 114 | 115 | return start; 116 | } 117 | ///Deletion in between or at the end 118 | p=start; 119 | while(p->link!=NULL) 120 | { 121 | if(p->link->info==x) 122 | break; 123 | p=p->link; 124 | } 125 | if(p->link==NULL) 126 | printf("Element %d is not in the list!!\n", x); 127 | else 128 | { 129 | temp=p->link; 130 | p->link=temp->link; 131 | free(temp); 132 | 133 | return start; 134 | } 135 | 136 | }; 137 | 138 | -------------------------------------------------------------------------------- /1. Singly Linked List/18 deletion between the list nodes.c: -------------------------------------------------------------------------------- 1 | /** Deletion in a single linked list 2 | 1. Deletion of the first node 3 | 2. Deletion of the only node 4 | 3. Deletion between the list 5 | 4. Deletion at the end 6 | NB: all codes are here 7 | **/ 8 | #include 9 | #include 10 | struct node { 11 | int info; 12 | struct node *link; 13 | 14 | }; 15 | struct node *createList(struct node *start); 16 | void displayList(struct node *start); 17 | struct node *insertInBeginning(struct node *start, int data); 18 | void insertAtEnd(struct node *start, int data); 19 | struct node *deleteNode(struct node *start, int x); 20 | 21 | int main() 22 | { 23 | struct node *start=NULL; 24 | int choice, data, x, k; 25 | start=createList(start); 26 | displayList(start); ///display lsit 27 | printf("Enter a element to be deleted: "); 28 | scanf("%d",&x); 29 | 30 | ///call function to delete node 31 | start=deleteNode(start, x); 32 | displayList(start); 33 | 34 | } 35 | struct node *createList(struct node *start) 36 | { 37 | int i, n, data; 38 | printf("Enter the no of nodes: "); 39 | scanf("%d",&n); 40 | if(n==0) 41 | return start; 42 | 43 | printf("Enter the first elemlent to be inserted: "); 44 | scanf("%d",&data); 45 | start=insertInBeginning(start, data); 46 | for(i=2; i<=n; i++) 47 | { 48 | printf("Enter the next element to be inserted: "); 49 | scanf("%d",&data); 50 | insertAtEnd(start, data); 51 | } 52 | return start; 53 | }; 54 | struct node *insertInBeginning(struct node *start, int data) 55 | { 56 | struct node *temp; 57 | temp=(struct node * )malloc(sizeof(struct node)); 58 | 59 | temp->info=data; 60 | temp->link=start; 61 | start=temp; 62 | 63 | return start; 64 | }; 65 | void insertAtEnd(struct node *start, int data) 66 | { 67 | struct node *temp, *p; 68 | temp=(struct node * )malloc(sizeof(struct node)); 69 | 70 | temp->info=data; 71 | 72 | p=start; 73 | 74 | while(p->link!=NULL) 75 | { 76 | p=p->link; 77 | } 78 | p->link=temp; 79 | temp->link=NULL; 80 | } 81 | void displayList(struct node *start) 82 | { 83 | struct node *p; 84 | if(start==NULL) 85 | { 86 | printf("List is empty!!!\n"); 87 | return; 88 | } 89 | printf("List is : "); 90 | ///Code for traverse list 91 | ///Traverse list means visiting each nodes exactly ones 92 | p=start; 93 | while(p!=NULL) 94 | { 95 | printf("%d ", p->info); 96 | p=p->link; 97 | } 98 | printf("\n"); 99 | } 100 | struct node *deleteNode(struct node *start, int x) 101 | { 102 | struct node *temp, *p; 103 | if(start==NULL) 104 | { 105 | printf("List is empty.\n"); 106 | return start; 107 | } 108 | ///Deletion of first node 109 | if(start->info==x) 110 | { 111 | temp=start; 112 | start=start->link; 113 | free(temp); 114 | 115 | return start; 116 | } 117 | ///Deletion in between or at the end 118 | p=start; 119 | while(p->link!=NULL) 120 | { 121 | if(p->link->info==x) 122 | break; 123 | p=p->link; 124 | } 125 | if(p->link==NULL) 126 | printf("Element %d is not in the list!!\n", x); 127 | else 128 | { 129 | temp=p->link; 130 | p->link=temp->link; 131 | free(temp); 132 | 133 | return start; 134 | } 135 | 136 | }; 137 | 138 | -------------------------------------------------------------------------------- /1. Singly Linked List/14 insert a new node before a given node.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct node { 4 | int info; 5 | struct node *link; 6 | 7 | }; 8 | struct node *createList(struct node *start); 9 | void displayList(struct node *start); 10 | struct node *insertInBeginning(struct node *start, int data); 11 | void insertAtEnd(struct node *start, int data); 12 | 13 | 14 | 15 | int main() 16 | { 17 | struct node *start=NULL; 18 | int choice, data, x, k; 19 | start=createList(start); 20 | displayList(start); ///display lsit 21 | printf("Enter a element to insert data before a specified node: "); 22 | scanf("%d",&data); 23 | printf("Enter the element before which to insert: "); 24 | scanf("%d",&x); 25 | 26 | ///call function to insert data before a specified node 27 | start=insertBefore(start, data, x); 28 | displayList(start); 29 | 30 | } 31 | struct node *createList(struct node *start) 32 | { 33 | int i, n, data; 34 | printf("Enter the no of nodes: "); 35 | scanf("%d",&n); 36 | if(n==0) 37 | return start; 38 | 39 | printf("Enter the first elemlent to be inserted: "); 40 | scanf("%d",&data); 41 | start=insertInBeginning(start, data); 42 | for(i=2; i<=n; i++) 43 | { 44 | printf("Enter the next element to be inserted: "); 45 | scanf("%d",&data); 46 | insertAtEnd(start, data); 47 | } 48 | return start; 49 | }; 50 | struct node *insertInBeginning(struct node *start, int data) 51 | { 52 | struct node *temp; 53 | temp=(struct node * )malloc(sizeof(struct node)); 54 | 55 | temp->info=data; 56 | temp->link=start; 57 | start=temp; 58 | 59 | return start; 60 | }; 61 | void insertAtEnd(struct node *start, int data) 62 | { 63 | struct node *temp, *p; 64 | temp=(struct node * )malloc(sizeof(struct node)); 65 | 66 | temp->info=data; 67 | 68 | p=start; 69 | 70 | while(p->link!=NULL) 71 | { 72 | p=p->link; 73 | } 74 | p->link=temp; 75 | temp->link=NULL; 76 | } 77 | void displayList(struct node *start) 78 | { 79 | struct node *p; 80 | if(start==NULL) 81 | { 82 | printf("List is empty!!!\n"); 83 | return; 84 | } 85 | printf("List is : "); 86 | ///Code for traverse list 87 | ///Traverse list means visiting each nodes exactly ones 88 | p=start; 89 | while(p!=NULL) 90 | { 91 | printf("%d ", p->info); 92 | p=p->link; 93 | } 94 | printf("\n"); 95 | } 96 | void insertBefore(struct node *start, int data, int x) 97 | { 98 | struct node *temp, *p; 99 | if(start==NULL) 100 | { 101 | printf("List is empty!!\n"); 102 | return start; 103 | } 104 | /// if x is in the first node , new node is to be inserted before first node 105 | if(x==start->info) 106 | { 107 | temp=(struct node *)malloc(sizeof(struct node)); 108 | 109 | temp->info=data; 110 | temp->link=start; 111 | start=temp; 112 | 113 | return start; 114 | } 115 | ///find pointer to predecessor of node containing x 116 | p=start; 117 | while(p->link!=NULL) 118 | { 119 | if(p->link->info==x) 120 | break; 121 | p=p->link; 122 | } 123 | if(p==NULL) 124 | printf("%d is not present in the list!!!\n"); 125 | else 126 | { 127 | temp=(struct node *)malloc(sizeof(struct node)); 128 | 129 | temp->info=data; 130 | temp->link=p->link; 131 | 132 | } 133 | return start; 134 | } 135 | 136 | 137 | 138 | -------------------------------------------------------------------------------- /10. Sorting/11. Merge Sort For Linked List.c: -------------------------------------------------------------------------------- 1 | /*P8.8 Program of merge sort for linked lists*/ 2 | 3 | #include 4 | #include 5 | 6 | struct node 7 | { 8 | int info; 9 | struct node *link; 10 | }; 11 | 12 | struct node *merge_sort(struct node *p); 13 | struct node *divide(struct node *p); 14 | struct node *merge(struct node *p,struct node *q); 15 | void display(struct node *start); 16 | struct node *create_list(struct node *start); 17 | struct node *addatbeg(struct node *start,int data); 18 | struct node *addatend(struct node *start,int data); 19 | 20 | main( ) 21 | { 22 | struct node *start=NULL; 23 | start = create_list(start); 24 | start = merge_sort(start); 25 | display(start); 26 | } 27 | 28 | struct node *merge_sort(struct node *start) 29 | { 30 | struct node *start_first, *start_second, *start_merged; 31 | 32 | if( start!=NULL && start->link!=NULL )/*if more than one element*/ 33 | { 34 | start_first = start; 35 | start_second = divide(start); 36 | start_first = merge_sort(start_first); 37 | start_second = merge_sort(start_second); 38 | start_merged = merge(start_first, start_second); 39 | return start_merged; 40 | } 41 | else 42 | return start; 43 | } 44 | 45 | struct node *divide(struct node *p) 46 | { 47 | struct node *q, *start_second; 48 | 49 | q=p->link->link; 50 | while(q!=NULL) 51 | { 52 | p=p->link; 53 | q=q->link; 54 | if(q!=NULL) 55 | q=q->link; 56 | } 57 | start_second = p->link; 58 | p->link = NULL; 59 | return start_second; 60 | } 61 | 62 | struct node *merge(struct node *p1,struct node *p2) 63 | { 64 | struct node *start_merged,*q; 65 | 66 | if(p1->info <= p2->info) 67 | { 68 | start_merged = p1; 69 | p1 = p1->link; 70 | } 71 | else 72 | { 73 | start_merged = p2; 74 | p2 = p2->link; 75 | } 76 | 77 | q = start_merged; 78 | while( p1!=NULL && p2!=NULL ) 79 | { 80 | if( p1->info <= p2->info ) 81 | { 82 | q->link = p1; 83 | q = q->link; 84 | p1 = p1->link; 85 | } 86 | else 87 | { 88 | q->link = p2; 89 | q = q->link; 90 | p2 = p2->link; 91 | } 92 | } 93 | 94 | if(p1!=NULL) 95 | q->link=p1; 96 | else 97 | q->link=p2; 98 | return start_merged; 99 | } 100 | 101 | void display(struct node *start) 102 | { 103 | struct node *p; 104 | if(start==NULL) 105 | { 106 | printf("List is empty\n"); 107 | return; 108 | } 109 | p=start; 110 | printf("List is :\n"); 111 | while(p!=NULL) 112 | { 113 | printf("%d ",p->info); 114 | p=p->link; 115 | } 116 | printf("\n"); 117 | }/*End of display() */ 118 | 119 | struct node *create_list(struct node *start) 120 | { 121 | int i,n,data; 122 | printf("Enter the number of nodes : "); 123 | scanf("%d",&n); 124 | start=NULL; 125 | printf("Enter the element to be inserted : "); 126 | scanf("%d",&data); 127 | start=addatbeg(start,data); 128 | for(i=2;i<=n;i++) 129 | { 130 | printf("Enter the element to be inserted : "); 131 | scanf("%d",&data); 132 | start=addatend(start,data); 133 | } 134 | return start; 135 | }/*End of create_list()*/ 136 | 137 | struct node *addatbeg(struct node *start,int data) 138 | { 139 | struct node *tmp; 140 | tmp=malloc(sizeof(struct node)); 141 | tmp->info=data; 142 | tmp->link=start; 143 | start=tmp; 144 | return start; 145 | }/*End of addatbeg()*/ 146 | 147 | struct node *addatend(struct node *start,int data) 148 | { 149 | struct node *p,*tmp; 150 | tmp=malloc(sizeof(struct node)); 151 | tmp->info=data; 152 | p=start; 153 | while(p->link!=NULL) 154 | p=p->link; 155 | p->link=tmp; 156 | tmp->link=NULL; 157 | return start; 158 | }/*End of addatend()*/ 159 | 160 | 161 | -------------------------------------------------------------------------------- /4. Stack/2_Stack Using Linked List.c: -------------------------------------------------------------------------------- 1 | ///Purpose: To implement stack using linked list 2 | ///Author : Bipul Islam, EEE, HSTU-5200 3 | #include 4 | #include 5 | struct node { 6 | int info; 7 | struct node *link; 8 | }; 9 | struct node *top; 10 | 11 | void initialize(); 12 | int isEmpty(); 13 | int size(); 14 | int peek(); 15 | void push(int x); 16 | int pop(); 17 | void display(); 18 | 19 | main() 20 | { 21 | int choice, x; 22 | initialize(); 23 | while(1) 24 | { 25 | printf("1. Push an element on the top.\n"); 26 | printf("2. Pop an element from the top.\n"); 27 | printf("3. Display the top element.\n"); 28 | printf("4. Display all stack elements.\n"); 29 | printf("5. Display size of the stack.\n"); 30 | printf("6. Quit.\n"); 31 | printf("Enter your choice : "); 32 | scanf("%d", &choice); 33 | 34 | if(choice == 6) 35 | break; 36 | switch(choice) 37 | { 38 | case 1: 39 | printf("Enter the element to be pushed : "); 40 | scanf("%d", &x); 41 | push(x); 42 | break; 43 | case 2: 44 | x = pop(); 45 | printf("Popped element is : %d\n",x); 46 | break; 47 | case 3: 48 | printf("Element at the top is : %d\n", peek()); 49 | break; 50 | case 4: 51 | display(); 52 | break; 53 | case 5: 54 | printf("Size of stack = %d\n", size()); 55 | break; 56 | default: 57 | printf("Wrong Choice!!\n"); 58 | break; 59 | } 60 | printf("\n"); 61 | } 62 | } 63 | void initialize() 64 | { 65 | top = NULL; 66 | } 67 | int size() 68 | { 69 | int s = 0; 70 | struct node *p = top; 71 | while(p! = NULL){ 72 | p = p->link; 73 | s++; 74 | } 75 | return s; 76 | } 77 | void push(int x) 78 | { 79 | struct node *temp; 80 | temp = (struct node*)malloc(sizeof(struct node)); 81 | if(temp == NULL) 82 | { 83 | printf("No Memory: Stack Overflow!!\n"); 84 | retrun; 85 | } 86 | ///else 87 | temp->info = x; 88 | temp->link = top; 89 | top = temp; 90 | } 91 | int pop() 92 | { 93 | struct node *temp; 94 | int x; 95 | if(isEmpty()){ 96 | printf("Stack Underflow!!!\n"); 97 | exit(1); 98 | } 99 | //else 100 | temp = top; 101 | x = top->info; 102 | top = top->link; 103 | free(temp); 104 | return x; 105 | } 106 | int peek() ///returns the top value 107 | { 108 | if(isEmpty()) 109 | { 110 | printf("Stack Underflow\n"); 111 | exit(1); 112 | } 113 | ///else 114 | return top->info; 115 | } 116 | int isEmpty() 117 | { 118 | if(top == NULL) 119 | return 1; 120 | else 121 | return 0; 122 | } 123 | 124 | int getStackSize(struct node* top){ 125 | ///input validation 126 | if(top == NULL){ 127 | pritnf("Error : Invalid stack pointer !!!\n"); 128 | return; 129 | } 130 | //else 131 | int cnt = 0; 132 | while(top != NULL){ 133 | top = top->link; 134 | cnt++; 135 | } 136 | return cnt; 137 | } 138 | 139 | void display() 140 | { 141 | struct node *p; 142 | p = top; 143 | 144 | if(isEmpty()){ 145 | printf("Stack is empty\n"); 146 | return; 147 | } 148 | //else 149 | printf("Stack is:\n\n"); 150 | while(p!=NULL){ 151 | printf("%d\n", p->info); 152 | p = p->link; 153 | } 154 | printf("\n"); 155 | } 156 | -------------------------------------------------------------------------------- /12. Hashing/P9_7 Quadrtic probing.C: -------------------------------------------------------------------------------- 1 | /*P9.7 Quadrtic probing*/ 2 | #include 3 | #include 4 | #define MAX 50 5 | 6 | enum type_of_record {EMPTY, DELETED,OCCUPIED}; 7 | 8 | struct employee 9 | { 10 | int empid; 11 | char name[20]; 12 | int age; 13 | }; 14 | struct Record 15 | { 16 | struct employee info; 17 | enum type_of_record status; 18 | }; 19 | 20 | void insert( struct employee emprec, struct Record table[]); 21 | int search(int key, struct Record table[]); 22 | void del(int key, struct Record table[]); 23 | void display(struct Record table[]); 24 | int hash(int key); 25 | 26 | main() 27 | { 28 | struct Record table[MAX]; 29 | struct employee emprec; 30 | 31 | int i,key,choice; 32 | 33 | for( i=0; i<=MAX-1; i++ ) 34 | table[i].status = EMPTY; 35 | 36 | while(1) 37 | { 38 | printf("1.Insert a record\n"); 39 | printf("2.Search a record\n"); 40 | printf("3.Delete a record\n"); 41 | printf("4.Display table\n"); 42 | printf("5.Exit\n"); 43 | printf("Enter your choice\n"); 44 | scanf("%d",&choice); 45 | 46 | switch(choice) 47 | { 48 | 49 | case 1 : 50 | printf("Enter empid, name, age : "); 51 | scanf("%d%s%d",&emprec.empid,emprec.name,&emprec.age); 52 | insert(emprec, table); 53 | break; 54 | case 2 : 55 | printf("Enter a key to be searched : "); 56 | scanf("%d", &key); 57 | i = search(key, table); 58 | if( i==-1) 59 | printf("Key not found\n"); 60 | else 61 | printf("Key found at index %d\n", i); 62 | break; 63 | case 3: 64 | printf("Enter a key to be deleted\n"); 65 | scanf("%d",&key); 66 | del(key, table); 67 | break; 68 | case 4: 69 | display(table); 70 | break; 71 | case 5: 72 | exit(1); 73 | } 74 | } 75 | }/*End of main()*/ 76 | 77 | int search(int key, struct Record table[]) 78 | { 79 | int i, h, location; 80 | h = hash(key); 81 | 82 | location = h; 83 | for( i=1; i!=MAX-1; i++ ) 84 | { 85 | if( table[location].status == EMPTY ) 86 | return -1; 87 | if( table[location].info.empid == key) 88 | return location; 89 | location = ( h + i*i ) % MAX; 90 | } 91 | return -1; 92 | }/*End of search()*/ 93 | 94 | void insert( struct employee emprec, struct Record table[] ) 95 | { 96 | int i, location, h; 97 | 98 | int key = emprec.empid; /*Extract key from the record*/ 99 | h = hash(key); 100 | 101 | location = h; 102 | for(i=1; i!=MAX-1; i++) 103 | { 104 | if( table[location].status == EMPTY || table[location].status == DELETED ) 105 | { 106 | table[location].info = emprec; 107 | table[location].status = OCCUPIED; 108 | printf("Record inserted\n\n"); 109 | return; 110 | } 111 | if( table[location].info.empid == key ) 112 | { 113 | printf("Duplicate key\n\n"); 114 | return; 115 | } 116 | location = (h+i*i)%MAX; 117 | } 118 | printf("Record can't be inserted : Table overFlow\n\n"); 119 | }/*End of insert()*/ 120 | 121 | void display(struct Record table[]) 122 | { 123 | int i; 124 | for(i=0; i 3 | #include 4 | #define MAX 50 5 | 6 | enum type_of_record {EMPTY, DELETED,OCCUPIED}; 7 | 8 | struct employee 9 | { 10 | int empid; 11 | char name[20]; 12 | int age; 13 | }; 14 | struct Record 15 | { 16 | struct employee info; 17 | enum type_of_record status; 18 | }; 19 | 20 | void insert( struct employee emprec, struct Record table[]); 21 | int search(int key, struct Record table[]); 22 | void del(int key, struct Record table[]); 23 | void display(struct Record table[]); 24 | int hash(int key); 25 | 26 | main() 27 | { 28 | struct Record table[MAX]; 29 | struct employee emprec; 30 | 31 | int i,key,choice; 32 | 33 | for( i=0; i<=MAX-1; i++ ) 34 | table[i].status = EMPTY; 35 | 36 | while(1) 37 | { 38 | printf("1. Insert a record\n"); 39 | printf("2. Search a record\n"); 40 | printf("3. Delete a record\n"); 41 | printf("4. Display table\n"); 42 | printf("5. Exit\n"); 43 | printf("Enter your choice\n"); 44 | scanf("%d",&choice); 45 | 46 | switch(choice) 47 | { 48 | 49 | case 1 : 50 | printf("Enter empid, name, age : "); 51 | scanf("%d%s%d",&emprec.empid,emprec.name,&emprec.age); 52 | insert(emprec, table); 53 | break; 54 | case 2 : 55 | printf("Enter a key to be searched : "); 56 | scanf("%d", &key); 57 | i = search(key, table); 58 | if( i==-1) 59 | printf("Key not found\n"); 60 | else 61 | printf("Key found at index %d\n", i); 62 | break; 63 | case 3: 64 | printf("Enter a key to be deleted\n"); 65 | scanf("%d",&key); 66 | del(key, table); 67 | break; 68 | case 4: 69 | display(table); 70 | break; 71 | case 5: 72 | exit(1); 73 | } 74 | } 75 | }/*End of main()*/ 76 | 77 | int search(int key, struct Record table[]) 78 | { 79 | int i, h, location; 80 | h = hash(key); 81 | 82 | location = h; 83 | for( i=1; i!=MAX-1; i++ ) 84 | { 85 | if( table[location].status == EMPTY ) 86 | return -1; 87 | if( table[location].info.empid == key) 88 | return location; 89 | location = ( h + i ) % MAX; 90 | } 91 | return -1; 92 | }/*End of search()*/ 93 | 94 | void insert( struct employee emprec, struct Record table[] ) 95 | { 96 | int i, location, h; 97 | 98 | int key = emprec.empid; /*Extract key from the record*/ 99 | h = hash(key); 100 | 101 | location = h; 102 | for( i=1; i!=MAX-1; i++ ) 103 | { 104 | if(table[location].status == EMPTY || table[location].status == DELETED) 105 | { 106 | table[location].info = emprec; 107 | table[location].status = OCCUPIED; 108 | printf("Record inserted\n\n"); 109 | return; 110 | } 111 | if(table[location].info.empid == key) 112 | { 113 | printf("Duplicate key\n\n"); 114 | return; 115 | } 116 | location = ( h + i) % MAX; 117 | } 118 | printf("Record can't be inserted : Table overFlow\n\n"); 119 | }/*End of insert()*/ 120 | 121 | void display(struct Record table[]) 122 | { 123 | int i; 124 | for(i=0; i 4 | #include 5 | #define MAX 11 6 | 7 | struct employee 8 | { 9 | int empid; 10 | char name[20]; 11 | int age; 12 | }; 13 | 14 | struct Record 15 | { 16 | struct employee info; 17 | struct Record *link; 18 | }; 19 | 20 | void insert(struct employee emprec, struct Record *table[]); 21 | int search(int key, struct Record *table[]); 22 | void del(int key, struct Record *table[]); 23 | void display(struct Record *table[]); 24 | int hash(int key); 25 | 26 | main() 27 | { 28 | struct Record *table[MAX]; 29 | struct employee emprec; 30 | int i,key,choice; 31 | 32 | for(i=0; i<=MAX-1; i++) 33 | table[i] = NULL; 34 | 35 | while(1) 36 | { 37 | printf("1.Insert a record\n"); 38 | printf("2.Search a record\n"); 39 | printf("3.Delete a record\n"); 40 | printf("4.Display table\n"); 41 | printf("5.Exit\n"); 42 | printf("Enter your choice\n"); 43 | scanf("%d",&choice); 44 | 45 | switch(choice) 46 | { 47 | case 1 : 48 | printf("Enter the record\n"); 49 | printf("Enter empid, name, age : "); 50 | scanf("%d%s%d",&emprec.empid,emprec.name,&emprec.age); 51 | insert(emprec, table); 52 | break; 53 | case 2 : 54 | printf("Enter a key to be searched : "); 55 | scanf("%d", &key); 56 | i = search(key, table); 57 | if(i==-1) 58 | printf("Key not found\n"); 59 | else 60 | printf("Key found in chain %d\n", i); 61 | break; 62 | case 3: 63 | printf("Enter a key to be deleted\n"); 64 | scanf("%d",&key); 65 | del(key,table); 66 | break; 67 | case 4: 68 | display(table); 69 | break; 70 | case 5: 71 | exit(1); 72 | } 73 | } 74 | }/*End of main()*/ 75 | 76 | void insert(struct employee emprec, struct Record *table[]) 77 | { 78 | int h, key; 79 | struct Record *tmp; 80 | 81 | key = emprec.empid; /*Extract the key from the record*/ 82 | 83 | if(search(key, table)!=-1) 84 | { 85 | printf("Duplicate key\n"); 86 | return; 87 | } 88 | h = hash(key); 89 | /*Insert in the beginning of list h*/ 90 | tmp=malloc(sizeof(struct Record)); 91 | tmp->info = emprec; 92 | tmp->link = table[h]; 93 | table[h] = tmp; 94 | }/*End of insert()*/ 95 | 96 | void display(struct Record *table[]) 97 | { 98 | int i; 99 | struct Record *ptr; 100 | for(i=0; iinfo.empid, ptr->info.name, ptr->info.age); 109 | ptr=ptr->link; 110 | } 111 | } 112 | } 113 | printf("\n"); 114 | }/*End of display()*/ 115 | 116 | int search(int key, struct Record *table[]) 117 | { 118 | int h; 119 | struct Record *ptr; 120 | h = hash(key); 121 | ptr = table[h]; 122 | while(ptr!=NULL) 123 | { 124 | if(ptr->info.empid == key) 125 | return h; 126 | ptr = ptr->link; 127 | } 128 | return -1; 129 | }/*End of search()*/ 130 | 131 | void del(int key, struct Record *table[]) 132 | { 133 | int h; 134 | struct Record *tmp, *ptr; 135 | h = hash(key); 136 | 137 | if(table[h]==NULL) 138 | { 139 | printf("Key %d not found\n", key); 140 | return; 141 | } 142 | if(table[h]->info.empid == key) 143 | { 144 | tmp=table[h]; 145 | table[h]=table[h]->link; 146 | free(tmp); 147 | return; 148 | } 149 | ptr = table[h]; 150 | while(ptr->link!=NULL) 151 | { 152 | if(ptr->link->info.empid == key) 153 | { 154 | tmp=ptr->link; 155 | ptr->link=tmp->link; 156 | free(tmp); 157 | return; 158 | } 159 | ptr=ptr->link; 160 | } 161 | printf("Key %d not found\n",key); 162 | }/*End of del()*/ 163 | 164 | int hash(int key) 165 | { 166 | return (key%MAX); 167 | }/*End of hash()*/ 168 | 169 | -------------------------------------------------------------------------------- /12. Hashing/P9_8 Double hashing.c: -------------------------------------------------------------------------------- 1 | /*P9.8 Double hashing*/ 2 | #include 3 | #include 4 | #define MAX 50 5 | 6 | enum type_of_record {EMPTY, DELETED,OCCUPIED}; 7 | 8 | struct employee 9 | { 10 | int empid; 11 | char name[20]; 12 | int age; 13 | }; 14 | struct Record 15 | { 16 | struct employee info; 17 | enum type_of_record status; 18 | }; 19 | 20 | void insert( struct employee emprec, struct Record table[]); 21 | int search(int key, struct Record table[]); 22 | void del(int key, struct Record table[]); 23 | void display(struct Record table[]); 24 | int hash(int key); 25 | int hash1(int key); 26 | main() 27 | { 28 | struct Record table[MAX]; 29 | struct employee emprec; 30 | 31 | int i,key,choice; 32 | 33 | for(i=0; i<=MAX-1; i++) 34 | table[i].status = EMPTY; 35 | 36 | while(1) 37 | { 38 | printf("1.Insert a record\n"); 39 | printf("2.Search a record\n"); 40 | printf("3.Delete a record\n"); 41 | printf("4.Display table\n"); 42 | printf("5.Exit\n"); 43 | printf("Enter your choice\n"); 44 | scanf("%d",&choice); 45 | 46 | switch(choice) 47 | { 48 | case 1: 49 | printf("Enter empid, name, age : "); 50 | scanf("%d%s%d",&emprec.empid,emprec.name,&emprec.age); 51 | insert(emprec, table); 52 | break; 53 | case 2: 54 | printf("Enter a key to be searched : "); 55 | scanf("%d", &key); 56 | i = search(key, table); 57 | if(i==-1) 58 | printf("Key not found\n"); 59 | else 60 | printf("Key found at index %d\n", i); 61 | break; 62 | case 3: 63 | printf("Enter a key to be deleted\n"); 64 | scanf("%d",&key); 65 | del(key, table); 66 | break; 67 | case 4: 68 | display(table); 69 | break; 70 | case 5: 71 | exit(1); 72 | } 73 | } 74 | }/*End of main()*/ 75 | 76 | int search(int key, struct Record table[]) 77 | { 78 | int i, h, location, h1; 79 | h = hash(key); 80 | location = h; 81 | h1 = hash1(key); 82 | 83 | for(i=1; i!=MAX-1; i++) 84 | { 85 | if(table[location].status == EMPTY) 86 | return -1; 87 | if(table[location].info.empid == key) 88 | return location; 89 | location = (h+i*h1)%MAX; 90 | } 91 | return -1; 92 | }/*End of search()*/ 93 | 94 | void insert( struct employee emprec, struct Record table[] ) 95 | { 96 | int i, location, h, h1; 97 | 98 | int key = emprec.empid; /*Extract key from the record*/ 99 | h = hash(key); 100 | location = h; 101 | h1 = hash1(key); 102 | 103 | for( i=1; i!=MAX-1; i++ ) 104 | { 105 | if( table[location].status == EMPTY || table[location].status == DELETED ) 106 | { 107 | table[location].info = emprec; 108 | table[location].status = OCCUPIED; 109 | printf("Record inserted\n\n"); 110 | return; 111 | } 112 | if( table[location].info.empid == key ) 113 | { 114 | printf("Duplicate key\n\n"); 115 | return; 116 | } 117 | location = (h+i*h1)%MAX; 118 | } 119 | printf("Record can't be inserted : Table overFlow\n\n"); 120 | }/*End of insert()*/ 121 | 122 | void display(struct Record table[]) 123 | { 124 | int i; 125 | for(i=0;i 3 | #include 4 | #define MAX 7 5 | 6 | int deque_arr[MAX]; 7 | int front=-1; 8 | int rear=-1; 9 | 10 | void insert_frontEnd(int item); 11 | void insert_rearEnd(int item); 12 | int delete_frontEnd(); 13 | int delete_rearEnd(); 14 | void display(); 15 | int isEmpty(); 16 | int isFull(); 17 | 18 | main() 19 | { 20 | int choice,item; 21 | while(1) 22 | { 23 | printf("1.Insert at the front end\n"); 24 | printf("2.Insert at the rear end\n"); 25 | printf("3.Delete from front end\n"); 26 | printf("4.Delete from rear end\n"); 27 | printf("5.Display\n"); 28 | printf("6.Quit\n"); 29 | printf("Enter your choice : "); 30 | scanf("%d",&choice); 31 | 32 | switch(choice) 33 | { 34 | case 1: 35 | printf("Input the element for adding in queue : "); 36 | scanf("%d",&item); 37 | insert_frontEnd(item); 38 | break; 39 | case 2: 40 | printf("Input the element for adding in queue : "); 41 | scanf("%d",&item); 42 | insert_rearEnd(item); 43 | break; 44 | case 3: 45 | printf("Element deleted from front end is : %d\n",delete_frontEnd()); 46 | break; 47 | case 4: 48 | printf("Element deleted from rear end is : %d\n",delete_rearEnd()); 49 | break; 50 | case 5: 51 | display(); 52 | break; 53 | case 6: 54 | exit(1); 55 | default: 56 | printf("Wrong choice\n"); 57 | }/*End of switch*/ 58 | printf("front = %d, rear =%d\n", front , rear); 59 | display(); 60 | }/*End of while*/ 61 | }/*End of main()*/ 62 | 63 | void insert_frontEnd(int item) 64 | { 65 | if( isFull() ) 66 | { 67 | printf("Queue Overflow\n"); 68 | return; 69 | } 70 | if( front==-1 )/*If queue is initially empty*/ 71 | { 72 | front=0; 73 | rear=0; 74 | } 75 | else if(front==0) 76 | front=MAX-1; 77 | else 78 | front=front-1; 79 | deque_arr[front]=item ; 80 | }/*End of insert_frontEnd()*/ 81 | 82 | void insert_rearEnd(int item) 83 | { 84 | if( isFull() ) 85 | { 86 | printf("Queue Overflow\n"); 87 | return; 88 | } 89 | if(front==-1) /*if queue is initially empty*/ 90 | { 91 | front=0; 92 | rear=0; 93 | } 94 | else if(rear==MAX-1) /*rear is at last position of queue */ 95 | rear=0; 96 | else 97 | rear=rear+1; 98 | deque_arr[rear]=item ; 99 | }/*End of insert_rearEnd()*/ 100 | 101 | int delete_frontEnd() 102 | { 103 | int item; 104 | if( isEmpty() ) 105 | { 106 | printf("Queue Underflow\n"); 107 | exit(1); 108 | } 109 | item=deque_arr[front]; 110 | if(front==rear) /*Queue has only one element */ 111 | { 112 | front=-1; 113 | rear=-1; 114 | } 115 | else 116 | if(front==MAX-1) 117 | front=0; 118 | else 119 | front=front+1; 120 | return item; 121 | }/*End of delete_frontEnd()*/ 122 | 123 | int delete_rearEnd() 124 | { 125 | int item; 126 | if( isEmpty() ) 127 | { 128 | printf("Queue Underflow\n"); 129 | exit(1); 130 | } 131 | item=deque_arr[rear]; 132 | 133 | if(front==rear) /*queue has only one element*/ 134 | { 135 | front=-1; 136 | rear=-1; 137 | } 138 | else if(rear==0) 139 | rear=MAX-1; 140 | else 141 | rear=rear-1; 142 | return item; 143 | }/*End of delete_rearEnd() */ 144 | 145 | int isFull() 146 | { 147 | if ( (front==0 && rear==MAX-1) || (front==rear+1) ) 148 | return 1; 149 | else 150 | return 0; 151 | }/*End of isFull()*/ 152 | 153 | int isEmpty() 154 | { 155 | if( front == -1) 156 | return 1; 157 | else 158 | return 0; 159 | }/*End of isEmpty()*/ 160 | 161 | void display() 162 | { 163 | int i; 164 | if( isEmpty() ) 165 | { 166 | printf("Queue is empty\n"); 167 | return; 168 | } 169 | printf("Queue elements :\n"); 170 | i=front; 171 | if( front<=rear ) 172 | { 173 | while(i<=rear) 174 | printf("%d ",deque_arr[i++]); 175 | } 176 | else 177 | { 178 | while(i<=MAX-1) 179 | printf("%d ",deque_arr[i++]); 180 | i=0; 181 | while(i<=rear) 182 | printf("%d ",deque_arr[i++]); 183 | } 184 | printf("\n"); 185 | }/*End of display() */ 186 | 187 | 188 | -------------------------------------------------------------------------------- /7. Binary Tree/1_Binary Tree with All Operation.c: -------------------------------------------------------------------------------- 1 | /* 2 | File Name : Binary Tree 3 | Purpose : To explain binary tree 4 | Date : 19 October 2018 5 | Author : Bipul Islam 6 | */ 7 | #include 8 | #include 9 | 10 | struct node{ 11 | struct node* lchild; 12 | char info; 13 | struct node* rchild; 14 | }; 15 | 16 | struct node * getnode(char x); 17 | void display(struct node *p, int level); 18 | void preorder(struct node *p); 19 | void inorder(struct node *p); 20 | void postorder(struct node *p); 21 | void levelOrder(struct node *p); 22 | int height(struct node *p); 23 | 24 | #define MAX 100 25 | struct node *queue[MAX]; 26 | int front = -1, rear = -1; 27 | void insertQueue(struct node *item); 28 | struct node *deleteQueue(); 29 | int queueEmpty(); 30 | 31 | main(){ 32 | 33 | struct node *root = NULL; 34 | root = getnode('P'); 35 | root->lchild = getnode('Q'); 36 | root->rchild = getnode('R'); 37 | root->lchild->lchild = getnode('A'); 38 | root->lchild->rchild = getnode('B'); 39 | root->rchild->lchild = getnode('X'); 40 | 41 | display(root, 0); 42 | printf("\n\n"); 43 | 44 | 45 | printf("Preorder : "); 46 | preorder(root); 47 | printf("\n\n"); 48 | 49 | printf("Inorder : "); 50 | inorder(root); 51 | printf("\n\n"); 52 | 53 | printf("Postorder : "); 54 | postorder(root); 55 | printf("\n\n"); 56 | 57 | printf("Level order : "); 58 | levelOrder(root); 59 | printf("\n\n"); 60 | 61 | printf("Height of tree is %d\n", height(root)); 62 | 63 | } 64 | struct node *getnode(char x) 65 | { 66 | struct node *p = (struct node *)malloc(sizeof(struct node)); 67 | p->info = x; 68 | p->lchild = p->rchild = NULL; 69 | } 70 | void display(struct node *p, int level) 71 | { 72 | int i; 73 | if(p == NULL) 74 | return; 75 | display(p->rchild, level+1); 76 | printf("\n"); 77 | 78 | for(i = 0; iinfo); 81 | 82 | display(p->lchild, level+1); 83 | 84 | }///End of display() 85 | 86 | void preorder(struct node *p) 87 | { 88 | if(p == NULL) 89 | return; 90 | printf("%c ", p->info); 91 | preorder(p->lchild); 92 | preorder(p->rchild); 93 | }///End of preorder() 94 | 95 | void inorder(struct node *p) 96 | { 97 | if(p == NULL) 98 | return; 99 | 100 | inorder(p->lchild); 101 | printf("%c ", p->info); 102 | inorder(p->rchild); 103 | }///End of inorder() 104 | 105 | void postorder(struct node *p) 106 | { 107 | if(p == NULL) 108 | return; 109 | 110 | postorder(p->lchild); 111 | postorder(p->rchild); 112 | printf("%c ", p->info); 113 | }///End of postorder() 114 | 115 | int height(struct node *p) 116 | { 117 | int hL, hR; 118 | if(p == NULL) 119 | return 0; 120 | 121 | hL = height(p->lchild); 122 | hR = height(p->rchild); 123 | 124 | if(hL > hR) 125 | return 1+hL; 126 | else 127 | return 1+hR; 128 | }///End of height() 129 | 130 | void levelOrder(struct node *p) 131 | { 132 | if(p == NULL) 133 | { 134 | printf("Tree is Empty\n"); 135 | return; 136 | } 137 | insertQueue(p); 138 | while(!queueEmpty()) 139 | { 140 | p = deleteQueue(); 141 | printf("%c ", p->info); 142 | 143 | if(p->lchild != NULL) 144 | insertQueue(p->lchild); 145 | if(p->rchild != NULL) 146 | insertQueue(p->rchild); 147 | } 148 | 149 | }///End of preorder() 150 | 151 | /*Functions for implementation of queue */ 152 | void insertQueue(struct node *item) 153 | { 154 | if(rear == MAX-1) 155 | { 156 | printf("Queue Overflow\n"); 157 | return; 158 | } 159 | if(front == -1) ///If queue is initially empty 160 | front = 0; 161 | rear = rear + 1; 162 | queue[rear] = item; 163 | } 164 | struct node *deleteQueue() 165 | { 166 | struct node *item; 167 | if(front == -1 || front == rear+1) 168 | { 169 | printf("Queue Underflow\n"); 170 | return 0; 171 | } 172 | item = queue[front]; 173 | front = front + 1; 174 | return item; 175 | }///End of deleteQueue() 176 | 177 | int queueEmpty() 178 | { 179 | if(front == -1 || front == rear+1) 180 | return 1; 181 | else 182 | return 0; 183 | } 184 | -------------------------------------------------------------------------------- /1. Singly Linked List/10 insertion in a linked list at the beginning.c: -------------------------------------------------------------------------------- 1 | /** 2 | * C program to insert a new node at the beginning of a Singly Linked List 3 | */ 4 | #include 5 | #include 6 | 7 | /* Structure of a node */ 8 | struct node { 9 | int data; // Data 10 | struct node *next; // Address 11 | }*head; 12 | 13 | 14 | void createList(int n); 15 | void insertNodeAtBeginning(int data); 16 | void displayList(); 17 | 18 | 19 | int main() 20 | { 21 | int n, data; 22 | 23 | /* 24 | * Create a singly linked list of n nodes 25 | */ 26 | printf("Enter the total number of nodes: "); 27 | scanf("%d", &n); 28 | createList(n); 29 | 30 | printf("\nData in the list \n"); 31 | displayList(); 32 | 33 | /* 34 | * Insert data at the beginning of the singly linked list 35 | */ 36 | printf("\nEnter data to insert at beginning of the list: "); 37 | scanf("%d", &data); 38 | insertNodeAtBeginning(data); 39 | 40 | printf("\nData in the list \n"); 41 | displayList(); 42 | 43 | return 0; 44 | } 45 | 46 | 47 | /* 48 | * Create a list of n nodes 49 | */ 50 | void createList(int n) 51 | { 52 | struct node *newNode, *temp; 53 | int data, i; 54 | 55 | head = (struct node *)malloc(sizeof(struct node)); 56 | 57 | /* 58 | * If unable to allocate memory for head node 59 | */ 60 | if(head == NULL) 61 | { 62 | printf("Unable to allocate memory."); 63 | } 64 | else 65 | { 66 | /* 67 | * Input data of node from the user 68 | */ 69 | printf("Enter the data of node 1: "); 70 | scanf("%d", &data); 71 | 72 | head->data = data; // Link data field with data 73 | head->next = NULL; // Link address field to NULL 74 | 75 | temp = head; 76 | 77 | /* 78 | * Create n nodes and adds to linked list 79 | */ 80 | for(i=2; i<=n; i++) 81 | { 82 | newNode = (struct node *)malloc(sizeof(struct node)); 83 | 84 | /* If memory is not allocated for newNode */ 85 | if(newNode == NULL) 86 | { 87 | printf("Unable to allocate memory."); 88 | break; 89 | } 90 | else 91 | { 92 | printf("Enter the data of node %d: ", i); 93 | scanf("%d", &data); 94 | 95 | newNode->data = data; // Link data field of newNode with data 96 | newNode->next = NULL; // Link address field of newNode with NULL 97 | 98 | temp->next = newNode; // Link previous node i.e. temp to the newNode 99 | 100 | temp = temp->next; 101 | } 102 | } 103 | 104 | printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n"); 105 | } 106 | } 107 | 108 | 109 | /* 110 | * Create a new node and inserts at the beginning of the linked list. 111 | */ 112 | void insertNodeAtBeginning(int data) 113 | { 114 | struct node *newNode; 115 | 116 | newNode = (struct node*)malloc(sizeof(struct node)); 117 | 118 | if(newNode == NULL) 119 | { 120 | printf("Unable to allocate memory."); 121 | } 122 | else 123 | { 124 | newNode->data = data; // Link data part 125 | newNode->next = head; // Link address part 126 | 127 | head = newNode; // Make newNode as first node 128 | 129 | printf("DATA INSERTED SUCCESSFULLY\n"); 130 | } 131 | } 132 | 133 | 134 | /* 135 | * Display entire list 136 | */ 137 | void displayList() 138 | { 139 | struct node *temp; 140 | 141 | /* 142 | * If the list is empty i.e. head = NULL 143 | */ 144 | if(head == NULL) 145 | { 146 | printf("List is empty."); 147 | } 148 | else 149 | { 150 | temp = head; 151 | while(temp != NULL) 152 | { 153 | printf("Data = %d\n", temp->data); // Print data of current node 154 | temp = temp->next; // Move to next node 155 | } 156 | } 157 | } 158 | -------------------------------------------------------------------------------- /1. Singly Linked List/10. insert a new node in a linked list at end of the list.c: -------------------------------------------------------------------------------- 1 | /** 2 | * C program to insert new node at the end of a Singly Linked List 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | 9 | /* Structure of a node */ 10 | struct node { 11 | int data; // Data 12 | struct node *next; // Address 13 | }*head; 14 | 15 | 16 | void createList(int n); 17 | void insertNodeAtEnd(int data); 18 | void displayList(); 19 | 20 | 21 | int main() 22 | { 23 | int n, data; 24 | 25 | /* 26 | * Create a singly linked list of n nodes 27 | */ 28 | printf("Enter the total number of nodes: "); 29 | scanf("%d", &n); 30 | createList(n); 31 | 32 | printf("\nData in the list \n"); 33 | displayList(); 34 | 35 | /* 36 | * Insert data at the end of the singly linked list 37 | */ 38 | printf("\nEnter data to insert at end of the list: "); 39 | scanf("%d", &data); 40 | insertNodeAtEnd(data); 41 | 42 | printf("\nData in the list \n"); 43 | displayList(); 44 | 45 | return 0; 46 | } 47 | 48 | 49 | /* 50 | * Create a list of n nodes 51 | */ 52 | void createList(int n) 53 | { 54 | struct node *newNode, *temp; 55 | int data, i; 56 | 57 | head = (struct node *)malloc(sizeof(struct node)); 58 | 59 | /* 60 | * If unable to allocate memory for head node 61 | */ 62 | if(head == NULL) 63 | { 64 | printf("Unable to allocate memory."); 65 | } 66 | else 67 | { 68 | /* 69 | * Reads data of node from the user 70 | */ 71 | printf("Enter the data of node 1: "); 72 | scanf("%d", &data); 73 | 74 | head->data = data; // Link the data field with data 75 | head->next = NULL; // Link the address field to NULL 76 | 77 | temp = head; 78 | 79 | /* 80 | * Create n nodes and adds to linked list 81 | */ 82 | for(i=2; i<=n; i++) 83 | { 84 | newNode = (struct node *)malloc(sizeof(struct node)); 85 | 86 | /* If memory is not allocated for newNode */ 87 | if(newNode == NULL) 88 | { 89 | printf("Unable to allocate memory."); 90 | break; 91 | } 92 | else 93 | { 94 | printf("Enter the data of node %d: ", i); 95 | scanf("%d", &data); 96 | 97 | newNode->data = data; // Link the data field of newNode with data 98 | newNode->next = NULL; // Link the address field of newNode with NULL 99 | 100 | temp->next = newNode; // Link previous node i.e. temp to the newNode 101 | temp = temp->next; 102 | } 103 | } 104 | 105 | printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n"); 106 | } 107 | } 108 | 109 | 110 | /* 111 | * Create a new node and inserts at the end of the linked list. 112 | */ 113 | void insertNodeAtEnd(int data) 114 | { 115 | struct node *newNode, *temp; 116 | 117 | newNode = (struct node*)malloc(sizeof(struct node)); 118 | 119 | if(newNode == NULL) 120 | { 121 | printf("Unable to allocate memory."); 122 | } 123 | else 124 | { 125 | newNode->data = data; // Link the data part 126 | newNode->next = NULL; 127 | 128 | temp = head; 129 | 130 | // Traverse to the last node 131 | while(temp->next != NULL) 132 | temp = temp->next; 133 | 134 | temp->next = newNode; // Link address part 135 | 136 | printf("DATA INSERTED SUCCESSFULLY\n"); 137 | } 138 | } 139 | 140 | 141 | /* 142 | * Display entire list 143 | */ 144 | void displayList() 145 | { 146 | struct node *temp; 147 | 148 | /* 149 | * If the list is empty i.e. head = NULL 150 | */ 151 | if(head == NULL) 152 | { 153 | printf("List is empty."); 154 | } 155 | else 156 | { 157 | temp = head; 158 | while(temp != NULL) 159 | { 160 | printf("Data = %d\n", temp->data); // Print data of current node 161 | temp = temp->next; // Move to next node 162 | } 163 | } 164 | } 165 | -------------------------------------------------------------------------------- /1. Singly Linked List/25 polynomial addition and multiplication using linked list.c: -------------------------------------------------------------------------------- 1 | /*Program of polynomial addition and multiplication using linked list */ 2 | #include 3 | #include 4 | 5 | struct node 6 | { 7 | float coef; 8 | int expo; 9 | struct node *link; 10 | }; 11 | 12 | struct node *create(struct node *); 13 | struct node *insert_s(struct node *,float,int); 14 | struct node *insert(struct node *,float,int); 15 | void display(struct node *ptr); 16 | void poly_add(struct node *,struct node *); 17 | void poly_mult(struct node *,struct node *); 18 | main( ) 19 | { 20 | struct node *start1=NULL,*start2=NULL; 21 | 22 | printf("Enter polynomial 1 :\n"); 23 | start1=create(start1); 24 | 25 | printf("Enter polynomial 2 :\n"); 26 | start2=create(start2); 27 | 28 | printf("Polynomial 1 is : "); 29 | display(start1); 30 | printf("Polynomial 2 is : "); 31 | display(start2); 32 | 33 | poly_add(start1, start2); 34 | poly_mult(start1, start2); 35 | }/*End of main()*/ 36 | 37 | struct node *create(struct node *start) 38 | { 39 | int i,n,ex; 40 | float co; 41 | printf("Enter the number of terms : "); 42 | scanf("%d",&n); 43 | for(i=1;i<=n;i++) 44 | { 45 | printf("Enter coeficient for term %d : ",i); 46 | scanf("%f",&co); 47 | printf("Enter exponent for term %d : ",i); 48 | scanf("%d",&ex); 49 | start=insert_s(start,co,ex); 50 | } 51 | return start; 52 | }/*End of create()*/ 53 | struct node *insert_s(struct node *start,float co,int ex) 54 | { 55 | struct node *ptr,*tmp; 56 | tmp=(struct node *)malloc(sizeof(struct node)); 57 | tmp->coef=co; 58 | tmp->expo=ex; 59 | /*list empty or exp greater than first one */ 60 | if(start==NULL || ex > start->expo) 61 | { 62 | tmp->link=start; 63 | start=tmp; 64 | } 65 | else 66 | { 67 | ptr=start; 68 | while(ptr->link!=NULL && ptr->link->expo >= ex) 69 | ptr=ptr->link; 70 | tmp->link=ptr->link; 71 | ptr->link=tmp; 72 | } 73 | return start; 74 | }/*End of insert()*/ 75 | 76 | struct node *insert(struct node *start,float co,int ex) 77 | { 78 | struct node *ptr,*tmp; 79 | tmp=(struct node *)malloc(sizeof(struct node)); 80 | tmp->coef=co; 81 | tmp->expo=ex; 82 | /*If list is empty*/ 83 | if(start==NULL) 84 | { 85 | tmp->link=start; 86 | start=tmp; 87 | } 88 | else /*Insert at the end of the list*/ 89 | { 90 | ptr=start; 91 | while(ptr->link!=NULL) 92 | ptr=ptr->link; 93 | tmp->link=ptr->link; 94 | ptr->link=tmp; 95 | } 96 | return start; 97 | }/*End of insert()*/ 98 | 99 | void display(struct node *ptr) 100 | { 101 | if(ptr==NULL) 102 | { 103 | printf("Zero polynomial\n"); 104 | return; 105 | } 106 | while(ptr!=NULL) 107 | { 108 | printf("(%.1fx^%d)", ptr->coef,ptr->expo); 109 | ptr=ptr->link; 110 | if(ptr!=NULL) 111 | printf(" + "); 112 | else 113 | printf("\n"); 114 | } 115 | }/*End of display()*/ 116 | void poly_add(struct node *p1,struct node *p2) 117 | { 118 | struct node *start3; 119 | start3=NULL; 120 | 121 | while(p1!=NULL && p2!=NULL) 122 | { 123 | if(p1->expo > p2->expo) 124 | { 125 | start3=insert(start3,p1->coef,p1->expo); 126 | p1=p1->link; 127 | } 128 | else if(p2->expo > p1->expo) 129 | { 130 | start3=insert(start3,p2->coef,p2->expo); 131 | p2=p2->link; 132 | } 133 | else if(p1->expo==p2->expo) 134 | { 135 | start3=insert(start3,p1->coef+p2->coef,p1->expo); 136 | p1=p1->link; 137 | p2=p2->link; 138 | } 139 | } 140 | /*if poly2 has finished and elements left in poly1*/ 141 | while(p1!=NULL) 142 | { 143 | start3=insert(start3,p1->coef,p1->expo); 144 | p1=p1->link; 145 | } 146 | /*if poly1 has finished and elements left in poly2*/ 147 | while(p2!=NULL) 148 | { 149 | start3=insert(start3,p2->coef,p2->expo); 150 | p2=p2->link; 151 | } 152 | printf("Added polynomial is : "); 153 | display(start3); 154 | }/*End of poly_add() */ 155 | 156 | void poly_mult(struct node *p1, struct node *p2) 157 | { 158 | struct node *start3; 159 | struct node *p2_beg = p2; 160 | start3=NULL; 161 | if(p1==NULL || p2==NULL) 162 | { 163 | printf("Multiplied polynomial is zero polynomial\n"); 164 | return; 165 | } 166 | while(p1!=NULL) 167 | { 168 | p2=p2_beg; 169 | while(p2!=NULL) 170 | { 171 | start3=insert_s(start3,p1->coef*p2->coef,p1->expo+p2->expo); 172 | p2=p2->link; 173 | } 174 | p1=p1->link; 175 | } 176 | printf("Multiplied polynomial is : "); 177 | display(start3); 178 | }/*End of poly_mult()*/ 179 | -------------------------------------------------------------------------------- /7. Binary Tree/7_construct a binary tree from inorder and preorder.C: -------------------------------------------------------------------------------- 1 | /*Program to construct a binary tree from inorder and preorder*/ 2 | #include 3 | #include 4 | 5 | struct treenode 6 | { 7 | int info; 8 | struct treenode *lchild; 9 | struct treenode *rchild; 10 | }*root=NULL; 11 | 12 | struct listnode 13 | { 14 | struct listnode *prev; 15 | int info; 16 | struct listnode *next; 17 | }*pre=NULL, *in=NULL; 18 | 19 | void display(struct treenode *ptr,int level); 20 | struct listnode *addtoempty(struct listnode *start,int data); 21 | struct listnode *addatend(struct listnode *start,int data); 22 | struct listnode *create_list(struct listnode *start, int n); 23 | struct treenode *construct(struct listnode *inptr,struct listnode *preptr, int num); 24 | void inorder(struct treenode *ptr); 25 | void postorder(struct treenode *ptr); 26 | void preorder(struct treenode *ptr); 27 | 28 | main( ) 29 | { 30 | int n; 31 | 32 | printf("Enter the number of nodes : "); 33 | scanf("%d",&n); 34 | 35 | printf("Enter inorder\n"); 36 | in = create_list(in, n); 37 | 38 | printf("Enter preorder\n"); 39 | pre=create_list(pre, n); 40 | 41 | root = construct(in,pre,n); 42 | 43 | printf("Inorder : "); inorder(root); 44 | printf("\nPostorder : "); postorder(root); 45 | printf("\nPreorder : "); preorder(root); 46 | printf("\n\nTree is : \n"); 47 | display(root,0); 48 | printf("\n"); 49 | } 50 | 51 | struct treenode *construct(struct listnode *inptr,struct listnode *preptr, int num ) 52 | { 53 | struct treenode *temp; 54 | struct listnode *q; 55 | 56 | int i,j; 57 | if(num==0) 58 | return NULL; 59 | 60 | temp=(struct treenode *)malloc(sizeof(struct treenode)); 61 | temp->info=preptr->info; 62 | temp->lchild = NULL; 63 | temp->rchild = NULL; 64 | 65 | if(num==1)/*if only one node in tree */ 66 | return temp; 67 | 68 | q = inptr; 69 | for(i=0; q->info != preptr->info; i++) 70 | q = q->next; 71 | 72 | /*Now q points to root node in inorder list and 73 | and number of nodes in its left subtree is i*/ 74 | 75 | /*For left subtree*/ 76 | temp->lchild = construct(inptr, preptr->next, i); 77 | 78 | /*For right subtree*/ 79 | for(j=1;j<=i+1;j++) 80 | preptr=preptr->next; 81 | temp->rchild = construct(q->next, preptr, num-i-1); 82 | 83 | return temp; 84 | }/*End of construct()*/ 85 | 86 | void display(struct treenode *ptr,int level) 87 | { 88 | int i; 89 | if ( ptr!=NULL ) 90 | { 91 | display(ptr->rchild, level+1); 92 | printf("\n"); 93 | for (i = 0; i < level; i++) 94 | printf(" "); 95 | printf("%d", ptr->info); 96 | display(ptr->lchild, level+1); 97 | } 98 | }/*End of display()*/ 99 | 100 | struct listnode *create_list(struct listnode *start, int n) 101 | { 102 | int i, data; 103 | start=NULL; 104 | for(i=1;i<=n;i++) 105 | { 106 | printf("Enter the element to be inserted : "); 107 | scanf("%d",&data); 108 | if(start==NULL) 109 | start=addtoempty(start,data); 110 | else 111 | start=addatend(start,data); 112 | } 113 | return start; 114 | }/*End of create_list()*/ 115 | 116 | struct listnode *addtoempty(struct listnode *start,int data) 117 | { 118 | struct listnode *tmp; 119 | tmp=malloc(sizeof(struct listnode)); 120 | tmp->info=data; 121 | tmp->prev=NULL; 122 | tmp->next=NULL; 123 | start=tmp; 124 | return start; 125 | }/*End of addtoempty( )*/ 126 | struct listnode *addatend(struct listnode *start,int data) 127 | { 128 | struct listnode *tmp,*p; 129 | tmp=malloc(sizeof(struct listnode)); 130 | tmp->info=data; 131 | p=start; 132 | while(p->next!=NULL) 133 | p=p->next; 134 | p->next=tmp; 135 | tmp->next=NULL; 136 | tmp->prev=p; 137 | return start; 138 | }/*End of addatend( )*/ 139 | 140 | void inorder(struct treenode *ptr) 141 | { 142 | if(root==NULL) 143 | { 144 | printf("Tree is empty"); 145 | return; 146 | } 147 | if(ptr!=NULL) 148 | { 149 | inorder(ptr->lchild); 150 | printf("%d ",ptr->info); 151 | inorder(ptr->rchild); 152 | } 153 | }/*End of inorder( )*/ 154 | void postorder(struct treenode *ptr) 155 | { 156 | if(root==NULL) 157 | { 158 | printf("Tree is empty"); 159 | return; 160 | } 161 | if(ptr!=NULL) 162 | { 163 | postorder(ptr->lchild); 164 | postorder(ptr->rchild); 165 | printf("%d ",ptr->info); 166 | } 167 | }/*End of postorder( )*/ 168 | 169 | void preorder(struct treenode *ptr) 170 | { 171 | if(root==NULL) 172 | { 173 | printf("Tree is empty"); 174 | return; 175 | } 176 | if(ptr!=NULL) 177 | { 178 | printf("%d ",ptr->info); 179 | preorder(ptr->lchild); 180 | preorder(ptr->rchild); 181 | } 182 | }/*End of preorder( )*/ 183 | 184 | -------------------------------------------------------------------------------- /7. Binary Tree/8_constructing a binary tree from inorder and postorder.C: -------------------------------------------------------------------------------- 1 | /*Program for constructing a binary tree from inorder and postorder */ 2 | 3 | #include 4 | #include 5 | 6 | struct treenode 7 | { 8 | int info; 9 | struct treenode *lchild; 10 | struct treenode *rchild; 11 | }*root=NULL; 12 | 13 | struct listnode 14 | { 15 | struct listnode *prev; 16 | int info; 17 | struct listnode *next; 18 | }*post=NULL, *in=NULL; 19 | 20 | void display(struct treenode *ptr,int level); 21 | struct listnode *addtoempty(struct listnode *start,int data); 22 | struct listnode *addatend(struct listnode *start,int data); 23 | struct listnode *create_list(struct listnode *start, int n); 24 | struct treenode *construct(struct listnode *inptr,struct listnode *postptr, int num); 25 | void inorder(struct treenode *ptr); 26 | void preorder(struct treenode *ptr); 27 | void postorder(struct treenode *ptr); 28 | 29 | main() 30 | { 31 | int n; 32 | 33 | printf("Enter the number of nodes : "); 34 | scanf("%d",&n); 35 | 36 | printf("Enter inorder\n"); 37 | in = create_list(in,n); 38 | 39 | printf("Enter postorder\n"); 40 | post = create_list(post,n); 41 | 42 | root = construct(in,post,n); 43 | 44 | printf("Inorder : "); inorder(root); 45 | printf("\nPostorder : "); postorder(root); 46 | printf("\nPreorder : "); preorder(root); 47 | printf("\n\nTree is : \n"); 48 | display(root,0); 49 | printf("\n"); 50 | }/*End of main()*/ 51 | 52 | struct treenode *construct(struct listnode *inptr,struct listnode *postptr, int num) 53 | { 54 | struct treenode *temp; 55 | struct listnode *q, *ptr; 56 | 57 | int i,j; 58 | if(num==0) 59 | return NULL; 60 | 61 | ptr=postptr; 62 | for(i=1;inext; 64 | 65 | /*Now ptr points to last node of postorder which is root*/ 66 | 67 | temp=(struct treenode *)malloc(sizeof(struct treenode)); 68 | temp->info=ptr->info; 69 | temp->lchild = NULL; 70 | temp->rchild = NULL; 71 | 72 | if(num==1)/*if only one node in tree */ 73 | return temp; 74 | 75 | q=inptr; 76 | for(i=0; q->info!=ptr->info; i++ ) 77 | q = q->next; 78 | 79 | /*Now i denotes the number of nodes in left subtree 80 | and q points to root node in inorder list*/ 81 | 82 | /*For left subtree*/ 83 | temp->lchild = construct(inptr, postptr, i); 84 | 85 | /*For right subtree*/ 86 | for(j=1;j<=i;j++) 87 | postptr = postptr->next; 88 | temp->rchild = construct(q->next, postptr, num-i-1); 89 | 90 | return temp; 91 | }/*End of construct()*/ 92 | 93 | struct listnode *create_list(struct listnode *start, int n) 94 | { 95 | int i,data; 96 | start=NULL; 97 | for(i=1;i<=n;i++) 98 | { 99 | printf("Enter the element to be inserted : "); 100 | scanf("%d",&data); 101 | if(start==NULL) 102 | start=addtoempty(start,data); 103 | else 104 | start=addatend(start,data); 105 | } 106 | return start; 107 | }/*End of create_list()*/ 108 | 109 | struct listnode *addtoempty(struct listnode *start,int data) 110 | { 111 | struct listnode *tmp; 112 | tmp=malloc(sizeof(struct listnode)); 113 | tmp->info=data; 114 | tmp->prev=NULL; 115 | tmp->next=NULL; 116 | start=tmp; 117 | return start; 118 | }/*End of addtoempty( )*/ 119 | 120 | struct listnode *addatend(struct listnode *start,int data) 121 | { 122 | struct listnode *tmp,*p; 123 | tmp=malloc(sizeof(struct listnode)); 124 | tmp->info=data; 125 | p=start; 126 | while(p->next!=NULL) 127 | p=p->next; 128 | p->next=tmp; 129 | tmp->next=NULL; 130 | tmp->prev=p; 131 | return start; 132 | }/*End of addatend( )*/ 133 | 134 | void inorder(struct treenode *ptr) 135 | { 136 | if(root==NULL) 137 | { 138 | printf("Tree is empty"); 139 | return; 140 | } 141 | if(ptr!=NULL) 142 | { 143 | inorder(ptr->lchild); 144 | printf("%d ",ptr->info); 145 | inorder(ptr->rchild); 146 | } 147 | }/*End of inorder( )*/ 148 | void postorder(struct treenode *ptr) 149 | { 150 | if(root==NULL) 151 | { 152 | printf("Tree is empty"); 153 | return; 154 | } 155 | if(ptr!=NULL) 156 | { 157 | postorder(ptr->lchild); 158 | postorder(ptr->rchild); 159 | printf("%d ",ptr->info); 160 | } 161 | }/*End of postorder( )*/ 162 | 163 | void preorder(struct treenode *ptr) 164 | { 165 | if(root==NULL) 166 | { 167 | printf("Tree is empty"); 168 | return; 169 | } 170 | if(ptr!=NULL) 171 | { 172 | printf("%d ",ptr->info); 173 | preorder(ptr->lchild); 174 | preorder(ptr->rchild); 175 | } 176 | }/*End of preorder( )*/ 177 | 178 | void display(struct treenode *ptr,int level) 179 | { 180 | int i; 181 | if(ptr!=NULL) 182 | { 183 | display(ptr->rchild, level+1); 184 | printf("\n"); 185 | for (i = 0; i < level; i++) 186 | printf(" "); 187 | printf("%d", ptr->info); 188 | display(ptr->lchild, level+1); 189 | } 190 | }/*End of display()*/ 191 | 192 | 193 | -------------------------------------------------------------------------------- /1. Singly Linked List/13 insertion a new node after a given node.c: -------------------------------------------------------------------------------- 1 | /** 2 | * C program to insert new node at the middle of Singly Linked List 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | 9 | /* Structure of a node */ 10 | struct node { 11 | int data; // Data 12 | struct node *next; // Address 13 | }*head; 14 | 15 | 16 | void createList(int n); 17 | void insertNodeAtMiddle(int data, int position); 18 | void displayList(); 19 | 20 | 21 | int main() 22 | { 23 | int n, data, position; 24 | 25 | /* 26 | * Create a singly linked list of n nodes 27 | */ 28 | printf("Enter the total number of nodes: "); 29 | scanf("%d", &n); 30 | createList(n); 31 | 32 | printf("\nData in the list \n"); 33 | displayList(); 34 | 35 | /* 36 | * Insert data at middle of the singly linked list 37 | */ 38 | printf("Enter data to insert at middle of the list: "); 39 | scanf("%d", &data); 40 | printf("Enter the position to insert new node: " ); 41 | scanf("%d", &position); 42 | insertNodeAtMiddle(data, position); 43 | 44 | printf("\nData in the list \n"); 45 | displayList(); 46 | 47 | return 0; 48 | } 49 | 50 | 51 | 52 | /* 53 | * Create a list of n nodes 54 | */ 55 | void createList(int n) 56 | { 57 | struct node *newNode, *temp; 58 | int data, i; 59 | 60 | head = (struct node *)malloc(sizeof(struct node)); 61 | 62 | /* 63 | * If unable to allocate memory for head node 64 | */ 65 | if(head == NULL) 66 | { 67 | printf("Unable to allocate memory."); 68 | } 69 | else 70 | { 71 | /* 72 | * Input data of node from the user 73 | */ 74 | printf("Enter the data of node 1: "); 75 | scanf("%d", &data); 76 | 77 | head->data = data; // Link the data field with data 78 | head->next = NULL; // Link the address field to NULL 79 | 80 | temp = head; 81 | 82 | /* 83 | * Creates n nodes and adds to linked list 84 | */ 85 | for(i=2; i<=n; i++) 86 | { 87 | newNode = (struct node *)malloc(sizeof(struct node)); 88 | 89 | /* If memory is not allocated for newNode */ 90 | if(newNode == NULL) 91 | { 92 | printf("Unable to allocate memory."); 93 | break; 94 | } 95 | else 96 | { 97 | printf("Enter the data of node %d: ", i); 98 | scanf("%d", &data); 99 | 100 | newNode->data = data; // Link the data field of newNode with data 101 | newNode->next = NULL; // Link the address field of newNode with NULL 102 | 103 | temp->next = newNode; // Link previous node i.e. temp to the newNode 104 | temp = temp->next; 105 | } 106 | } 107 | 108 | printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n"); 109 | } 110 | } 111 | 112 | 113 | 114 | /* 115 | * Creates a new node and inserts at middle of the linked list. 116 | */ 117 | void insertNodeAtMiddle(int data, int position) 118 | { 119 | int i; 120 | struct node *newNode, *temp; 121 | 122 | newNode = (struct node*)malloc(sizeof(struct node)); 123 | 124 | if(newNode == NULL) 125 | { 126 | printf("Unable to allocate memory."); 127 | } 128 | else 129 | { 130 | newNode->data = data; // Link data part 131 | newNode->next = NULL; 132 | 133 | temp = head; 134 | 135 | /* 136 | * Traverse to the n-1 position 137 | */ 138 | for(i=2; i<=position-1; i++) 139 | { 140 | temp = temp->next; 141 | 142 | if(temp == NULL) 143 | break; 144 | } 145 | 146 | if(temp != NULL) 147 | { 148 | /* Link address part of new node */ 149 | newNode->next = temp->next; 150 | 151 | /* Link address part of n-1 node */ 152 | temp->next = newNode; 153 | 154 | printf("DATA INSERTED SUCCESSFULLY\n"); 155 | } 156 | else 157 | { 158 | printf("UNABLE TO INSERT DATA AT THE GIVEN POSITION\n"); 159 | } 160 | } 161 | } 162 | 163 | 164 | /* 165 | * Display entire list 166 | */ 167 | void displayList() 168 | { 169 | struct node *temp; 170 | 171 | /* 172 | * If the list is empty i.e. head = NULL 173 | */ 174 | if(head == NULL) 175 | { 176 | printf("List is empty."); 177 | } 178 | else 179 | { 180 | temp = head; 181 | while(temp != NULL) 182 | { 183 | printf("Data = %d\n", temp->data); // Print data of current node 184 | temp = temp->next; // Move to next node 185 | } 186 | } 187 | } 188 | -------------------------------------------------------------------------------- /1. Singly Linked List/12 insert node at the middle of Singly Linked List.c: -------------------------------------------------------------------------------- 1 | /** 2 | * C program to insert new node at the middle of Singly Linked List 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | 9 | /* Structure of a node */ 10 | struct node { 11 | int data; // Data 12 | struct node *next; // Address 13 | }*head; 14 | 15 | 16 | void createList(int n); 17 | void insertNodeAtMiddle(int data, int position); 18 | void displayList(); 19 | 20 | 21 | int main() 22 | { 23 | int n, data, position; 24 | 25 | /* 26 | * Create a singly linked list of n nodes 27 | */ 28 | printf("Enter the total number of nodes: "); 29 | scanf("%d", &n); 30 | createList(n); 31 | 32 | printf("\nData in the list \n"); 33 | displayList(); 34 | 35 | /* 36 | * Insert data at middle of the singly linked list 37 | */ 38 | printf("Enter data to insert at middle of the list: "); 39 | scanf("%d", &data); 40 | printf("Enter the position to insert new node: " ); 41 | scanf("%d", &position); 42 | insertNodeAtMiddle(data, position); 43 | 44 | printf("\nData in the list \n"); 45 | displayList(); 46 | 47 | return 0; 48 | } 49 | 50 | 51 | 52 | /* 53 | * Create a list of n nodes 54 | */ 55 | void createList(int n) 56 | { 57 | struct node *newNode, *temp; 58 | int data, i; 59 | 60 | head = (struct node *)malloc(sizeof(struct node)); 61 | 62 | /* 63 | * If unable to allocate memory for head node 64 | */ 65 | if(head == NULL) 66 | { 67 | printf("Unable to allocate memory."); 68 | } 69 | else 70 | { 71 | /* 72 | * Input data of node from the user 73 | */ 74 | printf("Enter the data of node 1: "); 75 | scanf("%d", &data); 76 | 77 | head->data = data; // Link the data field with data 78 | head->next = NULL; // Link the address field to NULL 79 | 80 | temp = head; 81 | 82 | /* 83 | * Creates n nodes and adds to linked list 84 | */ 85 | for(i=2; i<=n; i++) 86 | { 87 | newNode = (struct node *)malloc(sizeof(struct node)); 88 | 89 | /* If memory is not allocated for newNode */ 90 | if(newNode == NULL) 91 | { 92 | printf("Unable to allocate memory."); 93 | break; 94 | } 95 | else 96 | { 97 | printf("Enter the data of node %d: ", i); 98 | scanf("%d", &data); 99 | 100 | newNode->data = data; // Link the data field of newNode with data 101 | newNode->next = NULL; // Link the address field of newNode with NULL 102 | 103 | temp->next = newNode; // Link previous node i.e. temp to the newNode 104 | temp = temp->next; 105 | } 106 | } 107 | 108 | printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n"); 109 | } 110 | } 111 | 112 | 113 | 114 | /* 115 | * Creates a new node and inserts at middle of the linked list. 116 | */ 117 | void insertNodeAtMiddle(int data, int position) 118 | { 119 | int i; 120 | struct node *newNode, *temp; 121 | 122 | newNode = (struct node*)malloc(sizeof(struct node)); 123 | 124 | if(newNode == NULL) 125 | { 126 | printf("Unable to allocate memory."); 127 | } 128 | else 129 | { 130 | newNode->data = data; // Link data part 131 | newNode->next = NULL; 132 | 133 | temp = head; 134 | 135 | /* 136 | * Traverse to the n-1 position 137 | */ 138 | for(i=2; i<=position-1; i++) 139 | { 140 | temp = temp->next; 141 | 142 | if(temp == NULL) 143 | break; 144 | } 145 | 146 | if(temp != NULL) 147 | { 148 | /* Link address part of new node */ 149 | newNode->next = temp->next; 150 | 151 | /* Link address part of n-1 node */ 152 | temp->next = newNode; 153 | 154 | printf("DATA INSERTED SUCCESSFULLY\n"); 155 | } 156 | else 157 | { 158 | printf("UNABLE TO INSERT DATA AT THE GIVEN POSITION\n"); 159 | } 160 | } 161 | } 162 | 163 | 164 | /* 165 | * Display entire list 166 | */ 167 | void displayList() 168 | { 169 | struct node *temp; 170 | 171 | /* 172 | * If the list is empty i.e. head = NULL 173 | */ 174 | if(head == NULL) 175 | { 176 | printf("List is empty."); 177 | } 178 | else 179 | { 180 | temp = head; 181 | while(temp != NULL) 182 | { 183 | printf("Data = %d\n", temp->data); // Print data of current node 184 | temp = temp->next; // Move to next node 185 | } 186 | } 187 | } 188 | --------------------------------------------------------------------------------