I'm having a problem with the delete_find_number
method of my code.
This is as follows: This method should exclude a specific number that is passed as a parameter. After receiving the parameter, this method calls another method called findNumber
to search the value and check if it exists.
When I add three values to my list, I ask the program to delete a value and, if this value is the last one in the list, the program crashes and closes (the same happens if the value is the first on the list). If it is an intermediate value, it deletes it correctly.
EDIT: I modified some things in the code, removed the find method and passed the entire deleting task to the delete_find_number
method, added the delete to the nodes that were deleted from the list and added the check to test if the searched value is in head or tail.
However, if I try to use the delete_find_number
method when the list has only one element, the crash program. If I test with 3 elements and request the deletion of a number, and that number is the last one on the list, also the crash.
Follow the code:
class Node{
public:
int value;
Node *next;
Node(){
next = NULL;
}
};
#include "Node.h"
#include <iostream>
using namespace std;
class List{
public:
Node *head;
Node *tail;
Lists(){
head = NULL;
tail = NULL;
}
void push_back(int num){
Node *node = new Node;
node->value = num;
if(head == NULL){
head = node;
tail = node;
}else{
Node *aux = tail;
tail = node;
aux->next = node;
}
}
void print(){
if(head == NULL){
cout << "A lista esta vazia, nao ha nada a exibir\n";
return;
}else{
Node *print = head;
cout << "Elementos da lista: ";
while(print != NULL){
cout << print->value << " ";
print = print->next;
}
cout << endl;
}
}
void push_after_aleatory(int num,Node *p){
if(p == tail){
return push_back(num);
}else{
Node *node = new Node;
node->value = num;
node->next = p->next;
p->next = node;
return;
}
}
void delete_after_aleatory1(Node *p){
if(head == NULL){
cout << "A lista esta vazia,nao ha nada a excluir\n";
return;
}
if(p == tail){
cout << "Nao e possivel excluir, nao ha nenhum elemento apos o no repassado\n";
return;
}
if(p == head){
if(head->next != NULL){
Node *aux = head->next;
head->next = head->next->next;
cout << "Exclusao concluida\n";
delete aux;
return;
}
cout << "A lista possui apenas um elemento, nao e possivel excluir";
return;
}else{
Node *aux = p->next;
p->next = p->next->next;
cout << "Exclusao concluida\n";
delete aux;
return;
}
}
void delete_find_number(int num){
if(head == NULL){
cout << "A lista esta vazia, nao ha nada a excluir\n";
return;
}
if(tail->value == num){
Node *aux = tail;
Node *pointer = head;
while(pointer->next != tail){
pointer = pointer->next;
}
tail = pointer;
tail->next = NULL;
delete aux;
return;
}else if(head->value == num){
Node *aux = head;
head = head->next;
delete aux;
return;
}else{
Node *node = head;
while(node != NULL){
if(node->next->value == num){
Node *aux = node->next;
node = node->next->next;
aux->next = NULL;
delete aux;
return;
}
node = node->next;
}
}
cout << "Elemento nao encontrado, nao e possivel excluir\n";
}
};
#include <iostream>
#include "List.h"
#include <stdlib.h>
using namespace std;
int main(int argc, char** argv) {
List list;
int sentinela,numero;
while(sentinela != 5){
system("cls");
cout << "Opcoes do menu:\n\n1-Inserir\n2-Excluir(parametro ponteiro)\n3-Excluir(parametro numero)\n4-Exbir a lista\n5-Sair\n\nOpcao desejada: ";
cin >> sentinela;
if(sentinela == 1){
system("cls");
cout << "Digite um numero: ";
cin >> numero;
list.push_back(numero);
}
if(sentinela == 2){
system("cls");
list.delete_after_aleatory1(list.head);
system("PAUSE");
}
if(sentinela == 3){
system("cls");
cout << "Digite um numero: ";
cin >> numero;
list.delete_find_number(numero);
system("PAUSE");
}
if(sentinela == 4){
system("cls");
list.print();
system("PAUSE");
}
}