码迷,mamicode.com
首页 > 编程语言 > 详细

学习java数据结构基础知识之链表

时间:2015-05-14 18:45:19      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:数据结构   java   java数据结构   链表   数据   

public class Link {
	public int iData;
	public double dData;
	public Link next;
	
	
	public Link(int iData, double dData) {
		super();
		this.iData = iData;
		this.dData = dData;
	}
	
	public void displayLink(){
		System.out.print("{"+iData+","+dData +"}");
	}
	
}


public class LinkList {
	private Link first;

	public void LinkList() {
		first =null;
	}
	//链表是否为空
	public boolean isEmpty(){
		return (first ==null);
	}
	//插入first头元素
	public void  insertFirst(int id,double dd) {
		Link newLink =new Link(id, dd);
		newLink.next =first;
		first =newLink;
	}
	//删除链表头元素
	public Link deleteFirst(){
		Link tempLink =first;
		first =first.next;
		return tempLink;
	}
	//找到特定节点
	public Link find(int key){
		Link current =first;
		while (current.iData !=key) {
			if(current.next ==null){
				return null;
			}else {
				current =current.next;
			}
		}
		return current;
	}
	
	//删除节点
	public Link delete(int key){
		Link currentLink =first;
		Link previous=first;
		while (currentLink.iData !=key) {
			if (currentLink.next ==null) {
				return null;
			}else {
				previous =currentLink;
				currentLink =currentLink.next;
			}
		}		
		if (currentLink ==first) {
			first =first.next;
		}else{
			previous.next =currentLink.next;
		}
		
		return currentLink;
	}
	//插入特定节点
	public Boolean insertLink(int key ,int iData, double dData){
		int i=0;
		Link currentLink =first;
		Link previous =first;
		Link nextLink;
		Link newLink =new Link(iData, dData);
		//插入头元素
		if ((i==key)&&(currentLink!=null)) {
			newLink.next =currentLink;
			first=newLink;
			return true;
		}else if (currentLink!=null){
		//	i++;
			while (i!= (key-1)) {
				if (currentLink.next ==null) {
					currentLink.next=newLink;
					return true;
				}else {
					previous =currentLink;
					currentLink =currentLink.next;
					i++;
				}
			}
			if (currentLink.next !=null) {
				nextLink =currentLink.next;
				currentLink.next=newLink;
				newLink.next=nextLink;
				return true;
			}else {
				currentLink.next =newLink;
				newLink.next =null;
				return true;
			}
		}else if (currentLink ==null) {
		
			currentLink =newLink;
			first=newLink;
			first.next=null;
			return true;
		}
		
		return false;

	}
	
	//显示链表所有数据
	public void displayList(){
		System.out.println("LinkList.displayList() List(first-->last):");
		Link currentLink =first;
		while (currentLink!=null) {
			currentLink.displayLink();
			currentLink =currentLink.next;
		}
		System.out.println("");
		System.out.print("LinkList.displayList() finish");
	}
	
	
}

public class LinkListApp {
	public static void main(String[] args){
		
		LinkList theLinkList =new LinkList();
		
		theLinkList.insertLink(9, 999, 999.999);
		theLinkList.displayList();
		
		theLinkList.insertFirst(22, 22.22);
		theLinkList.insertFirst(33, 33.33);
		theLinkList.insertFirst(44, 44.44);
		theLinkList.insertFirst(55, 55.55);
		theLinkList.displayList();
		
		theLinkList.insertLink(2, 333, 333.333);
		theLinkList.displayList();
		
		theLinkList.insertLink(7, 777, 777.777);
		theLinkList.displayList();
		
		System.out.println("");
		
		while (!theLinkList.isEmpty()) {
			Link aLink =theLinkList.deleteFirst();
			System.out.print("Deleted ");
			aLink.displayLink();
			System.out.println("");
		}
	    theLinkList.displayList();
	}

}


学习java数据结构基础知识之链表

标签:数据结构   java   java数据结构   链表   数据   

原文地址:http://blog.csdn.net/cool_easy/article/details/45723057

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!