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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:41:38+00:00 2026-06-11T12:41:38+00:00

Can someone help me understand what I’m doing wrong. I need to insert a

  • 0

Can someone help me understand what I’m doing wrong. I need to insert a character into a linked list.

It takes an input like a name of person, than it reverses it.
then it tells user to choose a position to add a character.

void insert_char(Node* plist, char x, int p){
  Node* d=plist;
  for (int i=1; i<p and 0!=d; i++)
    d=d->next;
  if (0 !=d)
    d->x=x;

However, this code changes the character, not adds it.

UPDATE:

I can’t still figure it out.

void insert_char(Node* plist, char x, int p){
    Node* d=plist;
    Node* d2=0;
    for (int i=1; i<p and 0!=d; i++)
        d2->next=d->next;
    d->next=d2;
    if (0 !=d)
        d2->x=x;
    return;
}

I am getting a segmentation error.

Ok, so i figured out, what i really wanted. Thanks for help

  void insert_char(Node* plist, char x, int p){
  Node* d=plist;
  Node* d2= new Node();
  for (int i=1; i<p and d; i++)
    d2->next=d->next;
    d->next=d2;
  if (0 !=d)
    d2->x=x;
  return;
}
  • 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-11T12:41:39+00:00Added an answer on June 11, 2026 at 12:41 pm
    d->x=x;
    

    Is overwriting whatever character was there previously. What do you expect to happen?

    0!=d
    

    Can be simplified to just d, no need to compare to 0.

    It might also be helpful is you use braces. I know it’s nice to be able to ignore them with one lines like this, but it will eventually come back to bite you one day.

    As for your update, you’re always going to get a segfault because of these lines:

    Node* d2=0;
    d2->next=d->next;
    d2->x=x;
    

    You’re creating a Node* and never assigning anything to it, nor allocating memory. You’re dereferencing an uninitialized pointer.

    Are you sure you’re not trying to do this?

    void insert_char(Node* plist, char x, int p){
      Node* d=plist;
      for (int i=1; i<p && d; i++)
        d=d->next;
      if (!d) // If this node is empty..
      {
        d = new Node; // Store a new node at the position.
        d->x = x; // Set the char value for the new node.
      }
      else // If this node is not empty..
      {        
        Node* next = d->next(); // Get the next node.
        d = new Node; // Create a new node and insert.
        d->x = x; // Set the char for this node.
        if(next) // If there was a mode above our insertion..
        {          
          newNode->next = next; // Store it in the next member of our new node.
        }
      } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone help me understand what I'm doing wrong with this method? I'm trying
Can someone help me understand the WPF stretch alignment. I often run into issues
Can someone help me understand what's wrong with this query: DELETE FROM noteproject INNER
I know this regex divides a text into sentences. Can someone help me understand
Can someone help me understand this line of jquery code?: $(this).find('input[type=submit]:not(.cancel), button').click(function () I'm
Can someone help me understand how to write this case statement properly its not
(Specifically, I'm using Backbone Model events) Can someone help me understand how javascript events
Can someone help me find the error when I tried to display the list
Can someone help me, how can i define the keyCode ,which, charcode into variable.
can someone help me understand this because it's working in as3 but not so

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.