單鏈表逆轉
單鏈表逆轉,將單鏈表中各結點的next域改為指向其前驅結點。原先第乙個結點的next域為空,head改為指向原先的最後乙個結點。逆轉剛才如下圖所示:
鍊錶類singlylist
public class singlylist
/*** 構造單鏈表,由values陣列提供元素
*/public singlylist(t values)
} /**
* 判斷鍊錶是否為空
*/public boolean isempty()
/*** 獲取鍊錶第i個結點的值
* * @return
*/public t get(int i)
return (i >= 0 && p != null) ? p.data : null;
} /**
* 返回單鏈表所有元素的值
*/public string tostring()
return str.tostring();
}}
單鏈表逆轉
public class singlylist_reverse
public static void reverse(singlylistlist)
list.head.next = front;
} public static void main(string args) ;
singlylistlist = new singlylist(values);
system.out.println("原鍊錶 = " + list.tostring());
reverse(list);
system.out.println("逆轉後 = " + list.tostring());
}}
執行結果如下:
原鍊錶 = abcdef
逆轉後 = fedcba
逆轉單鏈表
逆轉單鏈表 struct node reverse llist node head 此時temp表示以前的尾結點,p表示temp結點的前一結點 head temp 逆轉尾結點為頭結點 head next p 頭結點指標域指向p return head 逆轉迴圈鍊錶 struct node rever...
單鏈表逆轉
資料結構與演算法題目集 中文 list reverse list l 其中list結構定義如下 typedef struct node ptrtonode struct node typedef ptrtonode list 定義單鏈表型別 l是給定單鏈表,函式reverse要返回被逆轉後的鍊錶。i...
單鏈表逆轉
p 為指向非空單鏈表中第乙個結點的指標,本演算法逆轉鍊錶並返回逆轉後的頭指標。基本思路是 如果鍊錶中只有一 個結點,則空操作,否則先逆轉a2開始的鍊錶,然後將 a1聯接到逆轉後的鍊錶的表尾 即a2 之後。1 遞迴方法逆轉 單鏈表 2 node recreverselist node head 38 ...