简单数据结构实现——双链表

链表 (Linked List),基本数据结构之一,是一种逻辑上的线性表,但是物理存储上是非线性的。在存储空间上分散排布,通过构造节点,以及节点中的前驱后驱指针来串联起所有的节点,顾名曰链表。这是一种随用随建的结构,删除起来也非常方便。链表的强项在于插入删除操作,但是链表的遍历是弱项,不能做到随机遍历,只能从头到尾顺序遍历。链表的还有一个好处是大小没有任何限制,java中自带的集合 ArrayList 就是一种链表结构(应该是单链表结构)。

在后面即将给出的实现,是双链表。即每个节点中即有一个前驱,也有一个后继,可以向后找到后继节点,也可以向前找到前驱节点。

单链表比双链表更简单,实现情形相差不大。

循环链表是头尾相接的链表,实现难度不会很大,有一些边界情况需要考虑,留待以后有兴趣的时候来实现。

如C语言等支持指针的语言中,链表一般使用指针来实现的,但是在java中的情况更简单,直接构造一个Node类(等于C中的结构体)来模拟节点,Node类中有两个Node型的变量:head和tail分别标明头尾节点。引入这两个虚拟节点可以消除很多特殊情况的处理,大大简化编程。还有一个属性data,即这个节点中真正有用的数据内容。

Node类可以考虑作为Linked List类的一个静态内部类。作为内部类,是因为这个Node类是强烈附属于链表类的,而且一般外部用户也理应对节点类没有兴趣。用static静态修饰是因为创建一个节点的时候可以不需要一个链表类的实例。

加入节点的方法add(AnyType x), add(int idx, AnyType x) 都将最终调用 addBefore(Node<AnyType> node, AnyType x)来解决。

剩下的一些细节看具体代码。


public class MyLinkedList<AnyType> implements Iterable<AnyType>{
	private Node<AnyType> head=new Node<AnyType>(null, null, null);
	private Node<AnyType> tail=new Node<AnyType>(null, null, null);
	private int size=0;
	
	private static class Node<AnyType>{
		AnyType data;
		Node<AnyType> prev;
		Node<AnyType> next;
		
		public Node(AnyType d, Node<AnyType> p, Node<AnyType> n){
			data=d;
			prev=p;
			next=n;
		}
	}
	
	public MyLinkedList(){
		head.next=tail;
		tail.prev=head;
	}
	
	public boolean isEmpty(){
		return size==0;
	}
	
	public int size(){
		return size;
	}
	
	public void clear(){
		head.next=tail;
		tail.prev=head;
		size=0;
	}
	
	public boolean add(AnyType x){
		add(size(),x);
		return true;
	}
	
	public void add(int idx, AnyType x){
		addBefore(getNode(idx),x);
	}
	
	public void addBefore(Node<AnyType> node, AnyType x){
		Node<AnyType> newNode=new Node<AnyType>(x,node.prev,node);
		newNode.prev.next=newNode;
		node.prev=newNode;
		size++;
	}
	
	//This will return the data of the node at position index.
	public AnyType get(int index){
		return getNode(index).data;
	}
	
	//Change the data of node at index, the old data will be returned.
	public AnyType set(int index, AnyType newVal){
		Node<AnyType> node=getNode(index);
		AnyType old=node.data;
		node.data=newVal;
		return old;
	}
	
	//Old data will be returned.
	public AnyType remove(int index){
		return remove(getNode(index));
	}
	
	public AnyType remove(Node<AnyType> node){
		AnyType old=node.data;
		node.prev.next=node.next;
		node.next.prev=node.prev;
		size--;
		return old;
	}

	public boolean isContained(AnyType x){
		boolean found=false;
		for(int i=0;i<size();i++){
			if(x==getNode(i).data){
				found=true;
				break;
			}
		}		
		return found;
	}
	
	public int getIndex(AnyType value){
		if(isContained(value)){
			for(int i=0;i<size();i++){
				 if(value==getNode(i).data){
					return i;
				}
			}
			
			return Integer.MIN_VALUE;
		}
		else return Integer.MIN_VALUE;
	}
	
	//This is a private method because external users shouldn't be interested in the concept of nodes.
	//This will return the node at position index, not the data.
	private Node<AnyType> getNode(int index){
	/*/	This is my solution, but it's not good enough.
		Used below if the solution from a book.
		It's better because it throws exception,
		It can also search from the tail to he half center to accelerate the preocess.
	*/	
		if(index<0||index>size()) throw new IndexOutOfBoundsException();
		else{
			Node node=head.next;
			for(int i=0;i<index;i++){
				node=node.next;
			}
			return node;
		}
	
	/*
		Node<AnyType> p;
		if(index<0||index>size()) throw new IndexOutOfBoundsException();
		
		if(index<size()/2){
			p=head;
			for(int i=0;i<index;i++)
				p=p.next;
		}
		else{
			p=tail;
			for(int i=size();i>index;i--)
				p=p.prev;
		}
		return p;
	*/
	}
	
	public java.util.Iterator<AnyType> iterator(){
		return null;
	}
	
	public String toString(){
		String str="";
		str+="[";
		for(int i=0;i<size();i++){
			str+=" "+getNode(i).data.toString();
		}
		str+=" ]";
		return str;
	}
	
	public static void main(String[] args){
		MyLinkedList<Integer> ll=new MyLinkedList<Integer>();
		
		ll.add(3);
		ll.add(4);
		ll.add(3);
		ll.add(100);
		ll.remove(9);
		System.out.println(ll);
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值