0
I’m having a problem with the method delete_find_number
of my code.
It is as follows: this method must exclude a specific number which is passed as a parameter. After receiving the parameter, this method calls another method called findNumber
to search for the value and check if it exists.
When I add three values to my list and ask the program to delete a value, and if that value is the last one on the list, the program gives crash and closes (the same happens if the value is the first in the list). If it is an intermediate value, it excludes correctly.
EDIT:I modified some things in the code,I removed the find method and passed the entire task of deleting to the method delete_find_number
,added delete to 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 method delete_find_number
when the list has only one element, the crash program. If I test with 3 elements and request the exclusion of a number, and that number is the last one on the list, also from the crash.
Follows 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");
}
}
There’s a lot you’re not doing and another wrong. Missing
delete
every time you remove a node. To remove the node you have to change the pointer from the previous one. Classes must have the name in the singular, etc..– Isac
if(Node->next->value == num) should be if(Node->value == num).. such that you do not check whether the value passed exists in the first element.. and if by chance you reach the end of the list without finding vc will try to access the next one which is null... and will give pal. However, as it is a simple list. In order for you to remove the last element you have to have a reference to the previous one, you must update your code so that, or you keep this reference and send it to your methods to remove correctly. or send both nodes (the previous one and the one you will edit) so you can remove correctly in these cases.
– Israel Merljak
In the search method you could check first if the value you are looking for exists at any of the extremes, only check the middle of the list case in a != Tail->value and head->value. dai vc roda teu while while Node != Tail, evita null Pointer.
– Israel Merljak