鍊錶(三)鍊錶形式的荷蘭國旗

2021-08-31 23:38:57 字數 3253 閱讀 8135

問題:給定乙個單向鍊錶的頭節點head,節點的值型別是整型,再給定乙個整數point。實現乙個調整鍊錶的函式,將鍊錶調整為左部分都是值小於pointt的節點,中間部分都是值等於pivot的節點,右部分都是值大於point的節點。除這個要求外,對調整後的節點順序沒有更多的要求。

第一種解法:類似於荷蘭國旗,借助乙個陣列,把鍊錶所有節點放入乙個陣列中,將此陣列進行三個區域的劃分。

//劃分結束後再將陣列依次next串起來,(在分到陣列中時可以不用切斷next,因為後面的賦值會覆蓋掉,)返回陣列中第乙個元素即可(陣列型別為node)

//額外空間複雜度o(n)鍊錶長度為n 達不到穩定性

鍊錶結構:

public static class node 

}

借助陣列方式實現:

public static node listpartition1(node head, int point) 

node nodelist = new node[i];

cur = head;

for (i = 0; i < nodelist.length; i++)

arrpartition(nodelist, point);

// 陣列已經劃分為三部分

for (i = 0; i < nodelist.length - 1; i++)

nodelist[i].next = null;

return nodelist[0];

} private static void arrpartition(node nodelist, int point) else if (nodelist[i].value > point) else

i++;

} }private static void swap(node nodelist, int i, int j)

第二種解法: 額外空間複雜度o(1)只用幾個節點進行劃分,可以完成穩定性。借助六個節點,分別為小於等於大於區域的頭部和尾部,遍歷鍊錶,依次將相應的節點掛在相應區域的位置。

(注意節點next指標的處理,當把head掛上去時,為保證不出混亂,先記錄下來head.next的節點,然後把head.next設定為空,最後再將節點連回去時,要注意可能某區域為空)

**:

public static node listpartition2(node head, int point)  else 

} else if (head.value < point) else

} else else

}head = next;

} //空區處理,如果中間區域存在,與大區域相連(大區域存不存在並不重要,如果不存在即為null)

//如果不存在,中間區域頭就和大區域頭合併。

if (est != null)

eed.next = lst;

else if (est == null)

//如果小於區域不為空,與等於區域相連,返回值為小於區域頭節點。如果為空,則返回值為等於區域頭節點

if (sst != null)

sed.next = est;

if (sst == null)

return sst;

/* if (sed != null)

all reconnect

if (eed != null)

return sst != null ? sst : est != null ? est : lst;

*/ }

包括陣列的荷蘭國旗實現,也可以借助煉表達到穩定性

(以下可忽略)

public class smallequalbig 

} public static node listpartition1(node head, int point)

node nodelist = new node[i];

cur = head;

for (i = 0; i < nodelist.length; i++)

arrpartition(nodelist, point);

// 陣列已經劃分為三部分

for (i = 0; i < nodelist.length - 1; i++)

nodelist[i].next = null;

return nodelist[0];

} private static void arrpartition(node nodelist, int point) else if (nodelist[i].value > point) else

i++;

} }private static void swap(node nodelist, int i, int j)

// 額外空間複雜度o(1)只用幾個節點進行劃分,可以完成穩定性。注意節點next指標的處理。最後分組為空時的處理

public static node listpartition2(node head, int point) else

} else if (head.value < point) else

} else else

}head = next;

} //空區處理,如果中間區域存在,與大區域相連(大區域存不存在並不重要,如果不存在即為null)

//如果不存在,中間區域頭就和大區域頭合併。

if (est != null)

eed.next = lst;

else if (est == null)

//如果小於區域不為空,與等於區域相連,返回值為小於區域頭節點。如果為空,則返回值為等於區域頭節點

if (sst != null)

sed.next = est;

if (sst == null)

return sst;

/* if (sed != null)

all reconnect

if (eed != null)

return sst != null ? sst : est != null ? est : lst;

*/ }

public static void main(string args)

private static void printlinkedlist(node head)

system.out.println();

}}

荷蘭國旗問題(鍊錶)

用三個桶,每個桶包括 鍊錶的頭和鍊錶的尾 三個桶分別是less,equal,more.遍歷一次鍊錶把數值放入相應的桶。public class test demo public static node lessequalmore node head,int num else if head.valu...

棧 鍊錶形式

實現下列操作。1 初始化空棧。2.鍵盤輸入字元,使得輸入的字元依次入棧 結束符號自定,例如回車鍵 值為10 或 每插入乙個元素,必須輸出當時的棧頂元素 呼叫getlinkstacktop函式 3 判斷鏈棧是否為空。輸出判斷結果。4 呼叫出棧函式,列印出棧元素的值 反覆此步驟,直至棧為空。5 判斷鏈棧...

c 結構體鍊錶形式

標頭檔案 ifndef liststu define liststu define n 20 define null 0 define type struct stu define len sizeof struct stu pragma warning disable 4996 struct st...