求大神! 链表转置有问题 转置后只能输出两个数....
#include <iostream>typedef struct student //记住前面要加typedef
{
int grade;
student*next;
}list;
void set_list(list* &head,int a[],int n) //前插入法
{
list *p;
head=new list;
head->next=NULL;
for (int i=0;i<n;++i)
{ p=new list;
p->grade=a[i];
p->next=head->next;
head->next=p;
}
}
void print_list(list *head)
{
list*p=head->next;
while(p!=NULL)
{
std::cout<<p->grade<<"\t";
p=p->next;
}
std::cout<<std::endl;
}
bool InverseList(list*&L) //链表转置
{
if (L->next->next==NULL)
{return false;
}
list*p=L->next,*q;
q=p->next;
while (q!=NULL)
{
p->next=q->next;
q->next=p;
L->next=q;
q=p->next;
}
return true;
}
int main()
{
list *head;
int a[]={1,2,5,4,4,4,7};
set_list(head,a,7);
print_list(head);
InverseList(head);
print_list(head);
}