-
Linked List Problems
take a look at this code:
Code:
#ifndef __LIST_H__
#define __LIST_H__
template <class C> class list;
template <class C> class cnode
{
C data;
cnode *next;
public:
cnode()
{
data = 0;
next = 0;
}
friend class list<C>;
};
template <class C> class list
{
cnode<C> *head;
long lsize;
public:
list()
{
head = 0;
lsize = 0;
}
~list()
{
this->clear();
}
void add(C c)
{
lsize++;
cnode<C> *ptr = new cnode<C>;
ptr->data = c;
ptr->next = head;
head = ptr;
}
long size()
{
return lsize;
}
void clear()
{
lsize = 0;
cnode<C> *ptr = new cnode<C>;
cnode<C> *prev = new cnode<C>;
ptr = head;
while(ptr)
{
prev = ptr;
ptr = ptr->next;
delete prev;
}
head = 0;
delete ptr;
}
C at(long l)
{
long index = lsize-1;
cnode<C> *ptr = new cnode<C>;
ptr = head;
while(index > l && ptr)
{
ptr = ptr->next;
index--;
}
return ptr->data;
}
};
#endif // __LIST_H__
Code:
#include <iostream>
#include <cstdio>
#include "list.h"
using namespace std;
int main()
{
char szBuffer[32];
list <char*> lc;
list <int> li;
int i;
int x = 0;
li.add(x);
x = 2;
li.add(x);
for(i = 0; i < li.size(); i++)
cout << li.at(i) << endl;
cout << endl;
lc.add("string 1");
lc.add("string 2");
lc.add("string 3");
for(i = 0; i < lc.size(); i++)
cout << lc.at(i) << endl;
cout << endl;
lc.clear();
scanf("%s", szBuffer);
lc.add(szBuffer);
scanf("%s", szBuffer);
lc.add(szBuffer);
scanf("%s", szBuffer);
lc.add(szBuffer);
for(i = 0; i < lc.size(); i++)
cout << lc.at(i) << endl;
return 0;
}
The list works as expected with the integers and constant strings.
On the last part, the list doesn't print three unique strings since
there is just a reference to szBuffer so it prints copies of the last
string entered.
Is there a way to make sure that a copy of the string is added
to the list rather than a reference, other than having separate
buffers for every string?
-
No, besides writing template specializations.
Better idea would be to store a class that stores a string, namely std::string. And be careful: this code
Code:
template <class C> class cnode
{
C data;
cnode *next;
public:
cnode()
{
data = 0;
next = 0;
}
friend class list<C>;
};
will cause you problems because it assumes that whatever type C refers to has a = operator that accepts numbers.
-
Try using the ANSI string class and leave it the way it is (unless, that is, it is critical that your character array is absolutely necessary to make your applications work.) I think = is already defined.
-
= is defined in std::string, but not for numbers.
-
:confused:
Are you sayig that there is not an = operator for numbers (int, long, double, etc.) or is there another class that stores numbers that you are referring to?
-
There isn't a = operator you can use to assign numbers to any class. Not string, not vector, not most of the classes I write, not most of the classes written at all. It simply doesn't make sense. That's why there is a default constructor.
-
I just took it from a node class for ints.
This is fine.
Code:
template <class C> class cnode
{
C data;
cnode *next;
public:
cnode(){}
friend class list<C>;
};