HELP Modifying Linked List Program....

Hi,
I have a code for a linked list. Can't figure how to add 3 more functions to it. 1) COUNT that keeps track of the list size. 2) SEARCH, that searches the item with a certain value to see if a number is there or not. and 3)SORT that sorts the list in ascending or descending order.
PLZZZZZ HEELLPPP.... My linked list code is below...its 3 different file. 1st is .cpp file, 2ns is List.h & 3rd is Listnd.h
THANX AT TON!!

// List class test
#include
#include "list.h"

using std::cin;
using std::endl;

// Function to test an integer List
template< class T >
void testList( List< T > &listObject, const char *type )
{
cout << "Testing a List of " << type << " values
";

instructions();
int choice;
T value;

do {
cout << "? ";
cin >> choice;

switch ( choice ) {
case 1:
cout << "Enter " << type << ": ";
cin >> value;
listObject.insertAtFront( value );
listObject.print();
break;
case 2:
cout << "Enter " << type << ": ";
cin >> value;
listObject.insertAtBack( value );
listObject.print();
break;
case 3:
if ( listObject.removeFromFront( value ) )
cout << value << " removed from list
";

listObject.print();
break;
case 4:
if ( listObject.removeFromBack( value ) )
cout << value << " removed from list
";

listObject.print();
break;
}
} while ( choice != 5 );

cout << "End list test

";
}

void instructions()
{
cout << "Enter one of the following:
"
<< " 1 to insert at beginning of list
"
<< " 2 to insert at end of list
"
<< " 3 to delete from beginning of list
"
<< " 4 to delete from end of list
"
<< " 5 to end list processing
";
}

int main()
{
List< int > integerList;
testList( integerList, "integer" ); // test integerList

List< double > doubleList;
testList( doubleList, "double" ); // test doubleList

return 0;
}



//List.h
// Template List class definition
#ifndef LIST_H
#define LIST_H

#include
#include
#include "listnd.h"

using std::cout;

template< class NODETYPE >
class List {
public:
List(); // constructor
~List(); // destructor
void insertAtFront( const NODETYPE & );
void insertAtBack( const NODETYPE & );
bool removeFromFront( NODETYPE & );
bool removeFromBack( NODETYPE & );
bool isEmpty() const;
void print() const;
private:
ListNode< NODETYPE > *firstPtr; // pointer to first node
ListNode< NODETYPE > *lastPtr; // pointer to last node

// Utility function to allocate a new node
ListNode< NODETYPE > *getNewNode( const NODETYPE & );
};

// Default constructor
template< class NODETYPE >
List< NODETYPE >::List() : firstPtr( 0 ), lastPtr( 0 ) { }

// Destructor
template< class NODETYPE >
List< NODETYPE >::~List()
{
if ( !isEmpty() ) { // List is not empty
cout << "Destroying nodes ...
";

ListNode< NODETYPE > *currentPtr = firstPtr, *tempPtr;

while ( currentPtr != 0 ) { // delete remaining nodes
tempPtr = currentPtr;
cout << tempPtr->data << '
';
currentPtr = currentPtr->nextPtr;
delete tempPtr;
}
}

cout << "All nodes destroyed

";
}

// Insert a node at the front of the list
template< class NODETYPE >
void List< NODETYPE >::insertAtFront( const NODETYPE &value )
{
ListNode< NODETYPE > *newPtr = getNewNode( value );

if ( isEmpty() ) // List is empty
firstPtr = lastPtr = newPtr;
else { // List is not empty
newPtr->nextPtr = firstPtr;
firstPtr = newPtr;
}
}

// Insert a node at the back of the list
template< class NODETYPE >
void List< NODETYPE >::insertAtBack( const NODETYPE &value )
{
ListNode< NODETYPE > *newPtr = getNewNode( value );

if ( isEmpty() ) // List is empty
firstPtr = lastPtr = newPtr;
else { // List is not empty
lastPtr->nextPtr = newPtr;
lastPtr = newPtr;
}
}

// Delete a node from the front of the list
template< class NODETYPE >
bool List< NODETYPE >::removeFromFront( NODETYPE &value )
{
if ( isEmpty() ) // List is empty
return false; // delete unsuccessful
else {
ListNode< NODETYPE > *tempPtr = firstPtr;

if ( firstPtr == lastPtr )
firstPtr = lastPtr = 0;
else
firstPtr = firstPtr->nextPtr;

value = tempPtr->data; // data being removed
delete tempPtr;
return true; // delete successful
}
}

// Delete a node from the back of the list
template< class NODETYPE >
bool List< NODETYPE >::removeFromBack( NODETYPE &value )
{
if ( isEmpty() )
return false; // delete unsuccessful
else {
ListNode< NODETYPE > *tempPtr = lastPtr;

if ( firstPtr == lastPtr )
firstPtr = lastPtr = 0;
else {
ListNode< NODETYPE > *currentPtr = firstPtr;

while ( currentPtr->nextPtr != lastPtr )
currentPtr = currentPtr->nextPtr;

lastPtr = currentPtr;
currentPtr->nextPtr = 0;
}

value = tempPtr->data;
delete tempPtr;
return true; // delete successful
}
}

// Is the List empty?
template< class NODETYPE >
bool List< NODETYPE >::isEmpty() const
{ return firstPtr == 0; }

// Return a pointer to a newly allocated node
template< class NODETYPE >
ListNode< NODETYPE > *List< NODETYPE >::getNewNode(
const NODETYPE &value )
{
ListNode< NODETYPE > *ptr =
new ListNode< NODETYPE >( value );
assert( ptr != 0 );
return ptr;
}

// Display the contents of the List
template< class NODETYPE >
void List< NODETYPE >::print() const
{
if ( isEmpty() ) {
cout << "The list is empty

";
return;
}

ListNode< NODETYPE > *currentPtr = firstPtr;

cout << "The list is: ";

while ( currentPtr != 0 ) {
cout << currentPtr->data << ' ';
currentPtr = currentPtr->nextPtr;
}

cout << "

";
}

#endif



//Listnd.h
// ListNode template definition
#ifndef LISTND_H
#define LISTND_H

template< class NODETYPE > class List; // forward declaration

template
class ListNode {
friend class List< NODETYPE >; // make List a friend
public:
ListNode( const NODETYPE & ); // constructor
NODETYPE getData() const; // return data in the node
private:
NODETYPE data; // data
ListNode< NODETYPE > *nextPtr; // next node in the list
};

// Constructor
template
ListNode< NODETYPE >::ListNode( const NODETYPE &info )
: data( info ), nextPtr( 0 ) { }

// Return a copy of the data in the node
template< class NODETYPE >
NODETYPE ListNode< NODETYPE >::getData() const { return data; }

#endif
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories