/*一直以來對於指標、引用和取址都分不太清楚;所以這幾天晚上專門看了些關於該方面的視頻;然後今天下午看了一下午的書;感覺現在總算明白一些了;但是不確保正確,自己感覺,而且不能很清楚的表達。但是我想新手(包括我自己)對於這個總是很困惑的,所以我把自己所得分享一下。都在程式中了,要是你看不明白那是我寫的不清楚;要是你一下看的比我還明白那我恭喜你很聰明,呵呵。*/
以下是一個鏈表的程式,我主要用引用來寫:
Code:
- Code:
- /*注意引用的實用和指標的不同之處;注意區分引用和取值的不同意義;*/
-
- #include "malloc.h"
- #include "stdio.h"
- typedef int DataType;
- typedef struct Node
- {
- DataType data;
- struct Node *next;
- }LNode,*PNode,*LinkList;
-
- int InitList(PNode &h); //這裡一定要加引用為什嗎?
- int ListInsert(PNode h,int pos,DataType x); //以下四行加不加引用都可以,為什嗎?這是因為這裡的h本身就是指標--實參傳遞後和形參指向同一個地址
- int ListDelete(PNode h,int pos,DataType *item);
- void DestroyList(PNode h);
- void TraverseList(PNode h);
-
- int main()
- {
- int i;
- int data[7]={0,1,1,2,3,5,8};
- DataType item;
- PNode h=NULL; //是其指標域為空白
- InitList(h);
- for(i=0;i<7;i++)
- {
- if(!ListInsert(h,i+1,data[i]))
- {
- printf("插入失敗!/n");
- return 0;
- }
- }
- printf("/n/n刪除前單鏈表中的元素/n");
- TraverseList(h);
- if(!ListDelete(h,7,&item))
- {
- printf("error!/n");
- return 0;
- }
- printf("/n/n刪除後單鏈表的資料元素/n");
- TraverseList(h);
- DestroyList(h);
- return 0;
- }
-
-
-
- //初始化
- int InitList(PNode &h) //&h是引用(別名),但是用&h通過函數能改變主函數中h的值使其得到空間分配;如果去點&,直接將h傳遞過來
- { //那麼h在此函數中確實初始化成功了,但是不能返回到主函數中,即主函數中的h依然沒有分配空間----即此函數調用沒用
- h=(LNode *)malloc(sizeof(LNode));
- if(!h)
- {
- printf("初始化失敗!/n");
- return 0;
- }
- h->next=NULL;
- return 1;
- }
-
- //插入資料
- int ListInsert(PNode h,int pos,DataType x)
- {
- PNode p=h,q;
- int i=0;
- while (p&&i<pos-1)
- {
- p=p->next;
- i++;
- }
- if(!p||i>pos-1)
- {
- printf("不能插在此處!/n");
- return 0;
- }
- q=(PNode) malloc (sizeof(LNode));
- if(!q)
- {
- printf("error!/n");return 0;
- }
- q->data=x;
- q->next=p->next;
- p->next=q;
- return 1;
- }
-
- //刪除資料
- int ListDelete(PNode h,int pos,DataType *item)
- {
- PNode p=h,q;
- int i=0;
- while (p->next&&i<pos-1)
- {
- p=p->next;
- i++;
- }
- if(!p->next||i>pos-1)
- {
- printf("Error!/n");
- return 0;
- }
- q=p->next;
- p->next=q->next;
- *item=q->data;
- free(q);
- return 1;
- }
-
- //銷毀鏈表
- void DestroyList(PNode h)
- {
- PNode p=h->next;
- while (h)
- {
- p=h;
- h=h->next;
- free(p);
- }
- }
-
- //遍曆鏈表
- void TraverseList(PNode h)
- {
- PNode p=h->next;
- while (p)
- {
-
那麼用指標寫該程式如下,可進行對比,尤其是初始化的地方:
Code:
- #include "malloc.h"
- #include "stdio.h"
- typedef int DataType;
- typedef struct Node
- {
- DataType data;
- struct Node *next;
- }LNode,*PNode,*LinkList;
-
- int InitList(LinkList *h);
- int ListInsert(LinkList h,int pos,DataType x);
- int ListDelete(LinkList h,int pos,DataType *item);
- void DestroyList(LinkList h);
- void TraverseList(LinkList h);
-
- int main()
- {
- int i;
- int data[7]={0,1,1,2,3,5,8};
- DataType item;
- LinkList h=NULL; //是其指標域為空白
- InitList(&h);
- for(i=0;i<7;i++)
- {
- if(!ListInsert(h,i+1,data[i]))
- {
- printf("插入失敗!/n");
- return 0;
- }
- }
- printf("/n/n刪除前單鏈表中的元素/n");
- TraverseList(h);
- if(!ListDelete(h,7,&item))
- {
- printf("error!/n");
- return 0;
- }
- printf("/n/n刪除後單鏈表的資料元素/n");
- TraverseList(h);
- DestroyList(h);
- return 0;
- }
-
-
-
- //初始化
- int InitList(LinkList *h) //這裡多加一個*是為了使傳遞過來的LinkList h是地址得到改變;如果直接傳遞h過來實則主函數中h沒有變
- {
- *h=(LinkList)malloc(sizeof(LNode));
- if(!h)
- {
- printf("初始化失敗!/n");
- return 0;
- }
- (*h)->next=NULL;
- return 1;
- }
-
- //插入資料
- int ListInsert(LinkList h,int pos,DataType x)
- {
- PNode p=h,q;
- int i=0;
- while (p&&i<pos-1)
- {
- p=p->next;
- i++;
- }
- if(!p||i>pos-1)
- {
- printf("不能插在此處!/n");
- return 0;
- }
- q=(PNode) malloc (sizeof(LNode));
- if(!q)
- {
- printf("error!/n");return 0;
- }
- q->data=x;
- q->next=p->next;
- p->next=q;
- return 1;
- }
-
- //刪除資料
- int ListDelete(LinkList h,int pos,DataType *item)
- {
- PNode p=h,q;
- int i=0;
- while (p->next&&i<pos-1)
- {
- p=p->next;
- i++;
- }
- if(!p->next||i>pos-1)
- {
- printf("Error!/n");
- return 0;
- }
- q=p->next;
- p->next=q->next;
- *item=q->data;
- free(q);
- return 1;
- }
-
- //銷毀鏈表
- void DestroyList(LinkList h)
- {
- PNode p=h->next;
- while (h)
- {
- p=h;
- h=h->next;
- free(p);
- }
- }
-
- //遍曆鏈表
- void TraverseList(LinkList h)
- {
- PNode p=h->next;
- while (p)
- {
- printf("%d/t",p->data);
- p=p->next;
- }
- printf("/n");
- }
/*由於引用和取值的符號都是&,所以最好是弄明白他們的區別;在不同場合區分開。而引用基本上指標有的功能都有了(當然引用
沒有指標靈活,有些功能不能取代指標),所以大家喜歡用那個就用那個吧,個人覺得引用還是更方便一些吧。*/