今天做了一个简单的题目。

但是,我发现,在传参数的时候,

如果不使用引用传参,那么头指针

就会随着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);
}

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-11-13
  • 2022-12-23
  • 2022-12-23
  • 2022-01-08
猜你喜欢
  • 2022-01-05
  • 2022-01-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-21
相关资源
相似解决方案