Error while making changes in a linked list using functions outside main():

Viewed 25

I tried to create a linked list with 4 nodes and then added a node using the Insert() fucntion. The overall program has the following framework.

  1. struct Node: Defining the structure of each node of my linked list.
  2. Node_s* Initialize(): This is just to initialize an empty node and return a pointer to the rest of the list; this is made just to satisfy the requirement of the program.
  3. int Insert(): This function inserts a node such that the sorting of the list is maintained!
  4. main(): I am executing the program here in main, by calling the functions Initialize() and Insert()

I think there is some problem in the way the insert() function is interacting with the main function. The changes I am making in the insert() function are not showing up in the main function. Can someone help me figure out why this is happening?

Code:

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


typedef struct Node {
    int data;
    struct Node* next;
} Node_s;


Node_s* Initialize(){

Node_s init_node;

Node_s* headlist;
init_node.data = 0;
headlist = init_node.next ;

return headlist;

}

int Insert(Node_s* listhead, int data){

Node_s* ptr = listhead;

if(data>0){
    while((data > ptr->data)&&(ptr->next!=NULL)){
        ptr = ptr->next;
    }

    Node_s a;
    a.data = data;

    if(ptr->next==NULL){
        a.next = NULL;
    }
    else{
        a.next = ptr->next;
    }

    ptr->next = &a;
    int count =0;

    Node_s* potr = listhead;

    printf("\ninside the function\n ");

    while( potr!= NULL)
    {
        printf("count %d %d \n" , count, potr->data);
        count = count +1;
        potr = potr->next;
    }

    return 0;
}
else{
    printf("enter valid positive integer");
    return 1;
}
}

int main() 

{

// 1st Node
Node_s two;
Node_s* ptr = Initialize(); 
ptr = &two;

// 2nd Node
two.data = 1;
Node_s three;
two.next = &three;

// 3rd Node
three.data = 2;
Node_s four;
three.next = &four;

// 4th Node
four.data = 3;
four.next = NULL ;

Insert(ptr, 5);

printf("\n outside the function\n");

int count =0;
Node_s* potr = ptr;
    while( potr!= NULL)
    {
        printf("count %d %d \n" , count, potr->data);
        count = count +1;
        potr = potr->next;
    }

return 0;

}

Output message

inside the function
 count 0 1 
count 1 2 
count 2 3 
count 3 5 

 outside the function
count 0 1 
count 1 2 
count 2 3 
count 3 82624112 
count 4 84386816 
count 5 0 
count 6 -17958193 
zsh: segmentation fault  
"/Users/as/Desktop/A2/"tempCodeRunnerFile
1 Answers

In the comments under the OP, the question has been raised, "Can I make a linked list" without using malloc().

Here is a few lines of code that does that. Consider the limits of this approach. Perhaps you can slowly play-with and adapt some of this to increase your understanding of linked lists and memory usage.

#include <stdio.h>

typedef struct Node {
    int data;
    struct Node *next;
} Node_s;

int main() {
    Node_s nd5 = { 99, NULL };
    Node_s nd4 = { 50, &nd5 };
    Node_s nd3 = { 40, &nd4 };
    Node_s nd2 = { 30, &nd3 };
    Node_s nd1 = { 20, &nd2 };
    Node_s nd0 = { 10, &nd1 };

    int cnt = 0;
    for( Node_s *p = &nd0; p; p = p->next ) {
        printf( "count %d %d\n", cnt, p->data );
        cnt += 1;
    }

    return 0;
}

Output

count 0 10
count 1 20
count 2 30
count 3 40
count 4 50
count 5 99
Related