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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:20:46+00:00 2026-06-12T16:20:46+00:00

I more or less get the basic idea behind single linked list, but having

  • 0

I more or less get the basic idea behind single linked list, but having trouble with inserting elements in a doubly linked list.
Basically I am having trouble linking prev and next pointers to the appropriate nodes.
Thanks in advance for help. Here is how my code looks like.

LinkedList.h

template <class T>
class LinkedList{
      protected:
        LinkedListNode<T>* head;
      public:
        LinkedList():head(NULL){}
        ~LinkedList();
        void insert(const T& x);
};


//inserting
template <class T>
void LinkedList<T>::insert(const T& x) {
 LinkedListNode<T>* head = new LinkedListNode<T>(head->prev, x, head->next);
 if(head != NULL){
         head->prev = head->next;
         head->next = head;
  }       
}

LinkedListNode.h

class LinkedListNode{
      protected:
        LinkedListNode<T>* prev;
        T value;
        LinkedListNode<T>* next;
        LinkedListNode(LinkedListNode<T>* p, const T& x, LinkedListNode<T>* n):prev(p), value(x), next(n) {}
        ~doublyLinkedListNode();
        template <class S> friend class doublyLinkedList;
};

I tried modifying the insert function as following, but it gave segmentation fault.
What is wrong with my implementation?

template <class T>
void LinkedList<T>::insert(const T& x) {
 LinkedListNode<T>* head;
 if(head == NULL){
        head->value = x;
        head->prev = NULL;
        head->next = head;

 }
else{   LinkedListNode<T>* newnode;
        newnode->value = x;
        newnode->prev = head->next;
        newnode->next = newnode;
        head = newnode;
 }
  • 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-12T16:20:47+00:00Added an answer on June 12, 2026 at 4:20 pm

    You’re a victim of variable shadowing.

    Lets have a look at your original version:

    //inserting
    template <class T> void LinkedList<T>::insert(const T& x) {
       LinkedListNode<T>* head = new LinkedListNode<T>(head->prev, x, head->next);
       // ...
    

    We’re going to dissect the first instruction of your function:

       LinkedListNode<T>* head; // not initialized value
       new LinkedListNode<T>(head->prev, x, head->next); // oh oh....
    

    head is a new variable and your original this->head is shadowed. But even if you fix this problem, the initial this->head is still NULL and thus this->head->prev will result in a segmentation fault. You fix the latter in your second version, but only there’s still something wrong:

    template

    void LinkedList<T>::insert(const T& x) {
     LinkedListNode<T>* head; // #1
     if(head == NULL){
            // #2
            head->value = x;
            head->prev = NULL;
            head->next = head;
    
     }
    else{   LinkedListNode<T>* newnode;
            newnode->value = x;
            newnode->prev = head->next; // #3
            newnode->next = newnode;    // #3
            head = newnode;
     }
    

    The first error (#1) is, again, variable shadowing. #2 is again a segmentation fault, since you didn’t allocate memory for your head.

    #3 are logical errors. The previous node should be the head itself, and the node following a node shouldn’t be the node itself.

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

Sidebar

Related Questions

I know more or less how to do this, but I think I'm getting
I just don't get it, it's actually so basic, but I just can't make
This is less of a specific problem or error but more of a implementation
I guess this is more or less a two-part question, but here's the basics
I know that being open source does not necessarily makes a program more/less secure
Title more or less says it all. In response to a touchesBegan event, my
The title more or less says it all. I create a widget, add it
I am more or less wondering how time() is implemented in the C standard
The title more or less says it all: I have a function which takes
Is there more elegant (less code) way of find a matrix OUT, with colSums(OUT)<=a

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.