我正在学习数据结构(链接列表) 为链接类创建的引用如下一个,首先分别存储下一个链接和第一个链接?在实现类中,我们只是创建引用而不是定义任何东西? first.next如何移动?如何创建一个引用作为第一个引用列表中的第一个元素?LinkedList下一个的概念
class Link
{
public int iData; // data item (key)
public double dData; // data item
public Link next;// next link in list`
public Link(int id, double dd) // constructor
{
iData = id; // initialize data
dData = dd; // (‘next’ is automatically // set to null)
}`
`public void displayLink() // display ourself
{
System.out.print("{" + iData + ", " + dData + "} ");
}
}
class LinkList
{
private Link first; // ref to first link on list
public LinkList() // constructor
{
first = null; // no items on list yet
}
public boolean isEmpty() // true if list is empty
{
return (first==null);`
}
// insert at start of list
public void insertFirst(int id, double dd)
{ // make new link
Link newLink = new Link(id, dd);
newLink.next = first; // newLink --> old first
first = newLink; // first --> newLink
}
public Link deleteFirst() // delete first item
{ // (assumes list not empty)
Link temp = first; // save reference to link
first = first.next; // delete it: first-->old next
return temp; // return deleted link
}
public void displayList()
{
System.out.print("List (first-->last): ");
Link current = first; // start at beginning of list
while(current != null) // until end of list,
{
current.displayLink(); // print data
current = current.next; // move to next link
}
System.out.println("");
}
}
目前还不清楚你想要解决什么问题。 – Bobulous
我不明白downvote的原因。 @Arkanon ::我只想知道下一个店铺如何来到下一个元素?我们在哪里定义?与第一个相同。 –