Solved

help in adding items to the front of  the list

Posted on 2004-09-19
9
238 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

724 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