Simply chained list - Method problem

Asked

Viewed 303 times

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..

  • 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.

  • 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.

1 answer

1


Dude, I don’t 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 deals with this question of scanning the list in search of the element. Remove at the beginning, middle and end without problems.

I hope it helps you solve your bug. In case I need it 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();

  • Very obg for the help bro, I will try to implement these changes in the code

Browser other questions tagged

You are not signed in. Login or sign up in order to post.