Single chain table of Java data structure and algorithm

02. Single chain table of Java data structure and algorithm ...
Understanding single chain table
02. Single chain table of Java data structure and algorithm

Understanding single chain table

characteristic

The storage of data elements corresponds to discontinuous storage space, and each storage node corresponds to a data element to be stored.

Each node is composed of data field and pointer field. The logical relationship between elements is reflected by the link relationship between storage nodes. Logically adjacent nodes do not need to be physically adjacent.

shortcoming

1. The storage density is smaller than that of sequential storage structure (each node is composed of data domain and pointer domain, so if full storage is assumed in the same space, the sequential storage is more than chain storage).

2. Chain storage is slower than sequential storage when searching nodes (the address of each node is discontinuous and irregular, which leads to low efficiency of searching according to index.

advantage:

1. Flexible insertion and deletion (no need to move the node, just change the pointer in the node).

2. Only elements can allocate node space, and there will be no idle nodes.

Add node

Delete node

Single chain table of leading nodes

When using single chain table to implement linear table, in order to make the program more concise, we usually add a dummy node, also known as the header node, at the front of the single chain table.

No real data object is stored in the header node, and its next field points to the node of element 0 in the linear table

It can deal with the situation of empty table, non empty table and the first element node in a unified way. It is more convenient to program and commonly used. The structure of a single chain table without and with leading nodes to implement a linear table is shown in the figure.

Define List interface

public interface List <T>{ // -------Add------- void add(Object object); // -------Delete from coordinates------- void remove(int index); // -------Delete by content------- void removeobj(Object object); // -------Retrieve data------- Object get(int index); // -------Find the length of a set------- int size(); // -------Judge whether the set is empty------- boolean isEmpty(); // -------Find element coordinates based on content------- int IndexOf(Object object); // -------Determine whether the element exists------- boolean contions(Object object); // -------Insert element according to coordinate position------- void add(int index, Object object); // ------- toString ------- String toString(); }

SingLinkList implementation class

import java.util.NoSuchElementException; public class SingleLinkedList implements List{ private Node head = new Node(); //Head node private int size; //Number of nodes in the list @Override public String toString() { StringBuilder stringBuilder = new StringBuilder("["); Node p = head.next; while(p != null){ stringBuilder.append(p.data + ","); p = p.next; } //Delete the last comma stringBuilder.deleteCharAt(stringBuilder.length()-1); stringBuilder.append("]"); return stringBuilder.toString(); } @Override public void add(Object object) { //Let p first point to the head node Node p = head; //Move p to the last node while(p.next != null){ p = p.next; } //Add element p.next = new Node(object); size++; } @Override public void remove(int index) { /*Judge whether the index is legal*/ if(index < 0||index > size-1){ throw new NoSuchElementException(); } ////Previous node to delete Node prev = (Node) getNode(index - 1); //Current node to delete Node currNode = prev.next; Node nextNode = currNode.next; prev.next = nextNode; currNode.next = null; size--; } @Override public void removeobj(Object object) { int index = IndexOf(object); if(index >=0){ remove(index); } } @Override public Object get(int index) { Node o = (Node)getNode(index); return o.data; } public Object getNode(int index) { /*Judge whether the index is legal*/ if(index < 0||index > size-1){ throw new NoSuchElementException(); } // Points to the first node Node p=head.next; for (int i = 0; i <index ; i++) { p=p.next; } // p node returned return p; } @Override public int size() { return size; } @Override public boolean isEmpty() { return size==0; } @Override public int IndexOf(Object object) { //Determine if it is found int index = -1; Node p = head.next; while(p != null){ index++; if(p.data == object || p.data.equals(object)){ return index; } p = p.next; } return -1; } @Override public boolean contions(Object object) { return IndexOf(object) >= 0; } @Override public void add(int index, Object object) { //To determine whether the index is legal, it is allowed to be equal to size, because it can be added to the last position if(index<0||index > size){ throw new NoSuchElementException(); } //Get the previous node of the add location Node prev=(Node) getNode(index - 1); Node newNode = new Node(object,prev.next); prev.next = newNode; size++; } //Use Node as the inner class of single chain table class Node { Object data;//Storage is data Node next;//Pointer to next node public Node() { } public Node(Object data) { this.data = data; } public Node(Object data, Node next) { super(); this.data = data; this.next = next; } public String toString() { return "Node [data=" + data + ", next=" + next + "]"; } } }

24 May 2020, 07:40 | Views: 8692

Add new comment

For adding a comment, please log in
or create account

0 comments