完善教材上帶頭結點單鏈表的就地逆置問題
#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);}實驗結果如下:
新聞熱點
疑難解答