Ok, I'm freaking lost... I can't figure out how to get the "location" variable to work. Its a basic program, you input numbers and it HAS to use a recursive function to insert the new node into the list (even tho I could write 2 lines of code) and well I'm just... needing help.

Header

typedef int item;
//typedef digit* ptrType;

class number
{
    public:
        number();    // constructor
        ~number();   // deconstructor
        // member functions
        void number::insert(item* &location, int newValue);
        bool isEmpty() const;
        int getLength() const;
    private:
        struct digit
        {
            item Value;    // a digit value, 0-9
            digit* location;
            struct digit* next;
            struct digit* prev;
        };
        typedef digit* ptrType;
        
        
        int size;
        digit *head;
        digit *tail;
        digit *curr;
        digit *find(int index) const;
};

CLASS

#include <iostream>
#include <cstddef>
#include <new>
#include "number.h"

using namespace std;


number::number() : size(0), head(NULL)
{
}
number::~number()
{
    while (!isEmpty())
        remove(NULL);
}
bool number::isEmpty() const
{
    return size == 0;
}
int number::getLength() const
{
    return size;
}
void number::insert(item &location, int newValue)
{
    if (location==NULL)
    { 
    head = location;
    }
    if (location->next == NULL) // end of list
    {
      location->next = newValue;
      break;
    }
    if (newValue > location->Value) && (newValue < location->next->Value)
    {
        digit *newPtr = new digit;
        newPtr->Value = newValue;
        newPtr->next = location->next;
        newPtr->prev = location;
        location->next->prev = newPtr;
        location->next = newPtr;
    }
    else
    {
        Insert(location->next, newValue);  // general case
    }
}

Main (Basic)

#include <iostream>
#include <string>
#include <cmath>
#include "number.h"

using namespace std;

void main()
{
    int Entry1 = 1;
    int Entry2 = 1;
    number N;
    digit location;
    while (Entry1 != 0)
    {
        cout << "Enter Numbers to be inserted (0 the end): ";
        cin >> Entry1;
        
        N.insert(N.tail, Entry1);
    }
}

Recommended Answers

All 3 Replies

>and it HAS to use a recursive function to insert the new node into the list
Just so you know, recursion is a piss poor way to work with linked lists. As for the algorithm, it would go something like this:

node *add_end ( node *list, int data )
{
  if ( list != NULL ) 
    list->next = add_end ( list->next, data );
  else {
    list = new node;
    list->data = data;
    list->next = NULL;
  }

  return list;
}

Since this is a homework assignment, I'll leave it up to you to figure out how it works. I can't do everything for you, ne? ;)

Thanks again Narue... but the problem isn't with the actual function... What I don't understand is node *add_end ( node *list, int data ) My class is all self study, and the issue is this stupid "text" book if you want to call it that. The portion on the head pointer being private actually says "The details of this scenario are left as an exersize"

#1 for the most part recursion is retarded... I had more issues trying to "dumb" myself down for most of the recursive problems (this one would be done except for this recursion crap)

#2 How can one expect to learn from a book when the book doesn't even give you any sort of examples? It's freakin stupid.

Back to my question at hand, to get the node *add_end ( node *list, int data ) to work, what would I put in main.cpp

for instance

typedef int ListItemType;
class number
{
    public:
        number();    // constructor
        ~number();   // deconstructor
        // member functions
        void Insert(int newValue);
        void Retrieve(int Locaton, int newValue);
        bool isEmpty() const;
        int getLength() const;
    private:
        struct digit
        {
            ListItemType Value;    // a digit value, 0-9
            ListItemType Index; // Location in node
            struct digit* next;
            struct digit* prev;
        };
        typedef digit* ptrType;
        int size;
        digit *head;
        digit *cur;
        digit *tail;
        digit *find(int index) const;
        digit *add_end ( digit *cur, int data );
};
#include <iostream>
#include <cstddef>
#include <new>
#include "number.h"

using namespace std;


number::number() : size(0), head(NULL)
{
}
number::~number()
{
    while (!isEmpty())
        remove(NULL);
}
number::digit *number::add_end ( digit *cur, int data )
{
  if ( cur != NULL ) 
    cur->next = add_end ( cur->next, data );
  else {
    cur = new digit;
    cur->Value = data;
    cur->next = NULL;
  }

  return cur;
}

bool number::isEmpty() const
{
    return size == 0;
}
int number::getLength() const
{
    return size;
}
void number::Retrieve(int Locaton, int newValue)
{

}
void number::Insert(int newValue)
{
    add_end(cur,newValue);
}
#include <iostream>
#include <string>
#include <cmath>
#include "number.h"

using namespace std;

void main()
{
    int newItem;
    int PointedItem;
    //int LoCa = 1;
    number N;
    do{
        cout<<"Enter numbers to be inserted (0 to End): ";
        cin>> newItem;
        if (newItem != 0)
        N.Insert(newItem);
    }
    while(newItem != 0);
}

>for the most part recursion is retarded...
Recursion is great, when it's a good solution. Most people use recursion for retarded things. The Fibonacci and Factorial examples in books can attest to that.

>How can one expect to learn from a book when the book doesn't even give you any sort of examples?
You can't. Get a better book. Tell me which one you have right now and I'll recommend something better.

>what would I put in main.cpp
With the same function, you would probably want to create another member function for the public interface and leave add_end private:

class list {
  node *head;
public:
  //...
  void add ( int data ) { head = add_end ( head, data ); }
private:
  node *add_end ( node *list, int data );
  //...
};

Then you can just call add from main:

N.add ( newItem );
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.