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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:29:36+00:00 2026-06-12T03:29:36+00:00

I am working with a double linked list and I have run into a

  • 0

I am working with a double linked list and I have run into a problem with my pop() function.

//QueueElement describe the block in the cache
typedef struct _queue_ele_
{
    char *content;   //the data of the block
    struct _queue_ele_ *prev;
    struct _queue_ele_ *next;
}QueueElement;

typedef struct _queue_
{
    int queue_len;
    int max_queue_size;
    QueueElement *head;
    QueueElement *tail;
}MyQueue;

The pop function works until there is an input of 2 elements ( I clear the queue by poping one by one and freeing the memory)

pop:

// head is removed and returned
QueueElement* pop(MyQueue* myqueue)
{
    // if empty
    if(myqueue->queue_len == 0) return NULL;

    QueueElement *p = myqueue->head;

    // if one element
    if(myqueue->queue_len == 1) 
    {
        myqueue->queue_len--;
        myqueue->head = NULL;
        myqueue->tail = NULL;

        return p;
    }
    else
    {
        myqueue->queue_len--;

        //remove the head from the queue
        myqueue->head = myqueue->head->prev;
        myqueue->head->next = NULL; //******************Seg Fault here

        p->prev = NULL;

        return p;
        }   
}

The error I get when there are two elements is a segmentation fault in line shown, but it works for queues with more. Why wont it let me assign NULL to myqueue->head->next???

  • 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-12T03:29:37+00:00Added an answer on June 12, 2026 at 3:29 am

    Change this:

    myqueue->head = myqueue->head->prev;
    myqueue->head->next = NULL; //******************Seg Fault here
    

    To:

    myqueue->head = myqueue->head->prev;
    if (myqueue->head != NULL) {
        myqueue->head->next = NULL;
    }
    

    It is likely that you are trying to dereference a NULL pointer. It also would appear that you may have a memory leak from not calling free on the nodes you are deleting, but it is possible you do that elsewhere in the code.

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

Sidebar

Related Questions

I have this (working) CPU code: #define NF 3 int ND; typedef double (*POT)(double
Hello I have a double linked list set up, and i have a search
I have been working with a doubly linked list. Everything works OK with the
I have a double linked list (queue) I have made on my own. I
I am working with a double linked list in C and I came across
I have a simple doubly linked list example that I'm working on, but for
I'm working on a problem which requires me to use the STL linked list
I'm working on a hybrid data structure that is an ordered double-linked list where
I'm having a problem with a doubly linked list so I have two questions.
I'm working on a linked list for school and I am getting a ton

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.