// // CS 202 program 10_linklist2.cpp // Written by D.C. Williams for 10/28/03 class // // Implementation of a doubly linked list; doubly linked lists // have two pointers, one pointing forward and one pointing backwards. // // This version does not require a leading blank record. // #include #include #include const int MAXCHAR = 20; // for limiting the size of the strings struct Record { char number[MAXCHAR]; // each word has an assigned number char word[MAXCHAR]; // the word itself Record *pnext; // pointer to next record Record *pprev; // pointer (to previous record }; void Forward_show_list(Record *); void Reverse_show_list(Record *); void Delete(Record *, char del); int main() { char choice; char del[MAXCHAR]; Record *pnew = 0; Record *pfirst = 0; Record *plast = 0; Record *pcurrent = 0; Record *pactive = 0; Record *pprev_t = 0; // temporary pointers Record *pnext_t = 0; // temporary pointers cout << "\n\nProgram to manipulate words using a doubly linked list"; do { cout << "\n\n-----------------------------------------------"; cout << "\n(c)reate new record (q)uit" ; cout << "\n(d)elete record" ; cout << "\n(f)orward view list (r)everse view list" ; cout << "\n\nPlease select: "; cin >> choice; cin.ignore(); if (choice == 'q') exit(0); else if (choice == 'c') { pnew = new Record; // store address of newly-created record in pnew if (pfirst == 0) // if this is the first record, { pfirst = pnew; // then set these pcurrent = pnew; pnew->pprev = 0; } cout << "\n\nEnter data for new record: " << endl; cout << "\nWord number: "; cin.getline(pnew->number,MAXCHAR,'\n'); cout << "\nWord: "; cin.getline(pnew->word,MAXCHAR,'\n'); pnew->pnext = 0; // zero the forward pointer in new record if (pcurrent != pnew) // set pointers if not the first record { pcurrent->pnext = pnew; pnew->pprev = pcurrent; } pcurrent = pnew; // set pcurrent to filled-in pnew plast = pcurrent; // set plast to new end of list } else if (choice == 'f') Forward_show_list(pfirst); // show the whole list, forward direction else if (choice == 'r') Reverse_show_list(plast); // show the whole list, reverse direction else if (choice == 'd') { cout << "\n\n\nSelect a record to delete:" << endl; Forward_show_list(pfirst); cout << "\n\n\n\nDelete Word Number -->"; cin >> del; Delete(pfirst, del); } else cout << endl << "Invalid choice!"<< endl; } while (1); return 0; } void Delete(Record *pfirst, char del) { cout << "\n\npfirst word --==> " << pfirst->word; if (pfirst->number = del ) cout << endl << del; return; }