项目开发日报表

项目开发日报表

项目名称:嵌入式 linux C 第 7 天
今日进度以及任务:
学习了链表以及使用链表解决了四个简单练习题。

#include 
#include 
#include 

//1\定义节点类型:只要确定数据域保存哪些数据,链接域是固定的!<节点的指针>
struct node
{
    char name[100];
    int age; //数据域

    struct node *next; //链接域:保存下一个节点的地址
};

typedef struct node Node;
typedef Node *Link;

//Link head; 尽量不要使用全局变量,不安全;
void is_malloc(Link p)
{
    if (NULL == p)
    {
        printf("malloc error!\n");
        exit(1);
    }
}

void print_error(int ret)
{

    switch (ret)
    {
    case 0:
    {
        printf("handle success!\n");
        break;
    }
    case -1:
    {
        printf("link is null!\n");
        break;
    }
    case -2:
    {
        printf("node is not find!\n");
        break;
    }
    default:
    {
        printf("not error!\n");
        exit(1);
    }
    }
}

void create_link(Link *head)
{
    *head = (Link)malloc(sizeof(Node));
    is_malloc(*head);
    (*head)->next = NULL;
}

void insert_head_node(Link newnode, Link *head)
{
    newnode->next = (*head)->next;
    (*head)->next = newnode;
}

void insert_tail_node(Link newnode, Link *head)
{
    Link temp = *head;
    //对空表做处理
    while (temp->next != NULL)
    {
        temp = temp->next;
    }

    temp->next = newnode;
    newnode->next = NULL;
}

void display_link(Link head)
{
    Link temp = head->next;

    while (temp != NULL)
    {
        printf("name:%s age:%d\n", temp->name, temp->age);
        temp = temp->next;
    }
}

Link merge_link(Link head1, Link head2){
    Link newhead;
    create_link(&newhead);
    newhead->next = head1->next;
    while (head1->next != NULL)
    {
        head1 = head1->next;
    }
    head1->next = head2->next;
    
    return newhead;
}

// 返回最小节点的上一个节点
Link findminage_link(Link head){
    if(head->next == NULL){
        return NULL;
    }

    Link ans = NULL;
    int age = 1000;

    Link prehead = head;
    head = head->next;
    
    while (head->next != NULL)
    {
        if(head->age < age){
            ans = prehead;
            age = head->age;
        }
        prehead =  prehead->next;
        head = head->next;
    }
    if(head->age < age){
        return prehead;
    }else{
        return ans;
    }
    
}

void sort_link(Link head){
    //Link minagenode = findminage_link(head);
    
    while (head->next != NULL)
    {
        Link pre = findminage_link(head);

        Link cur = pre->next;
        pre->next = cur->next;
        cur->next = head->next;
        head->next = cur;
               
        head = head->next;
    }
    
}


void re_link(Link head){
    Link nodei = head->next->next;
    Link prenodei = head->next;
    
    if(nodei == NULL){
        return;
    }

    while (nodei != NULL){
        Link tempnodei = nodei->next;

        prenodei->next = nodei->next;    
        nodei->next = head->next;
        head->next = nodei;

        nodei = tempnodei;
    }

}

void findname_link(Link head, int age){
    Link ans = NULL;
    int cur = 100;

    head = head->next;
    while (head != NULL)
    {
        if(abs(head->age - age) < cur){
            ans = head;
            cur = abs(head->age - age);
        }
        head = head->next;
    }
    
    printf("%s\n", ans->name);
}

int main(int argc, char *argv[])
{
    //2、定义头指针
    Link gghead;
    Link mmhead;
    Link newnode;

    create_link(&gghead);
    create_link(&mmhead);

    // 男生链表
    newnode = (Link)malloc(sizeof(Node));
    if (NULL == newnode){
        printf("malloc error!\n");
        exit(1);
    }
    strcpy(newnode->name, "gg1");
    newnode->age = 22;
    insert_head_node(newnode, &gghead);

    newnode = (Link)malloc(sizeof(Node));
    if (NULL == newnode){
        printf("malloc error!\n");
        exit(1);
    }
    strcpy(newnode->name, "gg2");
    newnode->age = 23;
    insert_head_node(newnode, &gghead);


    //女生链表
    newnode = (Link)malloc(sizeof(Node));
    if (NULL == newnode){
        printf("malloc error!\n");
        exit(1);
    }
    strcpy(newnode->name, "mm1");
    newnode->age = 18;
    insert_head_node(newnode, &mmhead);

    newnode = (Link)malloc(sizeof(Node));
    if (NULL == newnode){
        printf("malloc error!\n");
        exit(1);
    }
    strcpy(newnode->name, "mm2");
    newnode->age = 18;
    insert_head_node(newnode, &mmhead);


    //合并链表
    Link mergenode;
    mergenode = merge_link(gghead, mmhead);

    //根据年龄排序
    sort_link(mergenode);

    //翻转
    re_link(mergenode);
    
    display_link(mergenode);
    //寻找
    findname_link(mergenode, 5);



    return 0;
}

本日任务完成情况:全部完成
本日开发中出现的问题汇总:无
本日未解决问题:无
本日开发收获:了解了C语言中的一些深层次的问题加深了对C语言的理解,完成了一些企业C语言面试题。
其他:无

你可能感兴趣的:(定期删除)