/*头插法实现单链表逆置*/
#include<stdio.h>
#include<malloc.h>
typedef struct LNode
{
char data;
struct LNode *next;
}LNode,*LinkList;
bool InitList(LinkList &L)
{
L=(LNode*)malloc(sizeof(LNode));
if(L==NULL)
return false;
L->next=NULL;
return true;
}
LinkList head_InsertList(LinkList &L,char e)
{
LNode *s=(LNode*)malloc(sizeof(LNode));
s->data=e;
s->next=L->next;
L->next=s;
return L;
}
LinkList Translate(LinkList &L)
{
if(L==NULL)
return NULL;
LNode *p=L->next,*q;
q=(LNode*)malloc(sizeof(LNode));
q->next=NULL;
while(p!=NULL)
{
head_InsertList(q,p->data);
p=p->next;
}
L=q;
}
void Print(LinkList L)
{
LNode *p=L->next;
while(p!=NULL)
{
printf("%c ",p->data);
p=p->next;
}
}
void main()
{
LinkList L;
InitList(L);
head_InsertList(L,'a');
head_InsertList(L,'b');
head_InsertList(L,'c');
head_InsertList(L,'d');
head_InsertList(L,'e');
Print(L);
printf("\n********************\n");
Translate(L);
Print(L);
printf("\n********************\n");
}