Hey all,

I've used this site quite a bit as a reference, and couldn't find a solution that worked for me. So I became a member. :)

Anyway, I can't get my insert function to...function when I go to add a node. The node is NOT to be just added to the beginning or end, but it must be inserted at the current location (there is a current pointer in there). The program as a whole is an introduction to Singly Linked Lists, so I can't use a "previous" pointer for anything. It is broken into three files, list.h, list.cpp, and main.cpp (They appear in that order). Any help with this would be much appreciated.

list.h

#ifndef LIST_H
#define LIST_H

#include <cstdlib>
#include <iostream>

class list
{
    private:
        struct node
        //declaration of the nodes
        {
            int info;
              //node's data contents
            node *link;
              //stores the address of the next
              //node in the linked list
        };
        
    public:
        list();
          //constructor
        ~list();
          //destructor
        void insert(const int);
          //Function to add a node after current
          //node with the value provided
        bool search();
          //search the list for value provided
          //return true if found, -1 if empty
        void removenode();
          //remove current node, return -1 if empty
        void display();
          //print out the contents of the list
        list operator<<(const list);
          //print all values of the list, 5 per line
        
    protected:
        node *current; //current element
        node *head;    //first element
        node *last;    //last element

};

list.cpp

#include <cstdlib>
#include <iostream>
#include "list.h"

using namespace std;

list::list()
{
    head->info = 0;
    head->link = 0;
}

list::~list()
{
    node *temp;
    while(head != 0)
    {
        temp = head;
        head = head->link;
        delete temp;
    }
    last = 0;
}

void list::display()
{
    if (head->link==0)
    {
        cout<<"The list is empty.\n\n";
        return;
    }
    int count;
    node *temp;
    temp->link=head->link;
    while (temp->link != 0)
    {
        //cout << "\nNode " + count ": " + temp->info;
        count++;
        temp->link=temp->link->link;
    }
}

/*************************
where the program currently hangs
***************************/
void list::insert(int newvalue)
{
    node *newnode;
    newnode -> info = newvalue;
    
    if (head->link==0)
    {
        head->link=newnode;
    }
    else
    {
        newnode->link=current->link;
        current->link=newnode;
    }
    current->link = newnode;
}

void list::removenode()
{
    if (head->link==0)
    {
        cout<<"The list is empty.\n\n";
        return;
    }
    node *temp;
    temp->link=head->link;
    while (temp->link != 0)
    {
        if(current->link==temp->link)
        {
            temp->link=current->link->link;
            break;
        }
        temp->link=temp->link->link;
    }
    removenode();
    current->link=temp;
}

bool list::search()
{
    if (head->link==0)
    {
        cout<<"The list is empty.\n\n";
        return false;
    }
    int searchval;
    cout << "\nWhat value would you like to search for?\n";
    cin >> searchval;
    node *temp;
    temp->link=head->link;
    while (temp->link != 0)
    {
        if(temp->info==searchval)
        {
            current->link=temp;
            return true;
        }
        temp->link=temp->link->link;
    }
    return false;
}

main.cpp

#include <cstdlib>
#include <iostream>
#include "list.h"

void menu();
//Prints out all of the menu options

using namespace std;

int main()
{
    //data
    int choice, value;
    bool cont=true;
    
    list mylist;

    do
    {
        cout << "What would you like to do?";
        menu();
        cin >> choice;

        switch (choice)
        {
            case 1:
                cout << "\nWhat value would you like to enter?\n";
                cin >> value;
                mylist.insert(value);
                break;
            case 2:
                //cout << "The node containing " + p->info + "was removed.";
                mylist.removenode();
                break;
            case 3:
                mylist.display();
                break;
            case 4:
                mylist.search();
                break;
            case 5:
                cont = false;
                break;
            default:
                cout << "\nInvalid input, please try again.\n";
                break;
        }
    }while(cont);
    
    system("PAUSE");
    return 0;
    
}

void menu()
{
    cout << "\n(1) Insert Node";
    cout << "\n(2) Remove Current Node";
    cout << "\n(3) View List";
    cout << "\n(4) Search List";
    cout << "\n(5) Quit" << endl;
}

Line 48:

node *newnode;

node is just a pointer, which points nothing in particular. Trying to dereference it may result in any strange consequences imaginable. You need to initialize it with

node *newnode = new node();

Regarding a temp nodes in other functions, you should initialize them as well, but in a different manner:

node *temp = head;

Can you see the difference and the reason why they are different?

For the first two snippets, I do see the difference, but im not sure why the change would affect anything.

And the third, all that effectively does is combine two lines of my code into one, right? As far as functionality it wouldn't do anything?

Also, I hit another error. In list.cpp on line 37 there is a cout statement that throws an error "expected ';' before string constant". (Its commented out, not sure why)

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.