Q BgQuestion:

Guru
Karma Points: 2,026
Respect (91%):
posted by  Duke12 on 7/23/2008 1:39:41 AM  |  status: Closed  

link list c++..implementing the search in a function

Course Textbook Chapter Problem
N/A N/A N/A N/A
Question Details:
very busy at the time and need some help.

Implement the search functionality in a function declared as follows:
int search(char target) const;
The function accepts a value to search the list for as a parameter. If the value is found in the list return the index of its first occurance. For example, if the value is found in the first node return 0, if the value is found in the second node return 1, etc. If the value is not found in the list return -1. Change main.cpp to ask the user for a char and display that chars position in the linked list.

Main.cpp
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include "part1.h"

using namespace std;

int main()
{
    char name;
    ifstream infile;
    infile.open("demoTest.txt");
    part1 list;
    
if(!infile)
    cout << "Can not open file" << endl;
else
{
    infile >> name;

    while(infile)
    {
        list.insertNode(name);
        infile >> name;
    }

    cout << "The list now contains:" << endl;
    list.displayList();
    list.~part1();
}
    return 0;
}

Part1.cpp#include "part1.h"
#include <iostream>
#include <fstream>
using namespace std;

void part1::appendList(char num)
{
    listNode *newNode;
    listNode *nodePtr;

    newNode = new listNode;
    newNode->letter = num;
    newNode->next = NULL;

    if(!head)
        head = newNode;
    else
    {
        nodePtr = head;

        while(nodePtr->next)
            nodePtr = nodePtr->next;

        nodePtr->next = newNode;
    }
}


void part1::displayList() const
{
    listNode *nodePtr; // move through the list

    nodePtr = head; //position nodePtr at head of the list

    while(nodePtr)
    {
        cout << nodePtr->letter << endl;//display value

        nodePtr = nodePtr->next;//move to next node
    }
}

void part1::insertNode(char num)
{
    // get a new node to insert
    listNode *newNode;
    listNode *nodePtr; // to traverse the list
    listNode *previousNode = NULL;  // the previous node
    // allocate a new node
    newNode= new listNode;
    newNode->letter=num;

    if(!head)
    {
        head = newNode;
        newNode->next=NULL;
    }
    else
    {
        nodePtr = head;
        previousNode= NULL;
   
      while ( nodePtr!=NULL && toupper(nodePtr->letter) <= toupper (num))
      {
         if (nodePtr->letter >num&&toupper (nodePtr->letter) == toupper (num))   //current is the last in list
         {
               break;
         }
              previousNode  = nodePtr;
              nodePtr=nodePtr->next;
            
      }
        if(previousNode ==NULL)
        {
            head=newNode;
            newNode->next=nodePtr;
        }
        else
        {
            previousNode->next=newNode;
            newNode->next=nodePtr;
        }
         
      }

   }


part1::~part1()
{
    cout << "\nDeleting the list\n" << endl;
    do
    {
        listNode *nodePtr=head;
        cout << "Delete element " << nodePtr->letter << endl;
        head=head->next; //move to next
        delete nodePtr; //delete the node
    }while(head!=NULL);

    cout << "\nDeletion complete" << endl;
} // end

Part1.h
#ifndef PART1_H
#define PART1_H
#include <iostream>
using namespace std;

class part1
{
public:
    part1()
    {
        head = NULL;
    }
    void appendList(char);
    void insertNode(char);
    void displayList() const;
    ~part1();

private:
    struct listNode
    {
        char letter;
        struct listNode *next;
    };
    listNode *head;

};
#endif


I take the time to answer your question. Please take the time to rate it.
Bonus Point Alert! Earn +4 additional karma points for helping this annual member.

AAnswers:

Answer Question
(Cramster SME)
Moderator
posted by Charlie (Cramster SME) on 7/23/2008 3:52:06 AM  |  status: Live
Asker's Rating: Lifesaver   
Duke12's comment:
"thankyou"
Response Details:
 
Dear User
 
You need the add the following line of code to the definition of the listNode class in the header file.
       int search(char target) const;
Then add the following code in the part1.cpp at your convenient place.
 
For more information, you may visit

http://answerboard.cramster.com/computer-science-topic-5-290704-0.aspx
 
 
 
 
Answer Question
Ask New Question

Join Cramster's Community

Cramster.com brings together students, educators and subject enthusiasts in an online study community. With around-the-clock expert help and a community of over 100,000 knowledgeable members, you can find the help you need, whenever you need it. Join for free today » How Cramster is different than tutoring »