Created
November 16, 2010 08:06
-
-
Save rangalo/701575 to your computer and use it in GitHub Desktop.
cpp linked list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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