1

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;
}
3
  • I was offline for a while. I'm glad to see that Aesthete was able to help you along. Are you taking a course in data structures right now? Commented Sep 17, 2012 at 5:45
  • yes, its like a course of c++ with data structures. i'm a amateur. Commented Sep 17, 2012 at 5:49
  • Not an amateur. A student. Make sure you tag questions like this as homework. In the long run, the hints you'll get will be far more valuable than a straight up answer. Commented Sep 17, 2012 at 5:54

2 Answers 2

2
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.
    }
  } 
Sign up to request clarification or add additional context in comments.

5 Comments

so what i suppose to do? the new node should be what character i am inserting or what?
@MW - Ok, I'm assuming that each node holds a character, and you're trying to insert a new node to the list? Have a look at my changes, and let me know if it's what you're trying to do.
newNode->x=x; was not declared in the scope
@MW - Oh sorry, it's supposed to be d.
I actually figured out a way of how to do it.
1

Presently the body of your final if statement is simply overwriting the x value of current node. In order to stick a new node into a linked list, you need to. 1. Create a new node 2. Choose a location for the new node in the list (you've already done that) 3. Point the preceding node at your node 4. Point your node at the next node

There are some nuances to accomplishing all of that, but hopefully that's enough to get started.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.