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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:36:31+00:00 2026-06-04T22:36:31+00:00

I was making a doubly-linked list in C and before it had three variables

  • 0

I was making a doubly-linked list in C and before it had three variables as its data (two ints and a bool after I threw in typedef int bool) and the two pointers next and prev. I inserted in the back with a function that roughly said

void insert(list *l, int x, int y, bool z)
{
    node *n = makeNode(x, y, z);
    if(isEmpty(*l))
        l->head = l->tail = NULL;
    else
    {
        l->tail->next = n;
        n->prev = l->tail;
        l->tail = n;
    }
}

And my makeNode function said something like:

node *makeNode(int x, int y, bool z)
{
    node *n = malloc(sizeof(n));
    n -> x = x;
    n -> y = y;
    n -> z = z;
    n -> next = NULL;
    n -> prev = NULL;
    return n;
}

So then I print it out with something that looked like:

void printList(list l)
{
    node *i;
    for(i = list.head; i != NULL; i = i -> next)
        printf("%d %d %d\n", i -> x, i -> y, i -> z);
    printf("\n");
}

That one worked perfectly, but then I had

void reversePrintList(list l)
{
    node *i;
    for(i = list.tail; i != NULL; i = i -> prev)
        printf("%d %d %d\n", i -> x, i -> y, i -> z);
    printf("\n");
}

And that one segfaulted. After playing around with the code, for some odd reason, while the “next” pointers remained intact every time, the “prev” pointers did not point to the nodes I originally had them pointing to, and apparently they weren’t pointing at NULL either.

Furthermore, earlier I was trying to implement it as a circular doubly-linked list and for some reason still the next pointers worked just fine but the prev pointers once again were pointing somewhere strange.

But what’s really weird about this is that when the nodes only contained one int, everything worked perfectly on both the linear and circular doubly-linked list. And when I do a few syntactic changes to translate the code to C++, it works perfectly with the bigger node. I looked all over and there was no code I wrote that was supposed to alter the prev pointers outside of the insert function. So what is going on to mess up the prev pointers and only the prev pointers when I used a bigger node in C?

Thanks!

Nerd With a Vengeance

P.S. Here are my structs

typedef struct node{
    int x;
    int y;
    bool z;
    struct node *next;
    struct node *prev;
} node;

typedef struct list{
    node *head;
    node *tail;
} list;
  • 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-04T22:36:33+00:00Added an answer on June 4, 2026 at 10:36 pm
    node *n = malloc(sizeof(n));
    

    This doesn’t make any sense. You want to allocate enough bytes to hold a node, not a pointer to a node. Try:

    node *n = malloc(sizeof node);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I coded for doubly linked list implementation in C. In that, after making insertion
Im trying to remove a single node from a doubly threaded linked list. What
To insert/delete a node with a particular value in DLL (doubly linked list) entire
Okay, I'm making a project that implements a Double Linked List using classes, templates
Making an adobe flex ui in which data that is calculated must use proprietary
Making a drop down list for my JSP and the values I'm entering won't
Well I'm making an program about payrolls and I'm stuck. In the program, after
I'm making a C program that needs to use two stacks. One needs to
I was making a Gauroud algorithm and when i had calculated point intensity on
Hi I'm making an app which uses only the broadcast receiver. It serves three

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.