能不能帮忙修改下这个C++程序

来源:百度知道 编辑:UC知道 时间:2024/05/11 00:18:42
#include <iostream>
#include <malloc.h>
using namespace std;
struct Node
{
int data;
Node *next;
};
struct List
{
Node* head;
void Addhead(int x)
{
Node* tplement=(Node* )malloc(sizeof(Node));
tplement->data=x;
Node*v=head;
if(head!=NULL)
{
head=tplement;
tplement->next=v;
}
else
{
head=tplement;

}
}
void Inverse()
{
Node* s=head;
Node *p=s->next;
Node *q=NULL;
for(int i=0;p->next!=NULL;i++)
{
s->next=q;
q=s;
s=p;
p=p->next;
}

}

};
void main()
{
List lt;
lt.Addhead(7);
lt.Addhead(6);
lt.Addhead(6);
lt.Addhead(5);
lt.Inverse();
}


void Inverse()
{
Node* s=head;
Node *p=s->next;

改起来很简单,可操作起来真困难

#include <iostream>
#include <malloc.h>
using namespace std;
struct Node
{
int data;
Node *next;
};
struct List
{
Node* head;
List()//add function
{
head = NULL;
}
void Addhead(int x)
{
Node* tplement=(Node* )malloc(sizeof(Node));
tplement->data=x;
Node*v=head;
if(head!=NULL)
{
head=tplement;
tplement->next=v;
}
else
{
head=tplement;
tplement->next = NULL;//add

}
}
void Inverse()
{
Node* s=head;
Node *p=s->next;
Node *q=NULL;
for(int i=0;p->next!=NULL;i++)
{
s->next=q;
q=s;
s=p;
p=p->next;
}

}

};
void main()
{
List lt;
lt.Addhead(7);
lt.Addhead(6);