Solved

help in adding items to the front of  the list

Posted on 2004-09-19
9
216 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
  • 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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 50 total points
ID: 12305859
I think I adequately answered this question...
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

816 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now