Posts

Custom Stack Implementation in java

Image
In the collection framework, stack comes with the extended functionalities of Vector class. There are 5 functionalities in the stack class which are the extension of vector class functionalities. There are 2 different ways to implement stack -  Using Array Using ArrayList 1. Using Array : Array is the basic memory for any implementation in java. We will see how to implement it using array. To build custom stack, please follow below steps: Declare StackArray class and it's constructor: Below image depicts that the above piece of code registers the two indexes space in memory and points to idx at 0 position: Now add the method of push and pop to the class which will take care of addition and removal of element to the stack: Our basic functionality of stack is ready. Now we will add the support functions to it: These size and print methods will help us while running the program to get the current status of the stack. Now have few methods to opt...

Stack in Java

Image
Stack Framework In the list of data structures, the next name is "Stack". In the collection framework, stack comes with the extended functionalities of Vector class.  It is the one which works on LIFO(Last in, first out) principle i.e. the element which inserted at last would be eligible to come out first and so on. e.g. if 1,2,3,4,5 inserted in a stack, the order to get the elements from it must be 5,4,3,2,1 As shown in the above figures, the stack is like cylinder and the data is like cylinder size disks. If the disks put in the cylinder and we need to take out the first disk out of it, we must take out all the disks which are above it, then only we would be able to get our disk. It extends Vector class and implement five operations to behave like stack.  Operations of Stack Stack supports the push and pop operations. Let's look at the details: 1. Push() : The push operation can be defined as putting the disk or element ...

Custom Doubly LinkedList Implementation

The doubly linked list is same as the linked list but every node has address of previous node also along with next node so that the traversal can be done in both ways. To implement the custom doubly linkedlist, please follow below steps: 1. Create the basic structure class 2. Implement the doubly linkedlist operations 3. Test the functionality Full implementation source code is as below: Output: 23->34->65->78 -1->23->34->3478->65->78->6445 -1->23->34->3478->65->78 -1->23->34->3478->65 65->3478->34->23->-1 Prev::3478 & Curr::65 Look at my other posts: Stack in Java Custom Stack Implementation Set Interface in Java Custom Doubly LinkedList Implementation Custom LinkedList Implementation How to fetch elements from LinkedList List Traversal How to insert elements in List How to remove element from list How to declare Linked List and Implement LinkedList Traversal In Reverse Sequential Order Array...

Custom LinkedList Implementation

Image
The Linkedlist represents the chain of data nodes as list in java. The current node has the address or connection with the next node in single linked list and of previous node also in double linked list. To build custom linkedlist, please follow below 3 steps: 1. First, we need to construct the structure of linked list 2. Second, implement the operations of the linked list in another class 3. Third, test the functionality The source code of program Output: 12->454->89->1 12->454->1 45->12->454->1->23 abc->raj->kiran abc->raj 65->Gyan Look at my other posts: Stack in Java Custom Stack Implementation Set Interface in Java Custom Doubly LinkedList Implementation Custom LinkedList Implementation How to fetch elements from LinkedList List Traversal How to insert elements in List How to remove element from list How to declare Linked List and Implement LinkedList Traversal In Reverse Sequential Order ...

Set Interface in Java

    In collection framework "Set" is an interface which has all the collection's methods with a contract of having only unique elements. It means if you want to save the n elements and m elements are duplicate out of them then set interface would contain only (n-m) elements and reject m elements during addition to the collection. Set can contain at most one null element. Output: Original Set :: [1, 2, 3] After Insertion test :: [1, 2, 3, 4]     Internally Set uses the hashmap while storing the elements. It has one static object declared which is used as value for this map. Whichever element we add in Set it adds it in the hashmap as a key. If the key is already present map returns the value of that key otherwise it returns null. So if we are getting the null from map, it means the value got added successfully. This property is used in the add method of Set. If we see the HashSet implementation we find the code like : public class HashSet ...

Fetch elements from LinkedList

There are several methods given in the linked list to retrieve the inserted elements. Each method has its own importance. E.g. If user wants to retieve the first element from the list, it can choose among get(int index), getFirst(), element(), peek() or peekFirst() methods but there usages are different like get(int index) method provides the element from any given position(index) whereas getFirst(), element(), peek() or peekFirst() methods returns only first element from the list. Let us see the different usages of these methods: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 package net . raj . test . list ; import java.util.LinkedList ; /** * @author Raj Saxena * */ public class LinkedListRetrieveImpl { public static void main ( String ... args ) { // Declare an LinkedList LinkedList < String > list = new LinkedList <...

LinkedList Traversal In Reverse Sequential Order

LinkedList traversal can be done in two directions - Ascending and Descending The ascending traversal or normal or forward traversal can be done using iterator method provided by List interface whereas for the descending order traversal is possible using method provided by Dequeue interface. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 package net . raj . test ; import java.util.Iterator ; import java.util.LinkedList ; /** * @author Raj Saxena * */ public class LinkedListTraversalImpl { public static void main ( String [] args ) { // Declare an LinkedList LinkedList < String > list = new LinkedList <>(); // populate list list . add ( "one" ); list . add ( "two" ); list . add ( "three" ); list . add ( "four" ); list . add ( "five" ); System . out . println ( "List traversal in...