引用(别名)、取值和指针的区别使用

/*一直以来对于指针、引用和取址都分不太清楚;所以这几天晚上专门看了些关于该方面的视频;然后今天下午看了一下午的书;感觉现在总算明白一些了;但是不确保正确,自己感觉,而且不能很清楚的表达。但是我想新手(包括我自己)对于这个总是很困惑的,所以我把自己所得分享一下。都在程序中了,要是你看不明白那是我写的不清楚;要是你一下看的比我还明白那我恭喜你很聪明,呵呵。*/

以下是一个链表的程序,我主要用引用来写:

Code:
  1. Code:   
  2. /*注意引用的实用和指针的不同之处;注意区分引用和取值的不同意义;*/     
  3.      
  4. #include "malloc.h"      
  5. #include "stdio.h"      
  6. typedef int DataType;      
  7. typedef struct Node      
  8. {      
  9.     DataType data;      
  10.     struct Node *next;      
  11. }LNode,*PNode,*LinkList;      
  12.      
  13. int InitList(PNode &h);               //这里一定要加引用为什么?      
  14. int ListInsert(PNode h,int pos,DataType x); //以下四行加不加引用都可以,为什么?这是因为这里的h本身就是指针--实参传递后和形参指向同一个地址      
  15. int ListDelete(PNode h,int pos,DataType *item);      
  16. void DestroyList(PNode h);      
  17. void TraverseList(PNode h);      
  18.      
  19. int main()      
  20. {      
  21.     int i;      
  22.     int data[7]={0,1,1,2,3,5,8};      
  23.     DataType item;      
  24.     PNode h=NULL;   //是其指针域为空      
  25.     InitList(h);      
  26.     for(i=0;i<7;i++)      
  27.     {      
  28.         if(!ListInsert(h,i+1,data[i]))      
  29.         {      
  30.             printf("插入失败!/n");      
  31.             return 0;      
  32.         }      
  33.     }      
  34.     printf("/n/n删除前单链表中的元素/n");      
  35.     TraverseList(h);      
  36.     if(!ListDelete(h,7,&item))      
  37.     {      
  38.         printf("error!/n");      
  39.         return 0;      
  40.     }      
  41.     printf("/n/n删除后单链表的数据元素/n");      
  42.     TraverseList(h);      
  43.     DestroyList(h);      
  44.     return 0;      
  45. }      
  46.      
  47.      
  48.      
  49. //初始化      
  50. int InitList(PNode &h)         //&h是引用(别名),但是用&h通过函数能改变主函数中h的值使其得到空间分配;如果去点&,直接将h传递过来      
  51. {                              //那么h在此函数中确实初始化成功了,但是不能返回到主函数中,即主函数中的h依然没有分配空间----即此函数调用没用      
  52.     h=(LNode *)malloc(sizeof(LNode));      
  53.     if(!h)      
  54.     {      
  55.         printf("初始化失败!/n");      
  56.         return 0;      
  57.     }      
  58.     h->next=NULL;      
  59.     return 1;      
  60. }      
  61.   
  62. //插入数据      
  63. int ListInsert(PNode h,int pos,DataType x)      
  64. {      
  65.     PNode p=h,q;      
  66.     int i=0;      
  67.     while (p&&i<pos-1)      
  68.     {      
  69.         p=p->next;      
  70.         i++;      
  71.     }      
  72.     if(!p||i>pos-1)      
  73.     {      
  74.         printf("不能插在此处!/n");      
  75.         return 0;      
  76.     }      
  77.     q=(PNode) malloc (sizeof(LNode));      
  78.     if(!q)      
  79.     {      
  80.         printf("error!/n");return 0;      
  81.     }      
  82.     q->data=x;      
  83.     q->next=p->next;      
  84.     p->next=q;      
  85.     return 1;      
  86. }      
  87.      
  88. //删除数据      
  89. int ListDelete(PNode h,int pos,DataType *item)      
  90. {      
  91.     PNode p=h,q;      
  92.     int i=0;      
  93.     while (p->next&&i<pos-1)      
  94.     {      
  95.         p=p->next;      
  96.         i++;      
  97.     }      
  98.     if(!p->next||i>pos-1)      
  99.     {      
  100.         printf("Error!/n");      
  101.         return 0;      
  102.     }      
  103.     q=p->next;      
  104.     p->next=q->next;      
  105.     *item=q->data;      
  106.     free(q);      
  107.     return 1;      
  108. }      
  109.      
  110. //销毁链表      
  111. void DestroyList(PNode h)      
  112. {      
  113.     PNode p=h->next;      
  114.     while (h)      
  115.     {      
  116.         p=h;      
  117.         h=h->next;      
  118.         free(p);      
  119.     }      
  120. }      
  121.      
  122. //遍历链表      
  123. void TraverseList(PNode h)      
  124. {      
  125.     PNode p=h->next;      
  126.     while (p)      
  127.     {      
  128.     

那么用指针写该程序如下,可进行对比,尤其是初始化的地方:

Code:
  1. #include "malloc.h"   
  2. #include "stdio.h"   
  3. typedef int DataType;   
  4. typedef struct Node   
  5. {   
  6.     DataType data;   
  7.     struct Node *next;   
  8. }LNode,*PNode,*LinkList;   
  9.   
  10. int InitList(LinkList *h);   
  11. int ListInsert(LinkList h,int pos,DataType x);   
  12. int ListDelete(LinkList h,int pos,DataType *item);   
  13. void DestroyList(LinkList h);   
  14. void TraverseList(LinkList h);   
  15.   
  16. int main()   
  17. {   
  18.     int i;   
  19.     int data[7]={0,1,1,2,3,5,8};   
  20.     DataType item;   
  21.     LinkList h=NULL;   //是其指针域为空   
  22.     InitList(&h);   
  23.     for(i=0;i<7;i++)   
  24.     {   
  25.         if(!ListInsert(h,i+1,data[i]))   
  26.         {   
  27.             printf("插入失败!/n");   
  28.             return 0;   
  29.         }   
  30.     }   
  31.     printf("/n/n删除前单链表中的元素/n");   
  32.     TraverseList(h);   
  33.     if(!ListDelete(h,7,&item))   
  34.     {   
  35.         printf("error!/n");   
  36.         return 0;   
  37.     }   
  38.     printf("/n/n删除后单链表的数据元素/n");   
  39.     TraverseList(h);   
  40.     DestroyList(h);   
  41.     return 0;   
  42. }   
  43.   
  44.   
  45.   
  46. //初始化   
  47. int InitList(LinkList *h)              //这里多加一个*是为了使传递过来的LinkList h是地址得到改变;如果直接传递h过来实则主函数中h没有变   
  48. {   
  49.     *h=(LinkList)malloc(sizeof(LNode));   
  50.     if(!h)   
  51.     {   
  52.         printf("初始化失败!/n");   
  53.         return 0;   
  54.     }   
  55.     (*h)->next=NULL;   
  56.     return 1;   
  57. }   
  58.   
  59. //插入数据   
  60. int ListInsert(LinkList h,int pos,DataType x)   
  61. {   
  62.     PNode p=h,q;   
  63.     int i=0;   
  64.     while (p&&i<pos-1)   
  65.     {   
  66.         p=p->next;   
  67.         i++;   
  68.     }   
  69.     if(!p||i>pos-1)   
  70.     {   
  71.         printf("不能插在此处!/n");   
  72.         return 0;   
  73.     }   
  74.     q=(PNode) malloc (sizeof(LNode));   
  75.     if(!q)   
  76.     {   
  77.         printf("error!/n");return 0;   
  78.     }   
  79.     q->data=x;   
  80.     q->next=p->next;   
  81.     p->next=q;   
  82.     return 1;   
  83. }   
  84.   
  85. //删除数据   
  86. int ListDelete(LinkList h,int pos,DataType *item)   
  87. {   
  88.     PNode p=h,q;   
  89.     int i=0;   
  90.     while (p->next&&i<pos-1)   
  91.     {   
  92.         p=p->next;   
  93.         i++;   
  94.     }   
  95.     if(!p->next||i>pos-1)   
  96.     {   
  97.         printf("Error!/n");   
  98.         return 0;   
  99.     }   
  100.     q=p->next;   
  101.     p->next=q->next;   
  102.     *item=q->data;   
  103.     free(q);   
  104.     return 1;   
  105. }   
  106.   
  107. //销毁链表   
  108. void DestroyList(LinkList h)   
  109. {   
  110.     PNode p=h->next;   
  111.     while (h)   
  112.     {   
  113.         p=h;   
  114.         h=h->next;   
  115.         free(p);   
  116.     }   
  117. }   
  118.   
  119. //遍历链表   
  120. void TraverseList(LinkList h)   
  121. {   
  122.     PNode p=h->next;   
  123.     while (p)   
  124.     {   
  125.         printf("%d/t",p->data);   
  126.         p=p->next;   
  127.     }   
  128.     printf("/n");   
  129. }

/*由于引用和取值的符号都是&,所以最好是弄明白他们的区别;在不同场合区分开。而引用基本上指针有的功能都有了(当然引用

没有指针灵活,有些功能不能取代指针),所以大家喜欢用那个就用那个吧,个人觉得引用还是更方便一些吧。*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值