链表常用内容和易犯错误

 

1.创建链表中没有分配空间

for (i=1;i<=n;i++)

{

//遗漏,从而使链表的每一个数据的地址都一样

s=(struct node *) malloc (sizeof(struct node));

scanf("%ld",&s->data);

s->next=p;

p=s;

}

2.对指针赋值为空后又对指针的内容进行赋值

    struct node

    {

        long data;

        struct node *next;

    }*p;

    p=(struct node *) malloc (sizeof(struct node));

    p=NULL;

    p->data=1;//错误

 

3.释放指针后又对指针进行操作

free(p);

p->data=1;//错误

 

4.链表指针

 #include <malloc.h>

    struct node

    {

        long data;

        struct node *next;

    }*p;

    //本身已经是struct node *point,创建数组再加"*"

//第一个括号为数据的类型(xxx的地址),所以为两个*;第二个括号为数据分配的单元(xxx)的数据类型,所以为一个*

struct node **point=(struct node **) malloc (sizeof(struct node *)*100000);

 

posted @ 2017-03-25 23:05  congmingyige  阅读(642)  评论(0编辑  收藏  举报