|
||||||
| C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
| Sponsored Links |
|
|
|
|||
|
On the contrary, your English is very good. I never would've guessed. I too am working on something similar. From what you posted, it seems that you want to emulate the folder tree system that Windows uses, e.g. "C:\Windows\MyFolder" and "C:\Windows\File.txt" have the same parent directory, etc. So you want to create a structure that holds information for each level in the tree, and create a linked list from those:
Assume Document is a class that contains document data. This creates a structure that holds two arrays: one of the next level of subfolders if any, and another of the documents in that folder. So, if a folder "F" had two folders called "A" and "B" and a text document called "text.txt" in it, there would be two elements in the subFolders array, and one in the documentsInThisFolder array. Note that if Document is a structure and not a class, then numberOfDocuments is just an array of Documents and not an array of pointer to Documents, and therefore should be declared "Document *documentsInThisFolder;". Code:
struct Folder
{
unsigned int numberOfSubFolders;
Folder *subFolders;
unsigned int numberOfDocuments;
Document **documentsInThisFolder;
};
|
|
|||||
|
First of all, thank you for answering...
![]() You were correct about guessing the point of my exam. Anyway, it doesn't have to be so realistic. I decided to PrintScreen and example of an imagined tree. I just need few functions to define tree. You mentioned a linked list, and it really is a good solution. The think is I'm not supposed to create a text (document) as a structure. Perhaps it can simply be declared as a character array: C Code:
![]() |
|
|||||
|
You can actually use a binary tree to handle this. The data would contain: name, child pointer (if directory), brother pointer, directory flag. A node is a directory if the directory flag is set. The directory points to the first file/directory with the child pointer. The attached Jpg is an example of your directory as a binary tree.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall |
|
|||
|
WingedPanther: If I understand you correctly, one would have to sort through a list of structures to find one document in the array of children, which contains both folders and documents. I think it'd be easier to keep them separate, i.e. faster to search through. Your idea does make things somewhat simpler, though.
|
| Sponsored Links |
|
|
|
|||||
|
I don't understand how is it possible to solve this problem with binary tree if it has to be possible that one folder can consist of many subfolders and documents (not only 2, which is the main idea of binary tree)???
![]() I've been trying to do anything, and my time is running out if I knew anyone here In Zagreb (Croatia) I would already pay and try to understand the code on my own. As far as it goes, i'll get nowhere, and the teacher won't let me go on easy as it seams.. 2 weeks and I still got nowhere, sounds bad as hell. If someone could make this program for me, I would be mostly grateful. Anyway thx for enormous help |
|
|||
|
I think WingedPanther misunderstood something, because now that I think about it, a binary tree isn't the best approach. Forget about that. (Sorry, WP). Can I see what you have written so far? Maybe I can help you better then.
Last edited by dargueta; 01-21-2008 at 02:00 PM. |
|
|||||
|
C Code:
The bad thing is it doesn't contain Main Function (is empty) but the functions are correctly written and can be used. I just needed several minutes to name the variables on the international level (Trust me, you don't wanna read Croatian code ). I'm not sure if this is going to help, but last 2-3 weeks I'm trying by modifying this code. ![]() Last edited by Zael; 01-21-2008 at 02:28 PM. Reason: Renaming some variables.. |
|
|||
|
Okay, so your problem is that you don't know how to add and remove folders, print out contents, etc.?
Issues with your code: (1)NEXT_BROTHER doesn't return anything. What is it supposed to return? (2)Your code is very unclear. Comments would be nice. A few pointers: (1) Constants are supposed to be in all caps, NOT function names. That makes reading your code confusing. (2) Make your type names longer if need be to make them obvious as to their function. I like your typedefs, because that helps; just make your type names fuller. I don't know what prost means (Croatian?), but it'd really help if I did. (3) "typedef struct" is unnecessary. Just do "struct <typename>". (4) Encapsulate your nodes in a class so that the node pointer doesn't have to be passed in. It also makes for a much cleaner look to your code. Last edited by dargueta; 01-21-2008 at 05:02 PM. Reason: Added another tip |
|
|||
|
Okay, here's what I have. There's a bug in here somewhere, but this is an example of what you might want your code to look like: C Code:
|