List simply chained - Problem in method

0

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");
        }


    }
    
asked by anonymous 23.02.2018 / 06:14

1 answer

1

Dude, I do not program in C ++ .. however this is a fairly common problem. So I made a quick solution here in Javascript so you can take a look at the logic. It is not complete (nor perfect), however it addresses the issue of scanning the list for the element. Remove at the beginning, middle and end without problems.

I hope it helps you solve your bug. If I need to, I can write this code in C (with a little time).

class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
};

class LinkedList {
  constructor() {
    this.head = null;
    this.tail = null;
  }

  insert(value) {
    let node = new Node(value);
    if (this.head == null) {
      this.head = node;
      this.tail = this.head;
    } else {
      this.tail.next = node;
      this.tail = this.tail.next;
    }
  }

  remove(value) {
    let tmp = null;
    if (this.head.value === value) {
      tmp = this.head;
      this.head = this.head.next;
    } else { // scan list
      let aux = this.head;
      while (aux.next != null) {
        if (aux.next.value === value) {
          tmp = aux.next;

          aux.next = aux.next.next; //update reference.
        }
        if (aux.next != null) {
          aux = aux.next; //move to next. 
        }
      }
    }

    return tmp;
  }

  print() {
    let aux = this.head;
    while (aux != null) {
      console.log(aux.value);
      aux = aux.next;
    }
    console.log("------------------------------")
  }
};

// Testando código....
let run = new LinkedList();

run.insert(1);
run.insert(2);
run.insert(3);
run.insert(4);
run.insert(5);

run.print();

run.remove(4);
run.print();
run.remove(1);
run.print();
run.remove(5);
run.print();
    
23.02.2018 / 13:54