Skip to content

Instantly share code, notes, and snippets.

@rangalo
Created November 16, 2010 08:06
Show Gist options
  • Select an option

  • Save rangalo/701575 to your computer and use it in GitHub Desktop.

Select an option

Save rangalo/701575 to your computer and use it in GitHub Desktop.
cpp linked list
#include <iostream>
using namespace std;
struct Node {
int data;
Node * next;
};
class LinkedList {
Node * head;
Node * current;
public:
LinkedList() {
head = current = NULL;
}
bool append(int val) {
Node * temp = new Node;
temp->data = val;
temp->next = NULL;
if (NULL == head) {
cout << "Setting head to temp" << endl;
head = temp;
current = temp;
} else {
current->next = temp;
current = temp;
}
return true;
}
void printList() {
Node * tmp = head;
if (NULL == head) {
cout << "Empty list" << endl;
return;
}
while(tmp->next) {
cout << "data: " << tmp->data << endl;
tmp = tmp->next;
}
}
};
int main() {
LinkedList list = LinkedList();
list.printList();
list.append(4);
list.printList();
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment