السلام عليكمورحمة الله وبركاته
عندي أستفسار بخصوص linked list أقدر أضيف عنصر او قيمة في القائمة بعد عنصر محدد ؟؟ باستخدام simple link?????
في الكود استخدمت ميثود double فهو بيضيف العنصر الي يدخله المستخدم بعد اول عنصر في القائمة
import java.util.Scanner;
class Link
{
public int iData; // data item (key) // data item
public Link next;
public Link previous; // next link in list
// -------------------------------------------------------------
public Link(int id) // constructor
{
iData = id;
}
// -------------------------------------------------------------
public void displayLink() // display ourself
{
System.out.print("{" + iData + "} ");
}
}
// end class Link
class LinkList
{
private Link first;
private Link After;
// ref to first link on list
// -------------------------------------------------------------
public LinkList() // constructor
{
first = null;
After=null; // no links on list yet
}
public void insertFirst(int id)
{ // make new link
Link newLink = new Link(id);
newLink.next = first; // it points to old first link
first = newLink;
// now first points to this
}
public Link find(int key) // find link with given key
{ // (assumes non-empty list)
Link current = first; // start at 'first'
while(current.iData != key) // while no match,
{
if(current.next == null) // if end of list,
return null; // didn't find it
else // not end of list,
current = current.next; // go to next link
}
return current; // found it
}
public Link insertAfter(int cd)
{ // (assumes non-empty list)
Link current = first;
Link newLink = new Link(cd); // make new link
if(current==After) // if last link,
{
newLink.next = null; // newLink --> null
After = newLink; // newLink <-- last
}
else // not last link,
{
newLink.next = current.next; // newLink --> old next
// newLink <-- old next
current.next.previous = newLink;
}
newLink.previous = current; // old current <-- newLink
current.next = newLink; // old current --> newLink
// found it, did insertion
}
public void displayList() // display the list
{
System.out.print("List (first-->last): ");
Link current = first; // start at beginning of list
while(current != null) // until end of list,
{
current.displayLink(); // print data
current = current.next; // move to next link
}
System.out.println("");
}
// -------------------------------------------------------------
} // end class LinkList
class LinkList2App
{
public static void main(String[] args)
{
LinkList theList = new LinkList(); // make list
Scanner input=new Scanner(System.in);
theList.insertFirst(22); // insert 4 items
theList.insertFirst(44);
theList.insertFirst(66);
theList.insertFirst(88);
theList.displayList(); // display list
System.out.println("Enter avalue to insert after");
int x=input.nextInt();
Link f = theList.find(x);
if( f != null)
System.out.println("Found link with key " + f.iData);
else
System.out.println("Can't find link");
System.out.println("Enter avalue to insert after");
int r=input.nextInt();
theList.insertAfter(r) ;
theList.displayList();
// display list
} // end main()
} // end class LinkList2App
/////////////////اقتباسالمشكله :المستخدم يدخل رقم معين مثلا 66 ((هنا يتم اضافة العنصر ))بعد الرقم المدخل من قبل اليوزرويدخل رقم اخر مثلا 34 ((هنا يتم اضافة هذا العنصر بعد رقم 66
