Chinaunix首页 | 论坛 | 博客
  • 博客访问: 478763
  • 博文数量: 144
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1190
  • 用 户 组: 普通用户
  • 注册时间: 2013-10-08 20:16
文章分类

全部博文(144)

文章存档

2017年(1)

2015年(5)

2014年(108)

2013年(30)

我的朋友

分类: C/C++

2014-07-21 17:46:58


点击(此处)折叠或打开

  1. #include "stdio.h"
  2. #include<malloc.h>
  3. #define ok 0
  4. #define error 1
  5. struct node
  6. {
  7.     int data;
  8.     struct node *next;
  9. };
  10. typedef struct node * LinkList;
  11. int CreatLinkList(LinkList h,int n)
  12. {
  13.     LinkList tmp,p;
  14.     int i;
  15.     // h=(LinkList)malloc(sizeof(struct node));
  16.     p= h;
  17.     for(i=0;i<n;i++)
  18.     {
  19.         tmp=(LinkList)malloc(sizeof(struct node));
  20.         tmp->data=i;
  21.         tmp->next=NULL;

  22.         p->next=tmp;
  23.         p=p->next;
  24.     }
  25.     return ok;
  26. }
  27. void PritLinkList(LinkList h)
  28. {
  29.     LinkList p;
  30.     p=h->next;
  31.     while(p)
  32.     {
  33.         printf("%d\n",p->data);
  34.         p=p->next;
  35.     }
  36. }
  37. void main()
  38. {
  39.   LinkList head =(LinkList)malloc(sizeof(struct node));
  40.   CreatLinkList( head,5);
  41.   //InsertLinkList(head,3,9);
  42.    //DeleteLinkList(head,6);
  43.   PritLinkList(head);

  44. }
  CreatLinkList( head,5);这样传递参数是可以的,注意是16行和23行,临时指针p指向,h指向的地址,h指向的是主函数中head指向的地址,因此这三个地址开始是相同的。在23行,p->next=tmp;实际上是对head指向地址内部next参数的操作。不会出错。和下面对比

点击(此处)折叠或打开

  1. #include "stdio.h"
  2. #include<malloc.h>
  3. #define ok 0
  4. #define error 1
  5. struct node
  6. {
  7.     int data;
  8.     struct node *next;
  9. };
  10. typedef struct node * LinkList;
  11. int CreatLinkList(LinkList h,int n)
  12. {
  13.     LinkList tmp,p;
  14.     int i;
  15.      h=(LinkList)malloc(sizeof(struct node));
  16.     p= h;
  17.     for(i=0;i<n;i++)
  18.     {
  19.         tmp=(LinkList)malloc(sizeof(struct node));
  20.         tmp->data=i;
  21.         tmp->next=NULL;

  22.         p->next=tmp;
  23.         p=p->next;
  24.     }
  25.     return ok;
  26. }
  27. void PritLinkList(LinkList h)
  28. {
  29.     LinkList p;
  30.     p=h->next;
  31.     while(p)
  32.     {
  33.         printf("%d\n",p->data);
  34.         p=p->next;
  35.     }
  36. }
  37. void main()
  38. {
  39.   LinkList head=(LinkList)malloc(sizeof(struct node));
  40.   CreatLinkList( head,5);
  41.   //InsertLinkList(head,3,9);
  42.    //DeleteLinkList(head,6);
  43.   PritLinkList(head);

  44. }
注意和上面代码对比,多了15行,运行会出错,因为15行 h=(LinkList)malloc(sizeof(struct node));h不是指向原来head的地址而是改为一个新的空间了,所以下面的操作都不是对head里面的地址内容进行操作了。在修改下:

点击(此处)折叠或打开

  1. #include "stdio.h"
  2. #include<malloc.h>
  3. #define ok 0
  4. #define error 1
  5. struct node
  6. {
  7.     int data;
  8.     struct node *next;
  9. };
  10. typedef struct node * LinkList;
  11. int CreatLinkList(LinkList * h,int n)
  12. {
  13.     LinkList tmp,p;
  14.     int i;
  15.      *h=(LinkList)malloc(sizeof(struct node));
  16.     p= *h;
  17.     for(i=0;i<n;i++)
  18.     {
  19.         tmp=(LinkList)malloc(sizeof(struct node));
  20.         tmp->data=i;
  21.         tmp->next=NULL;

  22.         p->next=tmp;
  23.         p=p->next;
  24.     }
  25.     return ok;
  26. }

  27. 点击(此处)折叠或打开

    1. void main()
    2. {
    3.   LinkList head=(LinkList)malloc(sizeof(struct node));
    4.   CreatLinkList( &head,5);
    5.   //InsertLinkList(head,3,9);
    6.    //DeleteLinkList(head,6);
    7.   PritLinkList(head);

    8. }



11行,15行,16行,修改了,注意和上面的区别。主函数没变,这里能运行了,但是原来head指向的地址修改了,在主函数中指向的那片区域没有释放,也没再用,而是改变了head的指向。
主函数CreatLinkList( &head,5);,传入head的地址
主函数可以修改成下面形式,就定义个head指针,在creatlinklist中初始化,和赋值。


点击(此处)折叠或打开

  1. void main()
  2. {
  3.   LinkList head ;
  4.   CreatLinkList( &head,5);
  5.   //InsertLinkList(head,3,9);
  6.    //DeleteLinkList(head,6);
  7.   PritLinkList(head);

  8. }



阅读(800) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~