摁回车的大雷 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 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助