總結常見的單鏈表操作函數,復習使用,僅供參考,代碼調試通過。
#include<stdio.h> typedef struct node{ int data; struct node *next; }node_t; //打印鏈表 void list_node(node_t *head){ while(head){ printf("%d",head->data); head=head->next; } } //求鏈表長度 int list_len(node_t *head){ int n=0; while(head) { ++n; head=head->next; } return n; } //反轉鏈表 void reverse(node_t *head){ node_t *r,*p,*q; r=NULL; p=head; q=p->next; while(p){ p->next=r; r=p; p=q; if(q){ q=q->next; } } head=r; } //測試函數 int main(){ node_t d={4,0},c={3,&d},b={2,&c},a={1,&b}; printf("%d\n",list_len(&a)); list_node(&a); reverse(&a); list_node(&d); return 0; }