鍊錶的面試題

2021-08-19 20:28:44 字數 4819 閱讀 7050

slist.h

#include #include #include typedef int datatype;

typedef struct listnode

node,*pnode;

//生成乙個新結點

pnode buyslistnode(datatype data);

//列印鍊錶

void printslist(pnode phead);

//尾插

void slistpushback(pnode* phead, datatype data);

pnode slistfind(pnode phead, datatype data);

// 逆序列印單鏈表

void printslistfromtail2head(pnode phead);

// 刪除鍊錶的非尾結點,要求:不能遍歷鍊錶

void deletelistnottailnode(pnode pos);

// 在鍊錶pos位置前插入值到data的結點

void inesrtposfront(pnode pos, datatype data);

// 查詢鍊錶的中間結點,要求只能遍歷一次鍊錶

pnode findmiddlenode(pnode phead);

// 查詢鍊錶的倒數第k個結點,要求只能遍歷一次鍊錶

pnode findlastknode(pnode phead, int k);

// 刪除鍊錶的倒數第k個結點,要求只能遍歷一次鍊錶

void deletelastknode(pnode phead, int k);

// 用單鏈表實現約瑟夫環

void josephcircle(pnode* phead, const int m);

// 鍊錶的逆置--三個指標

void reverseslist(pnode* phead);

// 鍊錶的逆置--頭插法

pnode reverseslistop(pnode phead);

// 用氣泡排序思想對單鏈表進行排序

void bubblesort(pnode phead);

// 合併兩個有序單鏈表,合併後依然有序

pnode mergeslist(pnode phead1, pnode phead2);

// 判斷兩個單鏈表是否相交---鍊錶不帶環

int isslistcross(pnode phead1, pnode phead2);

// 求兩個單鏈表相交的交點---鍊錶不帶環

pnode getcorssnode(pnode phead1, pnode phead2);

////測試

void testprintslistfromtail2head();

void testdeletelistnottailnode();

void testinesrtposfront();

void testfindmiddlenode();

void testfindlastknodeanddeletelastknode();

void testjosephcircleandreverseslist();

void testmergeslistandgetcorssnode();

slist.c

#include "slist.h"

void printslist(pnode phead)

while(pcur)

printf("null\n");

}void slistinit(pnode* phead)

pnode buyslistnode(datatype data)

pnewnode->data =data;

pnewnode->pnext =null;

return pnewnode;

}void slistpushback(pnode* phead, datatype data)

//鍊錶中有結點

else

pcur->pnext =buyslistnode(data); }}

pnode slistfind(pnode phead, datatype data)

while(phead)

phead=phead->pnext ;

} return null;

}void printslistfromtail2head(pnode phead)

while(pcur)

while(ptail!=phead)

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

ptail=ppre;

} printf("%d->null",phead->data );

}void deletelistnottailnode(pnode pos)

void inesrtposfront(pnode pos, datatype data)

//如果節點數為偶數返回的是中間數的後乙個

pnode findmiddlenode(pnode phead)

else if(phead->pnext==null )

else

}return slow;

}pnode findlastknode(pnode phead, int k)

while(k--)

while(fast)

return slow;

}void deletelastknode(pnode phead, int k)

while(k--)

while(fast)

pre->pnext =slow->pnext ;

free(slow);

slow=null;

}void josephcircle(pnode* phead, const int m)

//先遍歷鍊錶,讓尾指向頭

while(pcur->pnext )

pcur->pnext =*phead;

pcur=*phead;

//環內只剩乙個元素時停止

while((pcur->pnext) !=pcur)

//刪除節點

del=pcur->pnext ;

pcur->data =del->data ;

pcur->pnext =del->pnext ;

free(del);

del=null;

} //解環

pcur->pnext =null;

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

}void reverseslist(pnode* phead)

pcur=*phead;

while(pcur)

*phead=pre;

}pnode reverseslistop(pnode phead)

while(phead)

phead=pcur;

return phead;

}void bubblesort(pnode phead)

pcur=pcur->pnext ;

} ptail=pcur;

if(flag==0)

break;

} printslist(phead);

}pnode mergeslist(pnode phead1, pnode phead2)

else if(phead1->data data)

while(phead1&&phead2)

else if((phead1->data )<(phead2->data))

}while(phead1)//判斷phead1節點是否為空,如若不空則繼續

while(phead2)

printslist(newnode);

return newnode;

}int isslistcross(pnode phead1, pnode phead2)

//找到鍊錶的最後乙個結點

while(phead1->pnext )

while(phead2->pnext )

if(phead1 ==phead2 )

return 1;

return 0;

}pnode getcorssnode(pnode phead1, pnode phead2)

while(pcur1)

while(pcur2)

pcur1=phead1;

pcur2=phead2;

gap=size1-size2;

if(gap>0) }

else }

while(pcur2!=pcur1)

return pcur1;}//

//測試

void testprintslistfromtail2head()

void testdeletelistnottailnode()

void testinesrtposfront()

void testfindmiddlenode()

void testfindlastknodeanddeletelastknode()

void testjosephcircleandreverseslist()

void testmergeslistandgetcorssnode()

test.c

#include "slist.h"

int main()

鍊錶的面試題

1 比較順序表和煉表的優缺點,它們分別在什麼場景下使用?1 順序表支援隨機訪問,單鏈表不支援隨機訪問。2 順序表插入 刪除資料效率很低,時間複雜度為o n 除尾插和尾刪 單鏈表插入 刪除效率更高,時間複雜度為o 1 3 順序表的cpu高速緩衝效率更高,單鏈表cpu高速緩衝效率低。2 列印單向鍊錶 v...

鍊錶操作面試題

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 使用...