Last active
August 23, 2017 02:20
-
-
Save estebanz01/e3cdf7b1eb4eeb9eb6e7807c8288f62e to your computer and use it in GitHub Desktop.
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
| // Example program | |
| #include <iostream> | |
| #include <vector> | |
| #include <string> | |
| using namespace std; | |
| class Node { | |
| public: | |
| int data; | |
| Node* next; | |
| Node(): data(-1), next(NULL) {} | |
| Node(int d, Node* n = NULL) { | |
| this->data = d; | |
| if(n) | |
| this->next = n; | |
| else | |
| this->next = NULL; | |
| } | |
| void setMeData(int d) { this->data = d; } | |
| void setMeTheNextOne(Node* n) { this->next = n; } | |
| Node* nextInLine() { return this->next; } | |
| int showMeTheData() { return this->data; } | |
| }; | |
| class List { | |
| private: | |
| Node* head; | |
| public: | |
| List(): head(NULL){} | |
| List(Node* h) { this->head = h; } | |
| Node * getMeTheHead() { return this->head; } | |
| void appendMeANode(Node* newNode) { | |
| Node* imNext = this->head; | |
| bool iterate = (imNext == NULL ? false : true); | |
| while(iterate) { | |
| if(imNext->nextInLine() == NULL) { | |
| imNext->setMeTheNextOne(newNode); | |
| iterate = false; | |
| } else { | |
| imNext = imNext->nextInLine(); | |
| } | |
| } | |
| } | |
| bool deleteThisDamnNode(Node* delete_me) { | |
| Node* imNode = this->head; | |
| bool iterate = (imNode == NULL ? false : true); | |
| bool return_me = false; | |
| while(iterate) { | |
| if (imNode->nextInLine() == delete_me) { | |
| imNode->setMeTheNextOne(delete_me->nextInLine()); | |
| delete delete_me; | |
| return_me = true; | |
| iterate = false; | |
| break; | |
| } else { | |
| imNode = imNode->nextInLine(); | |
| } | |
| if(imNode == NULL) { | |
| iterate = false; | |
| } | |
| } | |
| return return_me; | |
| } | |
| Node* findMeTheNodeWith(int data) { | |
| Node* imNode = this->head; | |
| bool iterate = (imNode == NULL ? false : true); | |
| while(iterate) { | |
| if (imNode->showMeTheData() == data) { | |
| iterate = false; | |
| break; | |
| } else { | |
| imNode = imNode->nextInLine(); | |
| } | |
| } | |
| return imNode; | |
| } | |
| vector<Node*> findMeAllNodesWith(int data) { | |
| vector<Node*> allFinds; | |
| Node* imNode = this->head; | |
| bool iterate = (imNode == NULL ? false : true); | |
| while(iterate) { | |
| if (imNode->showMeTheData() == data) { | |
| allFinds.push_back(imNode); | |
| } else { | |
| imNode = imNode->nextInLine(); | |
| } | |
| if(imNode == NULL) { iterate = false; } | |
| } | |
| return allFinds; | |
| } | |
| }; | |
| int main() | |
| { | |
| Node* cabeza = new Node(0); | |
| List* list = new List(cabeza); | |
| for(int i = 1; i <= 10; i++) { | |
| Node* node = new Node(i); | |
| list->appendMeANode(node); | |
| } | |
| Node * c = list->getMeTheHead(); | |
| while(c->nextInLine()) { | |
| cout << c->showMeTheData() << endl; | |
| c = c->nextInLine(); | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment