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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:10:33+00:00 2026-06-15T13:10:33+00:00

Writing a function to do a head insert on a linked-list. It’s half working

  • 0

Writing a function to do a head insert on a linked-list. It’s half working as in it’s inserting the object into head and reattaching the list but I’m losing my original head node in the list somehow.

If list is [green, red, blue] and I try to insert yellow, it will work but the new list will be [yellow, red, blue].

Node class is:

template<class T>
class Node
{
public:
    Node(T theData, Node<T>* theLink) : data(theData), link(theLink){}
    Node<T>* getLink( ) const { return link; }

    const T& getData( ) const { return data; }

    void setData(const T& theData) { data = theData; }
    void setLink(Node<T>* pointer) { link = pointer; }

private:
    T data;
    Node<T> *link;
};

List is stored into a queue, so the head insert is a method of that class. Queue has private variables front and back that point to the corresponding positions of the list.

template<class T>
void Queue<T>::headInsert(T& theData)
{
   Node<T> *temp;
   temp = front->getLink();
   front->setLink(new Node<T>(theData, temp->getLink() ));
   front = front->getLink();
}
  • 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-15T13:10:34+00:00Added an answer on June 15, 2026 at 1:10 pm

    Your problem is in your setLink call:

    template<class T>
    void Queue<T>::headInsert(T& theData)
    {
       Node<T> *temp;
       temp = front->getLink();
       front->setLink(new Node<T>(theData, temp->getLink() )); // Right here
       front = front->getLink();
    }
    

    You actually have a number of problems. First off, let’s suppose we have the following test list:

    front = Red -> Green -> Blue -> NULL

    The call temp = front->getLink() yields the following output:

    temp = Green -> Blue -> NULL.

    The new Node<T>(theData, temp->getLink()) call, where theData = Yellow, then yields:

    new Node<T>(theData, temp->getLink()) = Yellow -> Blue -> NULL.

    Calling front->setLink(new(...) then gives you:

    front = Red -> Yellow -> Blue -> NULL

    Lastly, front = front->getLink():

    front = Yellow -> Blue -> NULL.

    This is not what you want. You simply want to take yellow and pop it on the front of the list:

    template<class T>
    void Queue<T>::headInsert(T& theData)
    {
       front = new Node<T>(theData, front);
    }
    

    No need to modify internal pointers. Just point front to be the new node containing your data, with it’s next pointer pointing to the old data.

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

Sidebar

Related Questions

I am writing a Java function to reverse a linked list in-place. I am
Writing a function in C that represents an integer as a singly linked list,
I am writing a function that will take in the head of a linked
I am writing a bubble sort function to sort nodes in a doubly linked
I'm writing a genetic algorithm to generate the string helloworld. But the evolve function
Visual Studio 2008 - compiling in C I am writing a linked list application
I'm currently writing a linked list and trying to free up memory allocations when
I have a linked List c file/head as an independent library i am using
I am writing function that involve other function from base R with a lot
I'm currently writing a function what would create a zip file, which will be

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.