完善教材上带头结点单链表的就地逆置问题
#include <iostream>using namespace std;typedef struct node{ char a; struct node *next;}node,*LinkList;//初始化单链表void initList(LinkList *L){ *L=(LinkList)malloc(sizeof(node)); (*L)->next=NULL;}//头插法建表void CreatFromHead(LinkList L){ node *s; char c; scanf("%c",&c); while(c!='$') { s=(node *)malloc(sizeof(node)); s->a=c; s->next=L->next; L->next=s; scanf("%c",&c); }}//就地逆置带头结点的单链表void ReverseList(LinkList L){ node *p,*q; p=L->next; L->next=NULL; while(p!=NULL) { q=p->next; p->next=L->next; L->next=p; p=q; }}//打印单链表void show(LinkList L){ node *p; p=L->next; while(p!=NULL) { PRintf("%c ",p->a); p=p->next; }}int main(){ LinkList L; initList(&L); printf("请输入字符:/n"); CreatFromHead(L); show(L); printf("/n"); ReverseList(L); show(L);}实验结果如下:
新闻热点
疑难解答