开发者

Making a linked list that stores the size of the list

I need to开发者_开发知识库 make a linked list that stores integer data. Also the list needs to know its size (the number of items in the list).

This is my code.

linked.h file

typedef struct node Node;
struct node{
int a;
Node* b;
};
Node m;

typedef struct {
node* head;
int size;
}list;
list list1;

Main file

#include <stdio.h>
#include "linked.h"
list init(list list1)
{
  list1.head = null;
  list1.size = 0;
  return list1;
}

void main()
{
list1=init(list1);
printf("%d",list1.size);
}

Now on running, the result is -" could not create process", using Turbo C on Windows.

  • Why am I getting this result?
  • Why is the process not being created?
  • Also, is this how I should initialize a linked list?


There are two errors: First, you should write Node* head; rather than node* head;, and second, you should write NULL or 0 instead of null.

Probably your compiler messages are somehow suppressed.

And, yes, this is how you can initiate a linked list, but

list init(void)
{
    list list1;
    list1.head = null;
    list1.size = 0;
    return list1;
}

would be better.

And there could be made other improvements as well.


Your header should not define list list1; though it might declare it extern list list1;. However, to be generally reusable, the header should not declare variables. (See What are extern variables in C.)

Your header should have multiple-inclusion guards in it. (See Should I use #include in headers.)

Your header should declare the functions that are provided to manipulate the lists.

You should have an implementation file that implements the list-manipulating functions (linked.c would be consistent with the header name linked.h). The main program would then simply use those functions.

Your main() program should normally have the return type int; that is required by standard C. Your compiler may have an alternative.

It would be nice if your types were consistently capitalized, instead of using Node and list.

Your init() function is poorly named. It is also poorly designed. You don't need both an input value and a return value. Your code treats the passed in structure as a conveniently allocated local variable and initializes it and copies that back; it would work as well with no argument and the list list1; defined as a local variable. And you should probably avoid using the same name as the global variable in the argument list (or as a local variable). The compiler isn't confused by it; you probably are. It would be more conventional to use:

static void list_init(list *new_list)
{
    new_list->head = 0;
    new_list->size = 0;
}

and:

list_init(&list1);

With all that said, there is still no obvious reason that your program won't run.

One outlandish possibility is that your init() function is confusing the runtime which normally runs its own function init(), and your initialization is not doing what the compiler expects.

Otherwise, are you able to compile and run any programs - a 'Hello World' program? If you can compile and run programs in general, then there is something about your program that is inimical to the system - and the init() function takes centre stage. If you cannot run a simple 'Hello Program', then you need to fix your compiler installation in general.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜