3 Replies - 5450 Views - Last Post: 21 October 2011 - 10:36 AM Rate Topic: -----

#1 jeevanbmanoj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 21-October 11

Template class implementation of linked list

Posted 21 October 2011 - 07:55 AM

I'm new to generic programming and I just cant figure out what is wrong in this . The program is supposed to make a linked list using class.
I have used 2 classes so that a single object of class LL can be used to manipulate a linked list . I want the program to work for both integers and characters . When I try without the generic part , the code is working perfectly !
Somebody please help me ! My exam is tomorrow!!


#include<iostream>
#include<stdlib.h>
using namespace std;
template<class T>
class node
{
    T data;
    node * link;
    friend class ll;
    public:

};
  template<class T>
  class ll
{
    node * <T>start;
    public:
    template<class T>
    ll()
    {
        <T>start=NULL;
    }
    //void search();
    void add();
    void disp();
   //void remove();
};
  template<class T>
  void ll::add()
  {
      T x;
      node * <T>temp=new node;
      cout<<"\nData : ";
      cin>>x;
      temp->data=x;
      temp->link=start;
      start =temp;
  }
  template<class T>
   void ll::disp()
   {
       node *<T>ptr;
       T x;
       ptr =start;
       while(ptr!=NULL)
       {
           x=ptr->data;
           cout<<x<<"\n";
           ptr=ptr->link;
       }
   }
   int main()
   {
       ll <int>list1;
       int ch;
       while(1)
       {
           cout<<"\n1.Add\n2.Display\n3.Exit\nEnter choice : ";
           cin>>ch;
           switch(ch)
            {
                case 1  : list1.add();      break;
                case 2  : list1.disp();     break;
                case 3  : exit(0);    break;
                default : cout<<"Invalid Entry "; break;
            }
      }
     return 0;
   }



Is This A Good Question/Topic? 0
  • +

Replies To: Template class implementation of linked list

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1155
  • View blog
  • Posts: 2,536
  • Joined: 05-May 05

Re: Template class implementation of linked list

Posted 21 October 2011 - 08:25 AM

How is this working and it doesn't compile?

(1) Scrap the friend class and use public setter/getters.

T getData() { return data; }
void setData(T data) { this->data = data; }
.
.
.
temp->setData(x);
temp->setLink(start);



(2) Declare your types and method signatures correctly.

node<T>* start;



template<class T> void ll<T>::add()



Once you've done those things your list will work correctly (with type arguments), although it appears to append elements to the front of the list instead of the back.
Was This Post Helpful? 0
  • +
  • -

#3 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 857
  • View blog
  • Posts: 2,341
  • Joined: 20-August 07

Re: Template class implementation of linked list

Posted 21 October 2011 - 10:02 AM

View Postblackcompe, on 21 October 2011 - 04:25 PM, said:

(1) Scrap the friend class and use public setter/getters.

Seems pointless to me. node is such a trivial structure that it would make more sense to turn it into a struct member of class ll instead

i.e
template <typename T>
class ll
{
private:
    struct node
    {
        // etc.
    };
}; 
This is how at least one popular implementation of std::list works; much better for encapsulation too since node is completely invisible to the outside world.

This post has been edited by Bench: 21 October 2011 - 10:04 AM

Was This Post Helpful? 1
  • +
  • -

#4 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1155
  • View blog
  • Posts: 2,536
  • Joined: 05-May 05

Re: Template class implementation of linked list

Posted 21 October 2011 - 10:36 AM

View PostBench, on 21 October 2011 - 11:02 AM, said:

View Postblackcompe, on 21 October 2011 - 04:25 PM, said:

(1) Scrap the friend class and use public setter/getters.

Seems pointless to me. node is such a trivial structure that it would make more sense to turn it into a struct member of class ll instead

i.e
template <typename T>
class ll
{
private:
    struct node
    {
        // etc.
    };
}; 
This is how at least one popular implementation of std::list works; much better for encapsulation too since node is completely invisible to the outside world.


Yes, normally clients wouldn't have access to the node class, and therefore it would be better to make the fields public.

This post has been edited by blackcompe: 21 October 2011 - 10:37 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1