Template Linked Lists

I have been working on an assignment in my CSII class over Linked Lists in template classes and would like someone to take a look at this. (I am not asking anyone to do my homework by the way) Just want to know if im on the right track with these two particular functions. The assignment is as follows:


Assignment

Your task is to complete the following LinkList as we discussed in class and then write a C++ program to test your LinkList. You need to test all of the functions in your LinkList with at least two types of LinkList, such as an integer LinkList and a string LinkList.

The following is the class declaration from the header file:



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
template <typename T> 
class LinkList
{ 
   private:
    class Node
    {
      public:
      T info;
      Node *next;
    };
    typedef Node *nodePtr;
 
   public:
  	LinkList(); 		//constructor 
	LinkList(const LinkList<T> & orig); //copy constructor
	~LinkList();		//destructor
	bool empty(); 		//determine if the LinkList is empty 
        T Front(); 			// returns item at front of LinkList
	T Back();			// returns item at back of LinkList
	Void Pus_back(const T & item);// add an item at the end of the LinkList
        void Push_front(const T & item); //add item at the begining of LinkList
        void pop_back(); 	//remove the last item of the LinkList
        void pop_front(); //remove the first item of the LinkList
        void insert(T item);//insert the item in ascending order
        void erase(T item);//remove the item in the list
        bool Search(T item);	//search a given item, if it can be found, return true, otherwise return false
	int Count();		//return the number of items in the list
	LinkList<T>& operator=(const LinkList<T> & orig); //overloaded assignment operator
	friend ostream& operator<<(ostream &out, const LinkList<T> & L); 
								//overloaded output operator 

  private:       
     nodePtr First;      //node pointer which points to the front(first node)
};  


I've written the TFront() and TBack() functions below as I thought they should be but I need the feedback on them. They are as follows:

1
2
3
4
5
6
7
8
//TFront(): returns item at the back of the list
//Postcondition: nodePtr points to the front of the list
template <typename T>
TFront()
{
  nodePtr nPtr = front;
  return front;
}


and

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//TBack(): returns the item at the back of the list
//Precondition: nodePtr points to the front of the list
//Postcondition: nodePtr points to back
template <typename T>
TBack()
{
  nodePtr nPtr = back;
  while (nPtr != 0)
   {
     nPtr->next = back;
   }

   if (nPtr->next == 0)
    {
      nPtr->next = back;
      return back;
    }
   
   return back;
}


Also, I wasnt sure if the if statement from the TBack() function should have gone inside the while loop or outside. So, I revised it for that too.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
template <typename T>
TBack()
{
  nodePtr nPtr = back;
  while (nPtr != 0)
   {
     nPtr->next = back;
     if (nPtr->next == 0)
      {
        nPtr->next = back;
        return back;
      }
   
     return back;
   }
}


Any constructive criticism would be greatly appreciated and again, I'm not asking anyone to do my homework. Just let me know if i'm on the right track. Thanks
Well, the main issue is they don't return a type T in your definition. Second, you aren't returning a T, you are returning a nodePtr. Finally, what if your list is empty? You don't have a front or a back then.
Ah, I knew i missed something there. Question about the nodePtr: could I simply state (example) nodePtr.'some function'() to access member functions directly or do I need to state something like:
1
2
3
nodePtr->next = front;   
return nodePtr->next;
 


as for the empty list, theres an empty() member function in the class that should take care of that right?
Topic archived. No new replies allowed.