Skip to content

Instantly share code, notes, and snippets.

@senapk
Last active May 31, 2016 21:21
Show Gist options
  • Save senapk/42b0f92297cadd78a23509b0bdecf73c to your computer and use it in GitHub Desktop.
Save senapk/42b0f92297cadd78a23509b0bdecf73c to your computer and use it in GitHub Desktop.
Lista ligada utilizando smart find com ponteiro duplo
#include <iostream>
using namespace std;
struct snode{
int value;
snode * next;
snode(int v = 0, snode * n = nullptr){
value = v;
next = n;
}
};
struct slist{
snode * head;
slist(){
head = new snode(0);
}
snode ** _find(snode ** node, int value){
if(*node == nullptr)
return node;
if((*node)->value == value)
return node;
return _find(&(*node)->next, value);
}
snode ** find(int value){
return _find(&(head->next), value);
}
void remove(int value){
auto place = find(value);
if(*place == nullptr)
return;
snode * next = (*place)->next;
delete (*place);
*place = next;
}
snode ** end(snode ** node){
if(*node == nullptr)
return node;
return end(&(*node)->next);
}
void push_back(int value){
auto place = end(&head);
*place = new snode(value);
}
};
ostream & operator <<(ostream & os, slist lista){
auto node = lista.head->next;
os << "[ ";
while(node != nullptr){
os << node->value << " ";
node = node->next;
}
os << "]" << endl;
return os;
}
int main(){
slist lista;
string op = "";
while(op != "q"){
cout << "i value - push_back; r value - remove; q quit" << endl;
cin >> op;
if(op == "i"){
int value;
cin >> value;
lista.push_back(value);
cout << lista << endl;
}
if(op == "r"){
int value;
cin >> value;
lista.remove(value);
cout << lista << endl;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment