Home > Net >  codeblocks output differs from online compilers
codeblocks output differs from online compilers

Time:04-25

In short, i should have " 0 1 2 3 4 5 6 7 8 9 10 ". When executed in CodeBlocks instead of 0 i am getting random numbers. But When compiling using online compilators I am getting expected output. Which output is reliable? Online or CodeblockIDE's? If needed there is the code that is linked-list practise: creating, inputting data elements, deleting at specified point and finally outputing the result.

#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);
}

CodePudding user response:

Problem is with the logic of your code. The good output is the Code::Blocks one.

The problem is within the insertNode function. Its behaviour is the following : it finds the i-1th node and create a new node after it. If i = 0, the 0th element is found in the first iteration of your first loop and there will be no error after that, because j = i - 1 = -1. Never in your code you edit the data of the head. Furthermore, each time you call your insertNode function, you ask for the value of the new node to be its position 1.

So you will have this :

----------       -----       -----
| random |  ---> | 1 |  ---> | 2 |  ---> etc.
----------       -----       -----
  head            0th         1st

After that, in the listGet function, you make a misconception about the way your list is made. As an exemple, when will call listGet(head, 0, &x), the condition p->next != NULL && j < i will always be satisfied at the first iteration, meaning that the returned node is not the 0th, but the head. You should instead write :

j = -1;  // 0 become -1
while(p->next != NULL && j < i)
{
    p = p->next;
    j  ;
}

Moreover, you must change the bound of your loop in the main. Indeed, the listLength returns the number of elements in your list (here, 10), but the indexation does not begin from 1 bt from 0, meaning that you must not stop at 10 (because there is no 10th node) but at 9. Therefore you must write :

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*/
}

There is another point that is not related to this problem but that you must correct anyway : your destroy function.

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;
}
  • Related