Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // CPPConsole.cpp : Define o ponto de entrada para a aplicaรงรฃo de console.
- //
- #include "stdafx.h"
- #include <functional>
- typedef std::function<void(int)> Action;
- class List {
- struct ListValue {
- int value;
- ListValue* next;
- ListValue(int val) {
- value = val;
- next = NULL;
- }
- ListValue(int val, ListValue* n) {
- value = val;
- next = n;
- }
- };
- int count = 0;
- ListValue* root = NULL;
- public:
- bool Add(int value) {
- if(root == NULL) {
- root = new ListValue(value);
- count++;
- return true;
- }
- if(value < root->value) {
- root = new ListValue(value, root);
- count++;
- return true;
- }
- ListValue* current = root;
- ListValue* previous = root;
- while((current = current->next) != NULL) {
- /*if(current->value == value)
- return false;
- else*/ if(value < current->value) {
- previous->next = new ListValue(value, current);
- count++;
- return true;
- }
- previous = current;
- }
- previous->next = new ListValue(value);
- count++;
- return true;
- }
- bool Remove(int value) {
- if(root == NULL || value < root->value)
- return false;
- if(root->value == value) {
- root = root->next;
- count--;
- return true;
- }
- ListValue* current = root;
- ListValue* previous = root;
- while((current = current->next) != NULL && current->value <= value) {
- if(current->value == value) {
- previous->next = current->next;
- count--;
- return true;
- }
- previous = current;
- }
- return false;
- }
- bool Contains(int value) {
- ListValue* current = root;
- while(current != NULL && current->value <= value) {
- if(current->value == value)
- return true;
- current = current->next;
- }
- return false;
- }
- void Iterate(Action action) {
- for(ListValue* current = root; current != NULL; current = current->next)
- action(current->value);
- }
- };
- int main() {
- List* list = new List();
- list->Add(5);
- list->Add(10);
- list->Add(1);
- list->Add(7);
- list->Add(-1);
- list->Add(8);
- list->Add(2);
- list->Remove(5);
- list->Remove(10);
- list->Remove(-1);
- list->Add(1);
- list->Remove(1);
- list->Remove(1);
- list->Iterate([](int i) {
- printf("%d\n", i);
- });
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement