Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Node
- {
- Node(int _value = 0, Node* _next = nullptr)
- : value{_value}, next{_next}
- {
- }
- void set_next(Node* _next)
- {
- next = _next;
- }
- int value;
- Node* next;
- };
- struct List
- {
- List()
- : first{nullptr}
- {
- }
- void add_node(int value)
- {
- if(first == nullptr)
- {
- Node* _new{new Node{value}};
- first = _new;
- }
- else if(first->value > value)
- {
- Node* _new{new Node{value, first}};
- first = _new;
- }
- else
- {
- Node* curr{first};
- while(curr->next != nullptr)
- {
- if(curr->next->value < value)
- {
- curr = curr->next;
- }
- else
- {
- break;
- }
- }
- Node* _new{new Node{value, curr->next}};
- curr->next = _new;
- }
- }
- Node* first;
- };
- int main()
- {
- List l{};
- l.add_node(5);
- l.add_node(9);
- l.add_node(8);
- std::cout << l.first->value << std::endl;
- std::cout << l.first->next->value << std::endl;
- //std::cout << l.first->next->next->value << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment