0

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?

1
  • 1
    Compile with all warnings & debug info (e.g. gcc -Wall -g) then use the debugger (e.g. gdb) Commented Aug 20, 2014 at 22:30

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post.
0

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 ;
}

Comments

0

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));
}

1 Comment

Nested functions are not standard C. They are supported by gcc as extensions to the standard.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.