0
#include<stdio.h>
 
typedef struct node
 {
    int data;
    struct node * link;
 }n;
 
	void addatbeg(n *);
 	void display(n *);
 	n* first=NULL;
 


int main()
 {
    int ch;
    printf("%u",first);
    while(1)
    {
    	printf("\n enter 0 to terminate else any other key");
    	scanf("%d",&ch);
   		if(ch==0)
    	break;
    	addatbeg(first);
    }
 }
    
	
	void addatbeg(n* first)
    {
    	int ele;
    	n *new;
    	printf("enter the element");
    	scanf("%d",&ele);
    	new=(n*)malloc(sizeof(n));
    	new->data=ele;
    	printf("\nddd%u\n",first);
    	if(first==NULL)
    	{
    		new->link=NULL; 
    	}
    	else
    	new->link=first;
    	printf("\nnew=%u\n",new);
    	first=new;
    	printf("\n first=%u\n",first);
    	printf("%u",first);
    	printf("\n%u",new->link);
    	printf("\n before function is called first=%u",first);
    	display(first);
    }
	
void display(n* temp)
{
	printf("\n inside function,first=%u",first);
	while(1)
	{		
		printf("\ne");
		printf("\n%d->",temp->data);
		if(temp->link==NULL)
			break;
		temp=temp->link;
	}
}

i am trying to implement a linked list with a function to ADD ELEMENTS AT THE BEGINNING.

i have used a variable,first that holds the first node of the linked list.

but when i pass it to the display function,its value becomes NULL without doing anything. so what is the mistake??

WHY IS THE VALUE OF THE VARIABLE first getting modified??

2
Contributors
1
Reply
3
Views
5 Years
Discussion Span
Last Post by Narue
0

WHY IS THE VALUE OF THE VARIABLE first getting modified??

You have too many variables named first , and it's confusing you. In particular, you have a global first and a local first which hides the global one. Compare and contrast with my version of your program:

#include <stdio.h>
#include <stdlib.h> /* This is needed for malloc() */

typedef struct node {
    int data;
    struct node *link;
} node; /* n is too short to be meaningful */

node *addfront(node *list, int value);
void display(node *list);

int main(void)
{
    node *list = NULL;
    int value;

    while (1) {
        printf("Enter an integer: ");
        fflush(stdout);

        /* An invalid integer or end-of-file will terminate the loop */
        if (scanf("%d", &value) != 1)
            break;
        
        /* Note how we're updating list */
        list = addfront(list, value);
        
        /* addfront() shouldn't be displaying the list */
        display(list);
        printf("\n");
    }
}

node *addfront(node *list, int value)
{
    /* This is the correct pattern for malloc() in C */
    node *new = malloc(sizeof *new);
    
    /* Always make sure malloc() succeeded */
    if (new != NULL) {
        new->data = value;
        
        /* This can be unconditional if list is initialized to NULL */
        new->link = list;
        
        list = new;
    }
    
    return list;
}

void display(node *list)
{
    while (list != NULL) {
        printf("%d", list->data);
        
        if(list->link==NULL)
            break;
        
        printf("->");
        list = list->link;
    }
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.