java data structure linked list

The linked list can be divided into the following types:

Singly linked list
Double-ended linked list
Ordered linked list
Doubly linked list

1. Singly linked list

In a linked list, each data item we insert can be considered to be contained in a link. And each link point is an object of a certain class, and we first (as in the following) call this class Link. And how do these nodes relate to each other? Here, a next field is introduced to indicate a reference to the next field. The entire linked list also has a header, which we call first. Data is inserted from here each time, that is, first points to the newly inserted data, and the newly inserted data passes through next points to the next data. As shown in the figure:


So, we can start by creating a class Link that contains some data and a reference to the next link point:

public class Link{
	
		public long dData;
		public Link next;
		public Link(long d)
		{
			dData = d;
		}
		
	}

Next is the question of how to insert data, let's take a look at the insertFirst() method. For a singly linked list, when inserting data, it is inserted at the header. For the newly inserted data, we can make it equal to the original value of first, and then change the value of first to point to the newly inserted data item to complete an insertion.


public void insert(int id) {
		// TODO Auto-generated method stub
          Link newLink = new Link(id);
          newLink.next = first;//Note that these two steps cannot be reversed! ! ! This refers to changing the value originally pointed to by first into the next value pointed to by the newly inserted data item
          first = newLink;//If it is reversed, then the link point pointed to at the beginning by first will be overwritten, and a complete chain cannot be formed. (Similar to the exchange of two data)
	}

Similarly, when deleting, it is the inverse operation of insertion. It disconnects the connection with the first link point by re-pointing first to the second link point, and how to point first to the second link point? Here you only need to find the second link point through the next field of the first link point (not first).

public Link delete() {
		// TODO Auto-generated method stub
       Link temp = first;
        first = first.next;
        return temp;
	}

The following shows the complete code:

package dailypratice;

public class Link {
	
		public long dData;
		public Link next;
		public Link(long d)
		{
			dData = d;
		}
		
	}
public class Linklist {
      private Link first;
      private int linkcount;
      public Linklist() {
		// TODO Auto-generated constructor stub
    	  first=null;
	}
      public boolean isEmpty() {
		// TODO Auto-generated method stub
           return (first==null);
	}
      public void insert(int id) {
		// TODO Auto-generated method stub
          Link newLink = new Link(id);
          newLink.next = first;
          first = newLink;
          linkcount++;
	}
      public Link delete() {
		// TODO Auto-generated method stub
       Link temp = first;
        first = first.next;
        return temp;
	}
      public void display() {
		// TODO Auto-generated method stub
        Link curr = first;//After creating a Linkd object when printing, search along the linked list through the next field until it is empty, indicating that the search is complete
        while(curr!=null){
        	System.out.print(curr.dData+" ");
        	curr=curr.next;
        }
	}
}
//测试
public class Linkapp {
     public static void main(String[] args) {
    Linklist theList = new Linklist();
  theList.insert(10);
  theList.insert(50);
  theList.insert(20);
  theList.insert(70);
  theList.insert(80);
  theList.insert(30);
  theList.insert(50);
  theList.display(); theList.delete(); theList.delete(); theList.delete(); theList.display(); } }
   
 
 
 
 
   

//before delete
50 30 80 70 20 50 10 
// after delete
70 20 50 10  

2. Double-ended linked list

1. The header of a double-ended linked list (note that it is distinguished from the following two-way linked list) is the same as that of a singly linked list. The difference lies in the reference to the last link point. One more reference is added to the last link point, pointing from first to the last link point, so that we can insert data at the end, just like inserting data from the header.



On the basis of the singly linked list, an insertLast() method is added. As the name implies, a link point is inserted from the end. It is worth mentioning that the double-ended linked list cannot implement the function of deleting the last link point, because there is no point to the penultimate link point. reference (this function will be implemented in the later doubly linked list).

Full code:

public class Link {
	
		public long dData;
		public Link next;
		public Link(long d)
		{
			dData = d;
		}
		
	}
public class LinkLastFirst {
	 private Link first;
	 private Link last;
	 public LinkLastFirst() {
		// TODO Auto-generated constructor stub
		 first=null;
		 last=null;
	}
	 public void insertFirst(long id) {
		// TODO Auto-generated method stub
		 Link newLink=new Link(id);
		 if(isEmpty())//Because a last reference is now added, so now consider last when inserting for the first time
			 last = newLink;
		 newLink.next=first;
		 first=newLink;
		 

	}
	 public void insertLast(long id) {
		 // TODO Auto-generated method stub
		 Link newLink=new Link(id);
		 if(isEmpty())
			 first=newLink;//Similarly, when the linked list is empty when inserting for the first time, the first reference should point to the first inserted data
		 else{
			 last.next=newLink;//Be careful to distinguish it from insertFirst, if the linked list is not empty, then the data item pointed to by last
		  } //The next one that becomes the old data item points to the new data item
		 last=newLink;//Finally, last points to newLink
		 
	 }
	 public long deleteFirst() {
			// TODO Auto-generated method stub
	    long temp=first.dData;
	    if(first.next==null){//If this linked list has only one connection point, when deleting, last must point to null
	    	last=null;
	    }
	    first=first.next;
	    return temp;

	}
	 public boolean isEmpty() {
		// TODO Auto-generated method stub
	      return (first==null);
	}
	  public void display() {
			// TODO Auto-generated method stub
	        Link curr = first;
	        while(curr!=null){
	        	System.out.print(curr.dData+" ");
	        	curr=curr.next;
	        }
	        System.out.println();
		}

}
//test
public class LinkFirstLastApp {
	 public static void main(String[] args) {
    	 LinkLastFirst theList = new LinkLastFirst();
  		 theList.insertFirst(100);
  		 theList.insertFirst(540);
  		 theList.insertFirst(278);
  		 theList.insertFirst(70);
  		 theList.insertFirst(88);
  		 theList.insertFirst(35);
  		 theList.insertFirst(40);
  		 theList.display();
  		 
  		theList.insertLast(25);
  		theList.insertLast(45);
  		theList.insertLast(55);
  		theList.display();
 		 
	}

}
//result
40 35 88 70 278 540 100 
40 35 88 70 278 540 100 25 45 55 

3. Ordered linked list

The key part of an ordered linked list is insertion. In a linked list, the inserted data is ordered, such as from small to large, so each time you insert data, you need to move the data along the linked list until you find a data item larger than it, stop. down. The insertion at this time is the same as the type of linked list mentioned above. The difference is that in the ordered linked list, we need to introduce an application previous, which is used to record the previous item from the link point to the appropriate insertion place, so that it can be connected. , as shown in the figure:


Here, only the code of the inserted part is posted, the others are the same as those of the singly linked list.

public void insert(int id) {
		// TODO Auto-generated method stub
          Link newLink = new Link(id);
          Link previous=null;
          Link current=first;//Record the current point so that the newly inserted link point can be linked by next
          while(current!=null&¤t.dData<id){
        	  previous=current;//Record the node when it was passed, (not all nodes passed by!!)
        	  current=current.next;//Because the inserted data item is larger than the current node, continue to search.
          }
          if(previous==null)//Indicates the start of the linked list
        	  first=newLink;
          else
        	  previous.next=newLink;
          newLink.next=current;
	}

//test

public class Linkapp {
     public static void main(String[] args) {
    	 Linklist theList = new Linklist();
  		 theList.insert(10);
  		 theList.insert(50);
  		 theList.insert(20);
  		 theList.insert(70);
  		 theList.insert(80);
  		 theList.insert(30);
  		 theList.insert(50);
  		 theList.display();	 
	}
}
10 20 30 50 50 70 80

4. Doubly linked list

The doubly linked list is more flexible than the above linked list, because it adds a prev reference at each node, so that the linked list is also connected in the reverse direction, so that the reverse can also be traversed, inserted, and deleted.


package dailypratice;

public class DoubleLink {
	public long dData;
	public DoubleLink next;
	public DoubleLink prev;
	public DoubleLink(long d)
	{
		dData = d;
	}
	
}
 
 
public class DoubleLinkList {
	private DoubleLink first;
	private DoubleLink last;
	public DoubleLinkList() {
		// TODO Auto-generated constructor stub
		first=null;
		last=null;
	}
	public boolean isEmpty() {
		// TODO Auto-generated method stub
        return first==null;
	}
	public void insertFirst(long id) {
		// TODO Auto-generated method stub
		DoubleLink newLink=new DoubleLink(id);
		    if(isEmpty())//This is the same as the double-ended linked list mentioned earlier
			  last = newLink;
		    Else//This is different. When the linked list is not empty, each time you insert data from the header, you must consider pointing prev to the newly inserted data.
		    	first.prev=newLink;
			newLink.next=first;
			first=newLink;
		}
	 public void insertLast(long id) {
		 // TODO Auto-generated method stub
		 DoubleLink newLink=new DoubleLink(id);
		 if(isEmpty())
			 first=newLink;//Similarly
		 else{
			 last.next=newLink;
			 newLink.prev=last;
		  }                  
		 last=newLink;//Finally, last points to newLink
	 }
	 public DoubleLink deleteFirst() {
			// TODO Auto-generated method stub
		 DoubleLink temp=first;
	    if(first.next==null){//If this linked list has only one connection point, when deleting, last must point to null
	    	last=null;
	    }else{
	    	first.next.prev=null;//As can be seen from the picture, the prev of the first link point always points to null
	    }
	    first=first.next;
	    return temp;
	}
	 public DoubleLink deleteLast() {
		 DoubleLink temp=last;
		    if(last.next==null){//If this linked list has only one connection point, when deleting, first must point to null
		    	first=null;
		    }else{
		    	last.prev=null;//As can be seen from the picture, the prev of the last link point should point to null, that is, disconnect the last link point
		    }
		    last=last.prev;
		    return temp;
	 }
	 //Two printing methods
	 public void displayFirst() {
			// TODO Auto-generated method stub
	        DoubleLink curr = first;
	        while(curr!=null){
	        	System.out.print(curr.dData+" ");
	        	curr=curr.next;
	        }
	        System.out.println();
		}
	 public void displayLast() {
			// TODO Auto-generated method stub
	        DoubleLink curr = last;
	        while(curr!=null){
	        	System.out.print(curr.dData+" ");
	        	curr=curr.prev;
	        }
	        System.out.println();
		}
}
public class DoubleLinkApp {
	public static void main(String[] args) {
		DoubleLinkList theList = new DoubleLinkList();
// 		 theList.insertFirst(10);
// 		 theList.insertFirst(50);
// 		 theList.insertFirst(20);
// 		 theList.insertFirst(70);
// 		 theList.insertFirst(80);
// 		 theList.insertFirst(30);
// 		 theList.insertFirst(50);
// 		 System.out.println("调用displayFirst()");
// 		 theList.displayFirst();
// 		System.out.println("调用displayLast()");
// 		 theList.displayLast();
 		 
 		System.out.println("调用insertLast()");
 		 theList.insertLast(10);
		 theList.insertLast(50);
		 theList.insertLast(20);
		 theList.insertLast(70);
		 theList.insertLast(80);
		 theList.insertLast(30);
		 theList.insertLast(50);
 		 
		 System.out.println("调用displayFirst()");
 		 theList.displayFirst();
 		  System.out.println("调用displayLast()");
 		 theList.displayLast();
		 
	}
}
call insertLast()
call displayFirst()
10 50 20 70 80 30 50 
call displayLast()
50 30 80 70 20 50 10 


The linked list chapter is here~ In our actual application, we can use iterators to traverse by directly calling the existing linked list class (will be discussed later when writing containers), but the principle of linked list still needs to be mastered, as a new generation program monkey.

In addition, the linked list does not necessarily delete at the head or the tail, insert data, you can also use a doubly linked list to insert and delete data from the middle~ If you don’t understand, you can privately chat or leave a comment~



 









Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325902684&siteId=291194637