![]() |
|
#21
|
|||
|
|||
Re: Link List In CQuote:
CPP / C++ / C Code:
You probably want to malloc(sizeof(struct LList)) and not the size of the pointer. Also, separate your list allocation from your list population. List population should probably NEVER be a "feature" of list allocation at the "bottom" API layer. Populate your list after you've allocated it. Having "said" that...it doesn't really look as if that's what you're trying to do. You seem to be allocating twice, since you failed to allocate for the size of the list structure, rather you allocated for the size of the list structure pointer and, undoubtedly, found that you couldn't access your tail member. CPP / C++ / C Code:
You usually want to make a call to the list management function that ensures that the list is emptied before freeing the list itself. CPP / C++ / C Code:
This is probably better done as part of the allocation so that there doesn't need to be two calls. A simpler choice is calloc, which will zero all of the members in the list structure. CPP / C++ / C Code:
...this code is a complete waste of effort, IMO. CPP / C++ / C Code:
Here you've screwed yourself up again by allocating for the size of the pointer and not the size of the structure. CPP / C++ / C Code:
I'd strongly recommend that you separate user I/O from node initialization functions. In other words, get the "value" from the user FIRST and then assign that value to the node value member. CPP / C++ / C Code:
What happened to your asserts...don't you want to ensure that the list and node are valid pointers? I'm not sure what you're trying to do with this function, but the name of the function is undoubtedly "wrong" for whatever it is you're trying to do and what the name "generally means" to someone else who might be reading it. Ask yourself the following: InitializeCurrent WHAT? The arguments of the function should provide a very powerful hint to the answer of that question. You are passing a list pointer and a node pointer. Are you initializing the current list pointer, the current node pointer or both? In other words, the "API" sorta-kinda sucks in that it is unclear and badly named for whatever it is that you're really trying to accomplish with it. I think that what you're trying to do is insert an initial node into the list. This is why we write a "list_append" function. If the list is empty, then append will correctly add the "initial node" to the list. All subsequent appends provide for the proper head/tail pointer management. CPP / C++ / C Code:
...another total waste of code. CPP / C++ / C Code:
I'm not very excited by the direction this function takes. Why even have the "int number" variable? Why not ensure that numberptr is a valid pointer and then simply scanf (if you're going to use scanf) with the pointer passed in...rather than spend so much additional "effort" to assign it later? Also, as I mentioned, I'd probably do something like: initialize a new node ask user for a number (can use node's value member to hold the response) append the node to the list CPP / C++ / C Code:
...so why do you have code to find the length of the list in two places? Why doesn't the code that needs to know the length of the list call its own "get length" function? ...and, WHY doesn't the get length function RETURN the length of the list? Here is a very good idea to learn right away and to keep in mind for as long as you are going to write C programs. Separate user I/O from "core API." That is, if you want to tell the user how many nodes are in the list, do something like: CPP / C++ / C Code:
This way, if a piece of code that manages the list needs to also know the length of the list, it can use the same body of code. This is how we separate the user I/O with the core API. :davis: |
|||
|
#22
|
|||
|
|||
Re: Link List In CQuote:
I not really understand what u mentioned above. What is list population ? Is it list_append function ? CPP / C++ / C Code:
I will use calloc to zero(NULL) the pointer rather than waster stack memory. Quote:
I really want to follow your advise but i don't know how to do that because i am quite stupid. Any good programming articles ? API is almost same with library where programmer can include this program into many other program to achieve reuseability. A billion thanks for your help. |
|
#23
|
|||
|
|||
Re: Link List In CCPP / C++ / C Code:
|
|
#24
|
|||
|
|||
Re: Link List In CQuote:
... a billion is perhaps overstated. A few hundred million is plenty... :davis: |
|
#25
|
|||||
|
|||||
Re: Link List In CQuote:
list_append would be one way of populating a linked list, right? By population, I mean "adding nodes to the list." list_insert_node_at ...would be another way. list_append_list ...is another. ...etc. Quote:
It isn't so much of a waste of stack space that I'm concerned about, rather, you have a function that is a total waste of your time and effort. Try to remember that whatever code you write, you'll have to spend time debugging and maintaining over time. A function that is an "internal function" that can be done away with very easily is a waste of "energy" from your perspective and from the perspective of CPU processing cycles. Quote:
...if that is what you believe, you're screwed! Quote:
I'm sure that there are many. Quote:
Yes...at least you've essentially said it. An API is a set of interfaces used by programmers to accomplish some meaningful work. A library is a packaging method used to provide (usually) object files that provide functionality desired by other programmers. A library *is* made available to other programmers THROUGH its API. Therefore, for any usable library, it MUST have an API...at least this is true in the context that we're talking about now. There are libraries that have "single APIs" that "evolve" or are "discovered" during runtime, but let's focus on the traditional set of libraries common to most C and C++ programmers. :davis: |
|
#26
|
|||
|
|||
Re: Link List In CWhat is the differences between library and API ?
Do i mixed between list allocate and list population ? I try to calloc it but this doesn't set my pointer to nowhere. My Initialize_list set head and tail point to nowhere, then if this head and tail is null, node added to it and else do another job. Is this a good method or techniques ? Below is my latest program : CPP / C++ / C Code:
CPP / C++ / C Code:
|
|
#27
|
|||
|
|||
Re: Link List In CHow to remove duplicates value or string in link list ?
What i try so far ? CPP / C++ / C Code:
|
|
#28
|
|||
|
|||
Re: Link List In CQuote:
Not particularly... I don't mean to be rude, but your code is all over the place. You do not seem to grasp the fundamentals of the basic notion of pointers, memory allocation and how all of this stuff works together. This is not your fault, but you're trying to do WAY TOO MUCH CODE before you understand the BASICS. Start VERY SIMPLY and work up from there... Here is a GOOD STARTING PLACE for you. You can take the code out of this "single big file" and put it into several smaller files, but for now, I recommend that you just make one file so that when you post here, we don't have to look at several files to see where you've made mistakes. CPP / C++ / C Code:
Output: (nothing) valgrind/memcheck (a portion of the output from running valgrind): Code:
BUILD on this starting point. If you ALLOCATE anything using malloc/calloc, be sure that you DEALLOCATE it with a corresponding call to free! Don't add functionality TOO QUICKLY. Figure out how to implement "node_allocate" and "list_append" BEFORE GOING FURTHER. Write code in main that allocates a single node and appends it to your list. At that point, write a "list_empty" function that removes all of the nodes in the list by traversing the links and freeing the nodes using "node_deallocate." Then, add a "list_print" (or whatever you want to call it) so that you can print out the value for each of the nodes in your list. A simple and good step is to loop through (in main or in a function that you call from main) adding nodes with values of 1 through 10 to the list, print the list and then empty the list and deallocate the list. Once you've done that much, you're ready for the more challenging list management and list manipulation functions. Also, in this case, I don't think that the "Full Reverse" task has anything to do with circularly linked lists. You can eliminate some complexity by making this list singularly linked AND not circular. :davis: |
|
#29
|
|||
|
|||
Re: Link List In CI will follow your advise.
Thanks. |
|
#30
|
|||
|
|||
Re: Link List In CQuote:
No problemo. Note that "advise" means to give advice. You will want to use "I will follow your advice" in the future. English is a strange and difficult language. :davis: |
Recent GIDBlog
Toyota - 2009 May Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| game problem using linkedListType.h and random function | eureka360 | C++ Forum | 5 | 16-Feb-2007 11:48 |
| C++ class -- Please help | vnca_1 | C++ Forum | 3 | 14-Jun-2006 13:31 |
| [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 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The