Open In App

Java Program to Implement VList

Last Updated : 17 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

VList is a data structure that combines fast indexing of arrays with the easy extension of singly-linked lists. VList generally supports the following functions.

  • Insert Element
  • Get Element at index k
  • Clear List
  • Display List
  • Etc.

VList has rows connected as Singly Linked List where the Nth row contains maximum 2^N elements.

Example:

VList:

[2]
↓
[1, 3]
↓
[5, 1, 5, 8]
↓
[10, 12, 5, 9, 1, 0, 8, 7]
↓
[3, 5, 7]

VList is Filled first Left to Right Then Top to Bottom.

Implementation:

Java




// Java Program to Implement VList
  
import java.io.*;
import java.util.*;
  
// This will act as each row of VList
class VListNode {
    VListNode next;
    List<Integer> list;
  
    public VListNode()
    {
        next = null;
        list = new ArrayList<Integer>();
    }
}
  
class VList {
  
    private VListNode start;
    private VListNode end;
    private int nodeNumber;
    private int size;
  
    // Constructor of VList
    public VList()
    {
        start = null;
        end = null;
        nodeNumber = 0;
        size = 0;
    }
  
    // Check if VList is Empty or Not
    public boolean isEmpty() { return start == null; }
  
    // Get Number of Elements in VList
    public void getSize()
    {
        System.out.println("VList size : " + size);
        System.out.println();
    }
  
    // Make VList Empty
    public void clearVList()
    {
        start = null;
        end = null;
        nodeNumber = 0;
        size = 0;
  
        System.out.println("VList is Cleared");
        System.out.println();
    }
  
    // Insert a new Element in VList
    public void insert(int x)
    {
        size++;
        int n = (int)Math.pow(2, nodeNumber);
  
        if (start == null) {
            start = new VListNode();
            start.list.add(x);
            end = start;
            return;
        }
  
        if (end.list.size() + 1 <= n) {
            end.list.add(x);
        }
        else {
            nodeNumber++;
  
            VListNode tempNode = new VListNode();
            tempNode.list.add(x);
  
            end.next = tempNode;
            end = tempNode;
        }
    }
  
    // Get value of Element at index k in VList
    public void searchElementWithPosition(int k)
    {
        System.out.print("Element at position " + k
                         + " is = ");
  
        if (k < 1 || k > size) {
            System.out.println("Does not Exist");
            System.out.println();
            return;
        }
  
        k--;
        VListNode startTemp = start;
  
        while (k >= startTemp.list.size()) {
            k -= startTemp.list.size();
            startTemp = startTemp.next;
        }
  
        System.out.println(startTemp.list.get(k));
        System.out.println();
    }
  
    // Print full VList
    public void displayVList()
    {
        System.out.print("VList : ");
        if (size == 0) {
            System.out.print("empty\n");
            return;
        }
  
        System.out.println();
  
        VListNode startTemp = start;
        int num = 0;
  
        while (startTemp != null) {
            for (int i = 0; i < startTemp.list.size();
                 i++) {
                System.out.print(startTemp.list.get(i)
                                 + " ");
            }
  
            System.out.println();
  
            startTemp = startTemp.next;
            num++;
        }
  
        System.out.println();
    }
}
  
class GFG {
    public static void main(String[] args)
    {
        // Driver Code
  
        // Initialize Vlist
        VList vlist = new VList();
  
        int[] arr
            = new int[] { 9, 1, 5, 4, 3, 5, 2, 1, 0, 8 };
  
        // Insert Elements in VList
        for (int val : arr) {
            vlist.insert(val);
        }
  
        // Display VList
        vlist.displayVList();
  
        // Search 1st Element
        vlist.searchElementWithPosition(1);
  
        // Search 5th Element
        vlist.searchElementWithPosition(5);
  
        // Search 8th Element
        vlist.searchElementWithPosition(8);
  
        vlist.getSize();
  
        // Make List Empty
        vlist.clearVList();
  
        System.out.println("Vlist IsEmpty = "
                           + vlist.isEmpty());
    }
}


Output

VList : 
9 
1 5 
4 3 5 2 
1 0 8 

Element at position 1 is = 9

Element at position 5 is = 3

Element at position 8 is = 1

VList size : 10

VList is Cleared

Vlist IsEmpty = true

Time Complexity:

  • Insert operation: O(1) 
  • Search operation: O(log(n))
  • Display: O(n)
  • Get Size & Check Empty: O(1)


Similar Reads

Java Program to Implement Control Table in Java
Control tables will be tables that control the control stream or have a significant influence on program control. There are no unbending guidelines about the structure or substance of a control table—its passing trait is its capacity to coordinate the control stream somehow or another through "execution" by a processor or mediator. The plan of such
3 min read
Java Program to Implement Circular Buffer
When data is constantly moved from one place to another or from one process to another or is frequently accessed, it cannot be stored in permanent memory locations such as hard drives as they take time to retrieve the data. This type of data needs to be accessed quickly and is stored in temporary memory locations such as RAM known as buffers. Examp
8 min read
Java Program to Implement HashTables with Linear Probing
Hashing is a technique that is used to uniquely identify a specific object from a group of similar objects. Suppose an object is to be assigned a key to it to make searching easy. To store the key/value pair, one can use a simple array like a data structure where keys (integers) can be used directly as an index to store values. However, in cases wh
5 min read
Java Program to Implement Stack API
A stack is a linear data structure that follows a particular order in which insertion/deletion operations are performed. The order is either LIFO(Last In First Out) or FILO(First In Last Out). Stack uses the push() function in order to insert new elements into the Stack and pop() function in order to remove an element from the stack. Insertion and
3 min read
Java Program to Implement Inversion Method for Random Number Generation
Here we will be going through the inversion method for random number generation in java. So basically we will be illustrating two approaches which are as follows: Shuffling elements in an arrayUsing Collection.shuffle() method Important Method here namely is setSeed(). It is used to set the seed via setSeed() method of Random class sets of the rand
9 min read
Java Program to Implement Hash Table Chaining with List Heads
A hash table is a data structure that executes a connected array, it is a structure that maps keys to its values. It uses a hash function to calculate an index position for the keys, also called hash code. It will store key and value pair in an array of buckets, and from that bucket, the values can be easily found with the help of keys. To find the
5 min read
Java Program to Implement Sieve of Eratosthenes to Generate Prime Numbers Between Given Range
A number which is divisible by 1 and itself or a number which has factors as 1 and the number itself is called a prime number. The sieve of Eratosthenes is one of the most efficient ways to find all primes smaller than n when n is smaller than 10 million or so. Example: Input : from = 1, to = 20 Output: 2 3 5 7 11 13 17 19 Input : from = 4, to = 15
3 min read
Java Program to Implement wheel Sieve to Generate Prime Numbers Between Given Range
A prime number is a whole number greater than 1, which is only divisible by 1 and itself. The first few prime numbers are 2 3 5 7 11 13 17 19 23. Given a range, L to R, the task is to generate all the prime numbers that exist in the Range. Examples Input: 1 10 Output 2 3 5 7 Input: 20 30 Output: 23 29 Approach 1: Check every element whether the ele
5 min read
Java Program to Implement JobStateReasons API
An API stands for Application Programming Interface which are simply a set of definitions and protocols for building and integrating application software to simplify app development to save time and money. For instance, if we want to forecast the weather of different cities using past data, instead of finding and typing all the past data manually,
3 min read
Java Program to Implement Naor-Reingold Pseudo Random Function
Naor-Reingold Pseudo-Random Function is a function of generating random numbers. Moni Naor and Omer Reingold described efficient constructions for various cryptographic primitives in the private key as well as public-key cryptography. Example: Input : N = 5 Output: 9.0, 9.0, 3.0, 9.0, 3.0 Input : N = 7 Output: 9.0, 81.0, 9.0, 9.0, 3.0, 3.0, 9.0 Alg
2 min read
Practice Tags :