📜  实现VList的Java程序

📅  最后修改于: 2022-05-13 01:54:25.296000             🧑  作者: Mango

实现VList的Java程序

VList 是一种数据结构,它结合了数组的快速索引和单链表的简单扩展。 VList 一般支持以下功能。

  • 插入元素
  • 获取索引 k 处的元素
  • 清晰的列表
  • 显示列表
  • 等等。

例子:

VList:

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

执行:

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 list;
  
    public VListNode()
    {
        next = null;
        list = new ArrayList();
    }
}
  
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 postition " + 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());
    }
}


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

Element at postition 1 is = 9

Element at postition 5 is = 3

Element at postition 8 is = 1

VList size : 10

VList is Cleared

Vlist IsEmpty = true

时间复杂度:

  • 插入操作: O(1)  
  • 搜索操作: O(log(n))
  • 显示: O(n)
  • 获取大小并检查空: O(1)