include a member function named rangeSearch. The function should search the list for a specified range values, defined by two numbers. All value found to be within the range shall be returned a pointer to a list of Nodes in search range. If no value is found, the function should return a Null pointer.

// A class template for holding a linked list.
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <iostream>     // For cout and NULL
using namespace std;

template <class T>
class LinkedList
{
private:
   // Declare a structure for the list
   struct ListNode
   {
      T value;
      struct ListNode *next;
   }; 

   ListNode *head;   // List head pointer

public:
   LinkedList()   // Constructor
      { head = NULL; }
   ~LinkedList(); // Destructor
   void appendNode(T);
   void insertNode(T);
   void deleteNode(T);
   void displayList();
   int rangesearch(T);    // search function
};




template <class T>
void LinkedList<T>::appendNode(T newValue)
{
   ListNode *newNode, *nodePtr;

   // Allocate a new node & store newValue
   newNode = new ListNode;
   newNode->value = newValue;
   newNode->next = NULL;

   // If there are no nodes in the list
   // make newNode the first node
   if (!head)
      head = newNode;
   else  // Otherwise, insert newNode at end
   {
      // Initialize nodePtr to head of list
      nodePtr = head;

      // Find the last node in the list
      while (nodePtr->next)
         nodePtr = nodePtr->next;

      // Insert newNode as the last node
      nodePtr->next = newNode;
   }
}



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

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



template <class T>
void LinkedList<T>::insertNode(T newValue)
{
   ListNode *newNode, *nodePtr, *previousNode = NULL;

   // Allocate a new node & store newValue
   newNode = new ListNode;
   newNode->value = newValue;

   // If there are no nodes in the list
   // make newNode the first node
   if (!head)
   {
      head = newNode;
      newNode->next = NULL;
   }
   else  // Otherwise, insert newNode
   {
      // Initialize nodePtr to head of list and
      // previousNode to NULL.
      nodePtr = head;
      previousNode = NULL;

      // Skip all nodes whose value member is less
      // than newValue.
      while (nodePtr != NULL && nodePtr->value < newValue)
      {  
         previousNode = nodePtr;
         nodePtr = nodePtr->next;
      }

      // If the new node is to be the 1st in the list,
      // insert it before all other nodes.
      if (previousNode == NULL)
      {
         head = newNode;
         newNode->next = nodePtr;
      }
      else  // Otherwise, insert it after the prev. node.
      {
         previousNode->next = newNode;
         newNode->next = nodePtr;
      }
   }
}


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

   // If the list is empty, do nothing.
   if (!head)
      return;

   // Determine if the first node is the one.
   if (head->value == searchValue)
   {
      nodePtr = head->next;
      delete head;
      head = nodePtr;
   }
   else
   {
      // Initialize nodePtr to head of list
      nodePtr = head;

      // Skip all nodes whose value member is 
      // not equal to searchValue.
      while (nodePtr != NULL && nodePtr->value != searchValue)
      {  
         previousNode = nodePtr;
         nodePtr = nodePtr->next;
      }

      // If nodePtr is not at the end of the list, 
      // link the previous node to the node after
      // nodePtr, then delete nodePtr.
      if (nodePtr)
      {
         previousNode->next = nodePtr->next;
         delete nodePtr;
      }
   }
}



template <class T>
LinkedList<T>::~LinkedList()
{
   ListNode *nodePtr, *nextNode;

   nodePtr = head;
   while (nodePtr != NULL)
   {
      nextNode = nodePtr->next;
      delete nodePtr;
      nodePtr = nextNode;
   }
}


template <class T>
int LinkedList<T>::rangesearch(T val)
{
   int count = 1;
   ListNode *nodePtr;

   nodePtr = head;
   while (nodePtr)
   {
      if( nodePtr->value == val)
      {
         return count;
      }
      else
      {

         nodePtr = nodePtr->next;
         count++;
      }
   }
   return 0;
}

this is the code i have got..how do i modify the range search code...i have no cluse what the question is aking for ..can any one help me

It helps to boil down linked list to its simplest form,
without answering your homework, here is my example of simplified form

struct Node
{
    int value;
    Node * next;

    Node() : next(NULL), value(0) {  }
    Node(int v) : next(NULL), value(v) {  }
    ~Node() { delete next; }

    // add a value to the end of the list
    void push( int v )
    {
      if( next ) next->push(v);
      else next = new Node(v);
    }

    Node * search( int v )
    { // Create a new list of objects matching v
      Node * return(NULL);
      // this basically means search the value
          // if( v == value ) ... This is different for complex types, though the principle remains

      // if it matches this exact node, instantiate the return node (construct with v)

              // Don't forget that return can (and will often be) NULL
      // if(next&&return) return->next = next->search( v ); ... Recursive search
      // else if(next) return = next->search( v );

      // Don't forget to delete the object this function returns
      // delete on NULL does nothing.
    }
};

Edited 3 Years Ago by Unimportant

This article has been dead for over six months. Start a new discussion instead.