鏈表面試題大全

2021-08-22 07:14:17 字數 4625 閱讀 6852

#include

"list.h"

//買乙個結點

pnode buynode(datatype d)

//銷毀鍊錶

void destroylist(plist* pplist)

del =

null;

*pplist =

null;

}//尾加 用cur指標找到最後乙個結點 然後將新結點新增到cur後

void pushback(plist* pplist, datatype d)

else

cur->next = tmp;

}}//尾刪

void popback(plist* pplist)

else

if((*pplist)->next==

null)

else

free(del->next);

del->next=

null;

}}//頭加

void pushfront(plist* pplist, datatype d)

else

}//頭刪

void popfront(plist* pplist)

}//列印

void printlist(plist plist)

printf("%d\n", tmp->

data);

}}//得到鍊錶長度

int getlistlength(plist plist)

}return ret;

}//按值查詢,返回結點位址

pnode find(plist plist, datatype d)

return tmp;

}}//按位址插入

void insert(plist* pplist, pnode pos, datatype d)

else

if (cur == pos)

}}//按位址刪除

void erase(plist *pplist, pnode pos)

else

else

if (cur->next == pos)}}

}}//按值移除

void remove(plist* pplist, datatype d)

else

if (cur->next !=

null)}}

}//移除所有這樣的值

void removeall(plist* pplist, datatype d)

else

if (cur->next !=

null)

else}}

}//面試題

//逆序列印單項鍊表====非遞迴方法

void printtailtohead(plist plist)

printf("%d ", cur->

data);

tail = cur;

cur = plist;}}

}//刪除無頭非尾結點

void delnodenottail(pnode pos)

}//插入結點

void insertnode(pnode pos, datatype d)

}//環

void josephuscycle(plist* pplist, int k)

cur = cur->next;

}*pplist = cur;

}}//逆置鍊錶

void reverselist(plist* pplist)

*pplist=tmp1;

}}//鍊錶氣泡排序

void bubblesort(plist plist)

else

if (cur->

data

< cur_next->

data)}}

}}//合併有序鍊錶

plist merge(plist p1, plist p2)

else

p2 = p2->next;

}else

else

p1 = p1->next;}}

if (p1 !=

null)

}if (p2!=

null)

}return ret;

}遞迴 合併有序鍊錶(公升序)

plist merge_r(plist p1, plist p2)

else

return mergehead;

}//尋找中間結點

pnode findmidnode(plist plist)

return slow;

}//尋找倒數第k個結點

pnode findlastknode(plist plist, int k)

while (k--&&fast)

while (fast)

return slow;

}//判斷是否是環

pnode iscircle(plist plist)

while (fast&&fast->next)

return

null;

}//得到環的長度

int getcirclelength(pnode meet)

tmp = meet->next;

while (tmp!=meet)

return count;

}//得到環的開始

pnode getcycleentrynode(plist plist, pnode meet)

return

null;

}//判斷是否交叉 交叉返回真 不交叉返回假

int checkcross(plist p1, plist p2)

while (shortlist)

subb = len1 - len2;

if (subb <

0)

else

while (subb--)

while (longlist!=

null

&& shortlist!=

null)

longlist = longlist->next;

shortlist = shortlist->next;

}return0;}

//得到交叉點

pnode getcrossnode(plist p1, plist p2)

while (shortlist)

subb = len1 - len2;

if (subb <

0)

else

while (subb--)

while (longlist !=

null

&& shortlist !=

null)

longlist = longlist->next;

shortlist = shortlist->next;

}return

null;

}//公升級版(複雜情況)====如果交叉返回交叉點 反正返回null

pnode complexsituation(plist p1, plist p2)

//兩個都是環 可能相交,也可能不相交

else

if (meet1!=

null

&&meet2 !=

null)

//cur回到了原點,說明meet1和meet2不是在同乙個環內 所以原p1和p2沒有相交點

if (cur == meet1)

//反正p1和p2有相交點,且裡面帶有環

else

}else

//乙個環和乙個無環單鏈表沒有交點

}//15. 求兩個有序單鏈表交集(差集)。

void unionset(plist p1, plist p2)

while (p1&&p2)

else

if (p1->

data

< p2->

data)

else

}}//買乙個複雜結點

complexnode* buycomplexnode(datatype d)

//列印複雜鍊錶

void printcomplexlist(complexnode* plist)

else

cur = cur->next;

}printf("\n");

}//複製複雜鍊錶

complexnode* copycomplexlist(complexnode* plist)

else

//根據老鍊錶,建立新鍊錶的隨機指標的關係,隨機指標的複製要在結點複製之後

cur = plist;

while (cur)

else

tmp->random =

null;

cur = cur->next->next;

}//從合併拆出新複製的結點,並連成鍊錶

cur = plist;

ret = cur->next;

while (cur)

else

}//返回已複製的新鍊錶

return ret;

}}

鏈表面試題

不改變鍊錶結構,從尾到頭列印單鏈表 遞迴實現 void printlistrevers recursively plist phead printf d phead data 當鍊表非常長的時候,遞迴實現的會導致函式呼叫層級很深,可能導致呼叫棧溢位。用棧不會出現此類情況,顯然用棧實現 的魯棒性會好一...

鏈表面試題

從尾到頭列印單鏈表 棧 遞迴 從尾到頭列印鍊錶 includevoid printtailtohead listnode head while s.empty 空間複雜度o n void printtailtoheadr listnode head cout data 刪除乙個無頭單鏈表的非尾結點 ...

鏈表面試題

typedef int datatype typedef struct node node,pnode,plist void initlist plist pplist void display const plist plist void pushback plist pplist,datatyp...