Originally posted by saraSS
Code:
struct headernode{
char x;
listNodeType *next;
};
struct headernode{
char x;
listNodeType *next;
};
#include <iostream>
#include <string>
using namespace std;
#define ItemType int
// the definition of a list node
class ListNodeClass
{
public:
// the data of a node
ItemType Info;
// the pointer to the next node
ListNodeClass *Next;
};
// the definition of a list (header!)
class ListClass
{
private:
// entry to the list
ListNodeClass *Front;
// number of elements
int Count;
// the ID to identify the set
char ID;
void FreeNode( ListNodeClass* );
public:
ListClass( char id ) : Front(0), Count(0), ID(id) {}
void InsertFront( ListNodeClass& );
void InsertRear( ListNodeClass& );
void RemoveFront();
void ClearList();
int NumItems() const { return Count; }
bool Empty() const;
void Printing();
ListNodeClass* Find(const ItemType & Item) const;
};
void ListClass::InsertFront( ListNodeClass& node ) {
node.Next = Front;
Front = &node;
Count++;
}
void ListClass::Printing() {
ListNodeClass *ptr = Front;
cout << "This is set " << ID << " (" << Count << " elements): ";
while( ptr!=NULL ) {
cout << ptr->Info << " ";
ptr = ptr->Next;
}
cout << endl;
}
int main( void )
{
ListNodeClass *node;
// list a
ListClass list_a( 'a' );
node = new ListNodeClass;
node->Info = 21;
list_a.InsertFront( *node );
node = new ListNodeClass;
node->Info = 33;
list_a.InsertFront( *node );
list_a.Printing();
// list b
ListClass list_b( 'b' );
node = new ListNodeClass;
node->Info = 3;
list_b.InsertFront( *node );
node = new ListNodeClass;
node->Info = 2;
list_b.InsertFront( *node );
node = new ListNodeClass;
node->Info = 1;
list_b.InsertFront( *node );
list_b.Printing();
return 0;
}
Comment