要從一個singly lisked linerar list中刪除文件,最簡單的方式莫過於用一個指標指向前一個node,如果現在的item需要刪除,將前面一項item指向後面的item即可。
1 | typedef struct node |
有人提出了更精巧的方法,利用two star pointer來達成,也就是指標的指標。
1 | void remove_if(node **head, remove_fn rm) |
在C++11的年代,增加了forward_list
,省得自己造輪子的麻煩。
以下是forward_list的介紹。
std::forward_list is a container that supports fast insertion and removal of elements from anywhere in the container. Fast random access is not supported. It is implemented as singly-linked list and essentially does not have any overhead compared to its implementation in C. Compared to std::list this container provides more space efficient storage when bidirectional iteration is not needed.
以下是一個示範程式,裡面同時使用了lambda expression跟Range-based for loop,日後有時間在寫。
1 |
|