Home  Writing  Games  Music  Dev  About 

Linked Lists, or Lists

Can implement in an array, which has expensive insert and delete operations, or through pointers and structs, which need double the space.

Implementation In C - Array
Implementation In C - Pointers

cheaper to dynamically move, add, delete items, as link to next item changed simply.

space needed for extra pointer field for each item

Implementation.

Linked Lists

Types

Singly Linked lists

   cout << "Contents of Linked List without custom iterator: ";
        //have to make member fields of classes public for this:
        
        LinkNode<int> *it = lList.m_root;
    
        while (it != NULL){
            cout << " " << it->m_data;
            it = it->m_next;
        }
    
        cout << "." << endl;
    

Double Ended Linked Lists

Doubly Linked Lists

Cicular Lists

STL list container

Lists vs Arrays

Lists Arrays
Fast insertion, deletion at ends of list Array must be shifted or reallocated
Can expand and shrink in almost constant time More expensive to expand, shrink
Slow to search for particular value Faster search algos
Do not have random access Random access for any index
Data + Pointer in node, extra memory Just Data, less memory
However, overall memory allocated is according to n actual elements (so easier and maybe more efficient on adding extra items at runtime) arrays usually allocated in blocks with unneeded memory buffer

Stacks, Queues, Double-ended Queues and Priority Queues

Stacks and Queues

Stacks

Data Structure that can be built using a linked list, but limited in operations.

Data is added and removed from the top of the list only. So only a head pointer needed.

Used by OS, compilers etc.

Implementation.

Queues

Data Structure using a list, with items added from one end, and removed from the other.

So two pointers to the list needed, head and tail.

Used by OS schedulers etc.

Queue, Dequeue

Priority Quees

Implementation.