Algorithm learning 7: linked list

Concept of linked list:

The linked list consists of a series of nodes, each of which consists of two parts: one is the data field for storing data elements, the other is the pointer field for storing the address of the next node.

There are single chain list and double end chain list

Code implementation single chain table:

//First, design a data structure of single chain table
public class SingleLink {
    public int data;
    public  SingleLink next;

    public SingleLink(int data){
        this.data = data;
    }

    public void displaySingleLink(){
        System.out.println("linkdata="+data);
    }
}


/**
 * Created by Administrator on 2018/3/26.
 * This is a set of linked lists
 */
public class SingleLinkList {
    private SingleLink first;

    public SingleLinkList(){
        first = null;
    }

    public void insertFirst(SingleLink newLink){
        if(first==null){//Description is the first time to insert an element. The collection is empty
            first = newLink;
            newLink.next = null;
        }else{//There are already inserted elements. The new element will be inserted as the first element
            newLink.next = first;//Save the first element first
            first = newLink;//The position of the first element places the inserted element
        }
    }

    public boolean isEmpty(){
        return first==null;
    }

    //Get the deleted node
    public SingleLink deleteFirst(){
        if(first==null){
            return null;
        }
        SingleLink current = first;
        //Pointer down
        first = first.next;
        return current;
    }

    public void displayList(){
        SingleLink current = first;//Pointer to first
        while (current!=null){
            current.displaySingleLink();
            current = current.next;
        }
    }

}


 //Delete specified element
    public SingleLink deleteLink(int data) {
        SingleLink current = first;
        SingleLink previous = first;
        if(current==null){
            return null;
        }
        while(current.data!=data){//No traversal to
            previous = current;
            current =current.next;
            if(current==null){
                return null;
            }
        }

        //Results after processing cycle:
        if(current==null){
            return null;
        }else{
            if(current==first){//Explanation is the first element: remove the first element
                first = first.next;
            }else{//current is not the first element:
                previous.next = current.next;
            }
            return current;
        }
    }

 /**
     * Find a Link
     *
     * @param data
     * @return
     */
    public SingleLink finkLink(int data) {
        if (first == null) {
            return null;
        }
        SingleLink current = first;
        while (current != null) {
            if (current.data == data) {
                break;
            }
            current = current.next;
        }

        return current;
    }

Now let's look at the result of inserting elements: every time an element is inserted, if it is put in the first place, the result of this time should be: 5-4-3-2-1

public static void  main(String[] args){

        SingleLinkList linkList = new SingleLinkList();
        SingleLink link1  = new SingleLink(1);
        SingleLink link2  = new SingleLink(2);
        SingleLink link3  = new SingleLink(3);
        SingleLink link4  = new SingleLink(4);
        SingleLink link5   = new SingleLink(5);
        linkList.insertFirst(link1);
        linkList.insertFirst(link2);
        linkList.insertFirst(link3);
        linkList.insertFirst(link4);
        linkList.insertFirst(link5);
        linkList.displayList();
    }

//Then delete:

       //Delete the first element:
        SingleLink link = linkList.deleteFirst();
        System.out.println("Print deleted elements:");
        link.displaySingleLink();
        System.out.println("Print elements after deletion:");
        linkList.displayList();

At this point, the exercise of single chain table is over.

Code implementation of double end linked list:

Now I'll introduce a double ended linked list: unlike a single linked list, there is a pointer that always points to the last element:

//Design data structure of double linked list
public class DoubleLinkList {

    private SingleLink first;
    private SingleLink last;

    public DoubleLinkList(){
        first = last = null;
    }


    /**
     * Always insert elements to the front
     */
    public void insertFirst(SingleLink newLink){
        if(first==null){//Note that the list is empty, and last always points to the first node
            first = last =newLink;
        }else{//There are already elements in the list
            newLink.next = first;
            first = newLink;
        }
    }

    /**
     * Always insert the element behind you, note that the last element always points to the first
     * @param newLink
     */
    public void insertLast(SingleLink newLink){
        if(first==null){
            first = last =newLink;
        }else{//If there are already elements: insert from last
            last.next = newLink;
        }

        last = newLink;//Move the last pointer back
    }


    /**
     * Delete first element
     * @return
     */
    public SingleLink deleteFirst(){
        if(first ==null){
            return null;
        }
        //Delete the first element if there is one
        SingleLink current = first;
        first = first.next;
        return current;
    }


    /**
     * Delete the last element.
     * @return
     */
    public void deleteLast(){
        if(first ==null){//None of the elements
            return;
        }else if(first.next==null){//Only one element
            first = null;
            last = null;
        }else{//Traverse to the last element
            SingleLink current = first;
            while(current.next!=null){
                if(current.next==last){//If the last element has been found
                    last = current;
                    last.next = null;
                    break;
                }
                current = current.next;
            }

        }
    }

    public void displayList() {
        SingleLink current = first;//Pointer to first
        while (current != null) {
            current.displaySingleLink();
            current = current.next;
        }
    }

    public void displayLast(){
        if(first==null){
            System.out.println("list Empty");
        }else{
            last.displaySingleLink();
        }

    }

Then verify the data structure of the design:

 public static void main(String[] args) {

        DoubleLinkList linkList = new DoubleLinkList();
        linkList.insertFirst(new SingleLink(1));
        linkList.insertFirst(new SingleLink(2));
        linkList.insertFirst(new SingleLink(3));
        linkList.insertFirst(new SingleLink(4));
        linkList.insertFirst(new SingleLink(5));

        linkList.displayList();
        linkList.displayLast();

    }

The last inserted element of data is placed at the front and knows that the last element is the first inserted element

//Let's look at mixed insert and delete:
 public static void main(String[] args) {
         DoubleLinkList linkList = new DoubleLinkList();
        linkList.insertLast(new SingleLink(1));
        linkList.insertLast(new SingleLink(2));
        linkList.insertLast(new SingleLink(3));
        linkList.insertLast(new SingleLink(4));
        linkList.insertFirst(new SingleLink(6));
        linkList.insertLast(new SingleLink(5));

        System.out.println("The collection elements before deletion are:");
        linkList.displayList();

        System.out.println("After deletion, the collection elements are:");
        linkList.deleteLast();
        linkList.displayList();

        System.out.println("Last element:");
        linkList.displayLast();
}

To view the results again:

Posted by jackel15 on Thu, 02 Apr 2020 21:45:44 -0700