鍊錶及鏈表面試題

2021-08-02 18:28:33 字數 3749 閱讀 2215

首先看一下順序表和煉表的優缺點,分別在什麼場景下使用?

源**:

list.h

#ifndef __list_h__

#define __list_h__

#include

#include

#include

typedef

int datatype;

typedef

struct node

node, *pnode, *plist;

//init list

void

initlist

(plist* pplist)

;pnode buynode

(datatype d)

;//pop/push

void

pushback

(plist* pplist, datatype d)

;void

popback

(plist* pplist)

;void

pushfront

(plist* pplist, datatype d)

;void

popfront

(plist* pplist)

;int

getsize

(plist list)

;void

printlist

(plist plist)

;pnode find

(plist plist, datatype d)

;void

remove

(plist* pplist, datatype d)

;void

removeall

(plist* pplist, datatype d)

;void

insert

(plist* pplist, pnode pos, datatype d)

;void

erase

(plist* pplist, pnode pos)

;void

reverselist

(plist* pplist)

;void

bubblesort

(plist* pplist)

;void

destroylist

(plist* pplist)

;#endif

// !__list_h__

#include "list.h"

void initlist(plist* pplist)

pnode buynode(datatype d)

pnode->data = d;

pnode->next = null;

return pnode;

}void pushback(plist* pplist, datatype d)

else

cur->next = pnewnode;

}}void popback(plist* pplist)

else

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)

else

}void printlist(plist plist)

cur = plist;

printf("list: ");

while (cur != null)

printf("null\n");

}pnode find(plist plist, datatype d)

cur = cur->next;

}return

null;

}//刪除指定元素

void remove(plist* pplist, datatype d)

else

if (cur->next == null)

else

return;

}cur = cur->next;

}return;

}//刪除全部指定元素

void removeall(plist* pplist, datatype d)

else

if (cur->next == null)

else

cur->data = tmp->data;

cur->next = tmp->next;

free(tmp);

tmp = null;}}

cur = cur->next;

}return;

}//隨機位置插入

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

if (pos == null)

//方法1 swap

/*pnode newnode = buynode(d);

datatype tmp = d;

tmp = newnode->data;

newnode->data = pos->data;

pos->data = tmp;

newnode->next = pos->next;

pos->next = newnode;*/

//方法2

pnode newnode = buynode(pos->data);

pos->data = d;

newnode->next = pos->next;

pos->next = newnode;

}//指定位置刪除

void erase(plist* pplist, pnode pos)

while (cur != pos)

prev->next = cur->next;

free(cur);

cur = null;

}//逆置鍊錶

void reverselist(plist* pplist)

*pplist = newhead;

}//getsize

int getsize(plist list)

return size;

}//氣泡排序

void bubblesort(plist* pplist)}}

}void destroylist(plist* pplist)

*pplist = null;

}

#include "list.h"

//pop/push/size

void test1()

//remove/removeall

void test2()

//find/insert/erase

void test3()

//test_reverselist

void test_reverselist()

//test_bubblesort

void test_bubblesort()

int main()

鍊錶篇 鏈表面試題集

1.給定單鏈表,檢測是否有環。如果有環,則求出進入環的第乙個節點。判斷單向鍊錶是否有環,可以採用快指標與慢指標的方式來解決。即定義乙個快指標fast和乙個慢指標slow,使得fast每次跳躍兩個節點,slow每次跳躍乙個節點。如果鍊錶沒有環的話,則slow與fast永遠不會相遇 這裡鍊錶至少有兩個節...

鏈表面試題3 複雜鍊錶

struct clnode 複雜鍊錶 clnode,clpnode,cllist,clplist clpnode clbuynode datatype d clplist data d clplist next null clplist random null return clplist void...

鏈表面試題

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