#include using namespace std; struct Node { int data; Node* previous; Node* next; }; Node* g_firstNode = 0; Node* g_currentNode = 0; // // print all the nodes // void PrintAllNodes() { // is list empty? if (g_firstNode == 0) { // print empty message cout << "List is empty." << endl; } else { // traverse list Node* p = g_firstNode; while (p != 0) { cout << p->data; p = p->next; if (p != 0) { cout << ", "; } } cout << endl << endl; } } // // deletes the current node // void Delete() { // if currentNode is 0, then there are no nodes in the list if (g_currentNode == 0) { cout << "There are no nodes in the list." << endl; return; } // delete this node // if there is a previous node, then link it to the next node // (works even if the next node is 0) if (g_currentNode->previous != 0) { g_currentNode->previous->next = g_currentNode->next; } else { // else, set the first node to the next node g_firstNode = g_currentNode->next; } // if there is a next node, then link it to the previous node // also, move the current node to the next node, and delete the current // (works even if the previous node is 0) if (g_currentNode->next != 0) { g_currentNode->next->previous = g_currentNode->previous; Node* next = g_currentNode->next; delete g_currentNode; g_currentNode = next; } else { // else, move the current node to the previous one, and delete the current Node* previous = g_currentNode->previous; delete g_currentNode; g_currentNode = previous; } cout << "Node deleted." << endl; } // // insert a node after current // void Insert() { // create the node and get the data from the user Node* newNode = new Node; cout << "Enter an integer value for the new node : " << flush; cin >> newNode->data; // if current node is 0, then the list is empty if (g_currentNode == 0) { // add node the first item, and set current to new node newNode->previous = 0; // no links after newNode->next = 0; // no links before g_firstNode = newNode; g_currentNode = newNode; } else { // add the node after this item, and increment current to the new item newNode->previous = g_currentNode; // new node links back to current newNode->next = g_currentNode->next; // new node links forward to the next node if (g_currentNode->next != 0) // link next node backwards { // there exists a next node, so link is backwards to new node g_currentNode->next->previous = newNode; } g_currentNode->next = newNode; // current links forward to new node g_currentNode = newNode; // set current node to new node } } // // move to the next node // void Next() { // list empty? if (g_currentNode == 0) { cout << "List is empty." << endl; return; } // is there a next node? if (g_currentNode->next != 0) { // there is, move to it g_currentNode = g_currentNode->next; } else { // else, display message cout << "Can't move to the next node, because I am at the end of the list." << endl; } } // // move to the previous node // void Previous() { // list emtpy? if (g_currentNode == 0) { cout << "List is empty." << endl; return; } // is there a previous node? if (g_currentNode->previous != 0) { // there is, move to it g_currentNode = g_currentNode->previous; } else { // else, display message cout << "Can't move to the previous node, because I am at the beginning of the list." << endl; } } // // prints the console controls // void PrintHelp() { cout << "HELP" << endl; cout << "? - help" << endl; cout << "l - List all node data" << endl; cout << "d - Delete current node" << endl; cout << "i - Insert a node after current" << endl; cout << "n - move current node to Next node" << endl; cout << "p - move current node to Previous node" << endl; cout << "x - eXit" << endl; } // // program entry // int main() { cout << "Doubly linked list test console." << endl; cout << "Hit '?' for a list of options." << endl; char option = '\0'; do { // print command prompt and get option cout << endl; if (g_currentNode != 0) { // if the list isn't empty, print the current node cout << "Current node value : " << g_currentNode->data << endl; } cout << "Command : " << flush; cin >> option; cout << endl; // make lowercase is uppercase if ((option >= 'A') && (option <='Z')) { option += 'a' - 'A'; } // dispatch to appropriate routines based on option switch (option) { case '?': PrintHelp(); break; case 'l': PrintAllNodes(); break; case 'd': Delete(); break; case 'i': Insert(); break; case 'n': Next(); break; case 'p': Previous(); break; case 'x': break; default: cout << "Unrecognized option. Hit '?' for a list of options." << endl; break; } } while (option != 'x'); return 0; }