overflow
overflow

Reputation: 33

Linked list C program produces no output

I was reading on linked lists, and the only good source I could find was one from Stanford CS Library. I was hoping to implement what I learned from it, and run it on my compiler. The program is to find the number of elements in a linked list of {1,2,3}.

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

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

int main()
{
  struct node* BuildOneTwoThree()
  {
    struct node* head   = NULL;
    struct node* second = NULL;
    struct node* third  = NULL;

    head   = malloc(sizeof(struct node)); // allocate 3 nodes in the heap
    second = malloc(sizeof(struct node));
    third  = malloc(sizeof(struct node));

    head->data = 1; // setup first node
    head->next = second; // note: pointer assignment rule

    second->data = 2; // setup second node
    second->next = third;

    third->data = 3; // setup third link
    third->next = NULL;

    return head;

    int Length(struct node* head)
    {
      struct node* current = head;
      int count = 0;

      while (current != NULL)
      {
        count++;
        current = current->next;
      }

      printf("%d",count);
      return count;
    }

  }
  return 0;
}

It is returning blank. I don't understand where O made a mistake, what am I doing wrong?

Upvotes: 0

Views: 197

Answers (4)

sanghas26
sanghas26

Reputation: 151

you're missing a curly brace to end the BuildOneTwoThree() function, then you must call that function in main. Try this:

#include <stdio.h>
#include <stdlib.h>
struct node {

int data;
struct node* next;

};

struct node* BuildOneTwoThree() {
struct node* head = NULL;
            struct node* second = NULL;
            struct node* third = NULL;
            head = malloc(sizeof(struct node)); // allocate 3 nodes in the heap
            second = malloc(sizeof(struct node));
            third = malloc(sizeof(struct node));
            head->data = 1; // setup first node
            head->next = second; // note: pointer assignment rule
            second->data = 2; // setup second node
            second->next = third;
            third->data = 3; // setup third link
            third->next = NULL;
            return head;
        }

int Length(struct node* head) {
            struct node* current = head;
            int count = 0;
            while (current != NULL) {
                count++;
                current = current->next;
                }
            return count;
        }

int main(){
    struct node* newNode = BuildOneTwoThree();
    printf("%d",Length(newNode));
}

Upvotes: 0

AnT stands with Russia
AnT stands with Russia

Reputation: 320381

Firstly, you are attempting to define functions inside main() function. BuildOneTwoThree is defined inside main, while Length appears to be defined inside BuildOneTwoThree. Why did you do it that way? C language has no such feature. You cannot nest function definitions. All functions have to be defined separately and independently at file level.

Secondly, you never call any of the functions you defined. Your main() function does nothing besides return 0;.

Take a look at any valid C program, and you should figure out immediately how functions should be defined. Note that while some compilers support nested function definitions as a non-standard extension, you still have to call your functions at some point.

Upvotes: 3

Nicholas Carey
Nicholas Carey

Reputation: 74187

Try using standard C and formatting your code so it's readable.

If you decompose things, you get to something like this:

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

typedef struct LLNODE
{
  struct LLNODE *next ;

  int payload ;

} LLNODE ;

LLNODE *create_node( int value )
{
  LLNODE *p = calloc( 1 , sizeof(LLNODE) ) ;

  p->next    = NULL  ;
  p->payload = value ;

  return p ;
}

LLNODE *create_list( int start_value , int count )
{
  LLNODE *root = NULL ;
  LLNODE *tail = NULL ;

  for ( int i = 0 , value = start_value ; i < count ; ++i )
  {
    LLNODE *node = create_node( value++ ) ;

    if ( root == NULL )
    {
      root = tail = node ;
    }
    else
    {
      tail->next = node ;
      tail = node ;
    }

  }

  return root ;
}

int compute_length( LLNODE *root )
{
  int len = 0 ;

  for ( LLNODE *p = root ; p != NULL ; p = p->next )
  {
    ++len ;
  }

  return len ;
}

int main( int argc, char *argv[] )
{
  LLNODE *root   = create_list( 101 , 50 ) ;
  int     length = compute_length( root ) ;

  printf( "list length is %d\n" , length ) ;

  int i = 0 ;
  for ( LLNODE *p = root ; p != NULL ; p = p->next )
  {
    printf( "Node #%d.\tcontains the value %d\n" , ++i , p->payload ) ;
  }

  return 0 ;
}

Upvotes: 0

Kurt Mueller
Kurt Mueller

Reputation: 113

I think this sort of post belongs somewhere else (stackoverflow maybe?). In any case, you are defining a function BuildOneTwoThree() and you do not call it, so it does not output anything.

Upvotes: 0

Related Questions