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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:22:27+00:00 2026-06-18T06:22:27+00:00

This is how i add elements to the head of a linked list //typedef

  • 0

This is how i add elements to the head of a linked list

 //typedef void* VoidPtr
 //typedef node* NodePtr
 struct Node
   {
       NodePtr next
       VoidPtr data
   };


void LinkedList::Addelement(VoidPtr horoscope)
{
    if(head == NULL)
  {

    head = new Node;
    head->data = horoscope;
     head->next = NULL;

  }

NodePtr temp = new Node;

temp -> data = horoscope;
temp -> next = head;

head = temp;
}

This is how i add elements to the tail of a linked list

void LinkedList::addelementfromback(VoidPtr horoscope)
{
   NodePtr temp=head;

   if(head == NULL)
   {

    head = new Node;
    head->data = horoscope;
    head->next = NULL;

   }

   while( temp->next != NULL)
   {
      temp=temp->next

   }

   NodePtr element=New Node;
   element->data=horoscope;
   element->next=NULL;

    temp->next=element;



}

i dont understand why we use temp=element for adding to the head of a linked list
but for adding to the tail of a linked list we use temp->next=element. I dont understand why we cant use while temp=next for adding element to tail of linked 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-18T06:22:28+00:00Added an answer on June 18, 2026 at 6:22 am

    In your Addelement method, you need an else clause because if the list is empty (head == NULL), you only need to point the head to the new node. Nothing else, there are no other nodes in the list.

    Also, rather than using a void pointer, consider using templates. Templates are great for data structures and algorithms where the data type changes, not the structure or algorithm, such as stacks and linked lists.

    I suggest you consider separating the node pointers from the data item as two separate structures. This will help you use common code between single linked lists and doubly linked lists. Also a great help when you don’t want to use templates.

    struct Node_Link
    {
        Node_Link * next;
    };
    
    struct Node_Integer
    : public Node_Link
    {
        int data;
    };
    
    struct Node_Double
    : public Node_Link
    {
       double data;
    };
    
    struct Node_String
    : public Node_Link
    {
        std::string data;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to add elements to a Linked List while Preorder Traversaling on a
I've created accordion like this: layout:'accordion', layoutConfig:{ animate:true } then i add elements by
This is my array and I want to add some elements to make it
So i have this code: foreach (string xmlfilelisted in xmlFileList) { resultXml.Root.Add(XDocument.Load(xmlfilelisted).Root.Elements()); resultXml.Save(filepath); }
I'm trying to add style elements to all ALL the head elements in a
Can't seem to work out why I'm getting garbage output from this Linked List
So i have my definition of the list as a global variable: typedef struct
I'm creating a linked list data structure in C. However, I'm receiving some weird
This is an assignment. I have to create a circular linked list and remove
I am trying to add new elements to an unordered list with the following

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.