程序代码:
#include<stdio.h>
#include<stdlib.h> //我是这么写的
#include<string.h>
struct node
{
int date;
struct node * pnext;
};
struct node * creat_list(struct node * h); //创建链表
struct node * change_list(struct node * h);//调用函数进行反转
void print_list(struct node * h); //输出链表
int main()
{
node * head;
head = NULL;
printf("请输入数据:\n");
head = creat_list(head);
printf("-------------------------------\n");
print_list(head);
printf("-------------------------------\n");
head = change_list(head);
print_list(head);
}
struct node * creat_list(struct node * h)
{
node * p1,* p2;
p1 = p2 = (node *)malloc(sizeof(node));
if( p2 != NULL)
{
scanf("%d",&p2->date);
p2->pnext = NULL;
}
while( p2->date != 0)
{
if(h == NULL)
h = p2;
else
p1->pnext = p2;
p1 = p2;
p2 = (node *)malloc(sizeof(node));
if( p2 != NULL)
{
scanf("%d",&p2->date);
p2->pnext = NULL;
}
}
return h;
}
void print_list(struct node * h)
{
struct node * temp;
temp = h;
while(temp != NULL)
{
printf("%-4d\n",temp->date);
temp = temp->pnext;
}
}
struct node *change_list(struct node * h)
{
if(h == NULL||h->pnext == NULL)
return h;
node * p,* q,* r;
p = h;
q = h->pnext;
h->pnext = NULL;
while(q != NULL)
{
r = q->pnext;
q->pnext = p;
p = q;
q = r;
}
h = p;
return h;
}