Question

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

struct node {
  int num;
  struct node *next;
}*head=NULL, *curr=NULL;

void print(){
  curr = head;
  while(curr != NULL){
    printf("%d\n", curr->num);
    curr = curr->next;
  }
}

struct node* memAlo(){
  return (struct node *)malloc(sizeof(struct node));
}

void addNode(int no){
  curr = head;
  while(curr != NULL){
    curr = curr->next;
  }
  curr = memAlo();
  if(curr == NULL){
    printf("\nmemory up\n");
    return;
  }
  else{
    curr->num = no;
    curr->next = NULL;
    printf("%d\n",curr->num);
  }
}

void hellop(){
  printf("%d", head->num);
}

int main(){
  int i;
  curr = head;
  for(i=1;i<10;i++){
    addNode(i);
  }
  print();
  /*head = memAlo();
  head->num = 1;
  head->next = NULL;
  hellop();*/
}

I am sure I have messed up somewhere. The thing is that the head pointer doesn't get the memory allocated by the memAlo() fn() but how to get there? Please help

What I am trying is to create a singly linked list holding numbers from 1 to 9 and to print them using print(). Actually AddNode() is to create single node at the end of the linked list each time the for loop in main() executes.

Was it helpful?

Solution

You set head = NULL at the point where you first defined head. Except in that one place, we never see head on the left-hand side of = anywhere in your program. So of course head is always equal to NULL and never anything else.

You will probably want to insert some code at the start of your addNode function to test whether head == NULL at that point; and if that is true, you will want to assign the result of memAlo() to head instead of curr. You will have to adjust some of the other logic as well.

OTHER TIPS

Your code for allocating a node is wrong. It should create a node, make some space for it, then return it.

struct node *memAlo() {
    struct node *nd = malloc(sizeof(*nd));
    return nd;
}

This creates a pointer to a node, properly allocates it, then returns it.

Problems I see:

  1. Not dealing with empty list, i.e. when head == NULL.
  2. Creating nodes that are not linked to each other.

    curr = memAlo();

    allocated memory for a node and returns it to you, but it does not connect the node with anything else.

Try this:

void addNode(int no){
    struct node* temp = NULL;

    // Deal with an empty list.
    if ( head == NULL )
    {
       head = memAlo();
       head->num = no;
       head->next = NULL;
    }

  // Move curr until we reach the last node of the list.
  curr = head;
  while(curr->next != NULL){
    curr = curr->next;
  }
  temp = memAlo();
  if(temp == NULL){
    printf("\nmemory up\n");
    return;
  }
  else{
    // Link the new node to the previous last node.
    temp->num = no;
    temp->next = NULL;
    printf("%d\n",temp->num);
    curr->next = temp;
  }
}

It seems that since head is initially NULL, and then you start allocating nodes without saving the address of the first one, you lose the address of the first one, and then can't walk the list from the beginning.

The part you commented out illustrate the problem.

As a side note, there is no free in your program. Remember to always free the memory you alloc

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

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

struct node *head, *curr;
struct node *pos;

void addNode(int n)
{
  if(head==NULL)
  {
    head = (struct node*)malloc(sizeof(struct node));
    head->num = n;
    head->next = NULL;
    curr = head;
  }
  else
  {
    while(curr != NULL)
    {
      pos = curr;
      curr = curr->next;
    }
    curr = (struct node*)malloc(sizeof(struct node));
    curr->num = n;
    curr->next = NULL;
    pos->next = curr;
  }
}

void printList()
{
  curr = head;
  while(curr != NULL)
  {
    printf("%d",curr->num);
    curr = curr->next;
  }
}

int main()
{
  head = NULL;
  curr = head;
  int i, a[] = {4,5,1,2,3,9,0};
  for(i=0;i<7;i++)
  {
    addNode(a[i]);
  }
  curr = head;
  printList();
}

This seems to have solved my problem. I figured it out though. Thanks for all your help.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top