Detailed explanation of LinkedList in java

Collection system:
------------| Collection The root interface of a singleton collection
----------------| List If it is a collection that implements the List interface Class, with the characteristics: orderly, repeatable. 
-------------------| ArrayList The bottom layer of ArrayList is to maintain an Object array implementation. Features: Fast query speed, slow addition and deletion.
-------------------| LinkedList The bottom layer of LinkedList is implemented by using the linked list data structure. Features: Slow query speed, fast addition and deletion.
-------------------| Vector (just understand) The bottom layer is also implemented by maintaining an array of Objects. The implementation is the same as ArrayList, but Vector is thread-safe. , the operation efficiency is low.


----------------| Set If it is a collection class that implements the Set interface, it has the following characteristics: Unordered and non-repeatable.
-------------------| The bottom layer of HashSet is supported by a hash table. Features: Fast access speed. 
----------- --------| TreeSet If the elements have the characteristics of natural order, they are sorted and stored according to the characteristics of the natural order of elements.


Linkedlist-specific methods:
1: Method introduction
           addFirst (E and) 
addLast(E e) 

getFirst() 
getLast() 

removeFirst() 
removeLast() 

2: Data structure
1: Stack (1.6): It is mainly used to implement the storage method of the stack data structure.
first in last out
push() 
pop()
2: Queue (double-ended queue 1.5): Mainly so that you can use LinkedList to simulate the storage method of queue data structure.
first in first out
offer()
poll()

3: Return the iterator object in reverse order      

descendingIterator() returns the iterator object in reverse order

import java.util.Iterator;
import java.util.LinkedList;
public class Demo8 {
	
	public static void main(String[] args) {
		LinkedList list= new LinkedList();
		list.add("张三");
		list.add("李四");
		list.add("王五");
/*
		list.addFirst("狗娃"); //把元素添加到集合的首位置上。
		list.addLast("狗剩");  //把元素添加到集合的末尾处。
		

		System.out.println("获取集合中首位置的元素:"+list.getFirst());
		System.out.println("获取集合中末尾的元素:"+ list.getLast());
	
		System.out.println("删除集合中的首位置元素并返回:"+ list.removeFirst());
		System.out.println("删除集合中的末尾素并返回:"+ list.removeLast());
			
		
		list.push("狗娃");   //将该元素插入此集合的开头处。 
		System.out.println("删除集合的首元素:"+list.pop()); // 移除并返回集合中的第一个元素 

		
		list.offer("狗剩");
		System.out.println("删除集合的首元素: "+list.poll());
	
		System.out.println("集合中的元素:"+ list);
		*/	
		Iterator  it = list.descendingIterator();
		while(it.hasNext()){
			System.out.println(it.next());
		}
		
		
	}

}

Running results:
Wang 5
Li Si
Zhang




3 1: Stack (1.6): It is mainly used to realize the storage method of the stack data structure.
first in last out
push() 
pop()
2: Queue (double-ended queue 1.5): Mainly so that you can use LinkedList to simulate the storage method of queue data structure.
first in first out
offer()
poll()

Computer test questions: Use LinkedList to realize the storage method of the stack data structure and the data structure storage method of the queue. Use LinkedList to simulate stack data structure storage



import java.util.LinkedList;
class StackList{
	
	LinkedList list;
	
	public StackList(){
		list = new LinkedList();
	}
	
	//进栈
	public void add(Object o){
		list.push(o);
	}
	
	//弹栈 : 把元素删除并返回。
	public Object pop(){
		return list.pop();
	} 
	
	//获取元素个数
	public int size(){
		return list.size();
	}
	
}

//使用LinkedList模拟队列的存储方式
class TeamList{
	
	LinkedList list;
	
	public TeamList(){
		list = new LinkedList();
	}
	
	public void add(Object o){
		list.offer(o);
	}
	
	public Object remove(){
		return list.poll();
	}
	
	//获取元素个数
	public int size(){
		return list.size();
	}
	
}



public class Demo9 {
	
	public static void main(String[] args) {
		TeamList list=  new TeamList();
		list.add("李嘉诚");
		list.add("马云");
		list.add("王健林");
		
		int size = list.size();
		for(int i = 0 ; i<size ; i++){
			System.out.println(list.remove());
		}
		
		
	}
}



Results:
Li Ka-shing ,
Ma Yun ,
Wang Jianlin


import java.util.LinkedList;


class Person{
	
	String name;
	
	int age;

	public Person(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	
	
	@Override 
	public String toString() {
		return "{ 名字:"+ this.name+" 年龄:"+ this.age+"}";
	}
	
}


public class Demo3 {

	public static void main(String[] args) {
		LinkedList list = new LinkedList();
		list.add(new Person("狗娃", 7));
		list.add(new Person("狗剩", 17));
		list.add(new Person("铁蛋", 3));
		list.add(new Person("美美", 30));
		
		//编写一个函数根据人的年龄及逆行排序存储。
		for(int i= 0 ; i<list.size() -1 ; i++){
			for(int j = i+1 ; j<list.size() ; j++){
				//符合条件交换位置
				Person p1 = (Person) list.get(i);
				Person p2 = (Person) list.get(j);
				if(p1.age>p2.age){
					//交换位置
					list.set(i, p2);
					list.set(j, p1);
					
				}
			}
		}
		System.out.println(list);
		
		
		
	}
	
	
}


Result:
[{Name: Tiedan Age: 3}, {Name: Gouwa Age: 7}, {Name: Gougi Age: 17}, {Name: Meimei Age: 30}]








Requirement: Use LinkedList to store a pair Playing cards, and then implement the shuffling function.


import java.util.LinkedList;
import java.util.Random;

/*
需求: 使用LinkedList存储一副扑克牌,然后实现洗牌功能。



*/
//扑克类
class Poker{
	
	String  color; //花色
	
	String num;	//点数

	public Poker(String color, String num) {
		super();
		this.color = color;
		this.num = num;
	}

	
	@Override
	public String toString() {
		return "{"+color+num+"}";
	}
}

public class Demo2 {
	
	public static void main(String[] args) {
		LinkedList pokers = createPoker();
		shufflePoker(pokers);
		showPoker(pokers);
	}
	
	
	//洗牌的功能
	public static void shufflePoker(LinkedList pokers){
		//创建随机数对象
		Random random = new Random();
		for(int i = 0 ; i <100; i++){ 
			//随机产生两个索引值
			int index1 = random.nextInt(pokers.size());
			int index2 = random.nextInt(pokers.size());
			//根据索引值取出两张牌,然后交换两张牌的顺序
			Poker poker1 = (Poker) pokers.get(index1);
			Poker poker2 = (Poker) pokers.get(index2);
			pokers.set(index1, poker2);
			pokers.set(index2, poker1);
		}
		
	}
	
	
	
	//显示扑克牌
	public static void showPoker(LinkedList pokers){
		for(int i = 0 ; i<pokers.size() ; i++){
			System.out.print(pokers.get(i));
			//换行
			if(i%10==9){
				System.out.println();
			}
		}
	
	}
	
	
	
	
	//生成扑克牌的方法
	public static LinkedList createPoker(){
		//该集合用于存储扑克对象。
		LinkedList list = new LinkedList();		
		//定义数组存储所有的花色与点数
		String[] colors = {"黑桃","红桃","梅花","方块"};
		String[] nums = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
		for(int i = 0 ; i < nums.length ; i++){
			for(int j = 0 ; j<colors.length ; j++){
				list.add(new Poker(colors[j], nums[i]));
			}
		}
		return list;
	}
	
	
}



运行结果:
{黑桃Q}{红桃K}{方块3}{红桃10}{梅花A}{梅花6}{红桃4}{红桃8}{黑桃10}{梅花9}
{黑桃2}{方块J}{红桃5}{梅花2}{梅花7}{黑桃A}{黑桃K}{梅花K}{红桃A}{黑桃6}
{方块2}{黑桃4}{红桃2}{梅花5}{梅花J}{方块4}{方块K}{红桃6}{黑桃5}{黑桃3}
{方块9}{红桃7}{红桃J}{红桃9}{红桃3}{红桃Q}{梅花4}{黑桃J}{方块A}{方块5}
{方块6}{黑桃7}{方块8}{梅花8}{方块10}{方块Q}{梅花Q}{梅花10}{梅花3}{黑桃8}
{黑桃9}{方块7}














Guess you like

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