Skip to content

Instantly share code, notes, and snippets.

@estebanz01
Created September 20, 2017 02:21
Show Gist options
  • Select an option

  • Save estebanz01/1fab80596f4f0dca508c7e633f79780c to your computer and use it in GitHub Desktop.

Select an option

Save estebanz01/1fab80596f4f0dca508c7e633f79780c to your computer and use it in GitHub Desktop.
Listas doblemente ligadas circulares
// Example program
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Node {
public:
int data;
bool tail;
Node* next;
Node* before;
Node(): data(-1), tail(false), next(NULL), before(NULL) {}
Node(int d, tail = false, Node* n = NULL, Node* b = NULL) {
this->data = d;
this->tail = tail;
if(n)
this->next = n;
else
this->next = NULL;
if(b)
this->before = b;
else
this->before = NULL;
}
void setMeData(int d) { this->data = d; }
void setMeTheNextOne(Node* n) { this->next = n; }
void setMeTheBeforeMe(Node* b) { this->before = b; }
void imTail() { this->tail = true; }
void imNotLongerTail() { this->tail = false; }
Node* nextInLine() { return this->next; }
Node* beforeInLine() { return this->before; }
int showMeTheData() { return this->data; }
bool amITail() { return this->tail; }
};
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() == this->head || imNext->nextInLine() == NULL) {
imNext->setMeTheNextOne(newNode);
newNode->setMeTheBeforeMe(imNext);
newNode->setMeTheNextOne(this->head);
this->head->setMeTheBeforeMe(newNode);
newNode->imTail();
imNext->imNotLongerTail();
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_me->nextInLine()->setMeTheBeforeMe(imNode);
delete delete_me;
return_me = true;
iterate = false;
break;
} else {
imNode = imNode->nextInLine();
}
if(imNode.amITail()) {
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 if(imNode->amITail()) {
iterate = false;
imNode = NULL;
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->amITail()) { 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