鏈表面試題

2021-08-04 11:06:54 字數 4303 閱讀 8472

typedef

int datatype;

typedef

struct node

node,*pnode,*plist;

void initlist(plist *pplist);

void display(const plist plist);

void pushback(plist *pplist,datatype d);

void popback(plist *pplist);

void pushfront(plist *pplist,datatype d);

void popfront(plist *pplist);

void distroylist(plist *pplist);

pnode find(plist *pplist,datatype d);

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

void erase(plist *pplist,pnode pos);

void remove(plist *pplist,datatype d);

void removeall(plist *pplist,datatype d);

void bubblesort(plist *pplist);

void reverse(plist *pplist); //逆序

void erasenottail(plist *pplist,pnode pos); //刪除無頭單鏈表的非尾節點

void reverseprint(plist plist); //逆序列印

void insertfrontnode(pnode pos, datatype x);//在無頭單鏈表的非頭結點前插入乙個元素

void josephcycle(plist* pplist, int k); //約瑟夫環問題

plist merge(plist* p1, plist* p2); //合併兩個有序列表

pnode findmidnode(plist plist); //查詢單鏈表的中間節點,要求只能遍歷一次鍊錶

pnode findknode(plist plist, int k); //查詢單鏈表的倒數第k個節點,要求只能遍歷一次鍊錶

pnode checkcircle(plist plist);//判斷鍊錶時候帶環

int getcirclelength(pnode meet); //求環的長度

pnode getcycleentrynode(plist plist, pnode meet); //求環的入口點

int checkcross(plist list1, plist list2); //判斷兩條單項鍊表時候相交

pnode getcrossnode(plist list1, plist list2);

void initlist(plist *pplist)

void display(const plist plist)

printf("null\n");

}pnode buynode(datatype d)

void pushback(plist *pplist,datatype d)

while (cur->next!=

null)

cur->next=newnode;

}void popback(plist *pplist)

if (cur->next==

null)

while (cur->next->next!=

null)

free(cur->next);

cur->next=

null;

}void pushfront(plist *pplist,datatype d)

void popfront(plist *pplist)

*pplist=(*pplist)->next;

free(cur);

cur=

null;

}void distroylist(plist *pplist)

*pplist=

null;

}pnode find(plist *pplist,datatype d)

else

}return

null;

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

newnode->next=pos->next;

pos->next=newnode;

}void erase(plist *pplist,pnode pos)

if (cur->next==

null)

cur->next=pos->next;

free(pos);

pos=

null;

}void remove(plist *pplist,datatype d)

void removeall(plist *pplist,datatype d)

}void bubblesort(plist *pplist)

while (end!=*pplist)

first=first->next;

second=second->next;

}end=first;

}}void reverse(plist* pplist)

else

tail->next =

null;

*pplist = cur;

}

}void erasenottail(plist *pplist,pnode pos)

pos->

data

= pos->next->

data;

del = pos->next;

cur = pos->next->next;

free(del);

pos->next = cur;

}void reverseprint(plist plist)

printf("%d-->",plist->

data);

}}void insertfrontnode(pnode pos, datatype x)

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;

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

if (p1->

data

data)

else

tail = newlist;

while (p1&&p2)

else

tail = tail->next;

}if (p1==

null)

else

return newlist;

}pnode findmidnode(plist plist)

return slow;

}pnode findknode(plist plist, int k)

else

}while (fast->next!=

null)

return slow;

}pnode checkcircle(plist plist)

if (fast==slow)

}}int getcirclelength(pnode meet)

return count;

}pnode getcycleentrynode(plist plist, pnode meet)

return cur;

}int checkcross(plist list1, plist list2)

while (tail->next!=

null)

if (cur==tail)

else

}pnode getcrossnode(plist list1, plist list2)

cur->next=list1;

meet = checkcircle(list2);

tmp = getcycleentrynode(list2,meet);

}

鏈表面試題

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

鏈表面試題

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

鏈表面試題

pragma once include typedef int datetype typedef struct listnode listnode 單鏈表實現約瑟夫環 listnode josephcycle listnode first,int k tail next first listnode...