Thursday, 15 August 2013

Linked List C++. insertion, deletion, search -



Linked List C++. insertion, deletion, search -

any advice or guidance great...

#include <iostream> #include <string.h> using namespace std; struct book { char title[1024]; // book name int id; // book id or isbn number float price; // book cost struct book* next; } *collection = null; //-- forwards declaration --// void menu(); void branching(char option); void insertion(); void printall(); struct book* search(); void deletion(); void quit(struct book* head); int main() { char ch; cout << "\n\nwelcome cse240: bookstore\n"; { menu(); ch = tolower(getchar()); // read char, convert lower case cin.ignore(); branching(ch); } while (ch != 'q'); homecoming 0; } void menu() { cout << "\nmenu options\n"; cout << "------------------------------------------------------\n"; cout << "i: insert book\n"; cout << "d: delete book\n"; cout << "s: search book\n"; cout << "p: review list\n"; cout << "q: quit\n"; cout << "\n\nplease come in selection (i, d, s, p, or q) ---> "; } void branching(char option) { switch(option) { case 'i': insertion(); break; case 'd': deletion(); break; case 's': search(); break; case 'p': printall(); break; case 'q': quit(collection); collection = null; break; default: cout << "\nerror: invalid input. please seek again..."; break; } } void insertion() { // add together code insert new book collection linked list. // hint: can insert new book @ origin of linked list } struct book* search() { // add together code search existing book in collection linked-list // hint: if no book matches tile homecoming error messag homecoming null; } void deletion() { // add together code deletion method. must phone call "delete" remove object heap. } void printall() { // add together code print book collection. (hint: need utilize loop.) } void quit(struct book* head) { // add together code delete objects/books lniked-list. // hint: refer slides in homework assignment }

i give clue insertion() - assuming have book want insert

void insertion(book* newbook) { // new book inserted @ origin of linked list, // original list next on newbook->next = collection; // new origin of collection should new book collection = newbook; }

and search() - assuming searching book particular id

book* search(int id) { book* current = collection; while(current != null) { if(current->id == id) { // found homecoming current; } // seek next book in list current = current->next; } // reached end, still not find book looking homecoming null; }

c++ search linked-list

No comments:

Post a Comment