C++ 您可以使用head->;数据作为整数变量,如果;“数据”;在链表中是整数类型吗?
如果链接列表中的“数据”是整数类型,则可以使用head->data作为整数变量。 例如,我有一个代码,其中我必须将一个简单链接的整数变量列表转换为两个列表,一个是正数,另一个是负数。我在我的代码中找不到错误C++ 您可以使用head->;数据作为整数变量,如果;“数据”;在链表中是整数类型吗?,c++,C++,如果链接列表中的“数据”是整数类型,则可以使用head->data作为整数变量。 例如,我有一个代码,其中我必须将一个简单链接的整数变量列表转换为两个列表,一个是正数,另一个是负数。我在我的代码中找不到错误 using namespace std; struct Node{ int data; Node* next; }; Node* head=new Node; Node* pozitiv=NULL; Node* negativ=NULL; Node* newNode(int dat
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();
}```
使用名称空间std;
结构节点{
int数据;
节点*下一步;
};
节点*头=新节点;
Node*pozitiv=NULL;
Node*negati=NULL;
Node*newNode(int数据)
{
Node*nodNou=新节点();
nodNou->data=数据;
nodNou->next=NULL;
返回nodNou;
}
无效推送(节点*&顶部,整数数据)
{
Node*nodNou=newNode(数据);
nodNou->next=顶部;
top=nodNou;
}
无效读取(){
Node*aux=新节点;
int r,i,v[1000];
coutv[0];
头部->数据=v[0];
head->next=NULL;
对于(i=1;i>v[i];
辅助->数据=v[i];
aux->next=头部;
头=辅助;
aux=新节点;
}
}
写(){
if(head==NULL)返回0;
否则{
while(head!=NULL){
库特达);
}
否则{
推送(负片,头部->数据);
}
头部=头部->下一步;
}
您的代码中可能出现错误:
- 必需的
#包含指令不存在
- 代码末尾有额外的“```”
- 没有为
write
指定返回类型
- 在函数
write
中,使用带值的return
,而函数末尾不显示任何return
语句,因此没有有效的返回类型
- 函数
write
中断head
的值,因此函数stivezlutat
将看到空列表
- 额外的节点分配给
pozitiv
和negative
,并在函数stivezlutat
中打印
固定代码(缩进也是固定的):
#包括
使用名称空间std;
结构节点{
int数据;
节点*下一步;
};
节点*头=新节点;
Node*pozitiv=NULL;
Node*negati=NULL;
Node*newNode(int数据)
{
Node*nodNou=新节点();
nodNou->data=数据;
nodNou->next=NULL;
返回nodNou;
}
无效推送(节点*&顶部,整数数据)
{
Node*nodNou=newNode(数据);
nodNou->next=顶部;
top=nodNou;
}
无效读取(){
Node*aux=新节点;
int r,i,v[1000];
coutv[0];
头部->数据=v[0];
head->next=NULL;
对于(i=1;i>v[i];
辅助->数据=v[i];
aux->next=头部;
头=辅助;
aux=新节点;
}
}
无效写入(){
if(head==NULL)返回;
否则{
节点*光标=头部;
while(光标!=NULL){
coutnext;
}
请问错误是什么,症状是什么?标题的答案是:是的,前提是head
指向有效的缓冲区。编译器将代替您查找一些错误。
#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();
}