// Chapter 4, pp 174 - 177 // Demonstrates SaveList and RestoreList; // includes DisplayList. #include // includes iostream.h #include "boolean.h" const int NAME_LENGTH = 12; typedef char nameType[NAME_LENGTH]; typedef struct node* ptrType; //ptr to node struct node { int Data; ptrType Next; }; // end struct void SaveLinkedList(ptrType Head, nameType SaveFileName) // -------------------------------------------------------------- // Saves a linked list's data in a binary file of integers. // Precondition: Head points to the linked list. // SaveFileName is the name of an external general file to // be created. // Postcondition: The binary file SaveFileName of integers // contains the linked list's data. The file is closed; // Head and the linked list are unchanged. // -------------------------------------------------------------- { ofstream F(SaveFileName, ios::binary); // output binary file // traverse the list to the end, writing out each item for (ptrType Cur = Head; Cur != NULL; Cur = Cur->Next) F << Cur->Data << " "; //without the above " ", the integers 4, 2, 8, 6 // are read back as one integer 4286. F.close(); } // end SaveLinkedList void RestoreLinkedList(ptrType& Head, nameType SaveFileName) // --------------------------------------------------------- // Creates a linked list from the data in a binary file. // Precondition: SaveFileName is the name of an existing // external binary file of integers. // Postcondition: Head points to the created linked list; // if the file is empty, Head is NULL. The file is unchanged // and closed. // --------------------------------------------------------- { int NextValue; ifstream F(SaveFileName, ios::binary); // input binary file if (F >> NextValue) // is file empty? { // file not empty: // add the first integer to the list Head = new node; Head->Data = NextValue; Head->Next = NULL; ptrType Tail = Head; // add the remaining integers to the linked list while (F >> NextValue) { Tail->Next = new node; Tail = Tail->Next; Tail->Data = NextValue; Tail->Next = NULL; } // end while } // end file not empty else Head = NULL; // file is empty: return empty list F.close(); } // end RestoreLinkedList void DisplayList(ptrType Head) // -------------------------------------------------------------- // Displays the data in a linked list. // Precondition: Head is a pointer variable that points to // the linked list. // Postcondition: Displays the data in the linked liste. // Both Head and the list are unchanged. // -------------------------------------------------------------- { // Loop invariant: Cur points to the next node to be displayed for (ptrType Cur = Head; Cur != NULL; Cur = Cur->Next) cout << Cur->Data << "\n"; } // end DisplayList // ******SAMPLE MAIN PROGRAM****** main() { ptrType LLHead, NewHead, TempPtr; booleanType Success; // create a list of integers in any order LLHead = NULL; LLHead = new(node); LLHead->Data = 4; TempPtr = LLHead; TempPtr->Next = new node; TempPtr = TempPtr->Next; TempPtr->Data = 2; TempPtr->Next = new node; TempPtr = TempPtr->Next; TempPtr->Data = 8; TempPtr->Next = new node; TempPtr = TempPtr->Next; TempPtr->Data = 6; TempPtr->Next = NULL; cout << "The current list is:" << "\n"; DisplayList(LLHead); // save the list in a general file SaveLinkedList(LLHead, "C4LL.DAT"); cout << "\nGeneral File written.\n"; // create a list from the data in the file RestoreLinkedList(NewHead, "C4LL.DAT"); cout << "This is the restored list of integers, as read from the file:\n"; DisplayList(NewHead); cout << "Program End.\n"; return(0); } // end main