單鏈表面試題

2021-08-20 22:08:11 字數 3675 閱讀 7382

1.//倒序列印鍊錶

void reverseprint(slistnode *pfirst);

有兩種方法:

1.遞迴操作

2.非遞迴操作

//2 非遞迴

slistnode *pend ;

slistnode *pcur;

pend = null;

while (pfirst != pend)

pend = pcur;

printf(" %d ", pend->data);

} printf("\n");

}2. //逆置鍊錶

}3.  //刪除非尾無頭單鏈表

4.//無頭鍊錶前插入乙個結點

5.//約瑟夫環

//2什麼時候約瑟夫環停止(pfirst->pnext = pfirst);

while (pfirst->pnext != pfirst)

pdel = pcur->pnext;

pcur->pnext = pdel->pnext;

free(pdel);

pfirst = pcur->pnext;

} printf("%d\n", pfirst->data);

pfirst->pnext = null;

slistprint(pfirst);

return null;

}6.//氣泡排序

}//對冒泡的優化,只要有一趟比較沒有發生結點交換,說明冒泡完成,就可以退出冒泡的**塊了

if (0 == flag)

ptailnode = pprenode;

} }}

7.//遍歷一遍找中間

}8//查詢倒數第k個結點

}9.//刪除倒數第k個結點

void removek(slistnode *pfirst, int k)

//讓pfast先走k步

while (k--)

while (pfast)

//是第乙個節點

if (pslow == ppre)

//不是第乙個節點

ppre->pnext = pslow->pnext;

free(pslow);

}

10.//合併兩個有序鍊錶

slistnode *mergeorderedlist(slistnode*p1first, slistnode*p2first)

if (p2 == null)

while ((p1 != null) && (p2 != null))

if ((p1->data) < (p2->data))

}while (p1)

while (p2)

//slistprint(pnewnode);

return pnewnode;

}

單鏈表 (面試題)

關於單鏈表的基本操作,之前已經總結過了,那些掌握之後算是了解了單鏈表是什麼?不過現在面試的題中,肯定不會只讓你回答單鏈表的基礎操作,總是會改變一些東西,或是擴充套件一下。下面就是一些關於單鏈表的擴充套件內容 include include include pragma warning disable...

單鏈表(面試題)

鍊錶反轉思路 1.單鏈表中有效節點的個數 2.查詢單鏈表中弟第k個節點 3.單鏈表的反轉 實現 如下 public class testlink 1單鏈表中有效節點的個數 遍歷得出個數 public static intcount heronode head int count 0 while tr...

鏈表面試題 反轉單鏈表

反轉乙個單鏈表。示例 輸入 1 2 3 4 5 null 輸出 5 4 3 2 1 null解決方案 頭插法開闢新鍊錶並逐個讀取舊鍊錶,頭插進新鍊錶,這樣新的鍊錶與原鍊錶的結構就是反的,需要借助輔助空間 definition for singly linked list.struct listnod...