Skip to content

Instantly share code, notes, and snippets.

@calebreister
Created April 7, 2014 23:19
Show Gist options
  • Save calebreister/10072722 to your computer and use it in GitHub Desktop.
Save calebreister/10072722 to your computer and use it in GitHub Desktop.
A very very very very very simple linked list implementation. It it will probably not compile at this point, it is incomplete.
#include <iostream>
struct Node {
int item;
Node* next;
};
class LinkedList {
private:
Node* head;
public:
LinkedList() {
head = NULL;
}
~LinkedList() {
delete head;
}
void insert(int item) {
//Initialize
Node* n = new Node;
n->item = item;
n->next = NULL;
if (head == NULL) //list empty?
head = n;
else if (n->item <= head->item) //1st item?
{
n->next = head;
head = n;
}
else //insert in the middle or at the end
{
Node* prev = head;
while (prev->next != NULL && prev->next->item < n->item)//figure out where to go
prev = prev->next;
n->next = prev->next;//insert
prev->next = n;
}
}
bool remove(int item) {
}
friend std::ostream& operator<<(std::ostream& stream,
const LinkedList& list);
};
std::ostream& operator<<(std::ostream& stream, const LinkedList& list) {
}
int main() {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment