这个逆序链表怎么只输出第一个结点呢??费解,求助
感觉是不是nixu()函数出嘞错哦,但看了很久都没看出来。看你们了哦。#include<stdio.h>
#include<malloc.h>
#define NULL 0
struct student
{
int num;
char name[20];
struct student *next;
}stu;
struct student *creat(struct student *head) //建立链表;
{
struct student *p1, *p2;
int n=0;
p1=(struct student *)malloc(sizeof(struct student));
printf("please input the data:\n");
while((++n)<=5) //依次输入学生数据;
{
scanf("%d,%s",&p1->num, p1->name);
if(n==1)
{
head=p1;
}
p2=p1;
p1=(struct student *)malloc(sizeof(struct student)); //开辟新结点;
p2->next=p1;
}
p2->next=NULL; //结束链表;
return(head);
}
struct student *nixu(struct student *head) //逆序链表函数;
{
int i, j;
struct student *p1, *p2, *new_head, *p3, *p4; //p3,p4为指向新链表的结构体指针;
p1=p2=head;
new_head=p3=p4=(struct student *)malloc(sizeof(struct student)); //为新链表申请表头;
for(i=0;i<5;i++) //是将原链表分别指向第5,4...1结点,分别接到新链表new_head;
{
p1=p2=head;
for(j=1;j<5-i;j++)
{
p2=p1;
p1=p1->next;
}
p2->next=NULL; //删除p1所指结点;
if(i==0)
{
new_head=p1; //建立新链表表头,即原链表第5个结点;
}
p3=p1;
p4=p3; //是p3,p4指向一个新的结点p1;
if(i==4) //当原链表已被删到只剩一个结点了;
{
p4->next=NULL;
break;
}
p3=(struct student *)malloc(sizeof(struct student)); //为新链表申请一个新结点内存
p4->next=p3; //连接新链表结点;
}
return(new_head);
}
void main()
{
struct student *head, *p=NULL;
head=&stu;
head=creat(head);
p=nixu(head);
printf("please output the new lianbiao:\n");
while(1) //输出经逆序的新链表;
{
printf("%d, %s\n", p->num, p->name);
if(p->next=NULL)
{
break;
}
p=p->next;
}
}