// // 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 *, Record *, Record *, Record *); 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; pactive = pfirst; while (strcmp (pactive->number, del) != 0) { pactive = pactive->pnext; } Delete(pactive, pcurrent, pfirst, plast); } else cout << endl << "Invalid choice!"<< endl; } while (1); return 0; } void Delete(Record *pactive, Record *pcurrent, Record *pfirst, Record *plast) { cout << "\n\nppactive/first word --==> " << pactive->word; if (pactive == pfirst) { cout << "\n\nFirst"; pfirst = pfirst->pnext; pfirst->pprev = 0; pactive->pnext = 0; Record *pactive = 0; delete pactive; } else if (pactive == plast) { cout << "\n\nlast"; pcurrent = pactive->pprev; pcurrent->pnext = 0; pcurrent->pprev = pactive->pprev; pcurrent->pnext = 0; pactive->pprev = 0; Record *pactive = 0; delete pactive; } else { cout << "\n\nmiddle"; pcurrent = pactive->pprev; pcurrent->pnext = pactive->pnext; pcurrent->pnext = pactive->pprev; pactive->pnext = 0; pactive->pprev = 0; // Record *pactive = 0; delete pactive; } return; } void Forward_show_list(Record *pforward) { if (pforward == NULL) cout << "\n\nNo Data available!" << endl; else { while(pforward != NULL) { cout << "\n-----------------"; cout << "\nWord number: "<< pforward->number << endl; cout << "Word: " << pforward->word << endl; pforward = pforward->pnext; } } return; } void Reverse_show_list(Record *preverse) { if (preverse == NULL) cout << "\n\nNo Data available!" << endl; else { while(preverse != NULL) { cout << "\n-----------------"; cout << "\nWord number: "<< preverse->number << endl; cout << "Word: " << preverse->word << endl; preverse = preverse->pprev; } } return; }