首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >代码块输出与联机编译器不同

代码块输出与联机编译器不同
EN

Stack Overflow用户
提问于 2022-04-24 12:26:58
回答 1查看 32关注 0票数 0

总之,我应该有“1 2 3 4 5 6 7 8 9 10”。当在CodeBlocks而不是0中执行时,我得到的是随机数。但是,当使用在线编译器进行编译时,我得到了预期的输出。哪个输出是可靠的?在线的还是CodeblockIDE的?如果需要的话,就有链接列表练习的代码:创建、输入数据元素、在指定的点删除并最终输出结果。

代码语言:javascript
运行
复制
#include <stdio.h>       
#include <stdlib.h>      
#include <malloc.h>     

typedef int DataType;    

typedef struct Node
{
    DataType data;
    struct Node *next;
} SLNode;

void ListInitiate(SLNode **head)        /* Initialization */
{
/* If there is memory space, apply the head node space and make the head pointer point to the head node */

    if((*head = (SLNode *)malloc(sizeof(SLNode))) == NULL) exit(1);
    (*head)->next = NULL;          /* set the last of the list as Null */
}


int ListLength(SLNode *head)     /* Get the size of the linked list */
{
    SLNode *p = head;             
    int size = 1;

    while(p->next != NULL)         /*count with loop*/
    {
        p = p->next;
        size ++;
    }
    return size;
}

int ListInsert(SLNode *head, int i, DataType x)

/* Insert a node that contains the data element x before the node ai (0 ≤ i ≤ size) of the linked list with header */

{
    SLNode *p, *q;
    int j;

    p = head;             /* p points to the head*/
    j = -1;               /* the initial value of j is -1*/
    while(p->next != NULL && j < i - 1)
    /* Finally let pointer p point to data element ai-1 node */
    {
        p = p->next;
        j++;
    }

    if(j != i - 1)
    {
        printf("Insert position parameter wrong!");
        return 0;
    }

    /* q points to the new node*/
    if((q = (SLNode *)malloc(sizeof(SLNode))) == NULL) exit(1);
    q->data = x;

    /* There is an error blow*/
    q->next = p->next;
    p->next = q;
    return 1;
}

int ListDelete(SLNode *head, int i, DataType *x)
/* delete the node ai of the list with a header*/
/* put the data element of the node in x. If success, return 1; if fail, return 0*/

{
    SLNode *p, *s;
    int j;
    p = head;
    j = -1;

    while(p->next != NULL && p->next->next!= NULL && j < i - 1)

    /*Finally let pointer p point to data element ai-1 node */

    {
        p = p->next;
        j++;
    }

    if(j!= i - 1)
    {
        printf("Insert position parameter wrong!");
        return 0;
    }


    s = p->next;         /*s points to ai*/
    *x = s->data; /*Assign the data field value of the node pointed by pointer s to x */
    p->next = s->next;     /* delete ai*/
    free(s);          /* free the memory space of s */
    return 1;
}

int ListGet(SLNode *head, int i, DataType *x)
/*The function of taking the data element ai is similar to deleting ai function, but do not delete the data element ai node*/
{
    SLNode *p;
    int j;

    p = head;
    j = 0;
    while(p->next != NULL && j < i)
    {
        p = p->next;
        j++;
    }

    if(j != i)
    {
        printf("The position of the parameter is wrong!");
        return 0;
    }


    *x = p->data;

    return 1;
}

void Destroy(SLNode **head)
{
    SLNode *p, *p1;

    p = *head;
    while(p != NULL)
    {
        p1 = p;
        p = p->next;
        free(p1);
    }
    *head = NULL;
}

void main(void)
{
    SLNode *head;
    int i , x;
    ListInitiate(&head);
    for(i = 0; i < 10; i++)
    {
        if(ListInsert(head, i, i+1) == 0)      /*insert ten data elements*/
        {
            printf("Error!! \n");
            return;
        }
    }

    if(ListDelete(head, 4, &x) == 0)     /* delete data element 5*/
    {
        printf("error! \n");
        return;
    }

    for(i = 0; i < ListLength(head); i++)
    {
        if(ListGet(head, i, &x) == 0)      /* take out the element*/
        {
            printf("Error! \n");
            return;
        }
        else printf("%d    ", x);        /* show data elements*/
    }

    Destroy(&head);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-24 13:15:41

问题在于代码的逻辑。好的输出是Code::Blocks。

问题在insertNode函数中。它的行为如下:它找到第一个节点并在它之后创建一个新节点。如果是i = 0,第0元素将在第一个循环的第一次迭代中找到,然后就不会出现错误,因为j = i - 1 = -1。永远不要在代码中编辑头部的数据。此外,每次调用insertNode函数时,都会请求新节点的值为其位置+ 1。

所以你会得到这个:

代码语言:javascript
运行
复制
----------       -----       -----
| random |  ---> | 1 |  ---> | 2 |  ---> etc.
----------       -----       -----
  head            0th         1st

之后,在listGet函数中,您对列表的生成方式产生了误解。例如,当调用listGet(head, 0, &x)时,条件p->next != NULL && j < i在第一次迭代时总是满足的,这意味着返回的节点不是第0,而是头。相反,你应该写:

代码语言:javascript
运行
复制
j = -1;  // 0 become -1
while(p->next != NULL && j < i)
{
    p = p->next;
    j++;
}

此外,您必须主要更改循环的界限。实际上,listLength返回列表中元素的数量(在这里,10),但是指数化并不从0开始,这意味着您不能停止在10 (因为没有第10个节点),而是在9个。因此您必须写:

代码语言:javascript
运行
复制
for(i = 0; i < ListLength(head) - 1; i++)
{
    if(ListGet(head, i, &x) == 0)      /* take out the element*/
    {
        printf("Error! \n");
        return 1;
    }
    else printf("%d    ", x);        /* show data elements*/
}

还有一点与这个问题无关,但无论如何你必须改正:你的破坏功能。

代码语言:javascript
运行
复制
void Destroy(SLNode **head)
{
    SLNode *p, *p1;

    p = *head;
    while(p != NULL)
    {
        p1 = p;
        p = p->next;
        
        /* make NULL assignment here */
        p1->next = NULL;
        
        free(p1);
    }
    /* head has been already freed in the above loop
    *  do not try to manipulate it */
    // *head = NULL;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71988393

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档