|
-
Feb 24th, 2003, 04:29 PM
#1
Thread Starter
Frenzied Member
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?
Last edited by wey97; Nov 10th, 2004 at 10:27 AM.
-
Feb 25th, 2003, 12:17 PM
#2
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Mar 1st, 2003, 12:05 AM
#3
Fanatic Member
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.
-
Mar 1st, 2003, 07:18 AM
#4
= is defined in std::string, but not for numbers.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Mar 1st, 2003, 06:27 PM
#5
Fanatic Member
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?
-
Mar 1st, 2003, 07:02 PM
#6
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Mar 14th, 2003, 02:55 PM
#7
Thread Starter
Frenzied Member
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>;
};
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|