Skip to main content

Posts

Finding Middle Node in Linked List

Linked List is dynamic data structure that is used to store information. Unlike arrays linked lists are not stored in contagious memory locations rather the are stored at any empty place in memory and the address of the next node is stored in the link field.  Hence we don't have direct access to the Linked List elements as we do in arrays. In order to find the middle node of the Linked List there are 2 methods Method 1 :  Traverse the Linked list count the number of nodes and then move a pointer to that node by using a counter. This method although useful requires us to traverse the List 2 times. Method 2 :  Use 2 pointers (say) fast and slow starting from the head node increment the fast pointer by 2 and increment the slow pointer by 1. In this way when the fast pointer reaches the end the slow pointer will point to the middle element. To know more about Linked List and its implementation click Here . The following is the implementation of the Met...

Mid-point Circle Generation Algorithm

Mid-Point circle generation algorithm aims to find out the points that lie on the circle (or approximately lie on the circle) in a pixel based display. This Algorithm is similar to Bresenhams Circle Generation Algorithm , According to wikipedia bresenhams algorithm is derived from mid-point circle generation algorithm. In this algorithm we divide the circle into 8 parts and calculate the points only for one part and then apply the property of symmetry to get the points in the remaining 7 parts. We divide the circle into the following parts. At the beginning we start with the point x = 0, y = r ; so we get the first point as (xc + x, yc + y) which lies on the circle. let us denote this point as (xp, yp). now we have check if the neighbouring point (xp - 1, yp + 1) or (xp, yp + 1) lies on the circle, We do this by checking the value of the variable called parameter which checks if the point lies on the circle or not and accordingly we update the value of x and y to plo...

Bresenhams Circle Generation Algorithm

Bresenhams circle generation algorithm aims to find out the points that lie on the circle (or approximately lie on the circle) in a pixel based display. In this algorithm we divide the circle into 8 parts and calculate the points only for one part and then apply the property of symmetry to get the points in the remaining 7 parts. We divide the circle into the following parts. At the beginning we start with the point x = 0, y = r ; so we get the first point as (xc + x, yc + y) which lies on the circle. let us denote this point as (xp, yp). now we have check if the neighbouring point (xp - 1, yp + 1) or (xp, yp + 1) lies on the circle, We do this by checking the value of the variable called parameter which checks if the point lies on the circle or not and accordingly we update the value of x and y to plot the next point. Algorithm Plot the initial point with x = 0, y = r and initialize parameter = 3 - 2*radius While x < y keep repeating steps 3, 4 and 5 If...

Infix to Prefix conversion using Stack

This post is about conversion of Infix expression to Prefix conversion. For this conversion we take help of stack data structure, we need to push and pop the operators in and out of the stack. Infix expressions are the expressions that we normally use, eg. 5+6-7; a+b*c etc. Prefix expressions are the expressions in which the 2 operands are preceded by the operator eg. -+56 7 , +a*bc etc. This method is very similar to the method that we used to convert Infix to Postfix but the only difference is that here we need to reverse the input string before conversion and then reverse the final output string before displaying it. NOTE: This changes one thing that is instead of encountering the opening bracket we now first encounter the closing bracket and we make changes accordingly in our code. So, to convert an infix expression to a prefix expression we follow the below steps (we have 2 string, 1st is the input infix expression string 2nd is the output string which is empty initially)...

Infix to Postfix conversion using Stack

This post is about conversion of Infix expression to Postfix conversion. For this conversion we take help of stack data structure, we need to push and pop the operators in and out of the stack. Infix expressions are the expressions that we normally use, eg. 5+6-7; a+b*c etc. Postfix expressions are the expressions in which the 2 operands are followed by the operator eg. 56 +7- , abc*+ etc. So inorder to convert an infix expression to a postfix expression we follow the below steps (we have 2 string, 1st is the input infix expression string 2nd is the output string which is empty initially) We start by going through the characters of the infix expression one by one. If we come across an operand we simply copy it to the Postfix output string. If we come across any opening parenthesis we push it on the stack. If we come across any closing parenthesis we pop the stack till we find the corresponding opening parenthesis. If we come across an operator then we have 2 cases based on...

4 Different methods to swap 2 numbers

Swapping 2 numbers is one of the favorite questions asked to a lot of people during their interviews. There are a lot of ways to swap 2 numbers. In this post we have 4 methods that can be used to swap 2 numbers. First Method : Using addition and subtraction method This method is a simple method that can be used to swap 2 numbers, the method can be used for all integer, float and double variables. The method involves one addition and two subtraction operations. Second Method : Using multiplication and division operators This method is simply obtained by replacing + by * and - by / operator in the above method, but this method cannot be used when one of the numbers is 0 as it involves division operation. Third Method : Using the EX-OR operation This method involves repeated ex-or operations between 2 numbers to swap 2 numbers. But this method can be used only for integers and cannot be used for float or double variables as bit operations for them are not defined...

Shell Sort

Shell sort is a in-place comparison sort method to sort the element. Unlike other sorting methods in this method we start by sorting elements that are far apart or at fixed gap from each other and after every iteration we get partially sorted sublists. Let us consider an array of 7 elements and sort them by gap of 3. In the above example we see that as we have an array we compare element that are 3 gaps away so we sort the sublists (a0, a3, a6) , (a1, a4) , (a2, a5) . The sorting of these sublists can be done using any method for my program I have used Insertion sort. After every iteration we decrease the gap unitl we reach the gap of one where it simply compares the adjacent elements. Chosing the values of gaps is very important as has effects on the complexity of the sorting. To know more about Shell sort click Here . C Program C++ Program Java program Python Program C Program C++ Program ...