a linked list is a collection of individual items called nodes, which has data and at least one pointer to the next node.
A pointer is an address so that it knows where the next node in line is.
What makes linked lists efficient over indexed data structures such as arrays or C++ STL vectors is that it allows for simple insertions and deletions of an item (of a node I mean). The reason is we have a front pointer to keep track of the entire list and a potentail end pointer that points to last node. This way (if for your purposes order of the elements (nodes I mean) don't matter), you can make a new node, point to the front of the list and update front pointer to point to the new node so basically new node becomes new front of the list.
A basic implementation of a linked list node is:
1 2 3 4 5
|
struct node
{
node* next;//a pointer to next node so you set it to NULL to indicate adding at end of list (so that's where you use the tail pointer)
int data;//you can have as much data, it could be string, bool whatever
}
| |
Keep in mind that you have to pass by pointers to pointers for your delete node functions as you need to modify the original pointer declared in main. (what I mean is if you use for e.g. void insert_node(node** list, int data); since parameters are passed by value so you need to point to the address of that actual pointer (declared in main) that points to the linked list or you just have a return type to the address of node so I mean: node* insert_node(node* list, int data); and then in main, update the front or end pointers depending of if, for your purposes, you only want to insert new nodes at front or end of list. so you would update these pointers as:
front = insert_node(front, 99);
for e.g.
It would be very helpful to read up on pointers and pass by value vs pass by addresses for functions first before diving into linked lists.