您的位置:首页 > 理论基础 > 数据结构算法

数据结构-有序链表(基于简单链表)

2012-11-22 17:41 429 查看
/*
* 文 件 名:  OrdLinkList.java
* 描    述:  有序链表
* 修改时间:  2012-11-22
*/
package linklist;

/**
* 有序链表,按照关键字升序
*/
public class OrdLinkList
{
private Link first;

public OrdLinkList()
{
first = null;
}

/**
* 判断链表是否为空
*
* @return
*/
public boolean isEmpty()
{
return first == null;
}

/**
* 按照整型key升序插入元素,规则为大于前一个元素,小于或者等于后一个元素
* @see [类、类#方法、类#成员]
*/
public void insertByAscend(int iData, double dData)
{
Link link = new Link(iData, dData);
//如果链表为空,或键值比表头还小也做表头
if (isEmpty() || iData <= first.iData)
{
link.next = first;
first = link;
}
else
{
Link current = first;
Link next = current.next;
while (iData > current.iData)
{
if (next == null || iData <= next.iData)
{
link.next = current.next;
current.next = link;
break;
}
current = next;
next = current.next;
}
}
}

/**
* 遍历链表
*/
public void displayList()
{
Link current = first;
while (current != null)
{
current.displayLink();
current = current.next;
}
}

/**
* 在链表头部删除元素
*
* @return
* @throws EmptyLinkListException
*/
public Link deleteFirst()
throws EmptyLinkListException
{
if (isEmpty())
{
throw new EmptyLinkListException();
}
else
{
Link temp = first;
first = first.next;
return temp;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: