Visualizzazione dei risultati da 1 a 8 su 8
  1. #1

    Help Puntatori In Lista!!! Urgente!!!! :''((

    Qualcuno mi può aiutare ad individuare l'errore "invalid conversion from int to node *" che mi assilla in questo programma?? Ho bisogno con urgenza per l'esame di Algoritmi e Strutture di Dati di creare una lista doppiamnete linkata di interi, ma proprio non ci riesco. Ogni aiuto è graditissimo. Grazie mille.


    #include <iostream.h>

    class node { // A simple class
    protected:
    int number; // Value stored in the node
    node *next; // Pointer to next node
    node *prev; // Pointer to previous node

    public:
    node() { next = NULL; prev = NULL; } // With a constructor
    void insertBefore(int number, node *nodeB); // Seven methods
    void insertAfter(int number, node *nodeA);
    void removeBefore(node *nodeB);
    void removeAfter(node *nodeA);
    void removeNode(node *newNode);
    void printDListFront();
    void printDListBack();
    ~node(); // and a destructor
    };


    // This function inserts a node before nodeB
    void node::insertBefore(int number, node *nodeB)
    {
    node *newNode;
    newNode = new node;
    newNode->prev = nodeB->prev;
    newNode->next = nodeB;
    newNode->number = number;

    if (nodeB->prev == NULL) {
    this->next = newNode;
    }
    nodeB->prev = newNode;
    removeNode(newNode);
    }

    // Inserts a node after nodeB
    void node::insertAfter(int number, node *nodeA)
    {
    node *newNode;
    node *nodeB;
    newNode = new node;
    newNode->next = nodeB->next;
    newNode->prev = nodeB;
    newNode->number = number;

    if (nodeB->next == NULL) {
    cout << "Enter a digit : \n";
    cin >> number;
    this->prev = newNode;
    }
    nodeB->next = newNode;
    }

    // Removes before a node
    void node::removeBefore(node *nodeB)
    {
    if (nodeB->prev == this->next) {
    this->next = nodeB;
    this->next->prev = NULL;
    } else {
    removeNode(nodeB->prev);
    }
    }

    // Removes after a node
    void node::removeAfter(node *nodeA)
    {
    if(nodeA->next == this->prev) {
    this->prev = nodeA;
    this->prev->next = NULL;
    } else {
    removeNode(nodeA->next);
    }
    }

    // Removes a particular node
    void node::removeNode(node *nodeToRemove)
    {
    if (nodeToRemove == this->next) {
    this->next;
    this->next->prev = NULL;
    } else if (nodeToRemove == this->prev) {
    this->prev;
    this->prev->next = NULL;
    } else {
    nodeToRemove->prev->next = nodeToRemove->next;
    nodeToRemove->next->prev = nodeToRemove->prev;
    }
    }

    // Prints out the doubly linked list from front
    void node:rintDListFront()
    {
    node* temp;
    temp = this->next;
    cout << "--------------------------------\n";
    cout << "List from front : \n";
    cout << "--------------------------------\n";

    while (temp != NULL) {
    cout << "|" << temp->number << "|";
    temp = temp->next;
    }
    cout << "\n\n";
    }

    // Prints out the doubly linked list from backwards
    void node:rintDListBack()
    {
    node* temp;
    temp = this->prev;
    cout << "--------------------------------\n";
    cout << "List from backwards : \n";
    cout << "--------------------------------\n";

    while (temp != NULL) {
    cout << "|" << temp->number << "|";
    temp = temp->prev;
    }
    cout << "\n\n";
    }


    int main()
    {
    node *llista;
    int num, NodeA, NodeB, NewNode;
    int choice;
    llista->node();

    switch (choice) {
    cout << "Enter 1 to insert a node before in the list; 2 to insert a node after; 3 to remove a node before; 4 to \n";
    cout << "remove a node after; 5 to remove any node; 6 to print out the doubly linked list frontally; 7 to print \n";
    cout << "out the doubly linked list from backwards; 0 to exit.\n\n";
    cout << "Please enter your choice by number : \n";
    cin >> choice;

    case 1 :
    llista->insertBefore(num, NodeB);
    break;

    case 2 :
    llista->insertAfter(num, NodeA);
    break;

    case 3 :
    llista->removeBefore(NodeB);
    break;

    case 4 :
    llista->removeAfter(NodeA);
    break;

    case 5 :
    llista->removeNode(NewNode);
    break;

    case 6 :
    llista->printDListFront();
    break;

    case 7 :
    llista->printDListBack();
    break;

    case 0 :
    cout << "Exiting program!\n\n";
    break;

    default :
    cout << "Sorry! Bad menu selection!\n\n";
    break;
    }

    system("pause");
    return 0;
    }

  2. #2
    Moderatore di Programmazione L'avatar di alka
    Registrato dal
    Oct 2001
    residenza
    Reggio Emilia
    Messaggi
    24,313

    Moderazione

    Ho corretto il titolo indicando il linguaggio e l'errore riscontrato.

    Invito a leggere il Regolamento per conoscere tutte le norme da seguire nella partecipazione e nell'apertura di discussioni.
    MARCO BREVEGLIERI
    Software and Web Developer, Teacher and Consultant

    Home | Blog | Delphi Podcast | Twitch | Altro...

  3. #3
    Utente di HTML.it L'avatar di Stoicenko
    Registrato dal
    Feb 2004
    Messaggi
    2,254
    se non dici la riga difficile trovare..

    ps: linguaggio in tag code

    cmq il problama è che la insertBefore vuole come se3condo parametro un node* e tu gli stai passando un int

  4. #4
    Grazie per la risposta, Stoicenko.. Purtroppo non riesco a capire ancora dove sia l'errore dato che comunque il secondo parametro della funzione insertBefore(int numero, node *nodeB) è un puntatore di tipo node.. L'errore inoltre riguarda tutte le funzioni (tranne le ultime due per la stampa), quindi le righe n.° 153, 157, 161, 165, 169. Ho provato a dichiarare node *nodeB e node *nodeA come membri della classe node, per poi poterli inizializzare all'interno del costruttore node(), ma l'errore suddetto sussiste ancora..

  5. #5
    Utente di HTML.it L'avatar di Stoicenko
    Registrato dal
    Feb 2004
    Messaggi
    2,254
    l'errore è qui:

    codice:
    int num, NodeA, NodeB, NewNode;
    NodeA e NodeB sono di tipo int..

    prova a mettere:

    codice:
    int num;
    node* NodeA, NodeB, NewNode;
    in più poi ci sono altri errori.. dichiari un puntatore a node* lista (non inizializzato) che poi vuoi usare un paio di righe dopo.. li ti darà sicuramente un Segmentation Fault

  6. #6
    Ciao, Stoicenko. Adesso grazie ai tuoi consigli il mio programma va un po' meglio, ma non funziona perfettamente. Adesso il file eseguibile stampa a video solo l'output del distruttore e del default dello switch nel main() e non riesco a visualizzare l'intero menu. Sicuramente ci saranno degli errori semantici..
    Adesso il sorgente appare così:

    codice:
    #include <iostream.h>
    
    class node {                               // A simple class
      protected:
        int number;        // Value stored in the node
        node *next;       // Pointer to next node
        node *prev;       // Pointer to previous node
        node *nodeB;
        node *nodeA;
        node *newNode;
        
      public:
        node()                                   // With a constructor
        { next = NULL; prev = NULL; number = 0; nodeB = NULL; nodeA = NULL; node *newNode = NULL; } 
        void insertBefore(node *nodeB); // Seven methods
        void insertAfter(int number, node *nodeA);
        void removeBefore(node *nodeB);
        void removeAfter(node *nodeA);
        void removeNode(node *newNode);
        void printDListFront();
        void printDListBack();
        ~node();                       // and a destructor
    };
    
    
    // This function inserts a node before nodeB
    void node::insertBefore(node *nodeB)
    {   
       newNode = new node;
       newNode->prev = nodeB->prev;
       newNode->next = nodeB;
       newNode->number = number;
       
       if (nodeB->prev == NULL) {
          this->next = newNode;	
       }
    	  nodeB->prev = newNode;
    	  removeNode(newNode);        
    }
    
    // Inserts a node after nodeB
    void node::insertAfter(int number, node *nodeA)
    {
         node *newNode;
         node *nodeB;
         newNode = new node;
         newNode->next = nodeB->next;
         newNode->prev = nodeB;
         newNode->number = number;
         
         if (nodeB->next == NULL) {
           cout << "Enter a digit : \n";
           cin >> number;
           this->prev = newNode;
         }
            nodeB->next = newNode;
    }
    
    // Removes before a node 
    void node::removeBefore(node *nodeB)
    {
       if (nodeB->prev == this->next) {
         this->next = nodeB;
         this->next->prev = NULL;
       } else {
                 removeNode(nodeB->prev);
    	 }
    }
    
    // Removes after a node 
    void node::removeAfter(node *nodeA)
    {
       if(nodeA->next == this->prev) {
         this->prev = nodeA;
         this->prev->next = NULL;
       } else {
    	         removeNode(nodeA->next);
    	 }
    }
    
    // Removes a particular node
    void node::removeNode(node *nodeToRemove)
    {
       if (nodeToRemove == this->next) {
         this->next;
         this->next->prev = NULL;
       } else if (nodeToRemove == this->prev) {
           this->prev;
           this->prev->next = NULL;
         } else {
                   nodeToRemove->prev->next = nodeToRemove->next;
                   nodeToRemove->next->prev = nodeToRemove->prev;
           }
    }
    
    // Prints out the doubly linked list from front
    void node::printDListFront()
    {
       node* temp;
       temp = this->next;
       cout << "--------------------------------\n";
       cout << "List from front : \n";
       cout << "--------------------------------\n";
       
       while (temp != NULL) {
         cout << "|" << temp->number << "|";
         temp = temp->next;
       }
          cout << "\n\n";
    }
    
    // Prints out the doubly linked list from backwards
    void node::printDListBack()
    {
       node* temp;
       temp = this->prev;
       cout << "--------------------------------\n";
       cout << "List from backwards : \n";
       cout << "--------------------------------\n";
       
       while (temp != NULL) {
         cout << "|" << temp->number << "|";
         temp = temp->prev;
       }
          cout << "\n\n";
    }
    
    node::~node()              // Destructor
    {
       cout << "Llist destructor speaking : \n";
       next = NULL; prev = NULL;
       
       if (next != NULL && prev != NULL) {
         std::cerr << "Error: trying to destroy a non-empty list!\n\n";
       }                           
    }
    
    
    int main()
    {
       node llista; 
       int num;
       node *NodeA, *NodeB, *NewNode;
       int choice;
       
       node();
       
       do {
             switch (choice) {
               cout << "Enter 1 to insert a node before in the list; 2 to insert a node after; 3 to remove a node before; 4 to \n";
               cout << "remove a node after; 5 to remove any node; 6 to print out the doubly linked list frontally; 7 to print \n";
               cout << "out the doubly linked list from backwards; 0 to exit.\n\n";
               cout << "Please enter your choice by number : \n";
               cin >> choice;
         
               case 1 :
                 cout << "Enter a digit : ";
                 cin >> num; 
                 llista.insertBefore(NodeB);
                 break;
           
               case 2 :
                 cout << "Enter a digit : ";
                 cin >> num; 
                 llista.insertAfter(num, NodeA);
                 break;
         
               case 3 :
                 cout << "Enter a digit to remove : ";
                 cin >> num; 
                 llista.removeBefore(NodeB);
                 break;
         
               case 4 :
                 cout << "Enter a digit t remove : ";
                 cin >> num; 
                 llista.removeAfter(NodeA);
                 break;
         
               case 5 : 
                 cout << "Choice a number to remove : ";
                 cin >> num;
                 llista.removeNode(NewNode);
                 break;
         
               case 6 :
                 llista.printDListFront();
                 break;
         
               case 7 :
                 llista.printDListBack();
                 break;
         
               case 0 :
                 cout << "Exiting program!\n\n";
                 break;
         
               default : 
                 cout << "Sorry! Bad menu selection!\n\n";
                 break;
             }
       } while (choice == 0);
       
    system("pause");
    return 0;
    }

  7. #7
    Utente di HTML.it L'avatar di Stoicenko
    Registrato dal
    Feb 2004
    Messaggi
    2,254
    bè subito così vedo che il cin del choice lo metti dopo lo switch e come tale è sbagliato..

    anche perchè choice non è inizializzato (chissà che valore ha) e non entra mai nello switch

    metti il cin prima e vedrai che fai già passi avanti

    ps: attenzione che stai facendo tanti errori sulle basi del linguaggio.. forse una ripassatina al tuo libro di c++ sarebbe una buona cosa

  8. #8
    Grazie ancora dei tuoi suggerimenti. Adesso il programma va molto meglio, dopo che ho rivisto e modificato il mio programma passo per passo. Effettivamente avevo fatto degli errori "inguardabili", come ad esempio l'aver scritto tutto all'interno dello switch, e non aver inizializzato la variabile choice..

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.