Skip to main content

Posts

Showing posts from October, 2016

Linked List in Cpp

Linked list is a simple linear data structure formed by collection of data elements called nodes. Each node consists of a data element and link field. There is a head node that points to the starting of the linked list. this diagram shows a simple representation of the linked list. Linked list can be used to implement stacks, queues, list, associative arrays, etc.  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. Also you don't need to declare the size of the linked list at the time of initialization you can dynamically keep adding elements to the linked list. Click for complete information on  Linked List  The following implementation of the linked list has the following methods implemented : 1. isEmpty() : method returns true if the list is empty 2. getHead() : returns head node 3. addToStart(): met

Linked List in Java

Linked list is a simple linear data structure formed by collection of data elements called nodes. Each node consists of a data element and link field. There is a head node that points to the starting of the linked list. this diagram shows a simple representation of the linked list. Linked list can be used to implement stacks, queues, list, associative arrays, etc.  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. Also you don't need to declare the size of the linked list at the time of initialization you can dynamically keep adding elements to the linked list. Click for complete information on  Linked List  The following implementation of the linked list has the following methods implemented : isEmpty() : method returns true if the list is empty addToEnd() : method to add a node at the end display() :

Linked List in python

Linked list is a simple linear data structure formed by collection of data elements called nodes. Each node consists of a data element and link field. There is a head node that points to the starting of the linked list. this diagram shows a simple representation of the linked list. Linked list can be used to implement stacks, queues, list, associative arrays, etc.  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. Also you don't need to declare the size of the linked list at the time of initialization you can dynamically keep adding elements to the linked list. Click for complete information on Linked List  The following implementation of the linked list has the following methods implemented : isEmpty() : Returns true if the Linked List is empty. addToStart() : Adds elements at the start of the linked List.

Finding square root of a number by Babylonian method

The Babylonian method for finding square roots involves dividing and averaging, over and over, to obtain a more accurate solution with each repeat of the process. Inorder to find the square root of a number you need to follow the following steps. Make an initial guess for the square root  Divide the original number by this guess Find out the mean of the 2 numbers check if the difference between guess and quotient is less than the error if not then repeat the process again. use this mean value as your next guess Let us take an example of finding out the square root of 7 to 3 decimal places let n = 7 and error = 0.001 let us take an initial guess = 3 FIRST ITERATION Step 1: Guess = 3  Step 2: Divide 7 by 3 = 2.3333333 Step 3: Find average of 3 and 2.333333 = 2.666666 (because (2+2.333333)/2 = 2.666666) Step 4: difference (3 - 2.3333333) > error hence repeat the process again Step 5: Next guess is 2.666666 SECOND ITERATION Step 1: Guess 2.666666

Finding next smallest Palindrome Number

A Palindrome  is a word, phrase,  number , or other sequence of  characters  which reads the same backward or forward for example '123321' or 'racecar'. Given a number inorder to find the next smallest palindrome we need to consider the following cases: Case 1 : if the number is less than 10 if yes then the smallest palindrome is 11. Case 2 : if the number contains all digits to be 9 eg. 9 , 99 , 999 , 9999 in such case the next smallest palindrome is 11 , 101 , 1001 , 10001 as we can see it contains n+1 digits with n-1 0's between two 1's. Case 3: considering if the entered number is already a palindrome then we have 2 cases to deal with  If it does not contain a 9 in its middle digits for eg 1771 or 171 in such cases it is very easy to find the next smallest palindrome you only need to add one to the middle digits so the answer for above cases is 1881 or 181. For the Second case if it contains one in the middle digits for eg. 1991 or 17971 in this c

Program to implement Balanced Brackets using stack

This post is about detecting if the brackets in a string are balanced, this problem is usually a sub problem of expression conversion (infix, prefix, postfix) using stack. In the following program we traverse the string and search for the opening bracket if an opening bracket is found then we push it on the stack and if the closing bracket is found then we pop the stack and compare if they are the same type of opening and closing brackets if yes then we continue else we stop. C++ Program Java program Python Program C++ Program Java Program Python Program You might also be interested in  Singly Linked List Double Linked List Linked List in Python Infix to Prefix Conversion Infix to Postfix Conversion Binary Search Tree