![]() |
|
#1
|
||||
|
||||
[Include] Doubly-linked ListGID Forums C/C++ Forums Code Submittal Form Name/Brief Description: Doubly Linked ListDate of Original Submission: June 6, 2004Submitted by: dsmithLicense: NoneDetailed Description: This is a simple implementation on a doubly linked list. Both are configured to put data in sorted order. There are two versions included. The first one uses a hash pattern for the find function. This greatly increases the seek times. The second does not implement this feature. For a speed comparison, refer to the Data Structure Test in this forum.Problems/Limitations: As always, comments and questions are appreciated. |
||||
|
#2
|
||||
|
||||
|
Doubly Linked List Explained
Introduction I posted this doubly linked list some time ago when I was doing some comparisons of different data structures. At the time, I just posted this without a whole lot of explanation. I rather like this structure and have decided to use it in a community project that we are doing here at GIDForums™. A big part of this project is to provide good documentation for anything that is added to the project. So I have decided to backtrack and provide some documentation for this structure. If any one sees any flaws in my logic or potential problems, let me know. Design First of all, I will try to explain what a doubly linked list is and why it is used. A linked list is a storage structure that is kind of a like an array in that it is a structured storage of similar items. Unlike an array though, a linked list is a dynamic storage structure as opposed to a static storage structure. This makes growing the list much easier. Here are some graphics that may help to visualize the advantages of a linked list over an array. First here is a graphical representation of an array. ![]() The top image shows a basic array setup. An array consists of a contiguous segment of memory that can be quickly and simply indexed by the user to get the nth element. It can be resized by calls to realloc to grow and shrink as necessary. The bottom image shows the problem with arrays. When you do a sorted insert on an array, you will need to move data around in order to do it. This makes inserting data extremely slow. The same thing happens when data is deleted. Now here is a graphical representation of a doubly linked list. ![]() The top image shows the basic setup of a doubly linked list. A doubly linked list consists of individually allocated nodes that are non-contiguous. These elements are linked together by pointers to the next or previous element. A singly linked list will only go in one direction (next), while a doubly linked list will go forwards or backwards through the data. Indexing is slower, because you have to navigate through each link to get to the destination. The bottom image shows why linked lists are used. To do an insert in a linked list, you simply snip the connectors between where the new data is to go and reconnect them to the new node. There is no need to move any data. So what are the advantages/disadvantages of a linked list. Linked List Advantages
Linked List Disadvantages
That should give a generic overview of a doubly linked list. Now for the specifics of the dlist implementation.
Function Documentation This is a documentation list of all of the public functions available to the end user (application progammer) in this implementation of a doubly linked list. All data is private and is not accessible to the user. First of all, here is a quick, complete list of all the public functions. Each function is described in detail below. CPP / C++ / C Code:
Detailed Function List
Necessary user Functions The intent of dlist is to be as quick and easy as possible. There are no external libraries, it has a limited set of commands and still has some unique features. However, the most difficult part is setting up the sort functions. In order to use dlist, it is absolutely necessary for the user program to define and implement these functions. They can be named anything, but they must have the parameters as shown and must have the return values as shown. Included in the dlist file is some commented out examples of these routines using a int type data. These are explained below.
Now for a sample. This is a bit longer, but I wanted something that at least had a pretense of being useful. In this sample, you can enter a list of strings into the list. You need to pass a file upon execution, in which these strings will be stored and retrieved. I set the hash value low and put in some language when it was called. CPP / C++ / C Code:
This program includes about 10 lines that are calls to the dlist functions, about 20 lines that are the implementation of the range and compare functions. The rest is code for input, file processing, etc. Other, more complete, examples can be found at the data comparison contest. In addition, this is the data storage structure that I am going to use initially for the contact list in GIM. Conclusion There you have it. Hopefully that clarifies doubly linked lists for anyone. I wrote this from a perspective of not only explaining what a doubly linked list is, but also what makes this implementation different than others. As always comments are welcome. Also, there is a slight change that I have done to the list. It is rather important and I would recommend that you download this version rather than the one above. __________________
The best damn Sports Blog period. |
|
#3
|
||||
|
||||
|
Another small but fairly important update. In working with the dlist in another project (gim), I found a small problem with the locate routine. It was looking for -1 and 1 exactly for return values from the compare routine. For a custom written compare this can be implemented, but if you want to use the exact return values from strcmp this is a problem. strcmp returns a positive number or a negative number which is rarely 1 or -1 because it returns the ASCII difference between the first non-matching charecters.
I have attached an updated version of this program to fix this little problem. __________________
The best damn Sports Blog period. |
|
#4
|
|||
|
|||
Re: [Include] Doubly-linked ListHI smith..
I have just gone through the coding of doubly linked list.. i have develop some coding and don't know to make them work... can u please help me. thanks |
|
#5
|
|||
|
|||
Re: [Include] Doubly-linked Listhey moorish
I can't understand what do you mean by "make them work" ? Does it mean that you was unable to understand the main objective of double linked list or the code is not working ? Please explain yourself and your problem as well. |
|
#6
|
||||
|
||||
Re: [Include] Doubly-linked ListSorry - didn't see this until real posted.
Like he said if you can go into a bit more detail, we can try to help you out a bit. Thanks. __________________
The best damn Sports Blog period. |
|
#7
|
|||
|
|||
Re: [Include] Doubly-linked ListQuote:
Help me understand why, if using C++, someone would want to use dlist versus std::list, std::vector or std::deque? Isn't an appropriate implementation of a doubly linked list is going to have some notion of how to initialize (allocate) nodes and how to deinitialize (deallocate) them. This one seems to push that responsibility out to the user. Wouldn't you rather use a better idiom? I don't mean to be rude, but what user category do you envision for this code? Do you consider dlist to be extensible? Why is "size" a signed value and not unsigned? Why is there a is_empty() operation that evaluates the status of the head pointer? Wouldn't checking "size" be more appropriate? Why even use the function call inside your implementation, couldn't you just as easily use: if( !size ) { ... } ...and save the overhead of the function call? Doesn't is_empty just tell us whether head is a null pointer or not? Why not use if( !head )? You are using function pointers in your search functions and sorting function, why not use them in your allocation/deallocation? Is there some reason that you're returning int for is_xxxx? and not bool? Do you think that "rem_data" should be a public member? Is the user expected to ever use it, or is it intended that the destructor will? Do you think that "goto_index" should be a public member? How will a user "know" where "where" is/should be? If we already have a valid "where," why not just go directly to it instead of calling next() or prev() who knows how many times? Don't you think that there should probably be a test to see if "where" is beyond "tail" or before "head" before next-ing or prev-ing? Considering that goto_index is public, couldn't a user pass in a "where" value that doesn't exist in the dlist such that it where will never equal the work product of next/prev? Do you feel that it is probably better to use a different name than "next" to advance? ...or a differently named data.next member? data.info is the data, but data is the node in the list. Is there some reason why you didn't use node as the node and node.data as the data? Considering that the license is GPL, doesn't that make any code that uses it GPL by requirement? If GPL'ing the source, why not GPL it per FSF's recommendations? Code:
I guess that the compiler doesn't like your comment block style very well... :davis: |
Recent GIDBlog
Configuring iptables for Webmin Servers Index Module by gidnetwork
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Merge sort on a linked list | Temujin_12 | C++ Forum | 1 | 06-Mar-2008 20:33 |
| [CONTEST?]Data Structure Test | dsmith | C Programming Language | 2 | 06-Jun-2004 15:13 |
| testing word file "need help" | alexandro | C Programming Language | 2 | 03-Jun-2004 14:38 |
| help on linked lists any1????? | nick4 | C Programming Language | 1 | 17-May-2004 09:32 |
| [include] list1.h -- Linked list class | dsmith | C Programming Language | 2 | 04-May-2004 09:42 |
Network Sites: GIDNetwork · GIDApp · GIDSearch · Learning Journal by J de Silva, The