Last active
September 24, 2020 18:37
-
-
Save sprintr/7908191 to your computer and use it in GitHub Desktop.
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; | |
| class LinkedList { | |
| public: | |
| LinkedList() { | |
| start = NULL; | |
| } | |
| void add(int d) { | |
| Node* n = new Node; | |
| if(start == NULL) { | |
| start = n; | |
| n->next = NULL; | |
| n->data = d; | |
| } else { | |
| n->next = start; | |
| n->data = d; | |
| start = n; | |
| } | |
| } | |
| void display() { | |
| Node* ptr = start; | |
| while(ptr != NULL) { | |
| cout << ptr->data << endl; | |
| ptr = ptr->next; | |
| } | |
| } | |
| private: | |
| struct Node { | |
| int data; | |
| Node* next; | |
| }; | |
| Node* start; | |
| }; | |
| int main(int argc, char **argv) | |
| { | |
| LinkedList a; | |
| a.add(20); | |
| a.add(32); | |
| a.display(); | |
| system("pause"); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment