#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, * p; h=p=(ElemSN *)malloc(sizeof(ElemSN)); h->next=NULL; for( i=0;i<N;i++){ p=p->next=(ElemSN *)malloc(sizeof(ElemSN)); p->data =a[i]; p->next=NULL; } return h; } void printlink(ElemSN * h){ ElemSN * p; for(p=h;p->next;p=p->next) printf("%2d\n",p->next->data); } void Prelink(ElemSN*h){ ElemSN*p,*r; r=h->next; //頭指針后移,給r指針 h->next=NULL; //h斷開,避免形成環(huán)(是一個頭指針與第一個結(jié)點的環(huán)) while(r){//頭指針為空,鏈表遍歷完 p=r; //當(dāng)前的結(jié)點 r=r->next;//r后移(頭指針),保證鏈表有頭指針 p->next=h->next;//掛鏈(逆置) h->next=p;//建立新的頭結(jié)點 } } int main(void){ int a[N]={1,2,3,4,5,6,7,8,9}; ElemSN * head; head=Createlink(a,9); Prelink(head); printlink(head); }
名稱欄目:鏈表的逆置(帶表頭的單向鏈表)
文章路徑:http://www.chinadenli.net/article10/ijoogo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、建站公司、品牌網(wǎng)站制作、靜態(tài)網(wǎng)站、移動網(wǎng)站建設(shè)、小程序開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)