// Chapter 4, p 179 // Displays a circular Linked List #include typedef char dataType; typedef struct node* ptrType; //ptr to node struct node { dataType Data; ptrType Next; }; // end struct void Display(dataType Data); void DisplayList(ptrType List) // --------------------------------------------------------- // Displays the data in a circular linked list. // Precondition: List is a pointer variable that points to // the last node of a circular linked list. // Postcondition: The data in the linked list is displayed. // Both the external pointer List and the linked list are unchanged. // Calls: Display. // --------------------------------------------------------- { if (List != NULL) { // list is not empty ptrType Cur = List; // start at head of list // Loop invariant: Cur points to the next node to display do { Cur = Cur->Next; // point to next node Display(Cur->Data); // write data portion } while (Cur != List); // quit at end of list } // end if } // end DisplayList void Display(dataType Data) { cout << Data << endl; } // end Display // ******* SAMPLE MAIN ********* main() { ptrType LLHead, FirstPtr, TempPtr; FirstPtr = new(node); // save ptr to first node FirstPtr->Data = 'b'; TempPtr = FirstPtr; TempPtr->Next = new(node); TempPtr = TempPtr->Next; TempPtr->Data = 'g'; TempPtr->Next = new(node); TempPtr = TempPtr->Next; TempPtr->Data = 'm'; TempPtr->Next = new(node); TempPtr = TempPtr->Next; LLHead = TempPtr; // head ptr points to last node TempPtr->Data = 'w'; TempPtr->Next = FirstPtr; // last node points to first node cout << "The current list is:" << "\n"; DisplayList(LLHead); cout << "\nProgram End.\n"; return 0; }