回复:(lucis009)[求助]链表逆向输出!
我给你写了一个完整的;挺匆忙的,你测试一下,应该没有大问题,你也可以完善一下;
/*************** data define ***********/
struct List
{
int data;
struct List *next;
};
typedef struct List Node;
typedef Node *Link;
/*************** fun declare***********/
Link Create_List(void);
void Print_List(Link Head);
Link Inverse_List(Link Head);
/*********** main function ************/
int main(void)
{
Link Head;
Head = Create_List();
Print_List(Head);
Head = Inverse_List(Head);
printf("after inverse:\n");
Print_List(Head);
return 0;
}
/********** fun definition ***********/
Link Create_List(void)
{
Link Head, Point, New;
Head = (Link)malloc(sizeof(Node));
if(Head == NULL)
{
printf("Memory Allocate Failed!\n");
return Head;
}
printf("please input number,end with zero:\n");
scanf("%d",&Head->data);
if(Head->data == NULL)
{
Head = NULL;
return Head;
}
Head->next = NULL;
Point = Head;
while(1)
{
New = (Link)malloc(sizeof(Node));
scanf("%d",&New->data);
New->next = NULL;
if(New->data == NULL)
break;
Point->next = New;
Point = New;
}
return Head;
}
void Print_List(Link Head)
{
Link Point;
if(Head == NULL){
printf("the List is Empty!\n");
return;
}
Point = Head;
printf("the List Output is:\n");
while(Point != NULL)
{
printf("%d\t",Point->data);
Point = Point->next;
}
printf("\n");
return;
}
Link Inverse_List(Link Head)
{
Link Point, New, Back;
if(Head == NULL) //List is Empty
{
printf("List is Empty!\n");
return Head;
}
if(Head->next == NULL) //List is Only One Node
{
return Head;
}
Back = Head;
Point = Back->next;
New = Point->next;
Back->next = NULL;
while(Point->next != NULL)
{
Point->next = Back;
Back = Point;
Point = New;
New = New->next;
}
Point->next = Back;
Head = Point;
return Head;
}