鏈表參數的引用與非引用
今天做了一個簡單的題目。
但是,我發現,在傳參數的時候,
如果不使用引用傳參,那么頭指針
就會隨著tmp指針向后移動。
如果使用引用傳參的話,它就
不會了。
題目:http://acm.swust.edu.cn/oj/problem/172/
View Code
#include<stdio.h> #include<stdlib.h> #include<string.h> typedef struct node{ int n; struct node *next; }node; node *A, *B, *ta, *tb, *p; void Init_AB(){ A = (node*)malloc(sizeof(node)); B = (node*)malloc(sizeof(node)); ta = A; tb = B; } void Creat_list(node *&ab, int x){ //引用傳參&ab p = (node*)malloc(sizeof(node)); p->n = x; p->next = NULL; ab->next = p; ab = ab->next; } void Connect_list(node *&a, node *b){//引用傳參&a a->next = b->next; } void Print_list(node *a){ while(a->next->next!=NULL){ printf("%d ", a->next->n); a=a->next; } printf("%d\n", a->next->n); } int main(){ int an, bn, num; Init_AB(); scanf("%d", &an); while(an--){ scanf("%d", &num); Creat_list(ta, num); } scanf("%d", &bn); while(bn--){ scanf("%d", &num); Creat_list(tb, num); } Connect_list(ta, B); Print_list(A); }
posted on 2012-05-08 22:50 More study needed. 閱讀(509) 評論(0) 收藏 舉報

浙公網安備 33010602011771號