الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

تصحيح برنامج

بدأه الطالبه بنان في 13 أبريل 2011 · 3 رد · 563 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكمورحمة الله وبركاته

عندي أستفسار بخصوص 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

410602433.gif
#2

لا اعلم ان كنت قد فهمت المطلوب بصورة صحيحة ام لا ولكني قمت بتعديل الكود بحيث ان المستخدم يختار قيمة العنصر الذي يريد ان يدخل عنصراً جديدا بعده

ان كان هذا المطلوب ف الكود التالي يفي بالغرض:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package arabteam;

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
	}

	/**
 	*  make the last entry the first node in the list.
 	* @param id
 	*/
	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 void insertAfter(int value,int cd) {
    	// (assumes non-empty list)


     	// assume that the key is already found.
    	Link current = find(cd);
    	Link newLink = new Link(value);   // make new link


        	//newLink.next = current.next; // newLink --> old next
      	//  // newLink <-- old next


        	//(current.next).previous = newLink;
        	newLink.next=(current.next);
        	current.next = 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("");
//    	System.out.println(After.iData);
	}
// -------------------------------------------------------------
}  // 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,x);



    	theList.displayList();
    	// display list
	}  // end main()
}  // end class LinkList2App
/////////////////

ارجو ان يكون ذلك كافياً

تحياتي

تم تعديل هذه المشاركة بواسطة Mina Fouad في 13 أبريل 2011 في 13:43

2

--

Mina Fouad

Computer & Systems Engineering Dpt.

Faculty of Engineering

Alexandria University

#3

الف الف الف شكر :)

410602433.gif

مواضيع مشابهة