引用(別名)、取值和指標的區別使用

來源:互聯網
上載者:User

/*一直以來對於指標、引用和取址都分不太清楚;所以這幾天晚上專門看了些關於該方面的視頻;然後今天下午看了一下午的書;感覺現在總算明白一些了;但是不確保正確,自己感覺,而且不能很清楚的表達。但是我想新手(包括我自己)對於這個總是很困惑的,所以我把自己所得分享一下。都在程式中了,要是你看不明白那是我寫的不清楚;要是你一下看的比我還明白那我恭喜你很聰明,呵呵。*/

以下是一個鏈表的程式,我主要用引用來寫:

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. }

/*由於引用和取值的符號都是&,所以最好是弄明白他們的區別;在不同場合區分開。而引用基本上指標有的功能都有了(當然引用

沒有指標靈活,有些功能不能取代指標),所以大家喜歡用那個就用那個吧,個人覺得引用還是更方便一些吧。*/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.