الــــــــــــــسلام عليكم و رحمـــــــــــة الله و بركاته
هذا برنامج لإضافة نود من البدايه و النهايه
سؤالي هو كيف أضيف كود لهذا البرنامج بحيث يضيف نود آخر و لكن في الوسط
هذا البرنامج
class IntSLLNode {
public int info;
public IntSLLNode next=null;
public IntSLLNode()
{
}
public IntSLLNode(int el)
{
info=el;
}
public IntSLLNode(int el,IntSLLNode ref)
{
info=el; next=ref;
}
}
class IntSLList
{
protected IntSLLNode head,tail;
public IntSLList()
{
head=tail=null;
}
public boolean isEmpty()
{
return head==null;
}
public void addToHead(int el)
{
head=new IntSLLNode(el,head);
if(tail==null)
tail=head;
}
public void addToTail(int el)
{
if(!isEmpty())
{
tail.next=new IntSLLNode(el);
tail=tail.next;
}
else head=tail=new IntSLLNode(el);
}
public void printAll()
{
for(IntSLLNode tmp=head;tmp!=null;tmp=tmp.next)
System.out.print(tmp.info+" ");
}
}
class IntSLListMain
{
public static void main(String args[])
{
System.out.println("The Linked List ....");
IntSLList l=new IntSLList();
l.printAll();
System.out.println("The Linked after add to head & tail ...");
l.addToHead(7);
l.addToTail(5);
l.addToHead(3);
l.printAll();
}
}