0
對於noob問題抱歉,但是這裏的語法有點混亂。我被要求填寫函數以將新元素插入到鏈表的前面。代碼:使用數據創建新的頭像列表
class LinkedList
{
public LinkedList()
{
this.head = null;
}
/* returns true, if the list is empty, else false */
public boolean isEmpty()
{
return head == null;
}
/* returns the first element of the list, assuming that the list is not empty */
public int front()
{
return head.data;
}
/* prints the list */
public void print()
{
System.out.print("(");
for (ListNode i = head; i != null; i = i.next)
System.out.print(i.data + " ");
System.out.println(")");
}
/* inserts x into the beginning of the list */
public void insertFront (int x)
{
/* FILL HERE!!! */
}
}
爲節點的代碼:
class ListNode
{
public ListNode()
{
this.data = 0;
this.next = null;
}
public int data;
public ListNode next;
}
我想我需要創建一個新的節點,指定當前頭爲未來運營商的價值,並設置相同的值到x。然後最後將節點設置爲新的頭部。
有人可以告訴我如何執行這些基本命令。