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 8528509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:49:01+00:00 2026-06-11T08:49:01+00:00

I am writing a three file C++ program for my class. This program is

  • 0

I am writing a three file C++ program for my class. This program is ordered linked list. The program compiles but crashes when I attempt to insert (Run the program, select choice press enter, type an int to insert and press enter). Any help would be greatly appreciated.

Driver File:

#include "SortedLinkedList.h"
#include <iostream>
using namespace std;


int displayMenu();
void proccessChoice(int, SortedLinkedList&);

int main()
{
    SortedLinkedList sSList;
    int choice = displayMenu();

    do
    {
        if (choice != 3)
        {
            proccessChoice(choice, sSList);
        }
    } while (choice != 3);


    return 0;
}

void proccessChoice(int input, SortedLinkedList& l)
{
    switch(input)
    {
        case 1:
            int num;
            cout << "Please enter a int: ";
            cin >> num;
            l.addItem(num);
        break;
        case 2:
            l.popFirst();
        break;
    }


}

int displayMenu()
{
    int choice;

    cout << "menu" << endl;
    cout << "===========" << endl;
    cout << "1. add an int" << endl;
    cout << "2. Show Sorted Linked List" << endl;
    cout << "3. Exit" << endl;
    cin >> choice;
    cin.ignore();

    return choice;
}

Declaration File:

struct sslNode
{
    sslNode* next;
    int item;
};

class SortedLinkedList
{
private:
    sslNode* head;
    bool isEmpty ();

public:
    SortedLinkedList();
    ~SortedLinkedList();
    void addItem(int);
    int popFirst();
};

Implementation File:

#include <iostream>
using namespace std;
#include "SortedLinkedList.h"

SortedLinkedList::SortedLinkedList()
{
    head = NULL;
}

SortedLinkedList::~SortedLinkedList()
{
    sslNode *temp, *nextLink;
    nextLink = head;

    while(nextLink != NULL)
    {
        temp = nextLink->next;
        delete nextLink;
        nextLink = temp;
    }
}

bool SortedLinkedList::isEmpty()
{
    return (head == NULL);
}

void SortedLinkedList::addItem(int itemToInsert)
{
    sslNode* cur; 
    sslNode* prev;
    sslNode* newNode = new sslNode();
    newNode->item = itemToInsert;
    newNode->next = NULL;

    cur = head;
    prev = NULL;
    bool moreToSearch (cur != NULL);

    while (moreToSearch) //Find insertion point
    {
        if (cur->item > newNode->item) // while current location has a greater value then what needs to be inserted move pointers forward.
        {
            prev = cur;
            cur = cur->next;
            moreToSearch = (cur != NULL);
        } 
        else // if current loacation and what is to be inserted are equal or less then we have found the point of insertion 
        {
            moreToSearch = false;
        }
    }

    if (prev = NULL)
    {
        newNode->next = head->next;
        head = newNode;
    }
    else
    {
        prev->next = newNode;
        newNode->next = cur;
    }

    //Insert as only item in list
    //Insert in found location
}

int SortedLinkedList::popFirst()
{
    sslNode* first;
    first = head->next;
    head = head->next;
    int item = first->item;

    return item;
}
  • 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-06-11T08:49:02+00:00Added an answer on June 11, 2026 at 8:49 am

    Your problem is you forgot an =

    if (prev = NULL)
    {
        newNode->next = head->next;
        head = newNode;
    }
    else
    {
        prev->next = newNode;
        newNode->next = cur;
    }
    
    if(prev = NULL)
    

    should be

    if(prev == NULL)
    

    right now this is always false because prev becomes null which evaluates to false
    and then it fails at

    prev->next = newNode;
    

    because your are dereferencing the null pointer.

    You’ll also want to treat the case where head == NULL before trying to insert anything. Basically if head == NULL, head = newNode;

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a class to implement an algorithm. This algorithm has three levels
I am facing this problem while trying to run my java file by writing
I'm writing a program where each component has an inheritance structure has three levels...
I'm writing a topic-based publish/subscribe system in C++. My generic Event class has three
I am writing a program to find adapters, and have made a class called
Writing a simple program that will find exact duplicate files on my computer, but
I am writing a PHP program which reads file with file_get_contents then attempts to
I'm writing a program to allow a user to select a csv file, with
OK, I'm having some trouble writing multiple lines to a text file. the program
I am having some problems with writing a CSV file. The program, I made

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.