首页 > 解决方案 > 如果“数据”在链表中是整数类型,你可以使用 head->data 作为整数变量吗?

问题描述

如果“数据”在链表中是整数类型,你可以使用 head->data 作为整数变量吗?例如,我有一个代码,我必须将一个简单的整数变量链表转换为 2 个列表,一个带有正数,另一个带有负数。我在我的代码中找不到错误...


using namespace std;


struct Node{
int data;
Node* next;

};
Node* head=new Node;
Node* pozitiv=NULL;
Node* negativ=NULL;

Node* newNode(int data)
{
    Node* nodNou = new Node();
    nodNou -> data = data;
    nodNou -> next = NULL;
    return nodNou;
}
void Push(Node* &top, int data)
{
    Node* nodNou = newNode(data);
    nodNou -> next = top;
    top = nodNou;
}

void read(){
   Node* aux=new Node;
    int r,i,v[1000];
cout<<"insert the number of elements in list"<<endl;
cin>>r;

    cin>>v[0];
    head->data=v[0];
    head->next=NULL;

for(i=1;i<r;i++){

    cin>>v[i];
    aux->data=v[i];

    aux->next=head;

    head=aux;


aux=new Node;
}
}

write(){
if(head==NULL) return 0;
else{
    while(head!=NULL){
     cout<<head->data<<"->";
head=head->next;
    }
}
cout<<endl<<endl;
}

void stivezlutat(){

Node* aux;

pozitiv=new Node;
pozitiv->next=NULL;

negativ=new Node;
negativ->next=NULL;


while(head!=NULL){
    if(head->data >= 0){
        Push(pozitiv, head->data);
    }
    else{
        Push(negativ, head->data);
    }

head=head->next;
}
cout<<"pozitive list:"<<endl;
while(pozitiv != NULL){
    cout<<pozitiv->data<<"->";
    pozitiv=pozitiv->next;
}
cout<<endl;
cout<<"negative list:"<<endl;
while(negativ != NULL){
    cout<<negativ->data<<"->";
    negativ=negativ->next;
}

}

int main()
{

read();
write();
stivezlutat();
}```

标签: c++

解决方案


您的代码中的错误:

  • 所需#include的指令不存在。
  • 代码末尾存在额外的“```”。
  • 没有为 指定返回类型write
  • 在函数write中,return使用 with value 而return函数末尾没有语句,因此没有返回类型对此有效。
  • 该函数write破坏了 的值head,因此该函数stivezlutat将看到空列表。
  • 额外的节点被分配给pozitiv并且negativ它们被打印在 funciton 中stivezlutat

固定代码(缩进也是固定的):

#include <iostream>

using namespace std;


struct Node{
    int data;
    Node* next;

};
Node* head=new Node;
Node* pozitiv=NULL;
Node* negativ=NULL;

Node* newNode(int data)
{
    Node* nodNou = new Node();
    nodNou -> data = data;
    nodNou -> next = NULL;
    return nodNou;
}
void Push(Node* &top, int data)
{
    Node* nodNou = newNode(data);
    nodNou -> next = top;
    top = nodNou;
}

void read(){
    Node* aux=new Node;
    int r,i,v[1000];
    cout<<"insert the number of elements in list"<<endl;
    cin>>r;

    cin>>v[0];
    head->data=v[0];
    head->next=NULL;

    for(i=1;i<r;i++){

        cin>>v[i];
        aux->data=v[i];

        aux->next=head;

        head=aux;


        aux=new Node;
    }
}

void write(){
    if(head==NULL) return;
    else{
        Node* cursor = head;
        while(cursor!=NULL){
            cout<<cursor->data<<"->";
            cursor=cursor->next;
        }
    }
    cout<<endl<<endl;
}

void stivezlutat(){

    Node* aux;

    pozitiv=NULL;

    negativ=NULL;


    while(head!=NULL){
        if(head->data >= 0){
            Push(pozitiv, head->data);
        }
        else{
            Push(negativ, head->data);
        }

        head=head->next;
    }
    cout<<"pozitive list:"<<endl;
    while(pozitiv != NULL){
        cout<<pozitiv->data<<"->";
        pozitiv=pozitiv->next;
    }
    cout<<endl;
    cout<<"negative list:"<<endl;
    while(negativ != NULL){
        cout<<negativ->data<<"->";
        negativ=negativ->next;
    }

}

int main()
{

    read();
    write();
    stivezlutat();
}

推荐阅读