Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 03-30-2005, 07:10 PM   #1 (permalink)
 
Super Techie

Join Date: Jan 2005

Posts: 295

gab00n

Default InsertAfter problem - C++

I have this program that has to accept a string from the user, then a second one and a place to insert the string into the first string. Both strings will be linked lists and i have to insert string 2 into string one where the user asked. The problem i am having is in the function insertafter. It was a worksheet that i had to complete the code, all i was given for the insertafter function was three pointers. I have been looking at it for a while and can't seem to figure out what is causing the problem. It is probably something stupid but i just can't see it. Any help will be most appreciated, thanks.

Code:
#include <iostream>
using namespace std;

struct StringRec
{
       char theCh;
       StringRec* nextCh;
};
typedef StringRec* StrPtr;

class String
{
       public:
               String();
               ~String();
               void SetString();
               void OutputString();
               int GetLength()const;
       protected:
               StrPtr head;
               int strLength;
};

class StringModifier: public String
{
       public:
               StringModifier();
               ~StringModifier();
               void InsertAfter(StringModifier& subStr, int num);
};

int main()
{

       StringModifier str1, str2;
       int num = 0;

       str1.SetString();
       cout<<endl<<"The string before modification: ";
       str1.OutputString();
       cout<<endl;

       str2.SetString();
       cout<<"\nEnter place to insert string: ";
       cin>>num;
       cout<<endl;
       str2.OutputString();
       cout<<endl;

       str1.InsertAfter(str2, num);
       cout<<endl;
       str1.OutputString();

       cout<<endl;

       return 0;
}

String::String(): head(NULL), strLength(0)
{

}

String::~String()
{
       StrPtr p;
       p = head;
       while(head != NULL)
       {
            delete p;
            p = NULL;
            head = head->nextCh;
            p = head;
       }
       delete p;
       p = NULL;
       head = NULL;
}

void String::SetString()
{
       StrPtr p, last;
       char tempCh;
       last = head;
       cout<<"\nEnter a string: ";
       cin.get(tempCh);
       while(tempCh != '\n')
       {
            p = new StringRec;
            p->theCh = tempCh;
            p->nextCh = NULL;
            strLength++;
            if(head == NULL)
            {
                head = p;
                last = p;
            }
            else
            {
                last->nextCh = p;
                last = p;
            }
            cin.get(tempCh);
       }

}

void String::OutputString()
{
       StrPtr p;
       p = head;
       while(p != NULL)
       {
               cout<<p->theCh;
               p = p->nextCh;
       }
       delete p;
}

int String::GetLength()const
{
       return strLength;
}

StringModifier::StringModifier():String()
{

}

StringModifier::~StringModifier()
{
       StrPtr p;
       p = head;
       while(head != NULL)
       {
           delete p;
           p = NULL;
           head = head->nextCh;
           p = head;
       }
       delete p;
       p = NULL;
}

void StringModifier::InsertAfter(StringModifier& subStr, int num)
{
       StrPtr p, q, r;
       p =  head;
       q = subStr.head;

       for(int i = 0; i<num; ++i)
       {
           p = p->nextCh;
       }
       r = p;
       r = r->nextCh;
       p->nextCh = q;

       while(q != NULL)
       {
           q = q->nextCh;
       }
       q->nextCh = r;
}

__________________
\"Today\'s scientists have substituted mathematics for experiments, and they wander off through equation after equation, and eventually build a structure which has no relation to reality.\" Nikola Tesla
gab00n is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On