|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct student
{
int num;
};
struct linklist
{
struct student data;
struct linklist *next;
};
int n=0;
struct linklist *creat()
{
struct linklist *p1;
struct linklist *p2;
struct linklist *head;
p1=p2=(struct linklist *)malloc(sizeof(struct linklist));
head=0;
scanf("%d",&p1->data.num);
while(p1->data.num!=0)
{
n+=1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct linklist *)malloc(sizeof(struct linklist));
scanf("%d",&p1->data.num);
}
p2->next=0;
return head;
}
struct linklist *reverse(struct linklist *head) //让一个链表逆置的函数
{
struct linklist *tmp,*p2,*p1;
tmp=head;
tmp->next=0;
p2=head->next;
p1=head->next->next;//问题就在这里,看上去好像没错吧,但是运行的时候老是说内存不能为read;
while(p2->next!=0)
{
p2->next=tmp;
tmp=p2;;
p2=p1;
p1=p1->next;
//if(tmp->next)
}
p2->next=tmp;
return p2;
}
void print(struct linklist *head)
{
struct linklist *p;
p=head;
while(p!=0)
{
printf("%d ",p->data.num);
p=p->next;
}
}
int main()
{
struct linklist *head;
head=(struct linklist *)malloc(sizeof(struct linklist));
printf("请输入一个数字\n");
head=creat();
print(head);
head=reverse(head);
print(head);
return 0;
}
|
|