Quantcast

Maximum PC

It is currently Sat Sep 20, 2014 11:39 am

All times are UTC - 8 hours




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: Programming assignment
PostPosted: Sun Apr 24, 2005 12:33 pm 
Clawhammer
Clawhammer
User avatar

Joined: Sat Jun 26, 2004 2:16 pm
Posts: 3600
How would I change the deleteNode member function that when it is passed the value 0, it will delete all nodes with a value less than that?

Thanks.

Code:
#include <iostream>
using namespace std;

template <class T>
class LinkedList
{
   private:
      //  Declare a type for the list node
      class ListNode
      {
         friend LinkedList<T>;
         T value;
         ListNode *next;
         ListNode(T value1, ListNode *next1 = NULL)
         {
            value = value1;
            next = next1;
         }
      };
      ListNode *head;
   public:
      LinkedList()
      {
         head = NULL;
      }
      ~LinkedList();
      void appendNode(T);
      void insertNode(T);
      void deleteNode(T);
      void displayList();
};

///////////////////////////////////////////////////////
//  appendNode appends a node containing the value
//  val passed as a parameter to the end of the list.
///////////////////////////////////////////////////////

template<class T>
void LinkedList<T>::appendNode(T val)
{
   if(head == NULL)
      head = new ListNode(val);
   else
   {
      //  nonempty list
      ListNode *nodePtr;  //  used to traverse the list
      nodePtr = head;
      //  traverse list to locate last node
      while (nodePtr ->next != NULL)
         nodePtr = nodePtr->next;
      //  ptr->next is NULL so nodePtr points to last node
      //  create new node and make it successor of what
      //  is now the last node
      nodePtr->next = new ListNode(val);
   }
}


///////////////////////////////////////////
//  displayList shows the value
//  stored in each node of the linked list
//  pointed to by the head.
///////////////////////////////////////////

template<class T>
void LinkedList<T>::displayList()
{
   ListNode *nodePtr;

   nodePtr = head;
   while(nodePtr)
   {
      cout<<nodePtr->value<<endl;
      nodePtr=nodePtr->next;
   }
}


///////////////////////////////////////////////
//  The insertNode function inserts a node with
//  val copied to its value member.
///////////////////////////////////////////////

template<class T>
void LinkedList<T>::insertNode(T val)
{
   ListNode *nodePtr, *previousNodePtr;

   if (head == NULL  || head->value >= val)
   {
      // new node goes at beginning
      head = new ListNode(val, head);
   }
   else
   {
      previousNodePtr = head;
      nodePtr = head->next;
      //  find the insertion point
      while(nodePtr != NULL && nodePtr->value < val)
      {
         previousNodePtr = nodePtr;
         nodePtr = nodePtr->next;
      }
      //  creates the new node and inserts it
      //  just before nodePtr
      previousNodePtr->next= new ListNode(val, nodePtr);
   }
}

/////////////////////////////////////////////////////
//  The deleteNode function searches for a node
//  with val as its value, the node, if found, is
//  delete from the list and from memory
/////////////////////////////////////////////////////

template<class T>
void LinkedList<T>::deleteNode(T val)
{
   ListNode *nodePtr, *previousNodePtr;

   //  if the list is empty, do nothing
   if(!head)
      return;
   //  determine if the first node is the one to delete
   if(head->value < val)
   {
      nodePtr = head;
      head = head->next;
      delete nodePtr;
   }
   else
   {
      //  initialize nodePtr to head of list
      nodePtr = head;

      //  skip all nodes whose value member is
      //  not equal to val
      while(nodePtr != NULL && nodePtr->value != val)
      {
         previousNodePtr = nodePtr;
         nodePtr = nodePtr->next;
      }
      //  Link the previous node to the node after
      //  nodePtr, then delete nodePtr
      if(nodePtr)
      {
         previousNodePtr->next = nodePtr->next;
         delete nodePtr;
      }
   }
}

////////////////////////////////////////////////
//  Destructor
//  This function deletes every node in the list
////////////////////////////////////////////////

template<class T>
LinkedList<T>::~LinkedList()
{
   ListNode *nodePtr, *nextNodePtr;
   
   nodePtr = head;
   while(nodePtr != NULL)
   {
      nextNodePtr = nodePtr->next;
      delete nodePtr;
      nodePtr = nextNodePtr;
   }
}


Top
  Profile  
 
 Post subject:
PostPosted: Sun Apr 24, 2005 3:22 pm 
Clawhammer
Clawhammer
User avatar

Joined: Sat Jun 26, 2004 2:16 pm
Posts: 3600
Code:
#include <iostream>
using namespace std;

class FloatList
{
   private:
      //  Declare a type for the list node
      class ListNode
      {
         friend FloatList;
         float value;
         ListNode *next;
         ListNode(float value1, ListNode *next1 = NULL)
         {
            value = value1;
            next = next1;
         }
      };
      ListNode *head;
      void print(FloatList *);
   public:
      FloatList()
      {
         head = NULL;
      }
      ~FloatList();
      void appendNode(float);
      void insertNode(float);
      void deleteNode(float);
      void displayList()
      {print(class FloatList::head);};
};

///////////////////////////////////////////////////////
//  appendNode appends a node containing the value
//  num passed as a parameter to the end of the list.
///////////////////////////////////////////////////////


void FloatList::appendNode(float num)
{
   if(head == NULL)
      head = new ListNode(num);
   else
   {
      //  nonempty list
      ListNode *nodePtr;  //  used to traverse the list
      nodePtr = head;
      //  traverse list to locate last node
      while (nodePtr ->next != NULL)
         nodePtr = nodePtr->next;
      //  ptr->next is NULL so nodePtr points to last node
      //  create new node and make it successor of what
      //  is now the last node
      nodePtr->next = new ListNode(num);
   }
}




///////////////////////////////////////////////
//  The insertNode function inserts a node with
//  num copied to its value member.
///////////////////////////////////////////////


void FloatList::insertNode(float num)
{
   ListNode *nodePtr, *previousNodePtr;

   if (head == NULL  || head->value >= num)
   {
      // new node goes at beginning
      head = new ListNode(num, head);
   }
   else
   {
      previousNodePtr = head;
      nodePtr = head->next;
      //  find the insertion point
      while(nodePtr != NULL && nodePtr->value < num)
      {
         previousNodePtr = nodePtr;
         nodePtr = nodePtr->next;
      }
      //  creates the new node and inserts it
      //  just before nodePtr
      previousNodePtr->next= new ListNode(num, nodePtr);
   }
}

/////////////////////////////////////////////////////
//  The deleteNode function searches for a node
//  with num as its value, the node, if found, is
//  delete from the list and from memory
/////////////////////////////////////////////////////


void FloatList::deleteNode(float num)
{
   ListNode *nodePtr, *previousNodePtr;

   //  if the list is empty, do nothing
   if(!head)
      return;
   //  determine if the first node is the one to delete
   if(head->value == num)
   {
      nodePtr = head;
      head = head->next;
      delete nodePtr;
   }
   else
   {
      //  initialize nodePtr to head of list
      nodePtr = head;

      //  skip all nodes whose value member is
      //  not equal to num
      while(nodePtr != NULL && nodePtr->value != num)
      {
         previousNodePtr = nodePtr;
         nodePtr = nodePtr->next;
      }
      //  Link the previous node to the node after
      //  nodePtr, then delete nodePtr
      if(nodePtr)
      {
         previousNodePtr->next = nodePtr->next;
         delete nodePtr;
      }
   }
}

////////////////////////////////////////////////
//  Destructor
//  This function deletes every node in the list
////////////////////////////////////////////////


FloatList::~FloatList()
{
   ListNode *nodePtr, *nextNodePtr;
   
   nodePtr = head;
   while(nodePtr != NULL)
   {
      nextNodePtr = nodePtr->next;
      delete nodePtr;
      nodePtr = nextNodePtr;
   }
}





//////////////////////////////////
//  Prints the contents of the node
//////////////////////////////////

FloatList::print(class FloatList::ListNode *head)
{
   if(nodePtr != NULL)
      {
         cout<< nodePtr->value<<endl;
         displayList(nodePtr->next);
      }
   return;

}


Top
  Profile  
 
 Post subject:
PostPosted: Sun Apr 24, 2005 3:32 pm 
In the lab!
In the lab!
User avatar

Joined: Sun Jun 06, 2004 10:47 am
Posts: 831
Location: Secret Laboratory
Code:
#include <iostream>
using namespace std;

template <class T>
class LinkedList
{
   private:
      //  Declare a type for the list node
      class ListNode
      {
         friend LinkedList<T>;
         T value;
         ListNode *next;
         ListNode(T value1, ListNode *next1 = NULL)
         {
            value = value1;
            next = next1;
         }
      };
      ListNode *head;
     void showList(ListNode*);
   public:
      LinkedList()
      {
         head = NULL;
      }
      ~LinkedList();
      void appendNode(T);
      void insertNode(T);
      void deleteNode(T);
      void displayList();
};

template<class T>
void LinkedList<T>::showList(ListNode * nodePtr)
{
   //ListNode *nodePtr;

   //nodePtr = head;
   if(nodePtr != NULL)
   {
      cout<<nodePtr->value<<endl;
      this->showList(nodePtr->next);
   }
}

///////////////////////////////////////////////////////
//  appendNode appends a node containing the value
//  val passed as a parameter to the end of the list.
///////////////////////////////////////////////////////

template<class T>
void LinkedList<T>::appendNode(T val)
{
   if(head == NULL)
      head = new ListNode(val);
   else
   {
      //  nonempty list
      ListNode *nodePtr;  //  used to traverse the list
      nodePtr = head;
      //  traverse list to locate last node
      while (nodePtr ->next != NULL)
         nodePtr = nodePtr->next;
      //  ptr->next is NULL so nodePtr points to last node
      //  create new node and make it successor of what
      //  is now the last node
      nodePtr->next = new ListNode(val);
   }
}


///////////////////////////////////////////
//  displayList shows the value
//  stored in each node of the linked list
//  pointed to by the head.
///////////////////////////////////////////

template<class T>
void LinkedList<T>::displayList()
{
   this->showList(this->head);
}


///////////////////////////////////////////////
//  The insertNode function inserts a node with
//  val copied to its value member.
///////////////////////////////////////////////

template<class T>
void LinkedList<T>::insertNode(T val)
{
   ListNode *nodePtr, *previousNodePtr;

   if (head == NULL  || head->value >= val)
   {
      // new node goes at beginning
      head = new ListNode(val, head);
   }
   else
   {
      previousNodePtr = head;
      nodePtr = head->next;
      //  find the insertion point
      while(nodePtr != NULL && nodePtr->value < val)
      {
         previousNodePtr = nodePtr;
         nodePtr = nodePtr->next;
      }
      //  creates the new node and inserts it
      //  just before nodePtr
      previousNodePtr->next= new ListNode(val, nodePtr);
   }
}

/////////////////////////////////////////////////////
//  The deleteNode function searches for a node
//  with val as its value, the node, if found, is
//  delete from the list and from memory
/////////////////////////////////////////////////////

template<class T>
void LinkedList<T>::deleteNode(T val)
{
   ListNode *nodePtr, *previousNodePtr;

   //  if the list is empty, do nothing
   if(!head)
      return;
   //  determine if the first node is the one to delete
   if(head->value < val)
   {
      nodePtr = head;
      head = head->next;
      delete nodePtr;
   }
   else
   {
      //  initialize nodePtr to head of list
      nodePtr = head;

      //  skip all nodes whose value member is
      //  not equal to val
      while(nodePtr != NULL && nodePtr->value != val)
      {
         previousNodePtr = nodePtr;
         nodePtr = nodePtr->next;
      }
      //  Link the previous node to the node after
      //  nodePtr, then delete nodePtr
      if(nodePtr)
      {
         previousNodePtr->next = nodePtr->next;
         delete nodePtr;
      }
   }
}

////////////////////////////////////////////////
//  Destructor
//  This function deletes every node in the list
////////////////////////////////////////////////

template<class T>
LinkedList<T>::~LinkedList()
{
   ListNode *nodePtr, *nextNodePtr;
   
   nodePtr = head;
   while(nodePtr != NULL)
   {
      nextNodePtr = nodePtr->next;
      delete nodePtr;
      nodePtr = nextNodePtr;
   }
}


Top
  Profile  
 
 Post subject:
PostPosted: Sun Apr 24, 2005 3:43 pm 
Clawhammer
Clawhammer
User avatar

Joined: Sat Jun 26, 2004 2:16 pm
Posts: 3600
Final class code as of now. I need to figure out how to setup a delete member function correctly. It needs to accept a value and any node less than that it deletes.

Code:
#include <iostream>
using namespace std;

template <class T>
class LinkedList
{
   private:
      //  Declare a type for the list node
      class ListNode
      {
         friend LinkedList<T>;
         T value;
         ListNode *next;
         ListNode(T value1, ListNode *next1 = NULL)
         {
            value = value1;
            next = next1;
         }
      };
      ListNode *head;
     void showList(ListNode*);
   public:
      LinkedList()
      {
         head = NULL;
      }
      ~LinkedList();
      void appendNode(T);
      void insertNode(T);
      void deleteNode(T);
      void displayList();
};

template<class T>
void LinkedList<T>::showList(ListNode * nodePtr)
{
   //ListNode *nodePtr;

   //nodePtr = head;
   if(nodePtr != NULL)
   {
      cout<<nodePtr->value<<endl;
      this->showList(nodePtr->next);
   }
}

///////////////////////////////////////////////////////
//  appendNode appends a node containing the value
//  val passed as a parameter to the end of the list.
///////////////////////////////////////////////////////

template<class T>
void LinkedList<T>::appendNode(T val)
{
   if(head == NULL)
      head = new ListNode(val);
   else
   {
      //  nonempty list
      ListNode *nodePtr;  //  used to traverse the list
      nodePtr = head;
      //  traverse list to locate last node
      while (nodePtr ->next != NULL)
         nodePtr = nodePtr->next;
      //  ptr->next is NULL so nodePtr points to last node
      //  create new node and make it successor of what
      //  is now the last node
      nodePtr->next = new ListNode(val);
   }
}


///////////////////////////////////////////
//  displayList shows the value
//  stored in each node of the linked list
//  pointed to by the head.
///////////////////////////////////////////

template<class T>
void LinkedList<T>::displayList()
{
   this->showList(this->head);
}


///////////////////////////////////////////////
//  The insertNode function inserts a node with
//  val copied to its value member.
///////////////////////////////////////////////

template<class T>
void LinkedList<T>::insertNode(T val)
{
   ListNode *nodePtr, *previousNodePtr;

   if (head == NULL  || head->value <= val)
   {
      // new node goes at beginning
      head = new ListNode(val, head);
   }
   else
   {
      previousNodePtr = head;
      nodePtr = head->next;
      //  find the insertion point
      while(nodePtr != NULL && nodePtr->value > val)
      {
         previousNodePtr = nodePtr;
         nodePtr = nodePtr->next;
      }
      //  creates the new node and inserts it
      //  just before nodePtr
      previousNodePtr->next= new ListNode(val, nodePtr);
   }
}
/////////////////////////////////////////////////////
//  The deleteNode function searches for a node
//  with val less than its value, the node, if found, is
//  delete from the list and from memory
/////////////////////////////////////////////////////

template<class T>
void LinkedList<T>::deleteNode(T val)
{
   ListNode *nodePtr, *previousNodePtr;

   //  if the list is empty, do nothing
   if(!head)
      return;
   //  determine if the first node is the one to delete
   if(head->value < val)
   {
      nodePtr = head;
      head = head->next;
      delete nodePtr;
   }
   else
   {
      //  initialize nodePtr to head of list
      nodePtr = head;

      //  skip all nodes whose value member is
      //  not equal to val
      while(nodePtr != NULL && nodePtr->value >= val)
      {
         previousNodePtr = nodePtr;
         nodePtr = nodePtr->next;
      }
      //  Link the previous node to the node after
      //  nodePtr, then delete nodePtr
      if(nodePtr)
      {
         previousNodePtr->next = nodePtr->next;
         delete nodePtr;
      }
   }
}



////////////////////////////////////////////////
//  Destructor
//  This function deletes every node in the list
////////////////////////////////////////////////

template<class T>
LinkedList<T>::~LinkedList()
{
   ListNode *nodePtr, *nextNodePtr;
   
   nodePtr = head;
   while(nodePtr != NULL)
   {
      nextNodePtr = nodePtr->next;
      delete nodePtr;
      nodePtr = nextNodePtr;
   }
}





Code to look at
Code:

/////////////////////////////////////////////////////
//  The deleteNode function searches for a node
//  with val less than its value, the node, if found, is
//  delete from the list and from memory
/////////////////////////////////////////////////////

template<class T>
void LinkedList<T>::deleteNode(T val)
{
   ListNode *nodePtr, *previousNodePtr;

   //  if the list is empty, do nothing
   if(!head)
      return;
   //  determine if the first node is the one to delete
   if(head->value < val)
   {
      nodePtr = head;
      head = head->next;
      delete nodePtr;
   }
   else
   {
      //  initialize nodePtr to head of list
      nodePtr = head;

      //  skip all nodes whose value member is
      //  not equal to val
      while(nodePtr != NULL && nodePtr->value >= val)
      {
         previousNodePtr = nodePtr;
         nodePtr = nodePtr->next;
      }
      //  Link the previous node to the node after
      //  nodePtr, then delete nodePtr
      if(nodePtr)
      {
         previousNodePtr->next = nodePtr->next;
         delete nodePtr;
      }
   }
}


Top
  Profile  
 
 Post subject:
PostPosted: Sun Apr 24, 2005 6:15 pm 
In the lab!
In the lab!
User avatar

Joined: Sun Jun 06, 2004 10:47 am
Posts: 831
Location: Secret Laboratory
Code:
#include <iostream>
using namespace std;

template <class T>
class LinkedList
{
   private:
      //  Declare a type for the list node
      class ListNode
      {
         friend LinkedList<T>;
         T value;
         ListNode *next;
         ListNode(T value1, ListNode *next1 = NULL)
         {
            value = value1;
            next = next1;
         }
      };
      ListNode *head;
     void showList(ListNode*);
   public:
      LinkedList()
      {
         head = NULL;
      }
      ~LinkedList();
      void appendNode(T);
      void insertNode(T);
      void deleteNode(T);
      void displayList();
};

template<class T>
void LinkedList<T>::showList(ListNode * nodePtr)
{
   //ListNode *nodePtr;

   //nodePtr = head;
   if(nodePtr != NULL)
   {
      cout<<nodePtr->value<<endl;
      this->showList(nodePtr->next);
   }
}

///////////////////////////////////////////////////////
//  appendNode appends a node containing the value
//  val passed as a parameter to the end of the list.
///////////////////////////////////////////////////////

template<class T>
void LinkedList<T>::appendNode(T val)
{
   if(head == NULL)
      head = new ListNode(val);
   else
   {
      //  nonempty list
      ListNode *nodePtr;  //  used to traverse the list
      nodePtr = head;
      //  traverse list to locate last node
      while (nodePtr ->next != NULL)
         nodePtr = nodePtr->next;
      //  ptr->next is NULL so nodePtr points to last node
      //  create new node and make it successor of what
      //  is now the last node
      nodePtr->next = new ListNode(val);
   }
}


///////////////////////////////////////////
//  displayList shows the value
//  stored in each node of the linked list
//  pointed to by the head.
///////////////////////////////////////////

template<class T>
void LinkedList<T>::displayList()
{
   this->showList(this->head);
}


///////////////////////////////////////////////
//  The insertNode function inserts a node with
//  val copied to its value member.
///////////////////////////////////////////////

template<class T>
void LinkedList<T>::insertNode(T val)
{
   ListNode *nodePtr, *previousNodePtr;

   if (head == NULL  || head->value <= val)
   {
      // new node goes at beginning
      head = new ListNode(val, head);
   }
   else
   {
      previousNodePtr = head;
      nodePtr = head->next;
      //  find the insertion point
      while(nodePtr != NULL && nodePtr->value > val)
      {
         previousNodePtr = nodePtr;
         nodePtr = nodePtr->next;
      }
      //  creates the new node and inserts it
      //  just before nodePtr
      previousNodePtr->next= new ListNode(val, nodePtr);
   }
}
/////////////////////////////////////////////////////
//  The deleteNode function searches for a node
//  with val less than its value, the node, if found, is
//  delete from the list and from memory
/////////////////////////////////////////////////////

template<class T>
void LinkedList<T>::deleteNode(T val)
{
   ListNode *nodePtr, *previousNodePtr, *tempPtr;

   //  if the list is empty, do nothing
   if(!head)
      return;
   //  determine if the first node is the one to delete
   if(head->value < val)
   {
      nodePtr = head;
      head = head->next;
      delete nodePtr;
   }
   else
   {
      //  initialize nodePtr to head of list
      nodePtr = head;

      //  skip all nodes whose value member is
      //  not equal to val
      while(nodePtr != NULL && nodePtr->value != val)
      {
         previousNodePtr = nodePtr;
         nodePtr = nodePtr->next;
      }
     
     //  Link the previous node to the node after
      //  nodePtr, then delete nodePtr
      if(nodePtr)
      {
     if(val == 0) {
        //nodePtr = nodePtr->next;
        while(nodePtr != NULL)
        {
              tempPtr = nodePtr;
           previousNodePtr->next = nodePtr->next;
              nodePtr = nodePtr->next;
           delete tempPtr;
        }
     } else {
       
        previousNodePtr->next = nodePtr->next;
         delete nodePtr;
      }
     }
   }
}


Top
  Profile  
 
 Post subject:
PostPosted: Sun Apr 24, 2005 6:18 pm 
Clawhammer
Clawhammer
User avatar

Joined: Sat Jun 26, 2004 2:16 pm
Posts: 3600
Working!!!

Code:
/////////////////////////////////////////////////////
//  The deleteNode function searches for a node
//  with val less than its value, the node, if found, is
//  delete from the list and from memory
/////////////////////////////////////////////////////

template<class T>
void LinkedList<T>::deleteNode(T val)
{
   ListNode *nodePtr, *previousNodePtr;

   //  if the list is empty, do nothing
   if(!head)
      return;
   //  determine if the first node is the one to delete
   if(head->value < val)
   {
      nodePtr = head;
      head = head->next;
      delete nodePtr;
   }
   else
   {
      //  initialize nodePtr to head of list
      nodePtr = head;

      //  skip all nodes whose value member is
      //  not equal to val
      while(nodePtr != NULL && nodePtr->value >= val)
      {
         previousNodePtr = nodePtr;
         nodePtr = nodePtr->next;
      }
      //  Link the previous node to the node after
      //  nodePtr, then delete nodePtr
      while(nodePtr)
      {
         previousNodePtr->next = nodePtr->next;
         delete nodePtr;
       nodePtr=previousNodePtr->next;
      }
   }
}



Top
  Profile  
 
 Post subject:
PostPosted: Sun Apr 24, 2005 7:24 pm 
Clawhammer
Clawhammer
User avatar

Joined: Sat Jun 26, 2004 2:16 pm
Posts: 3600
Code:
#include <iostream>
using namespace std;

template <class T>
class LinkedList
{
   private:
      //  Declare a type for the list node
      class ListNode
      {
         friend LinkedList<T>;
         T value;
         ListNode *next;
         ListNode(T value1, ListNode *next1 = NULL)
         {
            value = value1;
            next = next1;
         }
      };
      ListNode *head;
     void showList(ListNode*);
    int length(ListNode*);
   public:
      LinkedList()
      {
         head = NULL;
      }
      ~LinkedList();
     int numNodes()
     { return length(head);};
      void appendNode(T);
      void insertNode(T);
      void deleteNode(T);
      void displayList();
};

template<class T>
void LinkedList<T>::showList(ListNode * nodePtr)
{
   ListNode *nodePtr;

   nodePtr = head;
   if(nodePtr != NULL)
   {
      cout<<nodePtr->value<<endl;
      this->showList(nodePtr->next);
   }
}

///////////////////////////////////////////////////////
//  appendNode appends a node containing the value
//  val passed as a parameter to the end of the list.
///////////////////////////////////////////////////////

template<class T>
void LinkedList<T>::appendNode(T val)
{
   if(head == NULL)
      head = new ListNode(val);
   else
   {
      //  nonempty list
      ListNode *nodePtr;  //  used to traverse the list
      nodePtr = head;
      //  traverse list to locate last node
      while (nodePtr ->next != NULL)
         nodePtr = nodePtr->next;
      //  ptr->next is NULL so nodePtr points to last node
      //  create new node and make it successor of what
      //  is now the last node
      nodePtr->next = new ListNode(val);
   }
}


///////////////////////////////////////////
//  displayList shows the value
//  stored in each node of the linked list
//  pointed to by the head.
///////////////////////////////////////////

template<class T>
void LinkedList<T>::displayList()
{
   this->showList(this->head);
}


///////////////////////////////////////////////
//  The insertNode function inserts a node with
//  val copied to its value member.
///////////////////////////////////////////////

template<class T>
void LinkedList<T>::insertNode(T val)
{
   ListNode *nodePtr, *previousNodePtr;

   if (head == NULL  || head->value <= val)
   {
      // new node goes at beginning
      head = new ListNode(val, head);
   }
   else
   {
      previousNodePtr = head;
      nodePtr = head->next;
      //  find the insertion point
      while(nodePtr != NULL && nodePtr->value > val)
      {
         previousNodePtr = nodePtr;
         nodePtr = nodePtr->next;
      }
      //  creates the new node and inserts it
      //  just before nodePtr
      previousNodePtr->next= new ListNode(val, nodePtr);
   }
}
/////////////////////////////////////////////////////
//  The deleteNode function searches for a node
//  with val less than its value, the node, if found, is
//  delete from the list and from memory
/////////////////////////////////////////////////////

template<class T>
void LinkedList<T>::deleteNode(T val)
{
   ListNode *nodePtr, *previousNodePtr;

   //  if the list is empty, do nothing
   if(!head)
      return;
   //  determine if the first node is the one to delete
   if(head->value < val)
   {
      nodePtr = head;
      head = head->next;
      delete nodePtr;
   }
   else
   {
      //  initialize nodePtr to head of list
      nodePtr = head;

      //  skip all nodes whose value member is
      //  not equal to val
      while(nodePtr != NULL && nodePtr->value >= val)
      {
         previousNodePtr = nodePtr;
         nodePtr = nodePtr->next;
      }
      //  Link the previous node to the node after
      //  nodePtr, then delete nodePtr
      while(nodePtr)
      {
         previousNodePtr->next = nodePtr->next;
         delete nodePtr;
       nodePtr=previousNodePtr->next;
      }
   }
}



////////////////////////////////////////////////
//  Destructor
//  This function deletes every node in the list
////////////////////////////////////////////////

template<class T>
LinkedList<T>::~LinkedList()
{
   ListNode *nodePtr, *nextNodePtr;
   
   nodePtr = head;
   while(nodePtr != NULL)
   {
      nextNodePtr = nodePtr->next;
      delete nodePtr;
      nodePtr = nextNodePtr;
   }
}




//////////////////////////////////////////
//  Counts the number of nodes in the list
//////////////////////////////////////////

template<class T>
int LinkedList::length(ListNode *nodePtr)
{
   if(nodePtr == NULL)
      return 0;
   else
      return 1 + length(nodePtr->next);
}






Top
  Profile  
 
 Post subject:
PostPosted: Sun Apr 24, 2005 8:20 pm 
Clawhammer
Clawhammer
User avatar

Joined: Sat Jun 26, 2004 2:16 pm
Posts: 3600
Code:
//////////////////////////////////////////
//  Counts the number of nodes in the list
//////////////////////////////////////////

template<class T>
void LinkedList<T>::numNodes()
{
   float count = 0.0;
   ListNode *nodePtr;
   nodePtr=head;
   while(nodePtr != NULL)
   {
      count++;
      nodePtr = nodePtr->next;
   }
   divide = count;
   cout<<"   "<<count<<endl;
}



//////////////////////////////////////////
//  Totals up the values in the node
//////////////////////////////////////////

template<class T>
void LinkedList<T>::totalNodes()
{
   float add_up = 0;
   ListNode *nodePtr;
   nodePtr=head;
   while(nodePtr != NULL)
   {
      add_up = add_up+ nodePtr->value;
      nodePtr = nodePtr->next;
   }
   total=add_up;

}


//////////////////////////////////////////
//  Returns the average
//////////////////////////////////////////

template<class T>
float LinkedList<T>::avgNodes()
{

   totalNodes();
   numNodes();
   return total/divide;
}


Top
  Profile  
 
 Post subject:
PostPosted: Sun Apr 24, 2005 8:26 pm 
Clawhammer
Clawhammer
User avatar

Joined: Sat Jun 26, 2004 2:16 pm
Posts: 3600
Code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include "LinkedList.h"
using namespace std;

////////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////////////



int main()
{

   ofstream ofile;
   ifstream infile;
   LinkedList<float> list;
   float val;


   //  Opens data files for input and output
   ofile.open("output.txt",ios::app);
   infile.open("LinkedList.txt",ios::in);
   infile>>val;
   list.appendNode(val);

   while(!infile.eof())
   {
      infile>>val;
      list.insertNode(val);

   }

   list.displayList();
   cout<<endl;
   float total_avg = list.avgNodes();
   cout<<endl<<total_avg<<endl;

   list.deleteNode(0);

   list.displayList();
   float positive_avg = list.avgNodes();
   cout<<endl<<positive_avg<<endl;




   
   
   
   infile.close();
   ofile.close();

   return 0;
}





Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group