Skip to content

Instantly share code, notes, and snippets.

@hallfox
Created March 10, 2016 00:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hallfox/2ff55176c2ea786dd38c to your computer and use it in GitHub Desktop.
Save hallfox/2ff55176c2ea786dd38c to your computer and use it in GitHub Desktop.
class LinkedList;
class Node {
public:
// You can do it this way but eehhhh
friend LinkedList;
Node(int data): data{data}, next{nullptr} {}
// Otherwise just use this if you have it
Node *get_next() { return next; }
private:
int data;
Node *next;
};
// IMHO the better way to do it is to have Node be a struct
// Or have Node as an internal class of LinkedList
class LinkedList {
public:
LinkedList();
LinkedList(const LinkedList& ll);
class Iterator {
public:
Iterator();
Iterator operator++() {
// nullptr checking
curr = curr->next;
}
// yadda yadda
Iterator operator*();
Iterator *operator->();
};
Iterator begin();
// NOTE: Make sure it represents the theoretically place after the last
// Doing stuff to end() is undefined behavior
// Undefined behavior means: literally anything can happen
Iterator end();
private:
Node *head;
};
LinkedList::LinkedList(const LinkedList& ll) {
// LinkedLists are automatically friends with LinkedLists
Node *n = ll.head;
// Iterate over and deep copy...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment