Single Linked List Part : 2


class Node{

Object data;

Node next;

}

 

public class List{

Node head;

public List(){

head = null;

}

 

public void InsertFirst(Object o){

Node nn = new Node();

nn.data = o;

nn.next = head;

head = nn;

}

 

public Node FindLastNode(){

Node n=head;

if (n!=null){

while (n.next!=null){

n=n.next;

}

}

return n;

}

 

public void InsertLast(Object o){

if (head==null){

InsertFirst(o);

}

else{

Node n=FindLastNode();

Node new_n= new Node();

new_n.data=o;

new_n.next=null;

n.next=new_n;

}

}

 

public void print(){

System.out.println(“Printing the Single Linked List “);

Node n = head;

while (n!= null){

System.out.println(n.data);

n = n.next;

}

}

public static void main(String[] args){

List L = new List();

L.InsertFirst(1);

L.InsertFirst(2);

L.InsertFirst(3);

L.print();

}

}

 

Output program sebagai berikut :

Printing the Single Linked List

3

2

1

Process completed.

Beli T-Shirt, Raglan dan Kids T-Shirt Original Ocean Seven

  1. T-Shirt, Raglan dan Kids T-Shirt Anime, Manga, Gamers dan Cartoon World (OceanSeven)
  2. T-Shirt, Raglan dan Kids T-Shirt Footbal Series (OceanSeven)
  3. T-Shirt, Raglan dan Kids T-Shirt Music Series (OceanSeven)
  4. T-Shirt, Raglan dan Kids T-Shirt OceanSeven – Custom dan Basic Tees (OceanSeven)
  5. T-Shirt, Raglan dan Kids T-Shirt OceanSeven -Kids Collections (OceanSeven)
  6. T-Shirt, Raglan dan Kids T-Shirt OceanSeven -Signature Serues (OceanSeven)
  7. T-Shirt, Raglan dan Kids T-Shirt  Social Media dan Photography (OceanSeven)
  8. T-Shirt, Raglan dan Kids T-Shirt Sport, Automotive dan Adventure (OceanSeven)
  9. T-Shirt, Raglan dan Kids T-Shirt Superheroes dan Movies (OceanSeven)

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s