鍊錶的面試題

2021-08-03 09:51:31 字數 3237 閱讀 6396

1、比較順序表和煉表的優缺點,它們分別在什麼場景下使用?

1)順序表支援隨機訪問,單鏈表不支援隨機訪問。

2)順序表插入/刪除資料效率很低,時間複雜度為o(n)(除尾插和尾刪),單鏈表插入/刪除效率更高,時間複雜度為o(1)。

3)順序表的cpu高速緩衝效率更高,單鏈表cpu高速緩衝效率低。

2、列印單向鍊錶

void display(plist plist)

printf("over\n");

}

3、單鏈表的排序

void bubblesort(plist *pplist)

cur =

*pplist;

while (cur != tail)

cur = cur->next;

}tail = cur;

cur =

*pplist;

}}

4、逆序列印單項鍊表

void reverseprint(plist plist)

if (cur->next !=

null)

printf("%3d", cur->

data);

}

5、刪除無頭單鏈表的非尾結點

void erasenottail(pnode pos)

6、在無頭單鏈表的非頭結點前插入乙個元素

void insertfrontnode(pnode pos, datatype x)

7、約瑟夫環問題

void josephcycle(plist *pplist, int k)

del = cur->next;

printf("%d", cur->

data);

cur->

data

= cur->next->

data;

cur->next = cur->next->next;

free(del);

del =

null;

}printf("%d\n", cur->

data);

}

8、逆序單向鍊錶

void reverselist(plist *pplist)

cur = *pplist;

next = cur->next;

cur->next = null;

while (next != null)

*pplist = cur;

}

9、合併兩個有序列表

plist merge(const plist *p1, const plist *p2)

if (list1 ==

null)

if (list2 ==

null)

if (list1->

data

< list2->

data)//新鍊錶的首部

else

tail = newlist;

while (list1 && list2)

else

}if (list1 =

null)

else

return newlist;

}

10、查詢單鏈表的中間節點,要求只能遍歷一次鍊錶

//定義兩個指標分別為fast和slow,fast每次跳兩下,slow每次跳一下,

//當fast指向鏈尾時,show所指的位置就是中間結點

pnode findmidnode(plist plist)

return slow;

}

11、查詢單鏈表的倒數第k個節點,要求只能遍歷一次鍊錶

//定義兩個指標分別為fast和slow,讓fast先走k-1步後slow再走,當fast指向尾部時,slow所指向的就是倒數第k個元素

pnode findknode(plist plist, int k)

fast = fast->next;

}if (k <=

0)//執行完while後才能輸出

return

null;

}

12、判斷鍊錶帶環情況

//定義兩個指標分別為fast和slow,每次fast比slow多走一步,如果帶環,那麼它們一定相交

pnode checkcircle(plist plist)

}return

null;

}

13、求環的長度

int getcirclelength(pnode meet)

return i;

}

14、求環的入口點

pnode getcycleentrynode(plist plist, pnode meet)

while(pnode != meet)

return meet;

}

15、判斷兩條單項鍊表是否相交

int checkcross(plist list1, plist list2)

while (p2 && p2->next)

if ((p1 == p2) && (p1 !=

null))//相交

else

return-1;

}

16、轉化成不帶環的鍊錶求解

pnode getcrossnocycle(plist list1, plist list2)

cur->next = list2;

meet = checkcircle(list1);

crossnode = getcycleenternode(list1, meet);

return crossnode;

}

17、求交點

pnode getcrossnode(plist list1, plist list2)

else

if (p1 && p2)

return crossnode;

}

鍊錶的面試題

slist.h include include include typedef int datatype typedef struct listnode node,pnode 生成乙個新結點 pnode buyslistnode datatype data 列印鍊錶 void printslist ...

鍊錶操作面試題

include using namespace std struct node int value node next node find node phead,int t 一 刪除鍊錶中某個節點 思路 先找到要刪除的節點位置 bool deletet node phead,int t else e...

鍊錶操作面試題

include include include define datetype int typedef struct node node,pnode void printlist pnode head 列印鍊錶 printf null n void bubblesort pnode phead 使用...