#include<stdio.h> #include<stdlib.h> #define N 9 typedef struct node{ int data; struct node * next; }ElemSN; ElemSN * Createlink(int a[],int n) { int i; ElemSN * h=NULL, * p; for( i=N-1;i>=0;i--) { p=(ElemSN *)malloc(sizeof(ElemSN)); p->data =a[i]; p->next=h; h=p; } return h; } ElemSN* In_reverseList(ElemSN* H) { ElemSN * newHead ; if (H == NULL || H->next == NULL) //鏈表為空直接返回,而H->next為空是遞歸基 return H; newHead = In_reverseList(H->next); //一直循環(huán)到鏈尾 H->next->next = H; //翻轉(zhuǎn)鏈表的指向 H->next = NULL; //記得賦值NULL,防止鏈表錯(cuò)亂 return newHead; //新鏈表頭永遠(yuǎn)指向的是原鏈表的鏈尾 } void printlink(ElemSN * h) { ElemSN * p; for(p=h;p;p=p->next) printf("%d\n",p->data); } int main(void) { int a[N]={1,2,3,4,5,6,7,8,9}; ElemSN * head; head=Createlink(a,9); printlink(In_reverseList(head)); }
網(wǎng)站欄目:鏈表的逆置(遞歸)
網(wǎng)頁(yè)地址:http://www.chinadenli.net/article40/joejho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、企業(yè)建站、網(wǎng)站維護(hù)、外貿(mào)建站、做網(wǎng)站、網(wǎng)頁(yè)設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)