Results 1 to 7 of 7

Thread: Linked List Problems

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Resolved 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.

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  3. #3
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004
    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.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    = 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.

  5. #5
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004


    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?

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    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
  •  



Click Here to Expand Forum to Full Width