Hakutaku 2018-10-13 16:38 采纳率: 100%
浏览 721
已采纳

C语言 将node加入到linked list中间某个位置 的函数

具体要求:两个参数
1. 一个list_t mylist

2.一个新char p ,
比如mylist的样子是b-a-c-d-c-d ,p = ‘d’
要求p转成node加到d
*最后一次**出现的之前,变为
b-a-c-d-c-**d**-d

typedef struct node node_t;

struct node {
char data;
node_t *next;
};

typedef struct {
node_t *head;
node_t *foot;
} list_t;

void insert_in_list(list_t *mylist, char p) {
·····

  • 写回答

2条回答 默认 最新

  • threenewbee 2018-10-14 06:40
    关注
    // Q702498.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    
    #include "stdlib.h"
    #include "stdio.h"
    
    typedef struct node node_t;
    
    struct node {
        char data;
        node_t *next;
    };
    
    typedef struct {
        node_t *head;
        node_t *foot;
    } list_t;   
    
    void printlist(list_t list)
    {
        node_t * p = list.head;
        while (p != NULL)
        {
            if (p->next == NULL)
                printf("%c\n", p->data);
            else
                printf("%c->", p->data);
            p = p->next;
        }
    }
    
    node_t * gennode(char data)
    {
        node_t * p = (node_t *)malloc(sizeof(node_t));
        p->data = data;
        p->next = NULL;
        return p;
    }
    
    void insert(list_t list, char p)
    {
        node_t * p1 = list.head;
        node_t * lastfound = NULL;
        while (p1 != NULL)
        {
            if (p1->data == 'd')
                lastfound = p1;
            p1 = p1->next;
        }
        node_t * pnew = (node_t *)malloc(sizeof(node_t));
        pnew->next = lastfound->next;
        pnew->data = p;
        lastfound->next = pnew;
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        node_t * node1 = gennode('b');
        node_t * node2 = gennode('a');
        node1->next = node2;
        node_t * node3 = gennode('c');
        node2->next = node3;
        node_t * node4 = gennode('d');
        node3->next = node4;
        node_t * node5 = gennode('c');
        node4->next = node5;
        node_t * node6 = gennode('d');
        node5->next = node6;
        list_t mylist;
        mylist.head = node1;
        mylist.foot = node6;
        printlist(mylist);
        insert(mylist, 'd');
        printlist(mylist);
        return 0;
    }
    
    
    

    运行结果:
    b->a->c->d->c->d
    b->a->c->d->c->d->d
    Press any key to continue . . .

    如果问题得到解决,麻烦点下我回答右边的采纳

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器