Re: General tree assignment in C
- From: gw7rib@xxxxxxx
- Date: Tue, 29 Jan 2008 14:51:24 -0800 (PST)
On 28 Jan, 13:12, "Ivica" <prljavi_blu...@xxxxxxxxxxx> wrote:
Create program for defining hierarchical file-system structure in which node
can be file or subdirectory with limitation that file can only be a leaf.
Capabilities that program must have are:
- creating first subdirectoy or file (first child)
- creating next subdirectory or file (nex "brother")
- deleting leafs, subdirectories and files
- movement through filesystem structure to parent, first child and next
"brother"
- printing directory contents
- constant location printing
- searching filesystem structure (function PREORDER())
I think the first thing you need to do is to get out a piece of paper
and a pen. Draw a box at the top representing the root directory. Draw
a few boxes in a row underneath, representing the things in the root
directory - some of them other directories, some of them files -
represent them how you want. Draw a few more directories or files in
one or more of these directories. And add a few arrows so you know how
all the bits tie together.
I created a system a bit like what you're trying to do. I did it in C+
+, with a class Item which could be either a directory or an end leaf,
and then derived two other classes from it. If you're doing it in C,
it's probably best if you just use one kind of struct, which can
represent either a directory or a file. I'll call this an item.
You need to be able to represent the items stored in a particular
directory. I'm a big fan of double linked lists, and they seem
suitable for the job here. You store pointers to the first and last
elements of the list, and each element stores pointers to the previous
one and the next one (its siblings).
So what does our struct, representing an item, have to contain? Well,
suppose we have:
a flag to say whether it's a directory or a file;
a pointer "up" to the parent directory (this will be NULL for the root
directory);
two pointers "across" to the next and previous items in the same
directory as this one (again, these will be NULL for the root
directory, as well as for anything which is in a directory on its
own);
two pointers "down", which if the item is a directory point to the
first and last items in it (these will be NULL for a file, as well as
for an empty directory).
You will probabbly also want some space for a title, so that your
files can have different names. Plus something for the "content",
perhaps - in a very simple system you might have a char array, for
anything useful you could have a pointer to something so that
different length "files" don't all take up the same amount of space.
And that, I think, is all you need. Now you just have to write
functions to make the pointers point where they need to. This is a bit
of a chore (I've done the interesting bit for you, sorry!) but should
be well within your capabilities.
Hope that helps.
Paul.
.
- References:
- General tree assignment in C
- From: Ivica
- General tree assignment in C
- Prev by Date: Re: Decision Tables
- Next by Date: Re: Decision Tables
- Previous by thread: Re: General tree assignment in C
- Next by thread: Re: General tree assignment in C
- Index(es):
Relevant Pages
|