算法与数据结构【Java】:自组织链表

由于链表中,在某一时间段内每个元素使用的频率不同,所以,依赖于这种特点,产生了自组织链表,来提高链表的查找效率。
自组织链表可以动态的组织链表,有很多种方法,这里列举4种:
    1、前移法:找到需要的元素之后,将它放到链表开头
    2、换位法:找到需要的元素之后,将它和前驱交换位置
    3、计数法:在结点中记录被访问的次数,根据被访问的次数,对链表进行排序
    4、排序法:根据链表结点本身的属性进行排序
以前移法为例,在应用访问数据库时,通常是某个用户对与其相关的信息进行频繁的操作。这样,采用前移法时,就能将正在被访问的元素放在链表开头,大大提高了查找效率。

下面以前移法为例,实现一个自组织链表。
该实现在普通单向链表的代码上进行添加和修改,直接实现了前移法。

package autoOrganizedList;

public class LinkedList {
	
	public int length = 0;		//链表长度,该属性不重要,下面的方法中也没有用到,但是维护了该属性
	public Node head = null;		//链表头节点的指针
	public Node tail = null;		//链表尾节点的指针
	LinkedList(){
		
	}
	
	int isEmpty(){			//判断链表是否为空,头指针为0代表空
		return head == null?1:0;
	}

	//向链表头添加数据
	void addToHead(int aValue){
		head = new Node(aValue, head);	//添加节点
		if (tail == null) tail = head;		//如果尾指针为空,说明链表本身为空,更新尾指针
		length++;	
	}

	//向链表尾添加数据
	void addToTail(int aValue){
		//如果尾指针为空,说明链表本身为空,更新尾指针和头指针
		if (tail == null) head = tail = new Node(aValue);	
		//否则只更新尾指针
		else tail = tail.next = new Node(aValue);
		length++;
	}

	//从链表头删除数据
	int deleteFromHead(){	
		//如果链表为空,无法删除,返回-1
		if (head == null) return -1;
		//记录被删除的值
		int deletedValue = head.value;
		if (head == tail)	//如果链表只有一个结点,那么删除后头和尾都为空
			head = tail = null;
		else 
			head = head.next;
		length--;
		return deletedValue;	//返回被删除的值
	}
	
	int deleteFromTail(){
		//如果链表为空,无法删除,返回-1
		if (head == null) return -1;
		//记录被删除的值
		int deletedValue = tail.value;
		if (head == tail)	//如果链表只有一个结点,那么删除后头和尾都为空
			head = tail = null; 
		else {
			Node now = null;
			for(now=head; now.next!=tail; now=now.next);		//遍历链表,找到倒数第二个结点
			now.next = null;
			tail = now;
		}
		length--; 
		return deletedValue;	//返回被删除的值
	}
	
	int deleteNode(int index){
		if (index <= 0) return -1;
		//如果链表为空,无法删除,返回-1
		if (head == null) return -1;
		Node now = null;
		int cnt = 1;	//计数器
		/*
			下面的循环作用:
					情况1:正常情况下,找到要删除的结点的前一个结点;
					情况2:如果要删除的结点序号大于总结点数,使得找到的值停止在NULL
		*/
		for(now=head; cnt+1<index && now!=null; cnt++,now=now.next);

		if(now == null) return -1; 	//情况2发生,直接返回
		//记录被删除的结点指针和值
		Node deletedNode = now.next;
		int deletedValue = deletedNode.value;
		
		//情况1:将该结点删除
		if (cnt == index-1)
			now.next = now.next.next;
		length--;
		//如果末尾结点被删除,更新tail
		if (deletedNode.next == null){
			tail = now;
		}	
		return deletedValue;
	}

	//向指定序号处插入结点
	void addNode(int value, int index){	//index starts from 1
		if (index <= 0) return;
		//处理链表为空插入结点的情况
		if (head == null && index == 1)	{
			head = tail = new Node(value);
			length++;
			return ;
		}
		//处理在链表头插入数据的情况
		if (index == 1){
			head = new Node(value, head);
			length++;
			return ;
		}
		Node aheadOfAdd = null;
		int cnt = 1;
		//循环找到要插入的结点之前的那个结点
		for (aheadOfAdd=head,cnt=1; aheadOfAdd!=null&&cnt+1<index; cnt++,aheadOfAdd=aheadOfAdd.next);
		//如果以上循环因为条件“cnt+1<index”不成立而终止,则因为要插入的结点序号超过了链表总大小
		if (index != cnt+1)
			return ;
		if (aheadOfAdd == null) 
			return ;

		aheadOfAdd.next = new Node(value, aheadOfAdd.next);
		length++;
		//如果向尾部插入了结点,更新tail
		if (aheadOfAdd.next.next == null) tail = aheadOfAdd.next;
	}
	

	
	void printSelf(){	//打印链表内容
		Node nodeToPrint = this.head;
		System.out.println("Content of LinkedList:");
		while(nodeToPrint != null){
			System.out.print(String.format("%d ", nodeToPrint.value));
			if(nodeToPrint == tail)
				System.out.print("tail is reached!\n");
			nodeToPrint = nodeToPrint.next;
		}	
		System.out.print("\n");
	}
	
	//判断链表是否包含某个值
	Node contains(int value){
		for(Node now=head; now!=null; now=now.next)
			if (now.value == value)
				return now;
		return null;
	}
	
	int nodeIndex(int value){
		int cnt=0;
		Node now=null;
		if (head == null) return -1;
		for (cnt=1,now=head; now!=null && now.value!=value; now=now.next,cnt++);
		return cnt;
	}
	Node search(int value){
		Node now = contains(value);
		if (now == null)	return null;
		deleteNode(nodeIndex(value));
		addToHead(value);
		return head;
	}
	
	
	static public void main(String[] argv) {
		LinkedList list = new LinkedList();System.out.print(String.format("\t\t%d\n", list.length));
		list.addToHead(1);list.printSelf();System.out.print(String.format("\t\t%d\n", list.length));
		list.addToHead(2);list.printSelf();System.out.print(String.format("\t\t%d\n", list.length));
		list.addToHead(3);list.printSelf();System.out.print(String.format("\t\t%d\n", list.length));
		list.addToHead(4);list.printSelf();System.out.print(String.format("\t\t%d\n", list.length));
		list.addToTail(100);list.printSelf();System.out.print(String.format("\t\t%d\n", list.length));
		list.addToTail(101);list.printSelf();System.out.print(String.format("\t\t%d\n", list.length));
		list.addToTail(102);list.printSelf();System.out.print(String.format("\t\t%d\n", list.length));
		list.addToTail(103);list.printSelf();System.out.print(String.format("\t\t%d\n", list.length));
		//list.deleteNode(8);list.printSelf();System.out.print(String.format("\t\t%d\n", list.length));
		list.search(103);list.printSelf();System.out.print(String.format("\t\t%d\n", list.length));
		System.out.println(list.contains(21323)==null?0:1);
		
		
	}
	
}
class Node{
	public int value;	//存储节点的值 
	Node next;	//存储下一个节点的指针
	Node(int aValue){
		this.value = aValue;
		this.next = null;
	}
	Node(int aValue, Node aNext){	//构造函数,必须传入结点的值,下一个节点默认为NULL
		this.value = aValue;
		this.next = aNext;
	}	
};
发布了86 篇原创文章 · 获赞 56 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/WilliamCode/article/details/104087891