开发者

linked list program

I am expecting the below linked list program to print 1 but its not.can anyone figure out why?

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

struct node
{
  int data;
  struct node * link开发者_开发问答;
};

typedef struct node  NODE;
void display(NODE *);
void add(NODE *,int );

int main()
{
  NODE *head=NULL;
  add(head,1);
  display(head);
  printf("\n");
  getch();
  return 0;
}

void display(NODE *pt)
{
  while(pt!=NULL)
  {
    printf("element is");
    printf("%d",pt->data);
    pt=pt->link;
  }
}

void add(NODE *q,int num)
{
  NODE *temp;
  temp = q;
  if(q==NULL)
  {
    q=(NODE *)malloc(sizeof(struct node));
    temp = q;
  }
  else
  {
    while((temp=temp->link)!=NULL);
    temp->link = (NODE *)malloc(sizeof(struct node));
    temp=temp->link;
  }

  temp->data = num;
  temp->link  = NULL;
}


Your local variable head in main() is not modified by your add() function. This means you are calling display() with a parameter of NULL.

You'll need to pass a NODE **q into add, then update it in add().


Your add method when called first time (when head == NULL) should add the first node to the list thus changing head to point to the newly allocated node.

But this does not happen as add does not communicate back the changed head to the caller.

To fix this you can either return the modified head from the function:

// call as before but assign the return value to head.
head = add(head,1);

.....

// return type changed from void to NODE *
NODE* add(NODE *q,int num)
{
    // make the changes as before

    // return the head pointer.  
    return q;
}

or you can pass the pointer head by address to the function add as:

// pass the address of head.
add(&head,1);

.....

// change type of q from NODE * to NODE **
void add(NODE **q,int num)
{
  // same as before but change q to *q.

  // any changes made to the list here via q will be visible in caller.
}


The add() function is modifying the q argument, but it is passing it by value. Then head remains NULL after the add() call.


The problem is in signature of add method, to make your program work you should pass pointer to pointer of NODE, like this

void add(NODE **,int );

and work with him. Then in case

if(*q==NULL)

you can allocate memory and replace NULL pointer to new HEAD by it

*q=(NODE*)malloc(sizeof(struct node));

So it will work.

The problem is when you allocate memory you just replace local copy of null pointer to NODE but it doesn't affect head in main function.


int main()
{
  NODE *head=NULL;
  add(head,1);
  display(head);

NODE *head is local to main. It's value is NULL. You pass the value NULL to add, which then creates a NODE and sets its data to 1. You then return to main... ...where head is STILL NULL. You need to pass the address of head, so that it's actual value is changed in add(). You also need to change add() to work with a pointer.

Main should return EXIT_SUCCESS or EXIT_FAILURE. Don't typedef struct node; it's harmful to readability and you get no abstraction here from using it.


When you call Add the new head pointer never gets returned. So it still points to NULL.


Ah... you're getting tripped up by the pointers... Essentially, if you want to modify "head", you need to send a reference to THAT... otherwise you are just modifying the pointer... Change your code to this:

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

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

typedef struct node NODE; 
void display(NODE *); 
void add(NODE **,int ); 

int main() 
{ 
    NODE *head=NULL; 
    add(&head,1); 
    display(head); 
    printf("\n"); 
    getch(); 
    return 0; 
} 

void display(NODE *pt) 
{ 
    while(pt!=NULL) 
    { 
        printf("element is "); 
        printf("%d",pt->data); 
        pt=pt->link; 
    } 
} 

void add(NODE **q,int num) 
{ 
    NODE *temp; 
    temp = *q; 
    if(*q==NULL) 
    { 
        *q=(NODE *)malloc(sizeof(struct node)); 
        temp = *q; 
    } 
    else 
    { 
        while((temp=temp->link)!=NULL); 
            temp->link = (NODE *)malloc(sizeof(struct node)); 
            temp=temp->link; 
    } 

    temp->data = num; 
    temp->link = NULL; 
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜