2013-02-19 63 views
-2

任何意見或指導將是非常巨大的...鏈接列表C++。插入,刪除,搜索

#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 price 
struct Book* next; 
    } *COLLECTION = NULL; 

//-- Forward 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 to CSE240: Bookstore\n"; 

do { 
    menu(); 

    ch = tolower(getchar()); // read a char, convert to lower case 
    cin.ignore(); 

    branching(ch); 
} while (ch != 'q'); 

return 0; 
} 

void menu() 
    { 
cout << "\nMenu Options\n"; 
cout << "------------------------------------------------------\n"; 
cout << "i: Insert a book\n"; 
cout << "d: Delete a book\n"; 
cout << "s: Search a book\n"; 
cout << "p: Review your list\n"; 
cout << "q: Quit\n"; 
cout << "\n\nPlease enter a choice (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 try again..."; 
    break; 
} 
} 


    void insertion() 
    { 
// add code to insert a new book into the COLLECTION linked list. 
// HINT: You can insert a new book at the beginning of the linked list 


} 

    struct Book* search() 
{  
// add code to search for an existing book in the COLLECTION linked-list 
// HINT: If no book matches the tile return a error messag 



return NULL; 
} 

void deletion() 
{ 
// add code to the deletion method. You must call "delete" to remove the object from the heap. 
    } 

    void printall() 
    { 
// Add code to print the book collection. (HINT: You will need to use a loop.) 

    } 

    void quit(struct Book* HEAD) 
    { 
// Add code to delete the objects/books from the lniked-list. 
// HINT: Refer to the slides in the homework assignment 


    } 
+0

問題是什麼?你期望你的程序做什麼?它實際上做了什麼? – atk 2013-02-19 22:27:51

+2

在瞭解列表(和其他數據結構)時,我發現可以繪製列表的當前狀態的一張紙,操作之後希望列表處於的狀態,並且看到需要更新的狀態應用通常有幫助。除此之外,如果你已經在這裏工作了幾個小時,你應該至少有一些你可以展示的功能的粗略近似,解釋你打算做什麼... – 2013-02-19 22:30:15

+1

現在是最大的問題看到沒有嘗試實際*實現你提供的函數的骨架。 – WhozCraig 2013-02-19 22:30:47

回答

0

我會給你一個線索爲insertion() - 假設你要插入

void insertion(Book* newBook) 
{ 
    // The new book will be inserted at the beginning of the linked list, 
    // with the original list following on from it 
    newBook->next = COLLECTION; 

    // The new beginning of the collection should be the new book 
    COLLECTION = newBook; 
} 

一本書並且對於search() - 假設您正在搜索具有特定ID的書籍

Book* search(int id) 
{ 
    Book* current = COLLECTION; 

    while(current != NULL) 
    { 
    if(current->id == id) 
    { 
     // Found it 
     return current; 
    } 

    // Try the next book in the list 
    current = current->next; 
    } 

    // Reached the end, but still could not find the book you are looking for 
    return NULL; 
} 
+1

我會使用'while(current!= NULL)'而不是第一個,如果最後是'return current'。這是相同的效果,但更清潔,恕我直言。 – MatheusOl 2013-02-20 00:11:21

+0

非常真實!感謝馬修斯。我編輯了我的解決方案以反映您的建議。 – Steztric 2013-02-20 10:28:13