I am having a compiler error that causes a screen to pop up that says break or continue. I know it is something wrong with my program but I am at a lost as to what it is. I was just trying to test the first function as you can see from my main program at the bottom. Any help would be deeply appreciated I have been working non stop on this for days and haven't figured it out.
Here is my header file:

#include <iostream>
using namespace std;

struct Node 
{
    char cvalue;
    double dvalue;
    int keyvalue;
    Node *next;
};

Here is my .cpp file

#include "SimplelinkedListLab_2.h"


class SimpleLinkedList
{
private:
    Node *first; // This is the only member variable your are allowed!
public:
    SimpleLinkedList(); // Default constructor
    ~SimpleLinkedList(); // Destructor must delete all nodes in the list
    bool empty();// returns true if list is empty
    void insert (char, double, int);
    void remove(int);
    int listCount();
    bool getValue(int, char & c, double & d);
    void minValue( char & cval, double & dval);
    void maxValue(char & cval, double & dval);
    void displayList( ostream & out);
};


SimpleLinkedList ::SimpleLinkedList()
{
    first = new Node;
    first -> next = NULL;
}

SimpleLinkedList :: ~SimpleLinkedList()
{
    Node *temp = new Node;
    while (first != NULL)
    {
        temp = first -> next;
        delete temp;
    }
}

bool SimpleLinkedList :: empty()
{
    if (first -> next == NULL)
        return true;
    else
        return false;
}
void SimpleLinkedList :: insert( char c, double d, int key)
{
    char cvalue = c;
    double dvalue = d;
    int keyvalue = key;
    Node *tmp = new Node;
    tmp -> dvalue = dvalue;
    tmp -> cvalue = cvalue;
    tmp -> keyvalue = keyvalue;
    if (tmp == NULL)
        return;
    if (first == NULL || key < first -> keyvalue)
    {
        tmp -> next = first;
        first = tmp;
    }
    else
    {
        Node *pred = first;
        while (pred -> next != NULL && pred -> next -> keyvalue < key)
        {
            pred = pred -> next;
        }
        tmp -> next = pred -> next;
        pred -> next = tmp;
    }
    delete tmp;
}

void SimpleLinkedList :: remove(int key)
{
    Node *pred = NULL;
    Node *tmp = first;

    if (tmp == NULL)
        return;
    if (tmp -> keyvalue == key)
    {
        first = tmp -> next;
        delete tmp;
        return;
    }
    tmp = tmp -> next;
    while (tmp != NULL && tmp -> keyvalue != key)
    {
        pred = tmp;
        tmp = tmp -> next;
    }
    if (tmp != NULL)
    {
        pred -> next = tmp -> next;
        delete tmp;
    }
}

int SimpleLinkedList :: listCount()
{
    Node *tmp = first;
    int count = 0;
    while (tmp -> keyvalue != NULL)
    {
        count++;
    }
    delete tmp;
    return count;
}
// Returns the number of nodes in the list.
void SimpleLinkedList :: displayList( ostream & out)
{
    Node *tmp = first;
    while(tmp != NULL)
    {
        out << tmp -> cvalue << " "; 
        out << tmp -> dvalue << " ";
        out << tmp -> keyvalue << " ";
        tmp = tmp -> next;
    }
    cout << endl;
}
bool SimpleLinkedList :: getValue(int key, char & c, double & d)
{
    Node *tmp = first;

    while (tmp != NULL && tmp -> keyvalue != key)
    {
        tmp = tmp -> next;
    }
    c = tmp -> cvalue;
    d = tmp -> dvalue;
    if (tmp == NULL)
        return false;
    else
        return true;
}

void SimpleLinkedList :: minValue( char & cval, double & dval)
{
    Node *tmp = first;
    int min = cval;
    double dmin = dval;
    while (tmp != NULL)
    {
        if (cval > tmp -> cvalue)
            tmp = tmp -> next;
        else
            min = cval;
    }
    delete tmp;
    Node *tmp2 = first;
    while (tmp != NULL)
    {
        if (dval > tmp -> dvalue)
            tmp = tmp -> next;
        else
            dmin = dval;
    }
    delete tmp2;
}
void SimpleLinkedList :: maxValue(char & cval, double & dval)
{
    Node *tmp = first;
    int max = cval;
    double dmax = dval;
    while (tmp != NULL)
    {
        if (cval > tmp -> cvalue)
            tmp = tmp -> next;
        else
            max = cval;
    }
    delete tmp;
    Node *tmp2 = first;
    while (tmp != NULL)
    {
        if (dval > tmp -> dvalue)
            tmp = tmp -> next;
        else
            max = dval;
    }
    delete tmp2;
};


int main ()
{
    SimpleLinkedList list;
    list.insert('A', 1, 1);
    list.displayList(cout);
    system ("PAUSE");
}

Again any help would be much appreciated

Dani AI

Generated

The debugger hint from is useful: running under the debugger (step into the insert call and step line-by-line) will show where the access violation happens. correctly called out the constructor/destructor as key problems; several other logic and memory-management errors will also cause the crash (dangling pointers, infinite loops, and dereferencing NULL).

High‑priority fixes (conceptual):

  • Use a true empty list head (set first to NULL) or use a sentinel consistently. Mixing a allocated dummy node with code that assumes first can be NULL causes mismatched branches.
  • Do not delete a node immediately after inserting it. Deleting the node that was just linked produces a dangling pointer and will crash later.
  • The destructor must walk the list and delete each node once, advancing the traversal pointer before deleting.
  • Loop conditions must check the node pointer (tmp != NULL), not a field inside the node. Always advance the traversal pointer inside loops.
  • Never dereference a pointer before checking it for NULL (e.g., check the found node before assigning c and d).

Small, safe patterns to follow:

/* destructor */
Node* cur = first;
while (cur) {
    Node* next = cur->next;
    delete cur;
    cur = next;
}
first = nullptr;
/* insert (keep the new node alive) */
Node* newNode = new Node{c, d, key, nullptr};
if (!first || key < first->keyvalue) {
    newNode->next = first;
    first = newNode;
} else {
    Node* cur = first;
    while (cur->next && cur->next->keyvalue < key) cur = cur->next;
    newNode->next = cur->next;
    cur->next = newNode;
}
/* remove */
Node* prev = nullptr;
Node* cur = first;
while (cur && cur->keyvalue != key) { prev = cur; cur = cur->next; }
if (!cur) return;
if (!prev) first = cur->next; else prev->next = cur->next;
delete cur;

Other notes: listCount should iterate for (Node* p = first; p; p = p->next) ++count;. getValue must check the found node before accessing fields. minValue/maxValue should initialize from the first real node, then scan and update min/max. Use the debugger watch window to inspect pointer values after each operation and enable runtime heap/debug checks to catch double deletes or invalid writes early.

Recommended Answers

All 2 Replies

Using Visual Studio? Press F5 with the project open.

First, your constructor and destructor are bogus. This is how it should be:

SimpleLinkedList ::SimpleLinkedList()
{
    first = new Node;
    first -> next = NULL;

// Don't forget to initialize node's member fields. Unless
// Node has a default constructor that initializes them for
// you, they will contain random data values.
    first->cvalue = 0;
    first->dvalue = 0;
    first->keyvalue = 0;
}


SimpleLinkedList :: ~SimpleLinkedList()
{
// Implement a proper head-first deletion routine.
    while (first != 0)
    {
        Node* temp = first;
        first = first->next;
        delete temp;
    }
}

I'd look further, but this will get you started. In any case, you need to do a thorough analysis of your code.

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.