3 תשובות
איך לשרשר את הרשימות?
אנונימית
שואל השאלה:
וואי תודה רבה
וואי תודה רבה
שואל השאלה:
public static node<int> inorder(node<int> list1, node<int> list2)
{
node<int> sorted = null;
// חיבור הרשימות
node<int> last1 = list1;
while (last1.getnext() != null)
{
last1 = last1.getnext();
}
last1.setnext(list2); // חיבור הרשימה השנייה לראשונה
// מיון הרשימה המשותפת
node<int> first1 = list1;
while (first1 != null)
{
node<int> next = first1.getnext();
// אם הרשימה הממוינת ריקה או שהערך של first1 קטן מהערך הנוכחי של sorted
if (sorted == null || sorted.getvalue() >= first1.getvalue())
{
first1.setnext(sorted);
sorted = first1; // עדכון head של הרשימה הממוינת
}
else
{
node<int> temp = sorted;
while (temp.getnext() != null && temp.getnext().getvalue() < first1.getvalue())
{
temp = temp.getnext(); // למצוא את המקום המתאים
}
first1.setnext(temp.getnext()); // חיבור החוליה
temp.setnext(first1); // עדכון החוליה הקודמת להצביע עליה
}
first1 = next; // עדכון למעבר לחוליה הבאה
}
return sorted;
}
public static node<int> inorder(node<int> list1, node<int> list2)
{
node<int> sorted = null;
// חיבור הרשימות
node<int> last1 = list1;
while (last1.getnext() != null)
{
last1 = last1.getnext();
}
last1.setnext(list2); // חיבור הרשימה השנייה לראשונה
// מיון הרשימה המשותפת
node<int> first1 = list1;
while (first1 != null)
{
node<int> next = first1.getnext();
// אם הרשימה הממוינת ריקה או שהערך של first1 קטן מהערך הנוכחי של sorted
if (sorted == null || sorted.getvalue() >= first1.getvalue())
{
first1.setnext(sorted);
sorted = first1; // עדכון head של הרשימה הממוינת
}
else
{
node<int> temp = sorted;
while (temp.getnext() != null && temp.getnext().getvalue() < first1.getvalue())
{
temp = temp.getnext(); // למצוא את המקום המתאים
}
first1.setnext(temp.getnext()); // חיבור החוליה
temp.setnext(first1); // עדכון החוליה הקודמת להצביע עליה
}
first1 = next; // עדכון למעבר לחוליה הבאה
}
return sorted;
}
באותו הנושא: