Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

help in adding items to the front of  the list

Posted on 2004-09-19
9
Medium Priority
?
245 Views
Last Modified: 2010-04-01
Please help me in correcting my concatenatech code and understanding why it doesnt work that way. I have a huge struggle with these linked lists.
Thanks
#include<iostream.h>
#include<string>
typedef int ListItemType;

class Newstring

{
private:

    struct digit // a node on the List
    {
      ListItemType item; // data item on the list
      digit *next; // pointer to the next node
      };

     digit *number;
     digit *head;    // Points to front end of list.
     digit *tail;    // Points to a node one past the end of list.
   
public:

Newstring():head(NULL),tail(NULL){}
     
void concatenatech ( ListItemType );
void display ();

~Newstring();
};


void Newstring::concatenatech (ListItemType n) {

      
      number = new digit;
      number->item=n;

if (head==NULL)

{
      head=number;
}      

else {
number->next=head;
head= number;
}

}

/*void Newstring::display ()
{
      digit *curnode;
    curnode = head;
    if(NULL == curnode)
    return;

   while(curnode !=NULL)
    {
         cout<<curnode->item;
         curnode = curnode->next ;
             
    }
cout<<endl;

}
*/

Newstring::~Newstring()

{

digit* pTemp;
while( head )
{
    pTemp = head->next;
     delete head;
     head = pTemp;
}

head = NULL;

}


int main ()

{

Newstring number1;

ListItemType n;

cout<<"Insert number: ";
cin>>n;
while (n!=-1)
{
cout<<"Insert number: ";
cin>>n;
number1.concatenatech(n);
}


      return 0;
}

0
Comment
Question by:Gipsy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
9 Comments
 
LVL 19

Expert Comment

by:mrwad99
ID: 12095208
To insert items, you should be doing:

void Newstring::concatenatech (ListItemType n)
{      
      digit* pNewDigit = new digit();

      pNewDigit->item = n;
      pNewDigit->next = NULL;

      if (head == NULL)      {
            head = pNewDigit;
            tail = pNewDigit;
      } else {
            tail->next = pNewDigit;
            tail = pNewDigit;
      }      
}
0
 
LVL 19

Expert Comment

by:mrwad99
ID: 12095218
Also, you don't need

 digit *number

in your class declaration.

By saying

  number = new digit;
  number->item=n;

if (head==NULL)
{
     head=number;
}    

what you were effectively doing is re-using the same pointer 'number' over and over without actually adding any new items to the list.  You need to create new objects with 'new' and add them into the list.  Hence your destructor is incorrect:

Newstring::~Newstring()
{
      digit* pTemp = head;

      while(pTemp)
      {
            head = head->next;
            delete pTemp;
            pTemp = head;
      }
      head = NULL;      
}

This was the source of your crashes.

Finally, to display the list:

void Newstring::display()
{
      digit* pFirst = head;

      while (pFirst) {
            cout << pFirst->item << endl;
            pFirst = pFirst->next;
      }
}

HTH
0
 

Author Comment

by:Gipsy
ID: 12095275
It looks like you are inserting at the end of the list,  I need to insert in the beginning.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 19

Expert Comment

by:mrwad99
ID: 12095418
Sorry, will alter code.  "concatenatech" sounds like concatenate, which does mean "append to the end" but none the less will get back shortly.
0
 
LVL 19

Expert Comment

by:mrwad99
ID: 12095554
To insert at the head instead of at the tail, use:

      digit* pNewDigit = new digit();
      pNewDigit->item = n;
      pNewDigit->next = NULL;

      if (head == NULL)      {
            head = pNewDigit;
            tail = pNewDigit;
      } else {
            digit* pCurrentHead = head;
            head = pNewDigit;
            pNewDigit->next = pCurrentHead;
      }

essentially all this does (if the head is not NULL) is store the current head in a temporary pointer, assign the head to be the new object then assign the new object's 'next' member to point to the old head (the temporary pointer).  There is no need to mess with the tail, it will be the same.

HTH
0
 
LVL 30

Expert Comment

by:Axter
ID: 12096028
FYI:
<iostream.h> is not part of the C++ standard.
 You should use the extensionless version instead.
#include<iostream>
0
 
LVL 19

Expert Comment

by:mrwad99
ID: 12096032
with

using namespace std;

added too

:)
0
 
LVL 19

Accepted Solution

by:
mrwad99 earned 150 total points
ID: 12305859
I think I adequately answered this question...
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question