Skip to content

Instantly share code, notes, and snippets.

@huklee
Created March 3, 2017 10:51
Show Gist options
  • Save huklee/e337a7134f054cb5ce78d6c542d601f1 to your computer and use it in GitHub Desktop.
Save huklee/e337a7134f054cb5ce78d6c542d601f1 to your computer and use it in GitHub Desktop.
[C++ STL] double linked list example
#include <iostream>
#include <list>
#include <vector>
#include <unordered_map>
void std_list_test(){
std::cout << "=== std::list Test ===" << std::endl;
std::list<int> li;
li.push_back(2);
li.push_back(3);
li.push_front(1);
li.push_front(0);
// 01. simple fore statement
std::cout << "BEFORE" << std::endl;
for(int i : li) std::cout << i << std::endl;
// 02. insert
for(std::list<int>::iterator ii=li.begin(); ii != li.end(); ii++)
li.insert(ii, *ii);
// 03. erase the node
for(std::list<int>::iterator ii=li.begin(); ii != li.end(); ii++)
if (*ii == 2)
li.erase(ii);
// 04. remove all nodes those have the value
li.remove(1);
// confirm the result
std::cout << "AFTER" << std::endl;
for(int i : li) std::cout << i << std::endl;
// 05. clear the data of the list
li.clear();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment