Solved

help in adding items to the front of  the list

Posted on 2004-09-19
9
201 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
 
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

706 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

21 Experts available now in Live!

Get 1:1 Help Now