/**************************************************** * Purpose: * Demonstrate the use of basic operations * on a singly linked list. * ********************************************************/ #include #include "new-llist.h" using namespace std; void PrintList(Node *Head); /***************** Main Function Starts Here ***********/ int main() { Node *Head; Node *NewNode_Ptr; Node *Curr_Ptr; Node *Prev_Ptr; int Value; /*------------Create a linked list--------------------------*/ cout << "Enter values for linked list, one per line." << endl << "Enter 999 to end list." << endl; Head = new Node; cin >> Head->data; Curr_Ptr = Head; cin >> Value; while (Value != 999) { NewNode_Ptr = new Node; NewNode_Ptr->data = Value; Curr_Ptr->next = NewNode_Ptr; Curr_Ptr = NewNode_Ptr; cin >> Value; } // end while Curr_Ptr->next = NULL; // mark the tail PrintList(Head); // display the list /*------------Add a node to the linked list-------------------*/ cout << "Enter a value for a new last node: "; cin >> Value; /**************** ADD CODE HERE TO ADD A NODE TO THE END OF THE LINKED LIST *********************/ cout << endl << "The list after the addition follows:" << endl; PrintList(Head); // display the list /*------------Insert a node in linked list-------------------*/ cout << "Enter the value of node to insert in the list: "; cin >> Value; NewNode_Ptr = new Node; NewNode_Ptr->data = Value; Prev_Ptr = NULL; Curr_Ptr = Head; while ( (Curr_Ptr != NULL) && (Value > Curr_Ptr->data) ) { Prev_Ptr = Curr_Ptr; Curr_Ptr = Curr_Ptr->next; } if (Prev_Ptr == NULL) Head = NewNode_Ptr; else Prev_Ptr->next = NewNode_Ptr; NewNode_Ptr->next = Curr_Ptr; cout << endl << "The list after the insertion follows:" << endl; PrintList(Head); // display the list /*------------Delete a node from linked list-------------------*/ Node *Del_Ptr; cout << "Enter the value of a node to delete: "; cin >> Value; /************ ADD CODE HERE TO DELETE A NODE ***************/ // delete Del_Ptr; cout << endl << "The list after the deletion follows:" << endl; PrintList(Head); // display the list return 0; } // end main /***********************************************************/ /*--------------------------------------------------------- * Function Name: PrintList * Parameters: pointer to the head of a linked list * Returns: nothing -----------------------------------------------------------*/ void PrintList(Node *Head) { cout << endl << "The list contains the following values." << endl; /********** ADD A LOOP HERE TO PRINT OUT THE VALUES *******/ } // end PrintList