C语言链表

作者: 帅碧 | 来源:发表于2016-10-21 16:51 被阅读52次

    链表

    #include "stdio.h"
    typedef struct Home
    {
        int fridge;
        int washMachine;
        struct Home *address;
    }Home;
    
    int main()
    {
        Home zaiHome,jiangHome,worldHome;
        zaiHome.fridge=0;
        zaiHome.washMachine=0;
        zaiHome.address=&jiangHome;
        
        jiangHome.fridge=1;
        jiangHome.washMachine=0;
        jiangHome.address=&worldHome;
        
        worldHome.fridge=1;
        worldHome.washMachine=1;
        worldHome.address=NULL;
        
    
        Home *p;
        for(p=&zaiHome;p!=NULL;p=p->address)
        {
            printf("fridge=%d,washMachine=%d\n",p->fridge,p->washMachine);
        }
    
    }
    
    
    
    #include "stdio.h"
    typedef struct Home
    {
        int fridge;
        int washMachine;
        struct Home *address;
    }Home;
    
    int main()
    {
        Home zaiHome,jiangHome,worldHome;
        zaiHome.fridge=0;
        zaiHome.washMachine=0;
        zaiHome.address=&jiangHome;
        
        jiangHome.fridge=1;
        jiangHome.washMachine=0;
        jiangHome.address=&worldHome;
        
        worldHome.fridge=1;
        worldHome.washMachine=1;
        worldHome.address=NULL;
        Home *p=&zaiHome;
         printf("zaiHome.fridge=%d,washMachine=%d\n",p->fridge,p->washMachine);
        p=p->address;
        printf("jiangHome.fridge=%d,jiangMachine=%d\n",p->fridge,p->washMachine);
        p=p->address;
        printf("worldHome.fridge=%d,worldMachine=%d\n",p->fridge,p->washMachine);
        
    }
    
    
    #include "stdio.h"
    typedef struct P
    {
        int fridge;
        int washMachine;
        struct Home *address;
    }Home;
    
    
    • 作业
    111;xx1;xx1@qq.com
    112;xx2;xx2@qq.com
    113;xx3;xx3@qq.com
    
    xx1;xx1.@qq.com;111
    xx2;xx2.@qq.com;112
    xx3;xx3.@qq.com;113
    
    //1.先将文件中的内容读取到内存中,fgets()存到字符串数组
    
    //2.按照;分割字符串
    
    //3.交换位置
    
    //4.在文件里输出
    
    ## C语言链表
    
    ### 链表
    
    

    include "stdio.h"

    typedef struct Home
    {
    int fridge;
    int washMachine;
    struct Home *address;
    }Home;

    int main()
    {
    Home zaiHome,jiangHome,worldHome;
    zaiHome.fridge=0;
    zaiHome.washMachine=0;
    zaiHome.address=&jiangHome;

    jiangHome.fridge=1;
    jiangHome.washMachine=0;
    jiangHome.address=&worldHome;
    
    worldHome.fridge=1;
    worldHome.washMachine=1;
    worldHome.address=NULL;
    
    
    Home *p;
    for(p=&zaiHome;p!=NULL;p=p->address)
    {
        printf("fridge=%d,washMachine=%d\n",p->fridge,p->washMachine);
    }
    

    }

    
    

    include "stdio.h"

    typedef struct Home
    {
    int fridge;
    int washMachine;
    struct Home *address;
    }Home;

    int main()
    {
    Home zaiHome,jiangHome,worldHome;
    zaiHome.fridge=0;
    zaiHome.washMachine=0;
    zaiHome.address=&jiangHome;

    jiangHome.fridge=1;
    jiangHome.washMachine=0;
    jiangHome.address=&worldHome;
    
    worldHome.fridge=1;
    worldHome.washMachine=1;
    worldHome.address=NULL;
    Home *p=&zaiHome;
     printf("zaiHome.fridge=%d,washMachine=%d\n",p->fridge,p->washMachine);
    p=p->address;
    printf("jiangHome.fridge=%d,jiangMachine=%d\n",p->fridge,p->washMachine);
    p=p->address;
    printf("worldHome.fridge=%d,worldMachine=%d\n",p->fridge,p->washMachine);
    

    }

    
    

    include "stdio.h"

    typedef struct P
    {
    int fridge;
    int washMachine;
    struct Home *address;
    }Home;

    
    >- 作业
    
    

    111;xx1;xx1@qq.com
    112;xx2;xx2@qq.com
    113;xx3;xx3@qq.com

    xx1;xx1.@qq.com;111
    xx2;xx2.@qq.com;112
    xx3;xx3.@qq.com;113

    //1.先将文件中的内容读取到内存中,fgets()存到字符串数组

    //2.按照;分割字符串

    include "srdio.h"

    int *xxx()
    {
    //我们在堆区分配出来一块空间
    int *p=(int *)malloc(sizeof(int));
    return p;
    }

    int main()
    {
    int *p=xxx();
    *p=6;
    free(p);//释放p所指向的那块空间指向的堆区
    return *p;
    }

    
    

    include "stdio.h"

    include "stdib.h"

    typedef struct Property
    {
    int fridge;
    int washMachine;
    }Property;

    typedef struct Home
    {
    Property property;
    struct Home *next;
    }Home;
    int main()
    {
    Home *pZai=(Home *)malloc(sizeof(Home));
    Home *pJiang=(Home *)malloc(sizeof(Home));
    Home *pWorld=(Home *)malloc(sizeof(Home));
    pZai->property.fridge=0;
    pZai->property.washMachine=0;
    pZai->next=pJiang;

    pJiang->property.fridge=0;
    pJiang->property.washMachine=1;
    pJiang->next=pWorld;
    
    pWorld->property.fridge=1;
    pWorld->property.washMachine=1;
    pWorld->next=NULL;
    
    Home *temp;
    for(temp=pZai;temp!=NULL;temp=temp->next)
    {
        printf("fridge=%d.washMachine=%d\n",temp->property.fridge,temp->property.washMachine);
    }
    
    return 0;
    

    }

    
    

    include "stdio.h"

    include "stdlib.h"

    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));
    Home *pZai=(Home *)malloc(sizeof(Home));
    Home *pJiang=(Home *)malloc(sizeof(Home));
    Home *pWorld=(Home *)malloc(sizeof(Home));

    //head所对应的空间地址域部分只存储链表当中第一个有实际数据的节点地址;head所对应的空间数据域部分不存储信息,我们叫做有头节点.
    head->next=pZai;
    
    pZai->property.fridge=0;
    pZai->property.washMachine=0;
    pZai->next=pJiang;
    
    pJiang->property.fridge=0;
    pJiang->property.washMachine=0;
    pJiang->next=pWorld;
    
    pWorld->property.fridge=1;
    pWorld->property.washMachine=1;
    pWorld->next=NULL;
    
    Home *temp;
    for(temp=head->next;temp!=NULL;temp=temp->next)
    {
        printf("fridge=%d.washMachine=%d\n",temp->property.fridge,temp->property.washMachine);
    }
    
    return 0;
    

    }

    
    >- 利用头插法给链表填数据
    
    

    include "stdio.h"

    include "stdlib.h"

    typedef struct Student
    {
    char name[10];
    int num;
    }Student;
    typedef struct LINK
    {
    Student data;
    struct LINK next;
    }LINK,
    pLINK;

    int main()
    {
    //创建链表(创建头节点)
    pLINK head=(pLINK)malloc(sizeof(LINK));
    head->next=NULL;
    //1,头插法,给链表填入数据。
    pLINK p=(pLINK)malloc(sizeof(LINK));
    p->next=head->next;
    head->next=p;
    printf("请输入学生信息:\n");
    Student stu;
    scanf("%s%d",stu.name,&stu.num);
    p->data=stu;

    //2.打印函数
    pLINK temp=head->next;
    for(;temp!=NULL;temp=temp->next)
    {
        printf("name=%s,num=%d\n",temp->data.name,temp->data.num);
    }
    

    }

    
    >- 如果存在内存泄露(开辟了两个head,而只有一个head有用时),解决方法1
    
    

    include "stdio.h"

    include "stdlib.h"

    typedef struct Student
    {
    char name[10];
    int num;
    }Student;
    typedef struct LINK
    {
    Student data;
    struct LINK next;
    }LINK,
    pLINK;

    pLINK createList(pLINK head)//接收开辟的空间的地址
    {
    //创建链表(创建头节点)
    if(head==NULL)
    {

        head=(pLINK)malloc(sizeof(LINK));
        head->next=NULL;
    }
    return head;//不为NULL时,直接返回地址,不创建.
    

    }

    int main()
    {
    pLINK head=NULL;
    pLINK head=(pLINK *)malloc(sizeof(pLINK));
    head->next=NULL;
    head=createList(head);//将堆区建立的head空间的地址传给子函数里的(pLINK head).
    。。。。
    return 0;

    }

    
    >- 解决方法2
    
    

    include "stdio.h"

    include "stdlib.h"

    typedef struct Student
    {
    char name[10];
    int num;
    }Student;
    typedef struct LINK
    {
    Student data;
    struct LINK next;
    }LINK,
    pLINK;

    pLINK createList(pLINK head)
    {
    //创建链表(创建头节点)
    pLINK head=(pLINK)malloc(sizeof(LINK));
    if(head==NULL)
    {

        head=(pLINK)malloc(sizeof(LINK));
        head->next=NULL;
    }
    return head;
    

    }

    int main()
    {
    pLINK head=NULL;
    pLINK head=(pLINK *)malloc(sizeof(pLINK));
    head=createList(head);

    。。。。
    return 0;
    

    }

    
    

    include "stdio.h"

    include "stdlib.h"

    typedef struct Student
    {
    char name[10];
    int num;
    }Student;
    typedef struct LINK
    {
    Student data;
    struct LINK next;
    }LINK,
    pLINK;

    //创建链表(创建头节点)
    pLINK createList(pLINK head)
    {
    if(NULL==head)
    {

        head=(pLINK)malloc(sizeof(LINK));
        head->next=NULL;
    }
    return head;
    

    }
    //打印函数
    void printData(pLINK head)
    {
    if(NULL==head)
    {
    printf("无信息可打印:\n");
    return;
    }
    pLINK temp=head->next;
    for(;temp!=NULL;temp=temp->next)
    {
    printf("name=%s,num=%d\n",temp->data.name,temp->data.num);
    }

    }
    Student getData();
    void headInsertData(pLINK head)
    {
    if(NULL==head)
    {
    printf("链表没有创建:\n");
    return;
    }
    pLINK p=(pLINK)malloc(sizeof(LINK));
    p->next=head->next;
    head->next=p;
    p->data=getData();

    }
    Student getData()
    {
    printf("请输入学生信息:\n");
    Student stu;
    scanf("%s%d",stu.name,&stu.num);
    return stu;

    }

    int main()
    {

    pLINK head=NULL;
    head=(pLINK)malloc(sizeof(LINK));
    head=createList(head);
    headInsertData(head);
    printData(head);
    return 0;
    

    }

    
    

    include "stdio.h"

    include "stdlib.h"

    typedef struct Student
    {
    char name[10];
    int num;
    }Student;
    typedef struct LINK
    {
    Student data;
    struct LINK next;
    }LINK,
    pLINK;

    //创建链表(创建头节点)
    pLINK createList(pLINK head)
    {
    if(NULL==head)
    {

        head=(pLINK)malloc(sizeof(LINK));
        head->next=NULL;
    }
    printf("链表创建成功\n");
    return head;
    

    }
    //打印函数
    void printData(pLINK head)
    {
    if(NULL==head)
    {
    printf("无信息可打印:\n");

    }
    pLINK temp=head->next;
    printf("head-->");
    for(;temp!=NULL;temp=temp->next)
    {
        printf("[%s,%d]--->",temp->data.name,temp->data.num);
    }
    printf("NULL\n");
    

    }
    Student getData();
    void headInsertData(pLINK head)
    {
    if(NULL==head)
    {
    printf("链表没有创建:\n");
    return;
    }
    pLINK p=(pLINK)malloc(sizeof(LINK));
    p->next=head->next;
    head->next=p;
    p->data=getData();
    printf("数据插入成功\n");
    }
    Student getData()
    {
    printf("请输入学生信息[name,num]:\n");
    Student stu;
    scanf("%s%d",stu.name,&stu.num);
    return stu;

    }

    int main()
    {
    int select;
    pLINK head=NULL;
    while(1)
    {
    printf("==========\n");
    printf("1.创建链表\n");
    printf("2.头插数据\n");
    printf("3.打印数据\n");
    printf("==========\n");
    scanf("%d",&select);
    switch(select)
    {
    case 1:
    head=createList(head);
    break;
    case 2:
    headInsertData(head);
    break;
    case 3:
    printData(head);
    break;
    default:
    break;
    }
    }
    return 0;
    }

    include "stdio.h"

    include "stdlib.h"

    include "string.h"

    typedef struct Student
    {
    char name[10];
    int num;
    }Student;
    typedef struct LINK
    {
    Student data;
    struct LINK next;
    }LINK,
    pLINK;

    //创建链表(创建头节点)
    pLINK createList(pLINK head)
    {
    if(NULL==head)
    {

        head=(pLINK)malloc(sizeof(LINK));
        head->next=NULL;
    }
    printf("链表创建成功\n");
    return head;
    

    }
    //打印函数
    void printData(pLINK head)
    {
    if(NULL==head)
    {
    printf("无信息可打印\n");
    return;
    }
    pLINK temp=head->next;
    printf("head-->");
    for(;temp!=NULL;temp=temp->next)
    {
    printf("[%s,%d]--->",temp->data.name,temp->data.num);
    }
    printf("NULL\n");

    }
    Student getData();
    void headInsertData(pLINK head)
    {
    if(NULL==head)
    {
    printf("链表没有创建:\n");
    return;
    }
    pLINK p=(pLINK)malloc(sizeof(LINK));
    p->next=head->next;
    head->next=p;
    p->data=getData();
    printf("头部数据插入成功\n");
    }
    void myScanf(char *str,int size)
    {
    int i=0;
    for(i=0;i<size-1;i++)
    {
    str[i]=getchar();
    if(str[i]=='\n')
    {
    break;
    }
    }
    //说明在中途碰到\n
    if(str[i]=='\n')
    {
    str[i]='\0';
    }
    else
    {
    str[i]='\0';
    while(getchar()!='\n');//吸收缓存里存留的字符
    }
    }
    Student getData()
    {
    printf("请输入学生信息[name,num]:\n");
    Student stu;
    myScanf(stu.name,20);
    scanf("%d",&stu.num);
    return stu;

    }
    void tailInsertData(pLINK head)
    {
    //找尾指针:最后一个节点的地址
    pLINK temp;
    for(temp=head;temp->next!=NULL;temp=temp->next)
    {
    ;
    }
    //这个循环跳出的条件:temp->next==NULL;
    pLINK p=(pLINK)malloc(sizeof(LINK));
    p->data=getData();
    temp->next=p;
    p->next=NULL;
    printf("尾部数据插入成功\n");

    }
    void headDeleteData(pLINK head)
    {
    if(NULL==head||head->next==NULL)
    {
    printf("无信息可删\n");
    return;
    }
    pLINK p=head->next;
    head->next=p->next;
    free(p);
    p=NULL;
    printf("头删成功\n");
    }
    void tailDeleteData(pLINK head)
    {
    pLINK temp;
    if(head==NULL||head->next==NULL)
    {
    printf("无信息可删\n");
    return;
    }
    //相当于等待状态,等待temp->next->next==NULL;
    for(temp=head;temp->next->next!=NULL;temp=temp->next)
    {
    ;
    }
    pLINK p=temp->next;
    temp->next=NULL;
    free(p);
    p=NULL;
    printf("尾删成功\n");
    }
    void randomInsertData(pLINK head)
    {

    int n,count=1,i;
    printf("请输入距离头部多少个位置插入数据\n");
    scanf("%d",&n);
    getchar();
    if(n==0)
    {
        printf("输入错误\n");
    }
    pLINK temp;
    pLINK p=(pLINK)malloc(sizeof(LINK));
    for(temp=head;temp->next!=NULL;temp=temp->next)
    {   
        count++;
    }
    printf("count=%d\n",count);
    if(n>count)
    {
        printf("超出链表的长度了,输入无效\n");
    }
    else if(n==count)
    {
        temp->next=NULL;
        p->next=temp->next;
        temp->next=p;
        p->data=getData();
        printf("数据插入成功\n");
    }
    else if(n<count)
    {
        temp=head;
        for(i=1;i<n;i++)
        {
            temp=temp->next;
        }
        printf("目前num=%d\n",temp->data.num);
        p->next=temp->next;
        temp->next=p;
        p->data=getData();
        printf("数据插入成功\n");
    }
    

    }

    void insertData(pLINK head)
    {
    if(NULL==head)
    {
    printf("链表没有创建:\n");
    return;
    }
    int select;
    while(1)
    {
    printf("==========\n");
    printf("1.头插数据\n");
    printf("2.尾插数据\n");
    printf("3.任意位置插入数据\n");
    printf("4.返回上一层\n");
    printf("==========\n");
    scanf("%d",&select);
    getchar();//吸收存留的\n
    switch(select)
    {
    case 1:
    headInsertData(head);
    break;
    case 2:
    tailInsertData(head);
    break;
    case 3:
    randomInsertData(head);
    break;
    case 4:
    return;
    default:
    break;

        }
    
    }
    

    }

    void randomDeleteData(pLINK head)
    {
    int n,count=1,i;
    printf("请输入距离头部多少个位置删除数据\n");
    scanf("%d",&n);
    getchar();
    if(n==0)
    {
    printf("输入错误\n");
    }
    pLINK temp;
    pLINK p=(pLINK)malloc(sizeof(LINK));
    for(temp=head;temp->next!=NULL;temp=temp->next)
    {
    count++;
    }
    printf("count=%d\n",count);
    if(n>=count)
    {
    printf("删除失败\n");
    }
    else if(n==count-1)
    {
    temp=head;
    for(i=1;i<n;i++)
    {
    temp=temp->next;
    }
    p=temp->next;
    temp->next=NULL;
    free(p);
    printf("数据删除成功\n");

    }
    else if(n<count-1)
    {
        temp=head;
        for(i=1;i<n;i++)
        {
            temp=temp->next;
        }
        p=temp->next;
        temp->next=p->next;
        free(p);
        printf("数据删除成功\n");
    }
    

    }
    void deleteData(pLINK head)
    {
    if(head==NULL||head->next==NULL)
    {
    printf("无信息可删\n");
    return;
    }
    int select;
    while(1)
    {
    printf("==========\n");
    printf("1.头删数据\n");
    printf("2.尾删数据\n");
    printf("3.任意位置删除数据\n");
    printf("4.返回上一层\n");
    printf("==========\n");
    scanf("%d",&select);
    switch(select)
    {
    case 1:
    headDeleteData(head);
    break;
    case 2:
    tailDeleteData(head);
    break;
    case 3:
    randomDeleteData(head);
    break;
    case 4:
    return;
    default:
    break;

        }
    }
    

    }
    void searchData(pLINK head)
    {
    if(head==NULL||head->next==NULL)
    {
    printf("无信息可查询\n");
    return;
    }
    int num;
    printf("请输入要查询的学号\n");
    scanf("%d",&num);
    pLINK temp;
    for(temp=head->next;temp!=NULL;temp=temp->next)
    {
    if(temp->data.num==num)
    {
    printf("[%s,%d]\n",temp->data.name,temp->data.num);
    return;
    }
    }
    if(NULL==temp)
    {
    printf("查无此人\n");
    }
    }
    void changeData(pLINK head)
    {
    if(head==NULL||head->next==NULL)
    {
    printf("无信息可修改\n");
    return;
    }
    char name[20];
    printf("请输入名字\n");
    myScanf(name,20);
    pLINK temp;
    for(temp=head->next;temp!=NULL;temp=temp->next)
    {
    if(strcmp(temp->data.name,name)==0)
    {
    printf("该学生原来信息:[%s,%d]\n",temp->data.name,temp->data.num);
    printf("请输入要修改的信息[姓名,学号]:");
    Student stu;
    myScanf(stu.name,20);
    scanf("%d",&stu.num);
    getchar();
    temp->data=stu;
    printf("修改成功\n");
    return;

        }
    }
    if(temp==NULL)
    {
        printf("查无此人\n");
    }
    

    }

    int main()
    {
    pLINK head=NULL;
    int select;
    while (1)
    {
    printf("=========\n");
    printf("1.创建链表\n");
    printf("2.插入数据\n");
    printf("3.删除数据\n");
    printf("4.打印数据\n");
    printf("5.查找数据\n");
    printf("6.修改数据\n");
    printf("7.退出\n");
    printf("=========\n");
    scanf("%d",&select);
    getchar();
    switch (select)
    {
    case 1:
    head=createList(head);
    break;
    case 2:
    insertData(head);
    break;
    case 3:
    deleteData(head);
    break;
    case 4:
    printData(head);
    break;
    case 5:
    searchData(head);
    break;
    case 6:
    changeData(head);
    break;
    case 7:
    return 0;
    default:
    break;
    }
    }
    return 0;
    }

    
    

    相关文章

      网友评论

      本文标题:C语言链表

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