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

  • SEARCH
  • Home
  • 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 9120965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:45:00+00:00 2026-06-17T05:45:00+00:00

The two lines of code at the bottom tail = head; tail->next= NULL; causes

  • 0

The two lines of code at the bottom tail = head;
tail->next= NULL;
causes the program to crash, when I call the extractMin() function. If i comment them out, everything is happening as supposed. Is this happening cause they are pointing to addresses in memory that has been freed?

The only clue the compiler gives me is this:EXC_BAD_ACCESS (code=2, address=0x0). I notice immediately the address being 0x0 so there is a problem there, but what exactly?

string LinkedListPQueue::extractMin() {
    if (isEmpty())
        error("Tried to dequeue from epmpty queue!");

    cell *toBeDeleted = head;   //pointer to this head
    string value = head->value; //get value of this head
    head = head->next;          //move so this head is not the same as the one to be deleted
    delete toBeDeleted;         //delete previous head.
    return value;

}


/* Implementation notes: enqueue
 * -----------------------------
 * We have to search to find the proper position, which can be a bit tricky with
 * a singly-linked list.  We walk two parallel pointers, one a step behind the other,
 * until we find the correct position to insert the new cell, which we then splice
 * into place. Note the special case of inserting at the head. Alternatively, this
 * operation could work recursively.
 */
void LinkedListPQueue::enqueue(const string& elem) {
    cell *cur, *prev, *newOne = new cell;

    newOne->value = elem;


    for (prev = NULL, cur = head; cur != NULL; prev=cur, cur = cur->next) {
        if (elem > cur->value) break;
    }
    newOne->next = cur;
    if (prev) {
        prev->next = newOne;
        logSize++; 
    } else {
        head = newOne;
        tail = head;
        tail->next= NULL;
        logSize++;
    }
  • 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-17T05:45:02+00:00Added an answer on June 17, 2026 at 5:45 am

    Your else clause is broken. If prev was null, then you are trying to insert before the first element.

    else {
      cell *oldHead = head;
      head = newOne;
      head->next = oldHead;
      logSize++;
    }
    

    Setting tail->next = NULL is the core error.

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

Sidebar

Related Questions

The following two lines of code load an image from an SVG file and
I have the following two lines of code: var BadResult = (100).ToString(B, new CustomFormatter
I was reading another question pertaining the efficiency of two lines of code, and
Could someone please explain the difference between the following two lines of code: 1.
For the following two lines of C++ code map<string, vector<size_t> >::iterator beg = mapper.begin();
What is the difference between the two marked lines in the code below? template
I have a chart (code to replicate will be below) that has two lines
In Windows 7, and Java 1.7 (or if 1.6, comment-out the two lines indicated),
I have two links called Read More (bottom of code) that contain different values.
I'm trying to position two lines of text next to a single word like

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.