السلام عليكم
احاول كتابة كود لل linked list بلغة java ( قمت بكتابة جميع انواع linked list بلغة c++ ) ولا ادري هل الطريقة التي استخدمتها صحيحه او لا وهل يمكن ان تكون افضل ؟
قمت بفصل البرنامج الى ملفين ، الاول linkedList.java ويحتوي على linked list class & Node class والملف الاخر يوجد فيه main function
الملف الاول:
class Node
{
public Node next;
public String data;
Node( String str )
{
data = str;
next = null;
}
}
public class linkedList
{
public Node first;
linkedList()
{
first = null;
}
public void add( String str )
{
Node newNode = new Node( str );
if ( first == null )
{
first = newNode;
}
else
{
Node current = first;
while ( current.next != null )
current = current.next;
current.next = newNode;
}
}
public void print()
{
Node current = first;
if ( current != null )
{
while ( current != null )
{
System.out.println( current.data );
current = current.next;
}
}
else
System.out.println( "List is empty.");
}
}الملف الثاني
public class linkedListTest
{
public static void main( String args[] )
{
linkedList list = new linkedList();
list.add( "Ahmed");
list.add( "Ali");
list.add( "xxxx");
list.add( "gvgvg");
list.print();
}
}هل هناك مكان افضل لوضع Node class ؟؟