单向链表


单链表:

单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素。链表中的数据是以节点来表示的,每个节点的构成:元素(数据元素的映象) + 指针(指示后继元素存储位置),元素就是存储数据的存储单元,指针就是连接每个节点的地址数据。

package com.dataStructure.linearTable.singleLinearTable;

public class Star {
	private int no;
	private String name;
	private String nickName;
	private Star next = null;//指向堆中下一个Star实例
	
	public Star() {
		super();
	}

	public Star(int no, String name, String nickName) {
		this.no = no;
		this.name = name;
		this.nickName = nickName;
	}

	public int getNo() {
		return no;
	}

	public void setNo(int no) {
		this.no = no;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getNickName() {
		return nickName;
	}

	public void setNickName(String nickName) {
		this.nickName = nickName;
	}

	public Star getNext() {
		return next;
	}

	public void setNext(Star next) {
		this.next = next;
	}
}

 

package com.dataStructure.linearTable.singleLinearTable;

public class Test {
	public static void main(String[] args) {
		//创建一个空的Star作为链表的头
		Star head = new Star();
		Star star1 = new Star(1,"Avril Lavigne","Rock Beauty Girl");

		Star star2 = new Star(10,"Madonna Ciccone","Ray of Light");
		Star star3 = new Star(3,"Beyonce Giselle","Black Dimond");
		Star star4 = new Star(4,"Britney Spears","Sweaty");
		Star star5 = new Star(7,"Whitney Houston","When You Believe");
		addStarBySequence(head, star1);
		addStarBySequence(head, star2);
		addStarBySequence(head, star3);
		addStarBySequence(head, star4);
		addStarBySequence(head, star5);
		showStar(head);
		System.out.println("========================================");
		removeStar(head, star5);
		showStar(head);
		
		
	}
	
	private static void showStar(Star head){
		Star current = head;
		while(current.getNext() != null){
			System.out.println("ID :"+current.getNext().getNo()+"Name :"+current.getNext().getName());
			current = current.getNext();
		}
	}
	//单链表的添加操作
	private static void addStar(Star head, Star star){
		//1)直接在尾部加(找到链表尾部,不能动头节点)
		Star current = head;
		while(null != current.getNext()){//如果当前节点的next不为空
			current = current.getNext();
		}
		//如果为空表示你退出循环,即表示队尾
		current.setNext(star);
		//按照顺序加(确保添加有序)
		
	}
	
	private static void addStarBySequence(Star head, Star star){
		if(head == null){
			return;
		}
		Star current = head;
		while(null != current.getNext()){
			if(current.getNext().getNo() >= star.getNo()){
				break;
			} 
			current = current.getNext();
		}
		if(current.getNext() != null && current.getNext().getNo() >= star.getNo()){
			star.setNext(current.getNext());
		}
		current.setNext(star);
		
	}
	/**
	 * 从链表中删除某个元素
	 * @param head
	 * @param star
	 */
	private static void removeStar(Star head, Star star){
		if(head == null || star == null){
			return;
		}
		Star current = head;
		boolean flag = false;
		while(current.getNext()!= null){
			if(current.getNext().getNo() == star.getNo()){
				flag = true;
				break;
			}
			current = current.getNext();
		}
		if(!flag){
			return;
		}
		//让当前元素直接指向当前元素的下一个元素的下一个元素,中间这个没有引用指向他,被视为垃圾
		current.setNext(current.getNext().getNext());
		
	}
}

 

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