2011-03-05 1 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を削除します(ループは4回です)。 そして、ループの後の次のコードは、新しいノードの挿入にコードを使用しようとしたところです。リンクされたリストを作成する際に、どのようにノードの値を割り当てるのですか?

実行後はincompatible types in assignment of 'int' to char[20]'と書いてありますが、わかりません。 また、私のコードが上記の意図に合っているかどうかを知りたかったのです。 新しいノードを次のノードに接続し、tempを2番目のノードに接続することでtemp2を3番目のコードにしました...

誰かがエラーの意味を説明できるので、正しく取得できますか?ありがとうございました!

+0

してください...あなたのコードと少し違います... – fpointbin

+0

ありがとうございます。 – Surya

+0

なぜあなたは余分なブレースを使用しているのか分かりません。 'for(/ ** /){/ ** /} {/ ** /} 'となります。また、struct * outside * '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

オミー神はあなたにとても感謝しています。この方法で私はC++をもっと勉強することができます。 – Surya

+0

私は構造体コードをメイン関数の中に入れても、コードはまだ正しいです。 – Surya

0

あなたがTMP2を削除しているし、それが得たメモリが割り当てられていなかったので、あなたは、TEMP2のためのメモリをアロケーションする必要が...

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; 
} 

を持っています楽しい!