双向链表

双向链表:双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点

package com.dataStructure.linearTable.doubleLinearTable;

public class Student {
	private int id;
	private String name;
	private int score;

	public Student() {

	}
	public Student(int id, String name, int score) {
		this.id = id;
		this.name = name;
		this.score = score;
	}

	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getScore() {
		return score;
	}
	public void setScore(int score) {
		this.score = score;
	}
	
}

 

package com.dataStructure.linearTable.doubleLinearTable;

public class DoubleLinkList {
	private DoubleLinkList previous = null;
	private Student student;
	private DoubleLinkList next = null;
	
	public DoubleLinkList() {
		
	}

	public DoubleLinkList(Student student) {
		this.student = student;
	}

	public DoubleLinkList getPrevious() {
		return previous;
	}
	public void setPrevious(DoubleLinkList previous) {
		this.previous = previous;
	}
	public Student getStudent() {
		return student;
	}
	public void setStudent(Student student) {
		this.student = student;
	}
	public DoubleLinkList getNext() {
		return next;
	}
	public void setNext(DoubleLinkList next) {
		this.next = next;
	}
	
	
	
}

 

package com.dataStructure.linearTable.doubleLinearTable;

public class Test {
	public static void main(String[] args) {
		//创建头节点
		DoubleLinkList head = new DoubleLinkList();
		Student student1 = new Student(1,"Nicky Zhang",147);
		Student student2 = new Student(2,"Alice Xu",121);
		Student student3 = new Student(5,"Avril Lavigne",99);
		Student student4 = new Student(4,"Britney Spears",127);
		Student student5 = new Student(3,"Whitney Houston",150);
		DoubleLinkList item1 = new DoubleLinkList(student1);
		DoubleLinkList item2 = new DoubleLinkList(student2);
		DoubleLinkList item3 = new DoubleLinkList(student3);
		DoubleLinkList item4 = new DoubleLinkList(student4);
		DoubleLinkList item5 = new DoubleLinkList(student5);
		add(head, item1);
		add(head, item2);
		add(head, item3);
		add(head, item4);
		add(head, item5);
		remove(head, item1);
		remove(head, item3);
		getAll(head);
	}
	/**
	 * add item
	 * @param head
	 * @param item
	 */
	private static void add(DoubleLinkList head, DoubleLinkList item){
		if(head == null || item == null){
			return;
		}
		//头结点 不能动
		DoubleLinkList current = head;
		//check current element if exists
		boolean flag = false;
		//check current linkList is Empty
		if(current.getNext() == null){
			current.setNext(item);
			item.setPrevious(current);
		}else{
			//如果不是空节点,按照顺序来添加
			//寻找添加位置
			while(current.getNext() != null){
				if(current.getNext().getStudent().getId() > item.getStudent().getId()){
					break;
				}
				if(current.getNext().getStudent().getId() == item.getStudent().getId()){
					flag = true;
				}
				current = current.getNext();
			}
			if(!flag){
				item.setNext(current.getNext());
				item.setPrevious(current);
				current.setNext(item);
				if(item.getNext() != null){
					item.getNext().setPrevious(item);
				}
			}
		}
	}
	/**
	 * remove item
	 * @param head
	 * @param item
	 */
	private static void remove(DoubleLinkList head, DoubleLinkList item){
		if(head == null || item == null){
			return;
		}
		DoubleLinkList current = head.getNext();

		boolean flag = false;
		while(current != null){
			if(current.getStudent().getId() == item.getStudent().getId()){
				flag = true;
				break;
			}
			current = current.getNext();
		}
		if(flag){
			if(current.getNext() != null){
				current.getNext().setPrevious(current.getPrevious());
			}
			current.getPrevious().setNext(current.getNext());
		}
	}
	/**
	 * Query All Elements
	 * @param head
	 */
	private static void getAll(DoubleLinkList head){
		DoubleLinkList current = head;
		while(current.getNext() != null){
			System.out.println("ID :"+current.getNext().getStudent().getId()+" Name :"+current.getNext().getStudent().getName());
			current = current.getNext();
		}
	}
}

 

你可能感兴趣的:(双向链表)