数据结构之链表
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据结构之链表相关的知识,希望对你有一定的参考价值。
一、链表的简单实现
public class Link
{
public int iData;
public double dData;
public Link next;
public Link(int id,double dd)
{
iData = id;
dData = dd;
}
public void displayLink()
{
System.out.println(iData + "," + dData);
}
}
class LinkList
{
private Link first;
public LinkList()
{
first = null;
}
public boolean isEmpty()
{
return first == null;
}
public void insert(int id,double dd)
{
Link newLink = new Link(id, dd);
newLink.next = first;
first = newLink;
}
public Link deleteLink()
{
Link temp = first;
first = first.next;
return temp;
}
public void displayList()
{
System.out.println("List first-->last: ");
Link current = first;
while(current != null)
{
current.displayLink();
current = current.next;
}
System.err.println("");
}
}
二、双向链表
/**
* 双向链表
*
* @author yangyc
*/
public class DoubleLink
{
public long dData;
public DoubleLink next;
public DoubleLink previous;
public DoubleLink(long j)
{
dData = j;
}
public void displayLink()
{
System.out.println(dData + " ");
}
}
class DoubleLinkedList
{
private DoubleLink first;
private DoubleLink last;
public DoubleLinkedList ()
{
first = null;
last = null;
}
public boolean isEmpty()
{
return first == null;
}
public void insertFirst(long dd)
{
DoubleLink newDoubleLink = new DoubleLink(dd);
if(isEmpty())
last = newDoubleLink;
else
first.previous = newDoubleLink;
newDoubleLink.next = first;
first = newDoubleLink;
}
public void insertLast(long dd)
{
DoubleLink newDoubleLink = new DoubleLink(dd);
if(isEmpty())
first = newDoubleLink;
else
{
last.next = newDoubleLink;
newDoubleLink.previous = last;
}
last = newDoubleLink;
}
public DoubleLink deleteFirst()
{
DoubleLink temp = first;
if(first.next == null)
last = null;
else
first.next.previous = null;
first = first.next;
return temp;
}
public DoubleLink deleteLast()
{
DoubleLink temp = last;
if(last.previous == null)
first = null;
else
last.previous.next = null;
last = last.previous;
return temp;
}
}
以上是关于数据结构之链表的主要内容,如果未能解决你的问题,请参考以下文章