关于#c语言#的问题,如何解决?


#include 
#include
struct Node
{
    int data;          //数据域
    struct Node* next; //指针域
};
struct Node* createList()  //创建链表表头
{
    struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));
    //headNode 成为了结构体变量
    //变量使用前必须被初始化
    //headNode->data = 1;
    headNode->next = NULL;
    return headNode;
}
struct Node* creatNode(int data)  //创建结点
{
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}
void printList(struct Node* headNode)  //打印链表
{
    struct Node* pMove = headNode->next;
    while (pMove)
    {
        printf("%d\t", pMove->data);
        pMove = pMove->next;
    }
    printf("\n");
}
void insertNodeByHead(struct Node* headNode, int data)  //头插,插入结点,插入哪个结点,插入的数据是多少
{
    //1.创建插入的节点
    struct Node* newNode = creatNode(data);
    newNode->next = headNode->next;
    headNode->next = newNode;
}
void deleteNodeByAppoin(struct Node* headNode, int posData)  //删除指定位置结点
{
    struct Node* posNode = headNode->next;
    struct Node* posNodeFront = headNode;
    if (posNode == NULL)
        printf("无法删除链表为空\n");
    else
    {
        while (posNode->data != posData)
        {
            posNodeFront = posNode;
            posNode = posNodeFront->next;
            if (posNode == NULL)
            {
                printf("没有找到相关信息,无法删除\n");
                break;
            }
        }
        posNodeFront->next = posNode->next;
        free(posNode);
    }
}
int main()
{
    //struct Node Node1 = { 1,NULL };
    //struct Node Node2 = { 2,NULL };
    //struct Node Node3 = { 1,NULL };
    //Node1.next = &Node2;
    //Node2.next = &Node3;
    struct Node* list = createList();
    insertNodeByHead(list, 1);
    insertNodeByHead(list, 2);
    insertNodeByHead(list, 3);
    printList(list);
    deleteNodeByAppoin(list, 2);
    printList(list);
    system("pause");
    return 0;
}

img


49行这里有个提醒是什么意思?应该怎么修改?照着写的正确代码写的为什么我这不对?

朋友你代码怎么运行的
我这边直接运行是没问题的
代码发错了还是?

img

49行和你发的代码不一样
代码发错了吧

不是第49行的问题,问题在void deleteNodeByAppoin(struct Node* headNode, int posData) 删除指定位置结点函数里,当待删除的结点不在链表中时的处理,不光是break; 就完事了,还需让它这两句:posNodeFront->next = posNode->next; free(posNode); 也不需执行,修改如下,供参考:

#include <stdio.h>
#include<stdlib.h>
struct Node
{
    int data;          //数据域
    struct Node* next; //指针域
};
struct Node* createList()  //创建链表表头
{
    struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));
    //headNode 成为了结构体变量
    //变量使用前必须被初始化
    //headNode->data = 1;
    headNode->next = NULL;
    return headNode;
}
struct Node* creatNode(int data)  //创建结点
{
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}
void printList(struct Node* headNode)  //打印链表
{
    struct Node* pMove = headNode->next;
    while (pMove)
    {
        printf("%d\t", pMove->data);
        pMove = pMove->next;
    }
    printf("\n");
}
void insertNodeByHead(struct Node* headNode, int data)  //头插,插入结点,插入哪个结点,插入的数据是多少
{
    //1.创建插入的节点
    struct Node* newNode = creatNode(data);
    newNode->next = headNode->next;
    headNode->next = newNode;
}
void deleteNodeByAppoin(struct Node* headNode, int posData)  //删除指定位置结点
{
    struct Node* posNode = headNode->next;
    struct Node* posNodeFront = headNode;
    if (posNode == NULL)
        printf("无法删除链表为空\n");
    else
    {
        while (posNode->data != posData)
        {
            posNodeFront = posNode;
            posNode = posNodeFront->next;
            if (posNode == NULL)
            {
                printf("没有找到相关信息,无法删除\n");
                break;
            }
        }
        if (posNode){  // 修改 如果找到了,再执行删除结点的操作。
            posNodeFront->next = posNode->next;
            free(posNode);
        }
    }
}
int main()
{
    //struct Node Node1 = { 1,NULL };
    //struct Node Node2 = { 2,NULL };
    //struct Node Node3 = { 1,NULL };
    //Node1.next = &Node2;
    //Node2.next = &Node3;
    struct Node* list = createList();
    insertNodeByHead(list, 1);
    insertNodeByHead(list, 2);
    insertNodeByHead(list, 3);
    printList(list);
    deleteNodeByAppoin(list, 2);
    printList(list);
    system("pause");
    return 0;
}

//被我修改了,试试看行不?

#include <stdio.h>
#include <stdlib.h>
struct Node
{
    int data;           //数据域
    struct Node *next; //指针域
};
struct Node *createList() //创建链表表头
{
    struct Node *headNode = (struct Node *)malloc(sizeof(struct Node));
    //headNode 成为了结构体变量
    //变量使用前必须被初始化
    //headNode->data = 1;
    headNode->next = NULL;
    return headNode;
}
struct Node *creatNode(int data) //创建结点
{
    struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}
void printList(struct Node *headNode) //打印链表
{
    struct Node *pMove = headNode->next;
    while (pMove)
    {
        printf("%d\t", pMove->data);
        pMove = pMove->next;
    }
    printf("\n");
}
void insertNodeByHead(struct Node *headNode, int data) //头插,插入结点,插入哪个结点,插入的数据是多少
{
    //1.创建插入的节点
    struct Node *newNode = creatNode(data);
    newNode->next = headNode->next;
    headNode->next = newNode;
}
void deleteNodeByAppoin(struct Node *headNode, int posData) //删除指定位置结点
{
    struct Node *posNode = headNode->next;
    struct Node *posNodeFront = headNode;
    if (posNode == NULL)
        printf("无法删除链表为空\n");
    else
    {
        while (posNode)
        {
            if (posNode->data == posData)
            {
                posNodeFront->next = posNode->next;
                free(posNode);
                return;
            }
            posNodeFront = posNodeFront->next;
            posNode = posNode->next;
        }
    }
    printf("未找到相关的值!\n");
}
int main()
{
    //struct Node Node1 = { 1,NULL };
    //struct Node Node2 = { 2,NULL };
    //struct Node Node3 = { 1,NULL };
    //Node1.next = &Node2;
    //Node2.next = &Node3;
    struct Node *list = createList();
    insertNodeByHead(list, 1);
    insertNodeByHead(list, 2);
    insertNodeByHead(list, 3);
    printList(list);
    deleteNodeByAppoin(list, 2);
    printList(list);
    system("pause");
    return 0;
}