Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 895849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:37:28+00:00 2026-05-15T14:37:28+00:00

I have a problem either adding to or traversing a linked list. The main

  • 0

I have a problem either adding to or traversing a linked list. The main item class is used by another class but I can add the correct number of these but it looks like when I add more data to the list the app no longer works.

I am not sure exactly where the error is. I know that when I try to traverse the list the application crashes. Any ideas or any improvements would be appreciated.

I can make the crash not happen by changing the AddOccurence method to not do the while loop.

Do

void Item::AddOccurence(int Item,int placeInLine){
    ItemOccurence* ocr=myHead;
    if(ocr)
    {

    }

instead of

void Item::AddOccurence(int Item,int placeInLine){
    ItemOccurence* ocr=myHead;
    while(ocr)
    {

    }

Basically hitting the first node but no more.

I have an object that contains a list.
Here is the .h file
#include
using namespace std;

class ItemOccurence{
public:
    ItemOccurence(int line,int placeInLine,ItemOccurence* link=NULL) :myLine(line),myPlaceInLine(placeInLine),myLink(link){}

    int myLine;
    int myPlaceInLine;
    ItemOccurence* myLink;
};

class Item {
public:
    Item();
    Item(string Item,int line,int placeInLine);
    virtual ~Item();
    void deallocate(ItemOccurence* p);
    void AddOccurence(int Item,int placeInLine);
    string myItem;
    ItemOccurence* myHead;
private:
    bool isEmpty();
};

And the .cpp file

#include "Item.h"
#include <string>
#include<iostream>
using namespace std;
Item::Item(string Item,int line,int placeInLine):myHead(NULL){
    myItem=Item; 

    myHead= new ItemOccurence(line,placeInLine,NULL);
}

Item::Item():myHead(NULL){
myHead=0;
}

Item::~Item() {
    deallocate(myHead);
    myHead=0;
}

void Item::deallocate(ItemOccurence* p){
    ItemOccurence* tmp;
    while(p){
        tmp=p;
        p=p->myLink;
        delete tmp;
    }
}

void Item::AddOccurence(int Item,int placeInLine){
    ItemOccurence* ocr=myHead;
    while(ocr)
    {
          cout<<"orrucence head while adding " << myHead->myLine << " " << myHead->myPlaceInLine <<"\n";
        ocr=ocr->myLink;
    }

    myHead = new ItemOccurence(Item,placeInLine,myHead);

    return;
}

bool Item::isEmpty(){
    if(myHead)
        return false;
    else
        return true;
}

EDIT:
I updated AddOccurence to be.

void Item::AddOccurence(int line,int placeInLine){
    ItemOccurence* prev = myHead;
    ItemOccurence* curr = myHead->myLink;

    while(curr){
        prev=curr;
        curr=curr->myLink;
        }

    // insert new ItemOccurence
    cout<<"adding " <<line<< " and " << placeInLine <<"\n";
    prev->myLink = new ItemOccurence(line,placeInLine); 

    return;
}

But I am still crashing. I am trying to debug but not sure what to look for.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T14:37:29+00:00Added an answer on May 15, 2026 at 2:37 pm

    It’s very hard to tell what your code is trying to do. Unfortunately, the hard truth is, it’s pretty far away from “working”.

    Here’s a few hints:

    • Reconsider your classes. What is Item and ItemOccurrence? A linked list is a list. It has items. You should probably name it List and Item. If you want, you can do this in a single class representing Item (the List is only a special case of the front Item).
    • Each Item needs a string (the node data) and a next (the pointer to the next node).
    • If you use List, it will need to have a pointer to head (the first Item).
    • You don’t need to store placeInLine. placeInLine should only be used when searching for the place to insert a new Item.
    • It’s unclear what ItemOccurrence::myLine is supposed to represent.
    • When you initialize myHead(NULL), you don’t need to set it to 0.
    • isEmpty() is not usually a private method.
    • The algorithm for adding a node is:
      • Loop until you find the place before it needs to be inserted.
        • It looks like you need to check for two things: “end of list” and “placeInLine”
      • Set new_node->next = current->next->next node.
      • Set current->next = new_node node.
      • Be aware of the special case where current->next is NULL.
    • Instead of if (x) return true; else return false;, it’s common to return x;
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 452k
  • Answers 452k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer you may make a property of dialog to keep the… May 15, 2026 at 9:17 pm
  • Editorial Team
    Editorial Team added an answer self can refer to the window object, but typically that's… May 15, 2026 at 9:17 pm
  • Editorial Team
    Editorial Team added an answer The inline keyword does not actually cause functions to be… May 15, 2026 at 9:17 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.