#include using namespace std; struct Node{ int payload; Node* next; }; const int LIST_LENGTH = 15; void PrintList( Node* startOfList ); void BuildAList( Node* headPtr, int NumberOfNodes ); Node* BuildListFromScratch( int NumberOfNodes ); Node* AddAtBack( Node* headPtr, int value ); int main() { Node* headPtr = NULL;// BuildListFromScratch( LIST_LENGTH ); headPtr = AddAtBack ( headPtr, 55 ); BuildAList( headPtr, LIST_LENGTH ); PrintList ( headPtr ); } Node* AddAtBack( Node* headPtr, int value ) { //Create a new node Node* Last = new Node; //Set new nodes payload to value Last -> payload = value ; //set new nodes next pointer to null Last -> next = NULL; //1 Determine if headPtr is null //2 If headPtr is null if ( headPtr == NULL ) { //3 Return newly created node return Last; } //4 If headPtr is not null else { // 5 Create tempPtr, set equal to headPtr Node* tempPtr = headPtr; // 6 move tempPtr down list // 6 until tempPtr -> next equals null while ( tempPtr -> next != NULL ) { tempPtr = tempPtr -> next; } // 7 M Make tempptrs next ptr equal the newly created Ptr tempPtr -> next = Last; //8 Return headPtr; return headPtr; } } Node* BuildListFromScratch( int Length ) { Node* headPtr = new Node; headPtr -> payload = 1; headPtr -> next = NULL; BuildAList( headPtr, Length ); return headPtr; } // Builds a Linked List // Precondition: headPtr refers to a linked list with at least one node // Postcondition: Builds a linked list NumberOfNodes in length void BuildAList( Node* headPtr, int NumberOfNodes ) { Node* workingPtr; Node* trailingPtr = headPtr; for ( int i = 2; i < NumberOfNodes + 1; i++ ) { workingPtr = new Node; workingPtr -> payload = i; workingPtr -> next = NULL; trailingPtr -> next = workingPtr; trailingPtr = workingPtr; } } void PrintList( Node* startOfList ) { while ( startOfList != NULL ) { cout << startOfList -> payload << endl; startOfList = startOfList -> next; } }