/*
* 文 件 名: 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;
}
}
}
数据结构-有序链表(基于简单链表)
最新推荐文章于 2022-05-10 18:26:48 发布