How to create a linked list in pure C [closed] - c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I want to know how to create a single linked list in C where each node has:
A string of any length.
A list of integers (the total number of integers to store will vary between nodes, so I cant use an array of integers).
Thanks in advance.

I'm not going to write the code for you, but here are some steps to get you started. This is one possible way of doing it; there are others that might work better given your actual needs.
First, determine your data structures. You need two. The first is the linked list of integers. The second is the linked list that contains both the string and the list. Here is what the nodes in the latter might look like:
typedef struct node {
char* someStr;
LIST* integerList;
struct node* next;
} NODE;
Then, have one function that creates the linked list of integers. Have another function that allocates memory for the string and returns a char*.
Have a third function that takes as parameters the linked list of NODEs, a string, and a linked list of ints. This function appends a new NODE to the list, setting the pointers to the linked list of int and the string.
At least get started with this. If you get stuck on any of the functions, you can ask specific questions about them. But you cannot just post the problem and expect someone to write the code for you, without saying what you have tried and where you are stuck.

From what I understood from:
A list of integers (the total number of integers to store will vary between nodes, so I cant use an array of integers).
It seems that you want a dynamic array for numbers, you can use This CCAN Module (It's just a header, just remove the config.h inclusion and you should be good to go)
NB: a List of numbers seems like a bad idea, so dynamic array is probably the most effecient way of doing this.
NB2: It's not hard to code your own dynamic array, just have a look at it's algorithm
Simplest usage is something like:
#include "darray.h"
struct node {
char *string;
darray(int) numbers;
};
inline void push_number(struct node *node, int number) {
darray_push(node->numbers, number);
}

Related

Iterate trought struct in C [duplicate]

This question already has answers here:
Is there any way to loop through a struct with elements of different types in C?
(5 answers)
Closed 6 years ago.
I am writing program in C. I would like to print out values of each element in struct, so that I can print out according value for each bitmap header member. Hence, is it possible to iterate trough each element of struct?
Also is it possible to get number of elements in struct if each element is different size?
Regards
No, you cannot do this in C. There is no iterator over data types. You have to print each field in your struct separately in your function.
Addition:
One way to do this on your own would be to use X-Macros as suggested by coderredoc. But it might get a bit nast for different data types to print.
You can do this in C. Use some thing called X-macro . Not exactly iterator over data types but a smart way out.
link

explain notifier.c from the Linux kernel

I'm seeking to fully understand the following code snippet from kernel/notifier.c. I have read and built simple link lists and think I get the construct from K&R's C programming. However this is slightly more complex. The second line below which begins with the 'int' appears to be two items together which is unclear.
The first is the (*notifier_call) which I believe has independent but related significance with the second containing a 'notifier block' term.
Can you explain how it works in detail? I understand that there is a function pointer and multiple subscribers possible. But I lack the way to tie these facts together, and could use a primer or key so I exactly understand how the code works. The third line looks to contain the linking structure, or recursive nature. Forgive my terms, and correct them as fit as I am a new student of computer science terminology.
struct notifier_block {
int (*notifier_call)(struct notifier_block *, unsigned long, void *);
struct notifier_block *next;
int priority;
};
This is a simple pointer to a function returning int, taking as its arguments a pointer to a notifier_block structure, an unsigned long, and a pointer to void.
It has nothing to do with the linked list, that is linked by the next member.

duplicate char* in C [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Hello I am trying to create a hash function in C to remove duplicates from a set of strings a.k.a char*. The idea is to make what the hash function returns, to be the index of an array, so that if two strings are same they point to the same position at the array. But I am stuck at how could I achieve that.
On my first approach I tried to create an array of integers with size equal to the number of strings given and then make the int, that the hash function returns, modulo the size of the array but it didn't worked since for small number of strings many collisions were created.
To sum up, all I want is to implement a structure, probably a hashMap that getting an string will point me to a number, which is the index of the array, in which I store the number of occurences.
Is there any better idea?
You probably want string interning. Some libraries call them "quarks" (e.g. Glib) or "atoms" (like in X11: XInternAtom). See also symbols (e.g. in Lisp or Scheme).
You could store all your interned strings in a global hash table, and have some function
const char* internized_string(const char*str);
which given some string str returns a canonical interned string which compare equals (with strcmp) to it. But two calls of interned_string on equal (but not identical) strings would return the same address. It would work by first searching a similar string in the hash table; if none is found, add a new copy (perhaps using strdup) into the hash table, and return it.
You could use an hash table, a balanced tree (i.e. red black tree or an AVL tree etc....) a trie, or a hash array mapped trie, or any efficient container to implement your global collection of interned strings.
BTW, if you coded in C++11, you'll have many standard containers provided by the standard C++ library.
If you want to code all by yourself, you could have an hash table implemented as array of buckets; usually you want the size of the array to be prime. Each bucket could be a linked list, or a contiguous array, of string pointers. You re-organize your entire hash table when it gets full (e.g. when the mean -or perhaps the max- size of buckets reaches some threshold like 3 or 8) by growing the bucket array to a bigger prime size -e.g. a prime greater than 3/2 of the old one- (and refill the new table from the old one). Avoid collisions (but accept to have a few of them).
There are many hash table free software libraries in C; e.g. uthash, klib, tommyds, ulib, libstrhash, glib etc etc... GIYF. Study (at least for inspiration) the source code of some of them, and perhaps use one.

Structs, pointers, and trees in C

For our final semester project, everyone in my Operating Systems class has been tasked with implementing a pseudo "linux filesystem". The idea is to simulate handling files, folders, changing directories, and so forth.
I dislike having to work with Strings and pointers when I program in C, and unfortunately for my peace-of-mind, this project looks to involve both. Because I am relatively uncomfortable with pointers, I was hoping I could get a sanity check that my backend implementation of the underlying tree structure is sound.
typedef struct floorNode
{
char floorName[30]; //the name of the tree node
struct floorNode *parentPointer; //this is a pointer to the parent node. Null for the root node.
struct floorNode *childPointers[10]; //this is an array holding pointers to up to 10 child nodes.
char fileArray[10][30]; //this is an array of 10 'files', each of up to length 30.
//for this assignment, strings are the only type of "file"
} floorNode;
Is this the proper way to implement a tree in C?
That is more or less the proper data type.
I'm concerned about fileArray[][]. I don't think it is needed, unless I'm misunderstanding its purpose. To get floorName of the children, instead traverse childPointers[] to obtain the name in the children.
Something to consider if the nodes have 30 character strings is to make the storage for all of them a little bigger, 31 in this case, so that a trailing NUL is always present and no special yucky handling is needed to distinguish between a 30-character string without a NUL and all the shorter strings which have one.
You probably want a linked list of children. You definitely don't want arrays of pointers for this. You should also think about how to know if a file is actually a directory.

'Multipurpose' linked list implementation in pure C

This is not exactly a technical question, since I know C kind of enough to do the things I need to (I mean, in terms of not 'letting the language get in your way'), so this question is basically a 'what direction to take' question.
Situation is: I am currently taking an advanced algorithms course, and for the sake of 'growing up as programmers', I am required to use pure C to implement the practical assignments (it works well: pretty much any small mistake you make actually forces you to understand completely what you're doing in order to fix it). In the course of implementing, I obviously run into the problem of having to implement the 'basic' data structures from the ground up: actually not only linked lists, but also stacks, trees, et cetera.
I am focusing on lists in this topic because it's typically a structure I end up using a lot in the program, either as a 'main' structure or as a 'helper' structure for other bigger ones (for example, a hash tree that resolves conflicts by using a linked list).
This requires that the list stores elements of lots of different types. I am assuming here as a premise that I don't want to re-code the list for every type. So, I can come up with these alternatives:
Making a list of void pointers (kinda inelegant; harder to debug)
Making only one list, but having a union as 'element type', containing all element types I will use in the program (easier to debug; wastes space if elements are not all the same size)
Using a preprocessor macro to regenerate the code for every type, in the style of SGLIB, 'imitating' C++'s STL (creative solution; doesn't waste space; elements have the explicit type they actually are when they are returned; any change in list code can be really dramatic)
Your idea/solution
To make the question clear: which one of the above is best?
PS: Since I am basically in an academic context, I am also very interested in the view of people working with pure C out there in the industry. I understand that most pure C programmers are in the embedded devices area, where I don't think this kind of problem I am facing is common. However, if anyone out there knows how it's done 'in the real world', I would be very interested in your opinion.
A void * is a bit of a pain in a linked list since you have to manage it's allocation separately to the list itself. One approach I've used in the past is to have a 'variable sized' structure like:
typedef struct _tNode {
struct _tNode *prev;
struct _tNode *next;
int payloadType;
char payload[1]; // or use different type for alignment.
} tNode;
Now I realize that doesn't look variable sized but let's allocate a structure thus:
typedef struct {
char Name[30];
char Addr[50];
} tPerson;
tNode *node = malloc (sizeof (tNode) - 1 + sizeof (tPerson));
Now you have a node that, for all intents and purposes, looks like this:
typedef struct _tNode {
struct _tNode *prev;
struct _tNode *next;
int payloadType;
char Name[30];
char Addr[50];
} tNode;
or, in graphical form (where [n] means n bytes):
+----------------+
| prev[4] |
+----------------+
| next[4] |
+----------------+
| payloadType[4] |
+----------------+ +----------+
| payload[1] | <- overlap -> | Name[30] |
+----------------+ +----------+
| Addr[50] |
+----------+
That is, assuming you know how to address the payload correctly. This can be done as follows:
node->prev = NULL;
node->next = NULL;
node->payloadType = PLTYP_PERSON;
tPerson *person = &(node->payload); // cast for easy changes to payload.
strcpy (person->Name, "Bob Smith");
strcpy (person->Addr, "7 Station St");
That cast line simply casts the address of the payload character (in the tNode type) to be an address of the actual tPerson payload type.
Using this method, you can carry any payload type you want in a node, even different payload types in each node, without the wasted space of a union. This wastage can be seen with the following:
union {
int x;
char y[100];
} u;
where 96 bytes are wasted every time you store an integer type in the list (for a 4-byte integer).
The payload type in the tNode allows you to easily detect what type of payload this node is carrying, so your code can decide how to process it. You can use something along the lines of:
#define PAYLOAD_UNKNOWN 0
#define PAYLOAD_MANAGER 1
#define PAYLOAD_EMPLOYEE 2
#define PAYLOAD_CONTRACTOR 3
or (probably better):
typedef enum {
PAYLOAD_UNKNOWN,
PAYLOAD_MANAGER,
PAYLOAD_EMPLOYEE,
PAYLOAD_CONTRACTOR
} tPayLoad;
My $.002:
Making a list of void pointers (kinda diselegant; harder to debug)
This isn't such a bad choice, IMHO, if you must write in C. You might add API methods to allow the application to supply a print() method for ease of debugging. Similar methods could be invoked when (e.g.) items get added to or removed from the list. (For linked lists, this is usually not necessary, but for more complex data structures -- hash tables, for example) -- it can sometimes be a lifesaver.)
Making only one list, but having a union as 'element type', containing all element types I will use in the program (easier to debug; wastes space if elements are not all the same size)
I would avoid this like the plague. (Well, you did ask.) Having a manually-configured, compile-time dependency from the data structure to its contained types is the worst of all worlds. Again, IMHO.
Using a preprocessor macro to regenerate the code for every type, in the style of SGLIB (sglib.sourceforge.net), 'imitating' C++'s STL (creative solution; doesn't waste space; elements have the explicit type they actually are when they are returned; any change in list code can be really dramatic)
Intriguing idea, but since I don't know SGLIB, I can't say much more than that.
Your idea/solution
I'd go with the first choice.
I've done this in the past, in our code (which has since been converted to C++), and at the time, decided on the void* approach. I just did this for flexibility - we were almost always storing a pointer in the list anyways, and the simplicity of the solution, and usability of it outweighed (for me) the downsides to the other approaches.
That being said, there was one time where it caused some nasty bug that was difficult to debug, so it's definitely not a perfect solution. I think it's still the one I'd take, though, if I was doing this again now.
Using a preprocessor macro is the best option. The Linux kernel linked list is a excellent a eficient implementation of a circularly-linked list in C. Is very portable and easy to use. Here a standalone version of linux kernel 2.6.29 list.h header.
The FreeBSD/OpenBSD sys/queue is another good option for a generic macro based linked list
I haven't coded C in years but GLib claims to provide "a large set of utility functions for strings and common data structures", among which are linked lists.
Although It's tempting to think about solving this kind of problem using the techniques of another language, say, generics, in practice it's rarely a win. There are probably some canned solutions that get it right most of the time (and tell you in their documentation when they get it wrong), using that might miss the point of the assignment, So i'd think twice about it. For a very few number of cases, It might be feasable to roll your own, but for a project of any reasonable size, Its not likely to be worth the debugging effort.
Rather, When programming in language x, you should use the idioms of language x. Don't write java when you're using python. Don't write C when you're using scheme. Don't write C++ when you're using C99.
Myself, I'd probably end up using something like Pax's suggestion, but actually use a union of char[1] and void* and int, to make the common cases convenient (and an enumed type flag)
(I'd also probably end up implementing a fibonacci tree, just cause that sounds neat, and you can only implement RB Trees so many times before it loses it's flavor, even if that is better for the common cases it'd be used for.)
edit: based on your comment, it looks like you've got a pretty good case for using a canned solution. If your instructor allows it, and the syntax it offers feels comfortable, give it a whirl.
This is a good problem. There are two solutions I like:
Dave Hanson's C Interfaces and Implementations uses a list of void * pointers, which is good enough for me.
For my students, I wrote an awk script to generate type-specific list functions. Compared to preprocessor macros, it requires an extra build step, but the operation of the system is much more transparent to programmers without a lot of experience. And it really helps make the case for parametric polymorphism, which they see later in their curriculum.
Here's what one set of functions looks like:
int lengthEL (Explist *l);
Exp* nthEL (Explist *l, unsigned n);
Explist *mkEL (Exp *hd, Explist *tl);
The awk script is a 150-line horror; it searches C code for typedefs and generates a set of list functions for each one. It's very old; I could probably do better now :-)
I wouldn't give a list of unions the time of day (or space on my hard drive). It's not safe, and it's not extensible, so you may as well just use void * and be done with it.
One improvement over making it a list of void* would be making it a list of structs that contain a void* and some meta-data about what the void* points to, including its type, size, etc.
Other ideas: embed a Perl or Lisp interpreter.
Or go halfway: link with the Perl library and make it a list of Perl SVs or something.
I'd probably go with the void* approach myself, but it occurred to me that you could store your data as XML. Then the list can just have a char* for data (which you would parse on demand for whatever sub elements you need)....

Resources