Java SE 044 uses LinkedList to implement Stack and Queue

(1) As long as a person does not give up on himself, the whole world will not give up on you.
(2) I am born to be of great use . (3) If I
cannot bear the suffering of learning, I must bear the suffering of life. How painful it is Deep comprehension.
(4) You must gain from doing difficult things . (
5) Spirit is the real blade.
(6) Conquering opponents twice, the first time in the heart.
(7) Writing is really not easy. If you like it or have something for you Help remember to like + follow or favorite~

Java SE 044 uses LinkedList to implement Stack and Queue

1.LinkedList与ArrayList

(1) The bottom layer of the add method of LinkedList is to store an Entry type of data, which includes a predecessor address reference, a subsequent address reference, and an address reference of the data object.

(2) For a linked list, it must have a forward reference, a backward reference, and the data itself. If you just put the data in the linked list, the reference is gone, and the next one cannot be found. Element and the previous element. Therefore, when LinkedList is implemented, it generates a new Entry object in it. The Entry class itself contains three parts. The first is a forward reference, the second is a backward reference, and the third One is the data, and the data is the data added to the linked list. It uses this as a member variable of the Entry class, then constructs an Entry object, and puts the newly generated Entry object into the LinkedList. Therefore, when we execute linkedlist.add("aaa"), instead of putting the reference of the string into the LinkedList linked list, we put an Entry object constructed from the string into the LinkedList linked list.

(3) And ArrayList is to put the data directly into the array.

(4) Summary:
When adding an object to the ArrayList, it is actually placed in the array maintained at the bottom of the ArrayList. When an object is added to the LinkedList, an Entry object is actually generated inside the LinkedList. The Entry object The structure is:
Entry{ Entry previous; Object data; Entry next; } Among them, the element element of Object type is the element we add to the LinkedList, and then Entry constructs forward and backward references privious, next, and finally Add the generated Entry object to the linked list. In other words: LinkedList maintains Entry objects one by one. ## 2. Custom queue (Queue)






package com.javase.linkedlist;

import java.util.LinkedList;

public class MyQueue {
    
    
	@SuppressWarnings("unchecked")
	private LinkedList list = new LinkedList();
	
	//将元素对象放到队列里面去
	@SuppressWarnings("unchecked")
	public void put(Object o){
    
    
		list.addLast(o);
	}
	
	//取元素
	public Object get(){
    
    
		Object o = list.getFirst();
		list.removeFirst();
		return o;
	}
	
	public boolean isEmpty(){
    
    
		return list.isEmpty();
	}
	
	public static void main(String[] args) {
    
    
		MyQueue myQueue = new MyQueue();
		myQueue.put(new Integer(1));
		myQueue.put(new Integer(2));
		myQueue.put(new Integer(3));
		
		System.out.println(myQueue.get());
		System.out.println(myQueue.get());
		System.out.println(myQueue.get());
		
		System.out.println(myQueue.isEmpty());
	}
}

Guess you like

Origin blog.csdn.net/xiogjie_67/article/details/108540780