/*一直以来对于指针、引用和取址都分不太清楚;所以这几天晚上专门看了些关于该方面的视频;然后今天下午看了一下午的书;感觉现在总算明白一些了;但是不确保正确,自己感觉,而且不能很清楚的表达。但是我想新手(包括我自己)对于这个总是很困惑的,所以我把自己所得分享一下。都在程序中了,要是你看不明白那是我写的不清楚;要是你一下看的比我还明白那我恭喜你很聪明,呵呵。*/
以下是一个链表的程序,我主要用引用来写:
- 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)
- {
那么用指针写该程序如下,可进行对比,尤其是初始化的地方:
- #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");
- }
/*由于引用和取值的符号都是&,所以最好是弄明白他们的区别;在不同场合区分开。而引用基本上指针有的功能都有了(当然引用
没有指针灵活,有些功能不能取代指针),所以大家喜欢用那个就用那个吧,个人觉得引用还是更方便一些吧。*/