主题:谁来帮我解答下 ..关于颠倒顺序表的问题
[code=c]
#include "stdio.h"
#include "malloc.h"
#include "stdlib.h"
#define LEN sizeof(struct student)
struct student
{
int num;
struct student *next;
};
int n;
int main(void)
{
struct student *creat(void);
struct student *inverse(struct student *p);
void print(struct student *p);
struct student *head;
printf("Input list:\n");
printf("If input '0',stop inputing.\n");
head=creat();
printf("\nInverse list,print new list: \n");
head=inverse(head);
print(head);
system("pause");
return 0;
}
struct student *creat(void)
{
struct student *fhead;
struct student *p1,*p2;
n=0;
fhead=NULL;
printf("input number: ");
p1=p2=(struct student *)malloc(LEN);
scanf("%d",&p1->num);
while(p1->num!=0)
{
n=n+1;
if(n==1) fhead=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
printf("input number: ");
scanf("%d",&p1->num);
}
p2->next=NULL;
return(fhead);
}
struct student *inverse(struct student *p)
{
struct student *p1,*p2;
struct student *n,*nh;
nh=NULL;
do
{
p2=NULL;
p1=p;
while(p1->next!=NULL) p2=p1,p1=p1->next;
if(nh==NULL) nh=p1,nh=nh->next=p2;
n=n->next=p2;
p2->next=NULL;
} while(p->next!=NULL);
return(nh);
}
void print(struct student *p)
{
struct student *p0;
p0=p;
if(p0->next!=0)
do
{
printf("%d ",p0->num);
p0=p0->next;
}while(p0->next!=0);
}
[/code]
上面的码弄了半天 inverse函数部分还是有问题
貌似我关于颠倒顺序表的idea 有问题 谁解答下 !
#include "stdio.h"
#include "malloc.h"
#include "stdlib.h"
#define LEN sizeof(struct student)
struct student
{
int num;
struct student *next;
};
int n;
int main(void)
{
struct student *creat(void);
struct student *inverse(struct student *p);
void print(struct student *p);
struct student *head;
printf("Input list:\n");
printf("If input '0',stop inputing.\n");
head=creat();
printf("\nInverse list,print new list: \n");
head=inverse(head);
print(head);
system("pause");
return 0;
}
struct student *creat(void)
{
struct student *fhead;
struct student *p1,*p2;
n=0;
fhead=NULL;
printf("input number: ");
p1=p2=(struct student *)malloc(LEN);
scanf("%d",&p1->num);
while(p1->num!=0)
{
n=n+1;
if(n==1) fhead=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
printf("input number: ");
scanf("%d",&p1->num);
}
p2->next=NULL;
return(fhead);
}
struct student *inverse(struct student *p)
{
struct student *p1,*p2;
struct student *n,*nh;
nh=NULL;
do
{
p2=NULL;
p1=p;
while(p1->next!=NULL) p2=p1,p1=p1->next;
if(nh==NULL) nh=p1,nh=nh->next=p2;
n=n->next=p2;
p2->next=NULL;
} while(p->next!=NULL);
return(nh);
}
void print(struct student *p)
{
struct student *p0;
p0=p;
if(p0->next!=0)
do
{
printf("%d ",p0->num);
p0=p0->next;
}while(p0->next!=0);
}
[/code]
上面的码弄了半天 inverse函数部分还是有问题
貌似我关于颠倒顺序表的idea 有问题 谁解答下 !