摁回车的大雷 2022-07-30 14:36 采纳率: 100%
浏览 17
已结题

c语言链表插入结点问题 言之有理即采纳

newNode->pre被赋值的时候怎么会警告不兼容呢?

#include <stdio.h>
#include <stdlib.h>

typedef struct node_t
{
    int data;
    struct note_t* pre;
    struct node_t* next;
} Linknote_t;

Linknote_t* createNode(int x)
{
    Linknote_t* node = (Linknote_t*)malloc(sizeof(Linknote_t));
    if(node == NULL)
    {
        printf("malloc error\n");
        return;
    }
    node->pre = NULL;
    node->next = NULL;
    node->data = x;
    return node;
}

int lenthList(Linknote_t* p)
{
    int count = 0;
    Linknote_t* temp = p;
    while(temp !=NULL)
    {
        count++;
        temp = temp->next;
    }
    return count;
}

void insertNodeBack(Linknote_t* p,int pos,int x)//在pos位置后面插入结点
{
    if(pos<0||pos>lenthList(p)-1)//pos最大取到链表最后一个结点的位置
    {
        printf("position error\n");
        return;
    }
    Linknote_t* newNode = (Linknote_t*)malloc(sizeof(Linknote_t));
    newNode->data = x;
    Linknote_t* temp = p;
    int i;
    for(i=0;i<pos;i++)
    {
        temp = temp->next;
    }
    if(temp->next == NULL)//temp已经是最后一个结点,特殊考虑
    {
        temp->next = newNode;
        newNode->pre = temp;  //警告
        return;
    }
    else
    {
        newNode->next = temp->next;
        newNode->pre = temp;//警告
        temp->next->pre = newNode;//警告
        temp->next = newNode;
    }
    return;
}
void printLinkList(Linknote_t* p)
{
    Linknote_t* temp = p;
    while(temp!=NULL)
    {
        printf("%d->",temp->data);
        temp = temp->next;
    }
    printf("\n");
}
int main()
{
    Linknote_t* head = createNode(100);
    insertNodeBack(head,0,101);
    printf("有效结点数:%d\n",lenthList(head));
    printLinkList(head);

    return 0;
}

img

  • 写回答

1条回答 默认 最新

  • 快乐鹦鹉 2022-07-30 14:43
    关注

    定义写错了啊
    你写成了struct note_t * pre;
    应该是struct node_t * pre;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 8月7日
  • 已采纳回答 7月30日
  • 创建了问题 7月30日

悬赏问题

  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程