您的位置:首页 > 编程语言 > Java开发

【DataStructure&AlgorithmInJava】Ch05-LinkedListDemo1

2014-05-07 23:44 453 查看
linkedlist分析步骤-

1.画出链表图;

2.给每个Node分配具体reference(eg. 0x0012)

3.first, current.next 均代入地址值写code不容易出错

Note:

current Node均在块语句的最后update。

class Node{
int data;
Node next;
Node(int d){
this.data=d;
}
public void displayNode(){
System.out.println("data="+this.data);
}
}

class LinkedList{
Node first;
LinkedList(){
first=null;
}
public boolean isEmpty(){
return first==null;
}
void insertFirst(int data){
Node newNode=new Node(data);
newNode.next=first;
first=newNode;
}
Node deleteFirst(){ //to clear this part,PLZ assign 0x0012 to "next box"
Node temp =first;
first=first.next;
return temp;
}
void displayLinkedList(){
Node current = first;
while(current!=null)
{
current.displayNode();
current=current.next;
}
}
}
class LinkedListDemo{
public static void main(String[] args)
{
LinkedList ll=new LinkedList();

ll.insertFirst(11);
ll.insertFirst(22);
ll.insertFirst(33);
ll.insertFirst(44);
ll.displayLinkedList();

System.out.println("=========");
ll.deleteFirst();
ll.displayLinkedList();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: