Notes From CS Undergrad Courses FSU
This project is maintained by awa03
Efficiently = O(1) time complexity
// Insertion
auto I = Cities.begin();
for (; I != Cities.end(); ++I) {
if (“Miami” == *I) {
break;
}
}
//Insert the new string
Cities.insert(I, “Orlando”);
// “Jacksonville”, “Tallahassee”, “Gainesville”, “Orlando”, “Miami”
// Remove Orlando
List<string>::iterator I = Cities.begin();
// auto I = Cities.begin(); // c++11
while( I != Cities.end()) {
if (“Orlando” == *I) {
I = Cities.erase(I);
} else {
I++;
}}
Node -
template <typename Object>
class List
{
private:
struct Node
{
Object data;
Node *prev; // Points to previous Node
Node *next; // Points to next Node
Node( const Object & d = Object{ }, Node * p = nullptr, Node * n = nullptr )
: data{ d }, prev{ p }, next{ n } { }
Node( Object && d, Node * p = nullptr, Node * n = nullptr )
: data{ std::move( d ) }, prev{ p }, next{ n } { }
};
iterator insert( iterator itr, const Object & x )
{
Node *p = itr.current;
++theSize;
return iterator( p->prev = p->prev->next = new Node{ x, p->prev, p } );
}
iterator insert( iterator itr, Object && x )
{
Node *p = itr.current;
++theSize;
return iterator( p->prev = p->prev->next = new Node{ std::move( x ), p->prev, p } );
}
private:
int theSize;
Node *head;
Node *tail;
void init( )
// Doubly Linked List Init
{
theSize = 0;
head = new Node;
tail = new Node;
head->next = tail; // Head -> &Tail
tail->prev = head; // Tail (previos) -> &Head
}
};
iterator erase( iterator itr )
{
Node *p = itr.current;
iterator retVal( p->next );
p->prev->next = p->next;
p->next->prev = p->prev;
delete p;
--theSize;
return retVal;
}
iterator erase( iterator from, iterator to )
{
for( iterator itr = from; itr != to; )
itr = erase( itr );
return to;
}