DSA using C – Home

DSA using C Tutorial PDF Version Quick Guide Resources Job Search Discussion Data Structures are the programmetic way of storing data so that data can be used efficiently. Almost every enterprise application uses various types of data structures in one or other way. This tutorial will give you great understanding on Data Structures concepts needed to understand the complexity of enterprise level applications and need of algorithms, data structures. Audience This tutorial is designed for Computer Science graduatates as well as Software Professionals who are willing to learn Data Structures and algorithm Programming in simple and easy steps. This tutorial will give you great understanding on Data Structures concepts and after completing this tutorial you will be at intermediate level of expertise from where you can take yourself at higher level of expertise. Prerequisites Before proceeding with this tutorial you should have a basic understanding of C programming language, text editor and execution of programs etc. Print Page Previous Next Advertisements ”;

DSA using C – Priority Queue

DSA using C – Priority Queue ”; Previous Next Overview Priority Queue is more specilized data structure than Queue. Like ordinary queue, priority queue has same method but with a major difference. In Priority queue items are ordered by key value so that item with the lowest value of key is at front and item with the highest value of key is at rear or vice versa. So we”re assigned priority to item based on its key value. Lower the value, higher the priority. Following are the principal methods of a Priority Queue. Basic Operations insert / enqueue − add an item to the rear of the queue. remove / dequeue − remove an item from the front of the queue. Priority Queue Representation We”re going to implement Queue using array in this article. There is few more operations supported by queue which are following. Peek − get the element at front of the queue. isFull − check if queue is full. isEmpty − check if queue is empty. Insert / Enqueue Operation Whenever an element is inserted into queue, priority queue inserts the item according to its order. Here we”re assuming that data with high value has low priority. void insert(int data){ int i =0; if(!isFull()){ // if queue is empty, insert the data if(itemCount == 0){ intArray[itemCount++] = data; } else { // start from the right end of the queue for(i = itemCount – 1; i >= 0; i– ){ // if data is larger, shift existing item to right end if(data > intArray[i]){ intArray[i+1] = intArray[i]; } else { break; } } // insert the data intArray[i+1] = data; itemCount++; } } } Remove / Dequeue Operation Whenever an element is to be removed from queue, queue get the element using item count. Once element is removed. Item count is reduced by one. int removeData(){ return intArray[–itemCount]; } Example Live Demo #include <stdio.h> #include <string.h> #include <stdlib.h> #include <stdbool.h> #define MAX 6 int intArray[MAX]; int itemCount = 0; int peek(){ return intArray[itemCount – 1]; } bool isEmpty(){ return itemCount == 0; } bool isFull(){ return itemCount == MAX; } int size(){ return itemCount; } void insert(int data){ int i =0; if(!isFull()){ // if queue is empty, insert the data if(itemCount == 0){ intArray[itemCount++] = data; } else { // start from the right end of the queue for(i = itemCount – 1; i >= 0; i– ){ // if data is larger, shift existing item to right end if(data > intArray[i]){ intArray[i+1] = intArray[i]; } else { break; } } // insert the data intArray[i+1] = data; itemCount++; } } } int removeData(){ return intArray[–itemCount]; } int main() { /* insert 5 items */ insert(3); insert(5); insert(9); insert(1); insert(12); // —————— // index : 0 1 2 3 4 // —————— // queue : 12 9 5 3 1 insert(15); // ——————— // index : 0 1 2 3 4 5 // ——————— // queue : 15 12 9 5 3 1 if(isFull()){ printf(“Queue is full!n”); } // remove one item int num = removeData(); printf(“Element removed: %dn”,num); // ——————— // index : 0 1 2 3 4 // ——————— // queue : 15 12 9 5 3 // insert more items insert(16); // ———————- // index : 0 1 2 3 4 5 // ———————- // queue : 16 15 12 9 5 3 // As queue is full, elements will not be inserted. insert(17); insert(18); // ———————- // index : 0 1 2 3 4 5 // ———————- // queue : 16 15 12 9 5 3 printf(“Element at front: %dn”,peek()); printf(“———————-n”); printf(“index : 5 4 3 2 1 0n”); printf(“———————-n”); printf(“Queue: “); while(!isEmpty()){ int n = removeData(); printf(“%d “,n); } } Output If we compile and run the above program then it would produce following output − Queue is full! Element removed: 1 Element at front: 3 ———————- index : 5 4 3 2 1 0 ———————- Queue: 3 5 9 12 15 16 Print Page Previous Next Advertisements ”;

DSA using C – Recursion

DSA using C – Recursion ”; Previous Next Overview Recursion refers to a technique in a programming language where a function calls itself. The function which calls itself is called a recursive method. Characteristics A recursive function must posses the following two characteristics. Base Case(s) Set of rules which leads to base case after reducing the cases. Recursive Factorial Factorial is one of the classical example of recursion. Factorial is a non-negative number satisfying following conditions. 0! = 1 1! = 1 n! = n * n-1! Factorial is represented by “!”. Here Rule 1 and Rule 2 are base cases and Rule 3 are factorial rules. As an example, 3! = 3 x 2 x 1 = 6 int factorial(int n){ //base case if(n == 0){ return 1; } else { return n * factorial(n-1); } } Recursive Fibonacci Series Fibonacci Series is another classical example of recursion. Fibonacci series a series of integers satisfying following conditions. F0 = 0 F1 = 1 Fn = Fn-1 + Fn-2 Fibonacci is represented by “F”. Here Rule 1 and Rule 2 are base cases and Rule 3 are fibonnacci rules. As an example, F5 = 0 1 1 2 3 Example Live Demo #include <stdio.h> int factorial(int n){ //base case if(n == 0){ return 1; } else { return n * factorial(n-1); } } int fibbonacci(int n){ if(n ==0){ return 0; } else if(n==1){ return 1; } else { return (fibbonacci(n-1) + fibbonacci(n-2)); } } main(){ int n = 5; int i; printf(“Factorial of %d: %dn” , n , factorial(n)); printf(“Fibbonacci of %d: ” , n); for(i=0;i<n;i++){ printf(“%d “,fibbonacci(i)); } } Output If we compile and run the above program then it would produce following output − Factorial of 5: 120 Fibbonacci of 5: 0 1 1 2 3 Print Page Previous Next Advertisements ”;