2011-03-05 59 views
0
#include <iostream> 

using namespace std; 
int main() 
{ 
    struct list 
    { 
     string name; 
     int age; 
     double height; 
     list *next; 
    }; 
    list *first,*temp,*temp2; 
    for (int i=0 ;i<4;i++) 
    { 
     list *newlist; 
     newlist = new list; 
     cout << " Enter the name : "; 
     cin >> newlist->name; 
     cout << " Enter the age : "; 
     cin >> newlist->age; 
     cout << " Enter the height : "; 
     cin >> newlist->height; 
     cout << " Name is: " << newlist->name << " " ; 
     cout << " Age is: " << newlist->age << " "; 
     cout << " Height is: " << newlist->height <<endl; 
    } 
    { 
     list *newlist1; 
     newlist1 = new list; 
     newlist1->name = "Steve"; 
     newlist1->age = 23; 
     newlist1->height = 2.3; 
     newlist1->next=temp2; 
     temp->next=newlist1; 
     newlist1->next = temp2; 
     temp->next = newlist1; 
     temp2 = newlist1->next; 
     temp2->next = newlist1->next; 
     delete temp2; 
     cout << " Name is: " << newlist1->name << " "; 
     cout << " Age is: " << newlist1->age << " "; 
     cout << " Height is: " << newlist1->height; 
    } 
} 

基本上,我在做的是創建一個鏈表,並在節點2和節點3之間插入一個新節點,並從4個節點中刪除節點號3(注意for循環是4次)。 而循環後的下一個代碼是我嘗試使用代碼插入新節點的地方。創建鏈接列表時如何爲節點分配值?

但執行它後說incompatible types in assignment of 'int' to char[20]'我不明白。 另外,我想知道我的代碼對於上述意圖是否正確。 我通過將新節點連接到下一個節點並將溫度作爲第二個節點,將temp2作爲第三個代碼...

那麼有人可以解釋錯誤的含義是什麼,這樣我就可以解決它了嗎?謝謝!

+0

請...有點你的代碼... – fpointbin

+0

非常感謝你們。 – Surya

+0

我不確定你爲什麼使用額外的大括號。 '(/ ** /){/ ** /} {/ ** /}'。另外,你應該將結構體*移到* main之外。 –

回答

0

我沒有你的功課,花一些關注評論:

#include <iostream> 
// you forgot to include string! 
#include <string> 

using namespace std; 

// type definitions usually go outside the main function! 
struct list 
{ 
    string name; 
    int age; 
    double height; 
    list *next; 
}; 

int main() 
{ 

    // mark the beginning with NULL! 
    list *first = NULL,*temp,*temp2; 

    for (int i=0 ;i<4;i++) 
    { 
    list *newlist; 
    newlist = new list; 
    cout << " Enter the name : "; 
    cin >> newlist->name; 
    cout << " Enter the age : "; 
    cin >> newlist->age; 
    cout << " Enter the height : "; 
    cin >> newlist->height; 
    cout << " Name is: " << newlist->name << " " ; 
    cout << " Age is: " << newlist->age << " "; 
    cout << " Height is: " << newlist->height <<endl; 

    // is this the first node? 
    if(first == NULL) { 
     // yes, so set the first node 
     first = newlist; 
    } else { 
     // no, set this node as the next node of the previous node! 
     temp->next = newlist; 
    } 
    // set temp to the end of the list for next iteration 
    temp = newlist; 

    } 

    // mark ending of list with NULL! 
    temp->next = NULL; 

    // creating extra node 
    list *newlist1; 
    newlist1 = new list; 
    newlist1->name = "Steve"; 
    newlist1->age = 23; 
    newlist1->height = 2.3; 

    // insert between 2 and 3 
    // temp2 holds node 3 
    temp2 = first->next->next; 

    // set the "next" pointer of element 2 to the new node 
    first->next->next = newlist1; 

    // append the rest of the old tail to the new node 
    newlist1->next=temp2; 

    // delete node 4 
    // why 4? because 4 was the old 3 ! If we deleted the current node 3 
    // we would delete the node that we just have inserted! 

    //temp2 holds node 4 
    temp2 = first->next->next->next; 
    // link node 3 to 5 
    first->next->next->next = temp2->next; 

    // delete node 4 
    delete temp2; 

    // set to beginning of list 
    temp = first; 

    // separator 
    cout<<"---------------"<<endl; 

    // output the list to make sure it's correct! 
    while(temp != NULL) { 

    cout << " Name is: " << temp->name << " "; 
    cout << " Age is: " << temp->age << " "; 
    cout << " Height is: " << temp->height<<endl; 

    temp = temp->next; 
    } 

} 
+0

在這種情況下,你真的需要包含'string'嗎?也許你這樣做,但我不知道... –

+0

omy神非常感謝你。這種方式我可以學習C++甚至更容易我會學習這個代碼如此糟糕謝謝!!!!! – Surya

+0

只是爲了記錄..代碼仍然是正確的,即使我把結構代碼放入主函數之後.. – Surya

0

你必須分配存儲器TEMP2,因爲你刪除TMP2,並沒有得到已經分配的內存...

0

爲什麼不使用classes來處理所有「重複」的東西?

struct NODE 
{ 
    string name; 
    int age; 
    double height; 
    NODE * next; 
}; 


class LIST 
{ 
private: 
    LIST(); 
    ~LIST(); 

public: 
    NODE* first = NULL; // *should* be private, but what the heck; don't edit this 
    unsigned long nodes = 0; // *should* be private, but what the heck; don't edit this 
    NODE* get(unsigned long); 
    NODE* insert(NODE*, unsigned long); 
    void destroy(unsigned long); 
}; 


// Your constructor executed at object initialization. 
LIST::LIST() 
{ 
    first = new NODE; 
    nodes = 1; 
} 


// Your destructor executed at object destruction. (Duh? ;)) 
LIST::~LIST() 
{ 
    while(nodes > 0) 
    { 
     destroy(nodes - 1); 
    } 
} 


// gets the node at the specified index ("x") 
NODE* LIST::get(unsigned long x) 
{ 
    NODE* ret; 
    ret = first; 

    // Illegal! 
    if(x >= nodes) 
     return(ret); 

    for(unsigned long i = 0; i < x; i++) 
     ret = ret->next; 

    return(ret); 
} 


// inserts the node before the specified "idx" in the list 
NODE* LIST::insert(NODE* val, unsigned long idx = nodes) 
{ 
    if(idx > nodes) 
     return(val); 
    else if(idx == nodes) 
     val->next = NULL; 
    else  
     val->next = get(idx); 

    if(idx > 0) 
    { 
     // You can probably optimize this easily. 
     // HINT: Why use 2 get()s when you can do it in one? 
     NODE* prev; 
     prev = get(idx - 1); 
     prev->next = val; 
    } 
    else 
    { 
     first = val; 
    } 

    return(val); 
} 


void LIST::destroy(unsigned long idx) 
{ 
    if(idx >= nodes) 
     return; 

    NODE* toDel; 
    toDel = get(idx); 


    if(idx == 0) 
    { 
     first = NULL; 
    } 
    else 
    { 
     NODE* prev; 
     prev = get(idx - 1); 
     if((idx + 1) < nodes) 
      prev->next = get(idx + 1); 
     else 
      prev->next = NULL; 
    } 

    delete toDel;  
    nodes--; 
} 

現在你只是做一些類似的(我不明白你想要什麼,所以我假設它的這個):

LIST list; 
NODE* nTemp; 
NODE* nTemp2; 

for(int i = 0; i < 4; i++) 
{ 
    nTemp = new NODE; 

    cout << " Enter the name: "; 
    cin >> nTemp->name; 
    cout << " Enter the age: "; 
    cin >> nTemp->age; 
    cout << " Enter the height: "; 
    cin >> nTemp->height; 

    cout << " Name is: " << nTemp->name << " "; 
    cout << " Age is: " << nTemp->age << " "; 
    cout << " Height is: " << nTemp->height << endl; 

    list.insert(nTemp); 
} 


nTemp = new NODE; 

nTemp->name = "Steve"; 
nTemp->age = 23; 
nTemp->height = 2.3; 

// Insert nTemp between "node" 2 and 3. (Assuming "node" numbering starts with 1.) 
// So between list.first->next and list.first->next->next 
list.insert(nTemp, 2); 


// Display everything! 
cout << "Displaying contents of <list>" << endl; 
for(unsigned long i = 0; i < list.nodes; i++) 
{ 
    cout << "Name is: " << list.get(i)->name << " "; 
    cout << "Age is: " << list.get(i)->age << " "; 
    cout << "Height is: " << list.get(i)->height << endl; 
} 

有樂趣!