美文网首页
C语言基础啦啦啦(链表)

C语言基础啦啦啦(链表)

作者: I踏雪寻梅 | 来源:发表于2016-10-21 21:12 被阅读0次

    C语言基础

    链表

    //
    //  main.c
    //  15-链表思想
    //
    //  Created by ccj on 2016/10/21.
    //  Copyright © 2016年 ccj. All rights reserved.
    //
    
    #include <stdio.h>
    
    typedef struct Property
    {
        int fridge;
        int washMachine;
    
    }Property;
    
    typedef  struct Home
    {
        Property property;//财产
        struct Home *next;//下一家的地址
    }Home;
    
    int main()
    {
        
        Home zaiHome,jiangHome,worldHome;
        ahome.property.fridge=0;
        ahome.property.washMachine=0;
        ahome.next=&jiangHome;
        
        bhome.property.fridge=1;
        bhome.property.washMachine=0;
        bhome.next=&worldHome;
        
        
        chome.property.fridge=1;
        chome.property.washMachine=1;
        chome.next=NULL;
        Home *p=&ahome;
        printf("ahome.fridge=%d,ahome.washMachine=%d\n",p->fridge,p->washMachine);
        p=p->address;
        printf("bhome.fridge=%d,bhome.washMachine=%d\n",p->fridge,p->washMashine);
        p=p->address;
        printf("chome.fridge=%d,chome.washMachine=%d\n",p->fridge,p->washMashine);
        Home *p;
        for (p=&zaiHome; p!=NULL; p=p->next)
        {
            printf("fridge=%d,washMachine=%d\n",p->property.fridge,p->property.washMachine);
        }
    
        
        return 0;
    }
    
        
        Home *p=&ahome;
        printf("ahome.fridge=%d,ahome.washMachine=%d\n",p->fridge,p->washMachine);
        p=p->address;
        printf("bhome.fridge=%d,bhome.washMachine=%d\n",p->fridge,p->washMashine);
        p=p->address;
        printf("chome.fridge=%d,chome.washMachine=%d\n",p->fridge,p->washMashine);
        /*
        Home *p;
        for(p=&ahome;p!=NULL;p=p->address)
        {
            
             printf("home.fridge=%d,home.washMachine=%d\n",p->fridge,p->washMashine);
        }
        */
    }
    
    • malloc将堆区分出来一块空间
    int *test()
    {
        int *p=(int *)malloc(sizeof(int));
        return p;
    }
    int main()
    {
        int *p=test();
        *p=6;
        //释放p指向堆区的空间。
        free(p);
    }
    
    
    • 用堆区划分上程序
    typedef struct Property
    {
        int fridge;
        int washMachine;
    
    }Property;
    
    typedef  struct Home
    {
        Property property;
        struct Home *next;
    }Home;
    int main()
    {
       // Home *head=(Home *)malloc(sizeof(Home));
        //head所对应的空间地址域部分只能存储链表当中第一个有实际数据的节点地址,
        Home *pa=(Home *)malloc(sizeof(Home));
        Home *pb=(Home *)malloc(sizeof(Home));
        Home *pc=(Home *)malloc(sizeof(Home));
        
    //    head->next=pa;
        pa->property.fridge=0;
        pa->property.washMachine=0;
        pa->next=pb;
        
        pb->property.fridge=0;
        pb->property.washMachine=0;
        pb->next=pc;
         
        pc->property.fridge=0;
        pc->property.washMachine=0;
        pc->next=NuLL;
    
        Home *p;
        for(p=&ahome;p!=NULL;p=p->address)
        {
            
             printf("home.fridge=%d,home.washMachine=%d\n",p->property.fridge,p->property.washMashine);
        }
    /*  Home *p;
        for(p=head->next;p!=NULL;p=p->next)
        {
            
             printf("home.fridge=%d,home.washMachine=%d\n",p->property.fridge,p->property.washMashine);
        }*/
    }
    

    链表初级思想。再回首,真。。。。。

    相关文章

      网友评论

          本文标题:C语言基础啦啦啦(链表)

          本文链接:https://www.haomeiwen.com/subject/expcuttx.html