Skip to content

Instantly share code, notes, and snippets.

@willianrschuck
Last active May 18, 2019 00:22
Show Gist options
  • Select an option

  • Save willianrschuck/b11c93409e115b9df3a569c439b99064 to your computer and use it in GitHub Desktop.

Select an option

Save willianrschuck/b11c93409e115b9df3a569c439b99064 to your computer and use it in GitHub Desktop.
Biblioteca de listas desenvolvidas para a Disciplina de Estrutura de Dados 1
typedef int reg;
struct no {
reg data;
no* previous;
no* next;
no() {
data = 0;
this->previous = nullptr;
this->next = nullptr;
}
};
class listaDupla {
no* lStart;
no* lEnd;
unsigned int lSize;
void imprime(no* aux) {
if(!aux) {
cout << endl;
return;
}
else {
cout << aux->data;
if(aux->next)
cout << ", ";
imprime(aux->next);
}
}
void imprimeReverso(no* aux) {
if(!aux) {
cout << endl;
return;
}
else {
cout << aux->data;
if(aux->previous)
cout << ", ";
imprimeReverso(aux->previous);
}
}
public:
listaDupla() {
this->lStart = nullptr;
this->lEnd = nullptr;
this->lSize = 0;
}
~listaDupla() {
no *aux = lStart;
while(aux){
no *noDelete = aux;
aux = noDelete->next;
delete noDelete;
}
lStart = nullptr;
lEnd = nullptr;
}
int getSize() {
return this->lSize;
}
bool insereInicio(reg info) {
no* newNo = new no();
if(!newNo) return false;
newNo->data = info;
newNo->next = this->lStart;
newNo->previous = nullptr;
if (!this->lStart) { // Lista vazia
this->lEnd = newNo;
} else {
this->lStart->previous = newNo;
}
this->lStart = newNo;
this->lSize++;
return true;
}
bool insereFim(reg info) {
no* newNo = new no();
if(!newNo) return false;
newNo->data = info;
newNo->next = nullptr;
newNo->previous = this->lEnd;
if (!this->lEnd) { // Lista vazia
this->lStart = newNo;
} else {
this->lEnd->next = newNo;
}
this->lEnd = newNo;
this->lSize++;
return true;
}
bool removerInicio() {
if (!this->lStart) {
return false;
}
no* apagar = this->lStart;
no* aux = this->lStart->next;
this->lStart = aux;
if (!this->lStart) { // Último elemento
this->lEnd = nullptr;
} else {
this->lStart->previous = nullptr;
}
delete apagar;
return true;
}
bool removerFinal() {
if (!this->lEnd) {
return false;
}
no* apagar = this->lEnd;
no* aux = this->lEnd->previous;
this->lEnd = aux;
if (!this->lEnd) { // Último elemento
this->lStart = nullptr;
} else {
this->lEnd->next = nullptr;
}
delete apagar;
return true;
}
void imprime() {
no* aux = this->lStart;
if(!aux) {
cout << "Lista Vazia" << endl;
return;
}
else {
cout << aux->data;
if(aux->next)
cout << ", ";
imprime(aux->next);
}
}
void imprimeReverso() {
no* aux = this->lEnd;
if(!aux) {
cout << "Lista Vazia" << endl;
return;
}
else {
cout << aux->data;
if(aux->previous)
cout << ", ";
imprimeReverso(aux->previous);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment