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