2011-03-05 56 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; 
} 

有乐趣!