Solved

help in adding items to the front of  the list

Posted on 2004-09-19
9
224 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

829 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