Data structure "Tree" need some help

Hi guys! I wanna create a "Tree" for reading *xml files. Can't figure out how many and what kind of links and data i should have into my structure "XML_Tree". Here what i think, but this this don't wanna work or i can't realize it right. So here a peace of "code".
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct Atr_List {
    Atr_List ();
    char AtrName[20];
    char AtrValue[20];
    Atr_List *next;
}

struct XML_Tree {
    XML_Tree ();
    char name[20];
    XML_Tree *parent;
    XML_Tree *firstChild;
    XML_Tree *nextChild;
    Atr_List *firstAtr;
    Atr_List *nextAtr;    
}


So here is the idea how this structure should work. Every XML_Tree node points to tag in *xml file. *parent is pointing to tag which current tag belong to. So very firstTag doesn't has parent. It will be our "root". *firstChild and *nextChild points to all subTags of our current tag. The difference between them is that one of them needs for pointing to the very first "subTag" and the other one needs for going through our list of children. *firstAtr and *nextAtr has the same meaning but for atributes of xml tag. Will this structure work right? Cause i can't realise it and thought may be my structure is wrong. Sorry for my poor English and the same programming skill. Learning...
I suggest you to differentiate between a tree and a node of the tree.
1
2
3
class tree{
   node root;
};
Also, encapsulate more
1
2
3
4
class node{
   node *parent;
   container<node> children; //vector, list, ...
};
I don't see how `firstChild' is especial.

Cause i can't realise it and thought may be my structure is wrong
Sorry, I don't understand what you mean.
Also, encapsulate more

I have to write this without STL

Cause i can't realise it and thought may be my structure is wrong

I meant that i have mess into my structure when i am trying execute my programm. And didn't know it is because of invalid structure or wrong pointers assignments.
If you can't use the STL then create your own container class and use it.
So create a list and test it, after you think it works use it with your tree.

I would need to see your code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
struct atrElem {
    wchar_t atrName[20];
    wchar_t atrValue[20];
    atrElem *next;
};

struct tagElem {
    wchar_t tagName[20];
    wchar_t value[256];
    int status;
    tagElem *parent;
    tagElem *firstChild;
    tagElem *nextChild;
    atrElem *firstAtr;
    atrElem *nextAtr;

};

class TagContainer {
    tagElem * head;
    public: wchar_t *currentTag();
    public: void tagValue(wchar_t *);
    public: void closeTag();
    public: void addTag (wchar_t *);
    public: void addAtrName (wchar_t *);
    public: void addAtrValue (wchar_t *);
};

wchar_t *TagContainer::currentTag() {
    return head->tagName;
}

void TagContainer::tagValue(wchar_t *val) {
    size_t size = wcslen(val);
    wcsncpy(head->value, val, size);
}


void TagContainer::addAtrName(wchar_t *name) {
    size_t size = wcslen(name);
    atrElem *newAtr = new atrElem();
    wcsncpy(newAtr->atrName, name, size);
    newAtr->next = head->firstAtr;
    head->firstAtr = newAtr;
}

void TagContainer::addAtrValue(wchar_t *value) {
    size_t size = wcslen(value);
    wcsncpy(head->firstAtr->atrValue, value, size);
}

void TagContainer::addTag(wchar_t *name) {
    size_t size = wcslen(name);
    tagElem *newTag = new tagElem();
    wcsncpy(newTag->tagName, name, size);
    newTag->parent = head;
    if (head != NULL) {
        if (head->firstChild == NULL && head->parent != newTag->parent) {

            head->firstChild = newTag;

        } else if (head->firstChild != NULL) {

            head=head->firstChild;

            while (head->nextChild != NULL) head = head->nextChild;

            head->nextChild = newTag;
        }

    }
    head = newTag;
    head->status = 1;
}


void TagContainer::closeTag() {
    head->status = 0;
    head = head->parent;
}


And I have 1 more question. Sometimes when I am using some dynamic memory in such way:
1
2
3
4
5
6
7
8
wchar_t *buffer = new wchar_t[length];

wcsncpy(buffer, b, length);

test.tagValue(buffer);

buffer = NULL;
delete [] buffer;


after wcsncpy function buffer is filling with china symbols after word that was copied. So buffer is created with more length than i wanted. It doesn't happen every time. What does it depend of?
Topic archived. No new replies allowed.