romark
asked on
Double Link List
I am confused with template classes and lost in all errors I got when trying to debug doble link list project.
Please, help!!!
There is h and cpp files:
#ifndef __List__
#define __List__
#include <iostream.h>
#include <stdlib.h>
template <class Etype> // Incomplete class declaration
class ListItr; // so friend is visible
template <class Etype>
class List
{
public:
List();
friend class ListItr<Etype>;
class ListNode
{
Etype Element;
ListNode *Next;
ListNode *Prev;
ListNode();
friend class List<Etype>;
friend class ListItr<Etype>;
};
};
// ListItr class interface
template <class Etype>
class ListItr
{
public:
ListItr( const List<Etype> & L ) : Header(L.Header)
// ~ListItr( ) { }
// Insert X before Current position
// Current: Set to new node on success; unchanged otherwise
void InsertBefore( const Etype & X );
// Insert X after Current position
// Current: Set to new node on success; unchanged otherwise
void InsertAfter( const Etype & X );
// Find first occurence of X on the list
int FindFirst( const Etype & X );
// Find las occurence of X on the list
int FindLast(const Etype & X);
// Removes first occurrence of X on the list; do nothing if X is not found
int RemoveFirst( const Etype & X );
// Remove last occurence of X on the list; do nothing if X is not found
int RemoveLast(const Etype & X );
// Returns 1 if Current is not NULL or Header, 0 otherwise
int operator--( ) const { return Current && Current != Header; }
// Set Current to the header node
void MoveToHead(){ Current = Header; }
void MoveToTail(){ Current = Tail;}
// Set Current to the tail node
void First( );
// return 1st node on the list
private:
List<Etype>::ListNode *Current; // Current position
//List<Etype>::List( const List & ) { } // Disable copy constructor
}
#endif
#include "List.h"
#include <iostream.h>
#include <stdlib.h>
template <class Etype>
List<Etype>::ListNode( )
{
Header = new ListNode;
Tail = new ListNode;
Header->Next = *Tail;
Tail->Prev = *Header;
}
template <class Etype>
void
ListItr<Etype>::InsertAfte r( const Etype & X )
{
List<Etype>::ListNode *NewNode;
NewNode = new List<Etype>::ListNode( X );
NewNode->Prev = Current;
NewNode->Next = Current->Next;
NewNode->Prev->Next = NewNode;
NewNode->Next->Prev = NewNode;
Current = NewNode;
}
template <class Etype>
void
ListItr<Etype>::InsertBefo re( const Etype & X )
{
List<Etype>::ListNode *NewNode;
NewNode = new List<Etype>::ListNode( X );
NewNode->Prev = Current;
NewNode->Next = Current->Next;
NewNode->Prev->Next = NewNode;
NewNode->Next->Prev = NewNode;
Current = NewNode;
}
template <class Etype>
int
ListItr<Etype>::FindFirst( const Etype & X )
{
List<Etype>::ListNode *Ptr = Header->Next;
while( Ptr != NULL && Ptr->Element != X )
Ptr = Ptr->Next;
if( Ptr == Tail )
return 0;
Current = Ptr;
return 1;
}
int <class Etype>
int
ListItr<Etype>::FindLast( const Etype & X )
{
List<Etype>::ListNode *Ptr = Tail->Prev;
while( (Ptr != NULL) && (Ptr->Element) != X )
Ptr = Ptr->Prev;
if( Ptr == Header )
return 0;
Current = Ptr;
return 1;
}
template <class Etype>
int
ListItr<Etype>::RemoveFirs t( const Etype & X )
{
List<Etype>::ListNode *Ptr = Header;
while( Ptr->Next != NULL && Ptr->Next->Element != X )
Ptr = Ptr->Next;
if( Ptr->Next == NULL )
return 0; // Remove fails
// Bypass and reclaim memory
List<Etype>::ListNode *DeletedNode = Ptr->Next;
Ptr->Next = Ptr->Next->Next;
delete DeletedNode;
Current = Header; // Reset Current
return 1;
}
template <class Etype>
int
ListItr<Etype>::RemoveLast ( const Etype & X )
{
List<Etype>::ListNode *Ptr = Tail;
while( Ptr->Next != NULL && Ptr->Prev->Element != X )
Ptr = Ptr->Prev;
if( Ptr->Prev == NULL )
return 0; // Remove fails
// Bypass and reclaim memory
List<Etype>::ListNode *DeletedNode = Ptr->Prev;
Ptr->Prev->Next = Ptr;
delete DeletedNode;
Current = Tail; // Reset Current
return 1;
}
template <class Etype>
int
ListItr<Etype>::IsFound( const Etype & X ) const
{
List<Etype>::ListNode *Ptr = Header->Next;
while( Ptr != NULL && Ptr->Element != X )
Ptr = Ptr->Next;
return Ptr != NULL;
}
int <class Etype>
const Etype &
ListItr<Etype>::operator( ) ( ) const
{
return Current->Element;
}
template <class Etype>
void
ListItr<Etype>::First( )
{
Current = Header->Next;
}
int <class Etype>
void
ListItr<Etype>::Last( )
{
Current = Tail->Prev;
}
template <class Etype>
void
ListItr<Etype>::operator-- ( )
{
Current->Prev = Current->Prev->Next;
Current->Next = Current
Current = Current->Prev;
}
Please, help!!!
There is h and cpp files:
#ifndef __List__
#define __List__
#include <iostream.h>
#include <stdlib.h>
template <class Etype> // Incomplete class declaration
class ListItr; // so friend is visible
template <class Etype>
class List
{
public:
List();
friend class ListItr<Etype>;
class ListNode
{
Etype Element;
ListNode *Next;
ListNode *Prev;
ListNode();
friend class List<Etype>;
friend class ListItr<Etype>;
};
};
// ListItr class interface
template <class Etype>
class ListItr
{
public:
ListItr( const List<Etype> & L ) : Header(L.Header)
// ~ListItr( ) { }
// Insert X before Current position
// Current: Set to new node on success; unchanged otherwise
void InsertBefore( const Etype & X );
// Insert X after Current position
// Current: Set to new node on success; unchanged otherwise
void InsertAfter( const Etype & X );
// Find first occurence of X on the list
int FindFirst( const Etype & X );
// Find las occurence of X on the list
int FindLast(const Etype & X);
// Removes first occurrence of X on the list; do nothing if X is not found
int RemoveFirst( const Etype & X );
// Remove last occurence of X on the list; do nothing if X is not found
int RemoveLast(const Etype & X );
// Returns 1 if Current is not NULL or Header, 0 otherwise
int operator--( ) const { return Current && Current != Header; }
// Set Current to the header node
void MoveToHead(){ Current = Header; }
void MoveToTail(){ Current = Tail;}
// Set Current to the tail node
void First( );
// return 1st node on the list
private:
List<Etype>::ListNode *Current; // Current position
//List<Etype>::List( const List & ) { } // Disable copy constructor
}
#endif
#include "List.h"
#include <iostream.h>
#include <stdlib.h>
template <class Etype>
List<Etype>::ListNode( )
{
Header = new ListNode;
Tail = new ListNode;
Header->Next = *Tail;
Tail->Prev = *Header;
}
template <class Etype>
void
ListItr<Etype>::InsertAfte
{
List<Etype>::ListNode *NewNode;
NewNode = new List<Etype>::ListNode( X );
NewNode->Prev = Current;
NewNode->Next = Current->Next;
NewNode->Prev->Next = NewNode;
NewNode->Next->Prev = NewNode;
Current = NewNode;
}
template <class Etype>
void
ListItr<Etype>::InsertBefo
{
List<Etype>::ListNode *NewNode;
NewNode = new List<Etype>::ListNode( X );
NewNode->Prev = Current;
NewNode->Next = Current->Next;
NewNode->Prev->Next = NewNode;
NewNode->Next->Prev = NewNode;
Current = NewNode;
}
template <class Etype>
int
ListItr<Etype>::FindFirst(
{
List<Etype>::ListNode *Ptr = Header->Next;
while( Ptr != NULL && Ptr->Element != X )
Ptr = Ptr->Next;
if( Ptr == Tail )
return 0;
Current = Ptr;
return 1;
}
int <class Etype>
int
ListItr<Etype>::FindLast( const Etype & X )
{
List<Etype>::ListNode *Ptr = Tail->Prev;
while( (Ptr != NULL) && (Ptr->Element) != X )
Ptr = Ptr->Prev;
if( Ptr == Header )
return 0;
Current = Ptr;
return 1;
}
template <class Etype>
int
ListItr<Etype>::RemoveFirs
{
List<Etype>::ListNode *Ptr = Header;
while( Ptr->Next != NULL && Ptr->Next->Element != X )
Ptr = Ptr->Next;
if( Ptr->Next == NULL )
return 0; // Remove fails
// Bypass and reclaim memory
List<Etype>::ListNode *DeletedNode = Ptr->Next;
Ptr->Next = Ptr->Next->Next;
delete DeletedNode;
Current = Header; // Reset Current
return 1;
}
template <class Etype>
int
ListItr<Etype>::RemoveLast
{
List<Etype>::ListNode *Ptr = Tail;
while( Ptr->Next != NULL && Ptr->Prev->Element != X )
Ptr = Ptr->Prev;
if( Ptr->Prev == NULL )
return 0; // Remove fails
// Bypass and reclaim memory
List<Etype>::ListNode *DeletedNode = Ptr->Prev;
Ptr->Prev->Next = Ptr;
delete DeletedNode;
Current = Tail; // Reset Current
return 1;
}
template <class Etype>
int
ListItr<Etype>::IsFound( const Etype & X ) const
{
List<Etype>::ListNode *Ptr = Header->Next;
while( Ptr != NULL && Ptr->Element != X )
Ptr = Ptr->Next;
return Ptr != NULL;
}
int <class Etype>
const Etype &
ListItr<Etype>::operator( ) ( ) const
{
return Current->Element;
}
template <class Etype>
void
ListItr<Etype>::First( )
{
Current = Header->Next;
}
int <class Etype>
void
ListItr<Etype>::Last( )
{
Current = Tail->Prev;
}
template <class Etype>
void
ListItr<Etype>::operator--
{
Current->Prev = Current->Prev->Next;
Current->Next = Current
Current = Current->Prev;
}
ASKER
Thanks, but this 2 syntax corrections not solving a problem. I think it's need logical changes.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
#define __List__
#include <iostream.h>
#include <stdlib.h>
template <class Etype> // Incomplete class declaration
class ListItr;// so friend is visible
template <class Etype>
class List{
public:
List();
friend class ListItr<Etype>;
class ListNode{
Etype Element;
ListNode *Next;
ListNode *Prev;
ListNode();
friend class List<Etype>;
friend class ListItr<Etype>;
};
};
// ListItr class interface
template <class Etype>
class ListItr{
public:
ListItr( const List<Etype> & L ) : Header(L.Header) {};
// ~ListItr( ) { }
// Insert X before Current position
// Current: Set to new node on success; unchanged otherwise
void InsertBefore( const Etype & X );
// Insert X after Current position
// Current: Set to new node on success; unchanged otherwise
void InsertAfter( const Etype & X );
// Find first occurence of X on the list
int FindFirst( const Etype & X );
// Find las occurence of X on the list
int FindLast(const Etype & X);
// Removes first occurrence of X on the list; do nothing if X is not found
int RemoveFirst( const Etype & X );
// Remove last occurence of X on the list; do nothing if X is not found
int RemoveLast(const Etype & X );
int IsFound(const Etype & X ) const;
// Returns 1 if Current is not NULL or Header, 0 otherwise
// void operator--( ) { return Current && Current != Header; } - you got it twice - decide what it will be
void operator--();
const Etype& operator( ) ( ) const;
// Set Current to the header node
void MoveToHead(){ Current = Header; }
void MoveToTail(){ Current = Tail;}
void Last( );
// Set Current to the tail node
void First( );
// return 1st node on the list
private:
List<Etype>::ListNode *Current; // Current position
//List<Etype>::List( const List & ) { } // Disable copy constructor
};
#endif
#include "h1.h"
#include <iostream.h>
#include <stdlib.h>
template <class Etype>
List<Etype>::ListNode()
{
Header = new ListNode;
Tail = new ListNode;
Header->Next = *Tail;
Tail->Prev = *Header;
}
template <class Etype>
void
ListItr<Etype>::InsertAfte
{
List<Etype>::ListNode *NewNode;
NewNode = new List<Etype>::ListNode( X );
NewNode->Prev = Current;
NewNode->Next = Current->Next;
NewNode->Prev->Next = NewNode;
NewNode->Next->Prev = NewNode;
Current = NewNode;
}
template <class Etype>
void ListItr<Etype>::InsertBefo
{
List<Etype>::ListNode *NewNode;
NewNode = new List<Etype>::ListNode(X);
NewNode->Prev = Current;
NewNode->Next = Current->Next;
NewNode->Prev->Next = NewNode;
NewNode->Next->Prev = NewNode;
Current = NewNode;
}
template <class Etype>
int ListItr<Etype>::FindFirst(
{
List<Etype>::ListNode *Ptr = Header->Next;
while( Ptr != NULL && Ptr->Element != X )
Ptr = Ptr->Next;
if( Ptr == Tail )
return 0;
Current = Ptr;
return 1;
}
template <class Etype>
int ListItr<Etype>::FindLast( const Etype & X )
{
List<Etype>::ListNode *Ptr = Tail->Prev;
while( (Ptr != NULL) && (Ptr->Element) != X )
Ptr = Ptr->Prev;
if( Ptr == Header )
return 0;
Current = Ptr;
return 1;
}
template <class Etype>
int
ListItr<Etype>::RemoveFirs
{
List<Etype>::ListNode *Ptr = Header;
while( Ptr->Next != NULL && Ptr->Next->Element != X )
Ptr = Ptr->Next;
if( Ptr->Next == NULL )
return 0; // Remove fails
// Bypass and reclaim memory
List<Etype>::ListNode *DeletedNode = Ptr->Next;
Ptr->Next = Ptr->Next->Next;
delete DeletedNode;
Current = Header; // Reset Current
return 1;
}
template <class Etype>
int
ListItr<Etype>::RemoveLast
{
List<Etype>::ListNode *Ptr = Tail;
while( Ptr->Next != NULL && Ptr->Prev->Element != X )
Ptr = Ptr->Prev;
if( Ptr->Prev == NULL )
return 0; // Remove fails
// Bypass and reclaim memory
List<Etype>::ListNode *DeletedNode = Ptr->Prev;
Ptr->Prev->Next = Ptr;
delete DeletedNode;
Current = Tail; // Reset Current
return 1;
}
template <class Etype>
int ListItr<Etype>::IsFound(co
{
List<Etype>::ListNode *Ptr = Header->Next;
while( Ptr != NULL && Ptr->Element != X )
Ptr = Ptr->Next;
return Ptr != NULL;
}
template <class Etype>
const Etype& ListItr<Etype>::operator( ) ( ) const
{
return Current->Element;
}
template <class Etype>
void ListItr<Etype>::First( )
{
Current = Header->Next;
}
template <class Etype>
void ListItr<Etype>::Last( )
{
Current = Tail->Prev;
}
template <class Etype>
void ListItr<Etype>::operator--
{
Current->Prev = Current->Prev->Next;
Current->Next = Current;
Current = Current->Prev;
}
void main()
{}
hope this helps