Node.h:
- Code: Select all
#ifndef NODE_H
#define NODE_H
template < typename TYPE >
struct Node
{
TYPE data;
Node < TYPE > * next; // pointer to next node
Node(); // default constructor prototype
Node ( TYPE d, Node < TYPE > * n = NULL); // overloaded constructor prototype with default value for the pointer
};
//**********************************************************************************
template < typename TYPE >
Node < TYPE > :: Node()
{
data = 0;
next = NULL;
};
//**********************************************************************************
template < typename TYPE >
Node < TYPE > :: Node ( TYPE d, Node < TYPE > * n )
{
data = d;
next = n;
};
#endif
LList.h:
- Code: Select all
#ifndef LLIST_H
#define LLIST_H
#include "Node.h"
template < typename TYPE >
class LList
{
private:
Node < TYPE > * front; // first node in linked list
public:
LList(); // default constructor prototype
//~LList();
bool insert ( const TYPE& dataIn );
bool remove ( TYPE& dataOut );
bool retrieve ( TYPE& dataOut ) const;
void display () const;
int getSize() const;
bool isEmpty() const;
bool isFull() const;
};
//**********************************************************************************
template < typename TYPE >
LList < TYPE > :: LList()
{
front = NULL;
};
//**********************************************************************************
template < typename TYPE >
bool LList < TYPE > :: insert ( const TYPE& dataIn )
{
Node < TYPE > * pBefore;
pBefore = NULL;
Node < TYPE > * pAfter;
pAfter = front;
Node < TYPE > * pNew;
bool success = false;
while ( pAfter && pAfter -> data < dataIn ) // while pAfter is a valid address and that node's data is less than dataIn
{
pBefore = pAfter; // advance pBefore to next node
pAfter = pAfter -> next; // advance pAfter to next node
}
// Create and fill new node
pNew = new Node < TYPE > ( dataIn, pAfter );
if ( pBefore ) // checking pBefore is valid address
pBefore -> next = pNew; // insert new node/data
else
front = pNew; // this is first item in the linked list
success = true;
return success;
}
//**********************************************************************************
template < typename TYPE >
bool LList < TYPE > :: remove ( TYPE& dataOut )
{
bool success = false;
Node < TYPE > *pTemp = front;
while ( pTemp -> next && !success)
{
if ( pTemp -> next -> data == dataOut )
{
Node < TYPE > *pTemp2 = pTemp -> next;
pTemp -> next = pTemp2 -> next;
delete pTemp2;
success = true;
}
if ( !success )
pTemp = pTemp -> next;
}
return success;
}
//**********************************************************************************
template < typename TYPE >
bool LList < TYPE > :: retrieve ( TYPE& dataOut ) const
{
bool success = false;
Node < TYPE > *pTemp = front;
while ( pTemp && !success)
{
if ( pTemp -> data == dataOut )
success = true;
else
pTemp = pTemp -> next;
}
return success;
}
//**********************************************************************************
template < typename TYPE >
void LList < TYPE > :: display() const
{
int count = 0;
Node < TYPE > * pTemp = front;
cout << "\nCurrent values in list:\n";
while ( pTemp )
{
cout << pTemp -> data << "\t";
pTemp = pTemp -> next;
}
}
#endif
LDriver.cpp:
- Code: Select all
#include <iostream>
using namespace std;
#include "LList.h"
int main()
{
LList <double> myLList;
bool success = myLList.insert ( 2.4 );
success = myLList.insert ( 17.8 );
success = myLList.insert ( 6.1 );
if ( success )
cout << "Insert was successful.\n";
else
cout << "Insert failed.\n";
myLList.display();
double value;
cout << "\nEnter a value to remove. ";
cin >> value;
if ( myLList.remove ( value ) )
cout << "\n\nGood job. Removal of " << value << " was successful.\n";
else
cout << "\nRemoval failed.\n";
myLList.display();
return 0;
}
