c++ - 如果“数据”在链表中是整数类型,你可以使用 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();
}```
解决方案
您的代码中的错误:
- 所需
#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();
}
推荐阅读
- azure-api-management - 有没有办法在创建 API 操作后对其进行标记
- angular - PrimeNG Chart 选项的更新需要两次调用而不是一次调用
- ruby-on-rails - 使用复选框确认 Rails 设计用户
- azure-devops - AzDo 同时拉入 2 个不同的远程/目标分支
- eclipse - STS4-Eclipse 需要命令 org.springframework.ide.eclipse.boot.validation.springbootbuilder 吗?
- c# - C# set get not working first click using forms
- c# - LINQ 两个 Where 子句
- apache-kafka - Kafka 重新平衡 - 将 Kafka 消费者分配给分区
- java - 如何在警报触发 1 次后取消 pendingIntent AlarmManager
- c# - iTextSharp - 合并内存流时损坏的 PDF