Home > Blockchain >  Garbage value for certain C functions
Garbage value for certain C functions

Time:02-21

I'm learning C and I'm trying to make a program with multiple linked list operations all at once. They don't give me errors, but for the functions to delete a node at the end, and to insert at a certain position, I'm getting a garbage value at the beginning of the output.

Temporary program to implement just the insert function:

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

struct node
{
    int data;
    struct node* next;
};

void inp(struct node **start, int ele)                       
{
    struct node *NEW = (struct node*)malloc(sizeof(struct node));  
    NEW->data = ele;                                               

    struct node *ptr = *start;                                      
    while(ptr != NULL)
    {
        if(ptr->next == NULL)                                      
        {
            ptr->next = NEW;                                        
            NEW->next = NULL;                                       
            break;                                                
        }
        ptr = ptr->next;

    }
}

void insert_pos(struct node **start, int ele, int pos)
{
    struct node *ptr = *start;                                      
    for(int i = 1; i < pos; i  )
    {
        ptr = ptr->next;                                                                                                  
    }

    struct node *NEW = (struct node*)malloc(sizeof(struct node));
    NEW->data = ele;
    NEW->next = ptr->next;
    ptr->next = NEW;
}

void display(struct node* start)
{
    struct node* ptr = start;
    while(ptr != NULL)
    {
        printf("%d\n", ptr->data);
        ptr = ptr->next;
    }
}

int main()
{
    struct node *a;
    int m;
    for(int i = 0; i < 10; i  )
    {
        scanf("%d", &m);
        inp(&a, m);
    }

    insert_pos(&a, 594, 3);
    printf("\n\n");
    display(a);

}

Temporary program to delete at the end:

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

struct node
{
    int data;
    struct node* next;
};

void inp(struct node **start, int ele)                       
{
    struct node *NEW = (struct node*)malloc(sizeof(struct node));  
    NEW->data = ele;                                               

    struct node *ptr = *start;                                      
    while(ptr != NULL)
    {
        if(ptr->next == NULL)                                      
        {
            ptr->next = NEW;                                        
            NEW->next = NULL;                                       
            break;                                                
        }
        ptr = ptr->next;

    }
}

void del_end(struct node **start)                                   
{
    struct node *ptr = *start;
    while(ptr != NULL)
    {
        if(ptr->next->next == NULL)
        {
            ptr->next = NULL;
            free(ptr->next->next);
            break;
        }
        ptr = ptr->next;
    }
}

void display(struct node* start)
{
    struct node* ptr = start;
    while(ptr != NULL)
    {
        printf("%d\n", ptr->data);
        ptr = ptr->next;
    }
}

int main()
{
    struct node *a;
    int m;
    for(int i = 0; i < 10; i  )
    {
        scanf("%d", &m);
        inp(&a, m);
    }

    del_end(&a);
    printf("\n\n");
    display(a);

}

Any idea how to fix it?

CodePudding user response:

you can see that in here:

The inp problem:

struct node *a;

puts a garbage value in a that you don't handle and uses it like a node, add =NULL to solve.

also, your function that deletes from the end has the following error:

struct node *ptr = *start;
    while(ptr != NULL)
    {
        if(ptr->next->next == NULL)
        {
            ptr->nexStack Overflow for Teams – Collaborate and share knowledge with a private group.t = NULL;
            free(ptr->next->next);
            break;
        }
        ptr = ptr->next;
    }

you are changing ptr-> next and than accessing ptr->next->next...

  • Related