linked list of linked list

I am trying to write a home made database, and my approach is to use a linked list of linked lists. The first linked list is the db and the underlying linked lists are the db tables. I am having trouble in my display method finding a table by name.

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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include<iostream>
#include <stdio.h>
#include <vector>
using namespace std;
int counter=0;


template <class atype>
class Field
{
public:
    string name;
    atype val;
};


class dbTable
{
public:
    vector <int> intRow;
 
  	vector<Field<double> > doubleRow;

	vector<Field<string> > stringRow;
    
    string name;
};


//we need a link to point to the puppy and the next item
//something to hold the list and methods to add to the list and remove etc.
class Link
{
public:
    dbTable *table = new dbTable;
    Link *Next;
    Link *current;
    Field<int> *ifield;
    Field<string> *strfield;
    Field<double> *dfield;
};

class LinkedList
{
private:
    Link *First;
public:
    string name;
    LinkedList()
    {
        First = NULL;
    }
    

     vector<int> AddIntField(int i)
    {
        Link *newLink = new Link;
        dbTable *ntable = new dbTable;
        newLink->table=ntable;
	vector<int> m = newLink->table->intRow;
        vector<int>::iterator mit;
        mit=m.begin();
        m.insert(mit,i);
        newLink = newLink->Next;
        First= newLink;
        cout << "Int added";
        return m;
    }
    
    void AddStrField(Field<string> strfield)
    {
        Link *current;
        current = First;
        current->table->stringRow.push_back(strfield);
       // current = current->Next;
        cout << "String added";
    }
    
    void AddDoubleField(Field<double> dfield)
    {
        Link *current;
        current = First;
        current->table->doubleRow.push_back(dfield);
        //current = current->Next;
        cout << "Double added";
    }
    
    LinkedList addDbTable(string name)
    {
        Link *newLink = new Link;
        dbTable *nTable;
        newLink->table=nTable;
        newLink->Next=First;
        First=newLink;
        LinkedList LL;
        LL.name = name;
        counter++;
        return LL;
    }
    
    void AddTableEnd(int A,int B, string name)
    {
        
        
        Link *lastItem=new Link;
        Link *newNode=new Link;
        dbTable *nTable=new dbTable;
        lastItem=First;
       
        while (lastItem->Next != NULL)
        {
            lastItem=lastItem->Next;
        }
        lastItem->Next=newNode;
        newNode->table = nTable;
        newNode->table->name = name;
        newNode->Next=NULL;
    }
    
    void RemoveTable(string name)
    {
        string dName;
        while(First != NULL)
        {
            if(name.compare(name) == 0)
            {
                RemoveList(*First);
                cout << "Deleted " << endl;
                break;
            }
            First=First->Next;
        }
        
    }
    
    void RemoveList(Link L)
    {
        Link *current=new Link;
        Link *temp = new Link;
        dbTable *ntable = new dbTable;
        current->table=ntable;
        current=First;
        while (current != NULL)
        {
            temp=current->Next;
            delete current;
            current=temp;
        }
        delete current;
    }

      void DisplayTable(LinkedList L, string name)
    {
    Link *current = new Link;
    Link *temp = new Link;
    dbTable *nTable = new dbTable;
    current->table = current->table;
    cout << First->table->name;
	while (current != NULL)
	{
        string tname=current->table->name;
	cout << tname << "\n";
		if (name.compare(tname) == 0)
		{
		cout << "DISPLAY " << tname << "\n";
		break;
		}
	current=current->Next;
	}
    }
    

};

}
You have some issues with the code you've posted.

Line 35: You can't do an assignment in a class declaration. You need to do this in Link's constructor.

Line 131: You're modifying the head of your list. This is a memory leak. You will lose the entries you pass.

You need to investigate inheritance. You're intermixing functions related to tables within your database along with columns within a table within the same class (Link). This is poor design.

You need to look at using std::list. There is no reason to write your own linked list class when one already exists.

I suggest you rethink your class structure. Consider a DataBase class which contains a std::list of dbTables. A dbTable should contain a std::list of columns.

Line 136: Why are you passing L in? You're not using it.
Line 140: What's the point of creating a new dbTable instance here?
Line 141: dbTable has no default constructor, so current->table points to an uninitialized dbTable entry.
Line 149: The only way to get here is if current == NULL, therefore, you're trying to delete a NULL pointer. Guaranteed to crash your program.

Line 152: Why are you passing in L? You never use it.
Line 154: Link has no default constructor, therefore all variables in current and temp are undefined.
Line 156: What's the point of creating a new dbTable here?
Line 157: What's the point of this statement? It does nothing.
Line 161,168: Members of current are initialized.
Each table can have multiple data types. For example one table has a row that contains name(string) and ID number (int)
Topic archived. No new replies allowed.