RT...
我的源码如下:
#include
#include
typedef struct link{
int data;
struct link *next;
}node;
node *createList()
{
node *pre, *cur, *head;
int temp;
head = (node *)malloc(sizeof(node));
if(head == NULL)
{
printf("Malloc failed!\n");
exit(0);
}
head->data = 0;
head->next = NULL;
pre = head;
printf("Input data:");
scanf("%d", &temp);
while(temp!=0)
{
cur = (node *)malloc(sizeof(node));
if(cur == NULL)
{
printf("Malloc failed!\n");
exit(0);
}
cur->data = temp;
cur->next = NULL;
pre->next = cur;
pre = cur;
printf("Input data:");
scanf("%d", &temp);
}
return head;
}
void printList(node **head)
{
node *temp;
temp = (*head)->next;
if(head == NULL)
{
return;
}
while(temp)
{
printf("\t%d", temp->data);
temp = temp->next;
}
printf("Output complete~!\n");
}
node *reverseRecursive(node *p, node **head)
{
if(p == NULL || p->next == NULL)
{
(*head)->next = p;
return p;
}
node *temp = reverseRecursive(p->next, head);
temp->next = p;
return p;
}
int main()
{
node *head = NULL;
head = createList();
printList(&head);
ReverseLink(head);
reverseRecursive(head, &head);
printList(&head);
return 0;
}
但是这样实现会有一个问题,就是会形成环,请问有什么方法可以解决?