Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- struct Node
- {
- int value = 0;
- Node* next = nullptr;
- };
- struct LinkedList
- {
- Node* head = nullptr;
- void add(int value){
- if(head == nullptr)
- {
- head = new Node{value};
- return;
- }
- Node* node = head;
- while(node->next != nullptr)
- {
- node = node->next;
- }
- node->next = new Node{value};
- }
- void show()
- {
- Node* node = head;
- for(;node != nullptr; node = node->next)
- std::cout << node->value << ' ';
- }
- //then you'd have code for the destructor to prevent memory leaks
- };
- int main()
- {
- LinkedList list;
- list.add(5);
- list.add(10);
- list.add(15);
- list.show();
- }
Add Comment
Please, Sign In to add comment