Lost Password?

Go Back   CodeCall Programming Forum > Software Development > C and C++

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.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-20-2008, 07:05 AM
Zael's Avatar   
Zael Zael is offline
Newbie
 
Join Date: Jan 2008
Posts: 10
Rep Power: 0
Zael is on a distinguished road
Default An important exam!

Greetings, I have an enormous problem and it needs to be solved, one way or another. I understand that much of you don't have time to write code on your own, so it would be selfish to expect served-plate. I beg for instruction or at least a direction. Thx

I have to make program using C:
It's about a tree, a simple hiearchy tree which consists of two things, directories and document.. It's not a binary tree, and in the console user is supposed to be asked about:
- creating a first subdirectory or document (First Child)
- creating the next subdirectory or document (Next brother)
- deleting directory or document
- listing the contents of directory
- function preorder

I started to search for help over the internet 2 weeks ago, but all I found was some C++ example which i don't understand. I also found examples of binary tree but they simply don't fit in our case. Any help would be appreciated. Sorry if I said something wrong, English is not my primary language..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 01-20-2008, 06:42 PM
dargueta dargueta is offline
Guru
 
Join Date: Oct 2007
Age: 18
Posts: 501
Last Blog:
Programs Under the Hoo...
Rep Power: 9
dargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the rough
Default

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;
};
Using this format, you can create one base folder, and as many subfolders and documents as memory will allow. To add/delete folders and elements, I suggest that you make Folder and Document a class and provide functions to do this. Make sure that you free any memory or objects you allocate. Did that help?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 01-21-2008, 04:36 AM
Zael's Avatar   
Zael Zael is offline
Newbie
 
Join Date: Jan 2008
Posts: 10
Rep Power: 0
Zael is on a distinguished road
Default

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:
  1. char Document[12] = "document.txt";

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 01-21-2008, 12:12 PM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 2,035
Last Blog:
NaNoWriMo Day 23 The...
Rep Power: 24
WingedPanther is a jewel in the roughWingedPanther is a jewel in the roughWingedPanther is a jewel in the roughWingedPanther is a jewel in the rough
Default

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.
Attached Thumbnails
To view attachments your post count must be 1 or greater. Your post count is 0 momentarily.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 01-21-2008, 12:29 PM
dargueta dargueta is offline
Guru
 
Join Date: Oct 2007
Age: 18
Posts: 501
Last Blog:
Programs Under the Hoo...
Rep Power: 9
dargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the rough
Default

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 01-21-2008, 01:22 PM
Zael's Avatar   
Zael Zael is offline
Newbie
 
Join Date: Jan 2008
Posts: 10
Rep Power: 0
Zael is on a distinguished road
Default

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 01-21-2008, 01:41 PM
dargueta dargueta is offline
Guru
 
Join Date: Oct 2007
Age: 18
Posts: 501
Last Blog:
Programs Under the Hoo...
Rep Power: 9
dargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the rough
Default

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 01-21-2008, 02:22 PM
Zael's Avatar   
Zael Zael is offline
Newbie
 
Join Date: Jan 2008
Posts: 10
Rep Power: 0
Zael is on a distinguished road
Default

C Code:
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #define MAXNODES 20      //Can be lowered or raised as desired
  4. typedef int labeltype;
  5. typedef int node;
  6.  
  7. typedef struct{
  8.         labeltype label;
  9.         node first_child, next_sibling;
  10. } node_struct;
  11.  
  12. typedef struct{
  13.       node_struct space[MAXNODES];
  14.       node empty;
  15.       } prost;
  16.  
  17. node INITIALIZE(prost *pr){
  18.     int i;
  19.     for (i=0;i<MAXNODES-1;i++){
  20.         pr->space[i].next_sibling=i+1;
  21.         }
  22.     pr->space[MAXNODES-1].next_sibling=-1;
  23.     return 0;
  24.     }
  25.  
  26. node NEW_TREE(labeltype label, prost *pr){
  27.     node temp;
  28.     temp=pr->empty;
  29.     pr->empty=pr->space[pr->empty].next_sibling;
  30.     pr->space[temp].first_child=-1;
  31.     pr->space[temp].next_sibling=-1;
  32.     pr->space[temp].label=label;
  33.     return temp;
  34. }
  35.  
  36. node FIRST_CHILD(labeltype label, node parent, prost *pr){
  37.     node temp;
  38.     temp=pr->empty;
  39.     pr->empty=pr->space[pr->empty].next_sibling;
  40.     pr->space[parent].first_child=temp;
  41.     pr->space[temp].first_child=-1;
  42.     pr->space[temp].next_sibling=-1;
  43.     pr->space[temp].label=label;
  44.     return temp;
  45. }
  46.  
  47. node NEXT_BROTHER(labeltype label, node brother, prost *pr){
  48.     node temp;
  49.     temp=pr->empty;
  50.     pr->empty=pr->space[pr->empty].next_sibling;
  51.     pr->space[brother].next_sibling=temp;
  52.     pr->space[temp].first_child=-1;
  53.     pr->space[temp].next_sibling=-1;
  54.     pr->space[temp].label=label;
  55.     }
  56.  
  57. int main(void){
  58.  
  59. // ????????
  60.  
  61.     system("pause");
  62.     return 0;
  63.     }

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..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 01-21-2008, 04:50 PM
dargueta dargueta is offline
Guru
 
Join Date: Oct 2007
Age: 18
Posts: 501
Last Blog:
Programs Under the Hoo...
Rep Power: 9
dargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the rough
Default

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 01-21-2008, 05:00 PM
dargueta dargueta is offline
Guru
 
Join Date: Oct 2007
Age: 18
Posts: 501
Last Blog:
Programs Under the Hoo...
Rep Power: 9
dargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the rough
Default

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:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <malloc.h>
  4. #include <memory.h>
  5. #include <string.h>
  6.  
  7. class Folder;
  8. //linked list of folders for easy manipulation
  9. struct FolderList
  10. {
  11.     FolderList  *previousFolder;
  12.     Folder    *thisFolder;
  13.     FolderList  *nextFolder;
  14. };
  15. //linked list of documents for easy manipulation
  16. struct DocumentList
  17. {
  18.     DocumentList    *previousDocument;
  19.     char            *thisDocument;
  20.     DocumentList    *nextDocument;
  21. };
  22.  
  23. //actual folder class
  24. class Folder
  25. {
  26. private:
  27.     char            *folderName;
  28.     unsigned int    numberOfDocuments;
  29.     DocumentList    documents;
  30.     unsigned int    numberOfFolders;
  31.     FolderList    folders;
  32.     Folder      *parent;
  33. protected:
  34.     Folder(char *name,Folder *owner)
  35.     {
  36.         //copy the folder name
  37.         folderName = _strdup(name);
  38.         //copy folder owner
  39.         parent = owner;
  40.         //set the defaults--no docs, no subfolders
  41.         numberOfDocuments = 0;
  42.         numberOfFolders = 0;
  43.         //set all pointers to null
  44.         memset(&documents,NULL,sizeof(DocumentList));
  45.         //set folder pointers to null
  46.         folders.previousFolder = NULL;
  47.         folders.nextFolder = NULL;
  48.         folders.thisFolder = this;
  49.     }
  50. public:
  51.     Folder(char *name)
  52.     {
  53.         //copy the folder name
  54.         folderName = _strdup(name);
  55.         parent = NULL;
  56.         //set the defaults--no docs, no subfolders
  57.         numberOfDocuments = 0;
  58.         numberOfFolders = 0;
  59.         //set all pointers to null
  60.         memset(&documents,NULL,sizeof(DocumentList));
  61.         //set folder pointers to null
  62.         folders.previousFolder = NULL;
  63.         folders.nextFolder = NULL;
  64.         folders.thisFolder = this;
  65.     }
  66.     Folder(Folder *fol)
  67.     {
  68.         if(fol == NULL)
  69.             //if the Folder passed in is invalid, then call the default constructor.
  70.             Folder::Folder(NULL,NULL);
  71.  
  72.         FolderList *folderCopies = fol->getFolders();