zoukankan      html  css  js  c++  java
  • 用C语言实现一个链表结构(草稿)

    • 上机环境centos7下 Qt5.11 gcc
    • 一些简单的函数封装
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <stdlib.h>
    
    struct coach
    {
        char name[60];
        int age;
    };
    
    struct linktbl
    {
        struct coach* data;
        struct linktbl* next;
    };
    • 节点数据录入
    void addnodedata(struct coach* nodedata)
    {
        char name[60];
        int age;
        printf("name:
    ");
        scanf("%s",name);
        strcpy(nodedata->name,name);
        printf("age:
    ");
        scanf("%d",&age);
        nodedata->age=age;
        //printf("%s
    ",temp->data->name);
        //scanf("%s",temp->data->age);
        //printf("%s
    ",temp->data->name);
        //scanf("%s",temp->data->age);
    }
    • 初始化---在head节点后添加第一个节点
    void initltbl(struct linktbl* head)
    {
        struct linktbl* anext;
        anext = (struct linktbl*)malloc(sizeof(struct linktbl));
        struct coach* nodedata;
        nodedata = (struct coach*)malloc(sizeof(struct coach));
        addnodedata(nodedata);
        strcpy(anext->data.name,nodedata->name);
        anext->data.age=nodedata->age;
        head->next=anext;
    }
    • 打印所有节点
    void printall(struct linktbl* head)
    {
        struct linktbl*   temp=NULL;
        temp = (struct linktbl*)malloc(sizeof(struct linktbl));
        temp=head->next;
        while(temp!=NULL)
        {
            printf("name is %s, age is %d
    ",temp->data.name,temp->data.age);
            temp=temp->next;
        }
    }

    主函数调用(多次调用打印函数是为了确保能避免一些莫名其妙的错误:例如打印不出来或者结构体的值自己变了)

    int main()
    {
        struct linktbl* head;
        head = (struct linktbl*)malloc(sizeof(struct linktbl));
        initltbl(head);
        printall(head);
        printall(head);
        printall(head);
        printall(head);
        printall(head);
        printall(head);
        return 0;
    }

    测试结果:

    • 加上插入节点部分(这里不算头结点,头结点指向的下一个结点位置是1)
    void insertnode(struct linktbl* head)
    {
        int i, j=0;
        struct linktbl* temp;
        temp=(struct linktbl*)malloc(sizeof(struct linktbl));
        temp=head;
        struct linktbl* newnode;
        struct coach* newnodedata;
        newnode=(struct linktbl*)malloc(sizeof(struct linktbl));
        newnodedata=(struct coach*)malloc(sizeof(struct coach));
        printf("想插在哪个位置?
    ");
        scanf("%d",&j);
        addnodedata(newnodedata);
        strcpy(newnode->data.name,newnodedata->name);
        newnode->data.age=newnodedata->age;
        if(j<1||head->next==NULL)
        {
            printf("尚未初始化或插入位置有误
    ");
        }
        for(i=1;i<j;i++)
        {
            temp=temp->next;
        }
        newnode->next=temp->next;
        temp->next=newnode;
        printf("插入完成!
    ");
    
    }
    • 插入结点---增强
    void insertnode(struct linktbl* head)
    {
        int i, j=0;
        struct linktbl* temp;
        temp=(struct linktbl*)malloc(sizeof(struct linktbl));
        struct linktbl* p;
        p=(struct linktbl*)malloc(sizeof(struct linktbl));
        temp=head;
        struct linktbl* newnode;
        struct coach* newnodedata;
        newnode=(struct linktbl*)malloc(sizeof(struct linktbl));
        newnodedata=(struct coach*)malloc(sizeof(struct coach));
        printf("想插在哪个位置?
    ");
        scanf("%d",&j);
        addnodedata(newnodedata);
        strcpy(newnode->data.name,newnodedata->name);
        newnode->data.age=newnodedata->age;
        if(j<1||head->next==NULL)
        {
            printf("尚未初始化或插入位置有误
    ");
        }
    
        i=1;
        while(i<j)
        {
            temp=temp->next;
            if(temp==NULL)
            {
                break;
            }
            p=temp;
            i++;
        }
        if(temp)
        {
            newnode->next=temp->next;
            temp->next=newnode;
        }
        else
        {
            //处理一种特殊情况,如果指定的插入位置远大于链表本身长度,直接加入尾部节点
            p->next=newnode;
            newnode->next=NULL;
        }
    
        printf("插入完成!
    ");
    
    }

    输出结果:

  • 相关阅读:
    读取手机硬件信息
    Android存储空间不足的解决办法
    Android实现系统重新启动
    使用PackageManager获得应用(包)信息
    获得屏幕尺寸
    Android获取ROOT权限
    Android调用系统自带的设置界面
    TabHost的使用
    ListActivity的使用
    Ubuntu密码错误的问题
  • 原文地址:https://www.cnblogs.com/saintdingspage/p/12098734.html
Copyright © 2011-2022 走看看