![]() |
|
#1
|
||||
|
||||
Help analyzing a linked list programhello.
I have been trying to study some examples concerning the management of simple singly linked lists from the book: "mastering algorithms with C". Here are some of the questions that I have come across:
Waitin for comments from you, __________________
Hope to hear from you guys! -------------------------------------------------- Best Regards, Aijaz Baig. Last edited by aijazbaig1 : 17-Jul-2007 at 04:56.
|
|
#2
|
||||
|
||||
Re: help analyzing a linked list programNot sure how much help I can be, but here are some of my thoughts.
The destroy function is actually a function that needs to be written by the user of the list, as is the match function. Therefore when you use this list implementation, you need to pass a function that tells the list how to properly delete data. This solves a problem in that the list can hold any type of data, allocated in a number of ways, ie malloc or new. This allows the user to properly free (or delete) this memory. As for the type-casting, it may just be that the compiler gets confused. I am not sure on that. Your premise seems correct in that it should be a void** by default. Have you implemented this list or are you just trying to dissect it right now? __________________
The best damn Sports Blog period. |
|
#3
|
||||
|
||||
Re: help analyzing a linked list programHi there and thanks for your comments.
In addition i would like to draw attention to ur quote: Quote:
Quote:
Quote:
__________________
Hope to hear from you guys! -------------------------------------------------- Best Regards, Aijaz Baig. |
|
#4
|
|||
|
|||
Re: help analyzing a linked list programQuote:
Quote:
Lots of times (not in this case, I'm thinking) people use casts to suppress a compiler warning or error message about some suspicious usage. Use of seemingly superfluous casts is always a red flag to me (well, maybe a yellow flag). If a program has to use a cast where none should be required, maybe it's because a function prototype is missing or incorrect. Quote:
Yes. Whatever pointer value that you got from malloc() can be passed to free(). Instead of using a special function you can simply declare a pointer to a function with the same prototype as the standard library function and set its value to the address of the standard library function. An illustration in a stand-alone program: CPP / C++ / C Code:
I got the prototypes from <stdlib.h> Regards, Dave Last edited by davekw7x : 17-Jul-2007 at 12:35.
|
|
#5
|
|||
|
|||
Re: help analyzing a linked list programQuote:
Quote:
If you are freeing a single item, then you can make a pointer whose address is that of the standard library function free(), as I showed in my example. For a function to free every element in list, assuming that each of the elements was dynamically allocated by malloc(), then you need a loop that traverses the list and calls free() for each element. The List function pointer "destroy" is set to the address of this function. If you just called the standard library function free() with the value of the "head" pointer, it would only deallocate storage for the first element. CPP / C++ / C Code:
Obviously, to be useful, there has to be at least one other function: to allocate storage and set values for "nest" pointers. Insertion at beginning or end of list will also change list "head" and/or "tail" pointers. Regards, Dave |
|
#6
|
||||
|
||||
Re: help analyzing a linked list programHello Everybody.
thanks for taking a look at my questions and putting the time and the effort to answer them. A special thanks to dave for having such a great sense of pedagogy! Referring to the first reply by dave, can I now save with certainty that "whenever i have a function pointer as one of the arguments in a given function, I can pass a function which confirms to the prototype specified by that function pointer to that pointer and all will be good" ?. Quote:
Referring to your second reply,I suppose that the function deallocatelist does the job of removing all the elements in the list by checking if the head is a NULL. So during the initialization phase of the list, you have just made the function pointers match and destroy to point to listcmp and deallocatelist respectively. But in the process of initialization does making those pointers point to these functions result in these functions being executed? Because if it does then we haven't passed any arguments to those functions. Does that mean that passing no arguments to those functions result in them assuming that they (the arguments) are NULL pointers? I just made a very simple addition to the listcmp function whch is here: CPP / C++ / C Code:
the rest of the program remaining the same. However when i tried to compile the program above using a simple makefile shown below, as expected i encountered some errors which are shown further below: makefile: CPP / C++ / C Code:
Code:
If at all, if anything may depend on my platform or the gcc version here is the output of the gcc -v command: Code:
So this had made me think that one has to pass some arguments to the listcmp function inorder for it to work or else it wont compile or so it seems. I would read further in that book and perhaps im missing something here. Your comments are always welcome. Look to hear from you guys, __________________
Hope to hear from you guys! -------------------------------------------------- Best Regards, Aijaz Baig. |
|
#7
|
||||||
|
||||||
Re: help analyzing a linked list programQuote:
Try it: CPP / C++ / C Code:
Quote:
Use of an initializer list generates code that assigns values of the members of the struct to the values in the initializer list in the declaration statement. The result would have been the same if I had written something like the following, where there is no initializer list. The struct members are assigned values by separate assignment statements. It's a matter of style. Either way is OK by me. CPP / C++ / C Code:
As I mentioned above, when the compiler sees a function name by itself (no parentheses), it is treated as a pointer whose value is the address where the function is loaded. Assigning a value to a pointer does not invoke the function. Quote:
Quote:
Quote:
For the next two: Why can't you dereference a 'void *' pointer? Well, in order to dereference a pointer, the code must know what kind of variable to look at. So the code must use types appropriate to the values that it is comparing. Now, the data pointer in the definition of the struct is a pointer to void. (I guess that was the original author's attempt to make the list stuff as general as possible.) In order to get some data values into the list, you would write a function that would allocate storage and get data values of a particular type into memory. Let's say that for the particular list we are dealing with that the actual data values are ints. Then one way to make the listcmp function work would be: CPP / C++ / C Code:
Note that in C++, the pointer to void is not automatically cast by assignment, so you might write something like CPP / C++ / C Code:
You don't have to make separate variables to do the job and you could write something like: CPP / C++ / C Code:
Anyhow, doing it "right" gets rid of all of the error messages. Quote:
If a given program has only one type of list, then the individual functions to add elements to the list and to compare elements could be specialized for whatever data type the list uses. (Like the listcmp function that I showed above.) Since I don't have your book, I have no way of knowing where the author is going with this (or how you got to this point in the exposition), but I was using the prototypes that you started with. Since the pointer to the struct "match" function has no argument that could be used to indicate data type, that is what I demonstrated. Regards, Dave |
|
#8
|
||||
|
||||
Re: Help analyzing a linked list programhello there.
found some more time to monkey around with the various great suggestions. I tried writing a simple header file which I would then include with the actual C file. This header file contains the declarations of the various functions that i wish to use in the C file and it also contains the function-like macros. First these macros are flagging errors when i try to compile the whole program. Actually there are a lot of other errors but id like to point out the ones generated by the header file as they are seemingly less in number and hopefully easy to fix. So heres my header file called "list.h" : CPP / C++ / C Code:
And then there is the C file called list.c which contains the definition of all these functions. Heres that file(viz. list.c): CPP / C++ / C Code:
Now this C file could be a hot bed of errors and it sure is. However it would be easier for me to first fix the errors in the header file. So comments on that file are welcome and they could even be on some other issues besides the syntactic accuracy of the code, i mean stuff like my coding style or something similar that i would be better off with. So here are the errors the compiler flagged in the list.h file (as suggested by dave, i've turned on the Wall, W and the pedantic flags in the makefile) : Code:
Additionally what does the compiler say when it refers to 28:24..does it mean line 24 to 28? now thats kinda confusing too! Anyways..id love to hear from you guys!! __________________
Hope to hear from you guys! -------------------------------------------------- Best Regards, Aijaz Baig. |
|
#9
|
|||
|
|||
Re: Help analyzing a linked list programQuote:
Macros are not functions. Macros are not C, but they are C-aware. They make text substitutions for C identifiers defined in the macro. Macros are a way of redefining input text to make stuff that's not C look enough like C to be acceptable to a compiler. Your macros only have one parameter. The data type information that you would use in a function definition have no place in the text-substitution macro. Period. My advice: don't use macros unless you really (really) need them. If you decide that you really (really) need a macro that accepts your input as though it were a function, then you could try something like: CPP / C++ / C Code:
Now, anywhere you have something that looks like a function call to list_sze, it will just evaluate the expression as list->size. The problem: By making it a macro instead of a function the compiler is completely incapable of doing any type checking. If you use something as an argument to the macro that is not a pointer to some kind of struct that has an element names "size", you might get some very confusing error message. Or, worse than that, depending on how the macro is actually defined, the compiler might accept it but do something completely, utterly different than what you had in mind and give you no indication that something is wrong, but at run time might cause some bad behavior, or might give a wrong answer without any clue that it went wild. I hate to repeat myself, but I really, really recommend that you not use macros unless you really, really need them In this case, why not just write xxx->size whereever you want to find the value of the size element of the struct to which xxx is pointing? I think it would be silly (but not "wrong") to make a function that does something as fundamental as dereference a pointer to struct to get a member. I think it is bordering on pathological to make a macro to do so. But that's just my opinion. Do whatever the heck you want to in your programs. However, if you ever find yourself in a work environment where other people look at your code (code reviews), and maybe have to maintain it, you should consider getting one of those fireman protective suits and wearing it all of the time, because you will be flamed. Seriously. Quote:
In this case, the errors are quite explicit: The first one is telling you that it doesn't like what it sees at character position 24 on line number 28 of the file list.h. Etc. Regards, Dave |
|
#10
|
||||
|
||||
Re: Help analyzing a linked list programHi there.
thanks for the helpful comments..before proceeding any further...do u know if it would be possible for me to archive some of the threads that I have started for future reference. Because it contains some very valuable information for me and id hate to ask those questions again and again. so if u know of a way to keep them in the safe somehow..please do let me know.. ok..here we go! so i removed all those macros as u suggested and i instead plan to use them directly whenever i need any of that information. So heres the C file again (with minor modifications of course) and the errors i get when i try to compile the C file: CPP / C++ / C Code:
Code:
some very intriguing thing is about the conditions for the if statements. Since most of my if statements only had 1 single value to evaluate i did not put parenthesis around them. However, it flagged it as "syntax errror before so and so" where 'so and so' refers to the first variable in the conditional expression. As an example, i left the second to last 'else-if' statement intact without adding any parenthesis around the conditional expression and it again flags an error there as Code:
also what can be made of errors like the one on line 96? which is the very last else if the program. Hope to hear from you guys! __________________
Hope to hear from you guys! -------------------------------------------------- Best Regards, Aijaz Baig. |
Recent GIDBlog
Python ebook by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Airport Log program using 3D linked List : problem reading from file | batrsau | C Programming Language | 11 | 29-Feb-2008 08:44 |
| [Include] Doubly-linked List | dsmith | C Programming Language | 6 | 14-Apr-2006 14:12 |
| linked list error message | Krandygrl00 | C++ Forum | 4 | 22-Jun-2005 15:13 |
| search linked list | itsmecathys | C++ Forum | 20 | 18-Apr-2005 02:34 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The