#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
int data;
struct Node *next;
}*PNode,Node;
void init(PNode *p);
void Create(PNode Head);
void printfValue(PNode Head);
void ReverseList(PNode Head);
int main()
{
PNode Head;
init(&Head);
Create(Head);
printfValue(Head);
printf("逆置以后的结点输出!\n");
ReverseList(Head);
printfValue(Head);
return 0;
}
void init(PNode *p)
{
(*p) = (PNode)malloc(sizeof(Node));
(*p)->next = NULL;
}
void Create(PNode Head)
{
PNode r,s;
int n = 0,m;
r = Head;
printf("请输入m个结点:\n");
scanf("%d",&m);
printf("请输入每个结点的值:\n");
while(n < m)//尾插法
{
s = (PNode)malloc(sizeof(Node));
scanf("%d",&s->data);
r->next = s;
r = s;
n++;
}
r->next = NULL;
//
}
void printfValue(PNode Head)
{
printf("输出各个结点的值:\n");
PNode p;
p = Head->next;
while(p != NULL)
{
printf("%d ",p->data);
p = p->next;
}
printf("\n");
}
void ReverseList(PNode Head)//头插法思想
{
PNode p = Head->next;//首先将头结点复制给p
PNode q;
Head->next = NULL;//将单链表初始化为空链表
while(p != NULL)
{
q = p->next;//q指针保留原链表的当前处理结点的下一个结点
p->next = Head->next;//p插入到Head的表头
Head->next = p;
p = q;
}
}