0
void LinkedList::insert(int num, int pos) {
  Node *newNode = new Node;
  newNode->data = num;
  newNode->next = NULL;
  if(pos == 0) {
    newNode->next = head;
    head = newNode;
  }
  else {
    Node *temp = head;
    for (int i = 1; i < pos-1; ++i) {
      if (temp != NULL) {
        temp = temp->next;
      }
    }
    if (temp != NULL) {
      newNode->next = temp->next;
      temp->next = newNode;
    }
    else {
      cout << " The previous node is full.";
    }
  }
}

This is my insert function. The code that runs in main.cpp is:

// adding through insert
    nums.insert(1, 0);
    nums.insert(5, 4);
    nums.insert(3, 7);

And the output is:

List after append: 
8   6   7   8   0   9

List after inserting: 
1   8   6   5   7   8

As you can see, something is getting overwritten, or the end of the list just gets cut off. I have searched the internet for hours to no avail. The output needs to increase the length of the list and not overwrite anything. Any help would be appreciated.

7
  • Could you show how the output code looks like? Commented Apr 21, 2022 at 21:37
  • pastebin.com/xg9sWbJ5 Commented Apr 21, 2022 at 21:41
  • 1
    The exact code as shown, running only what is demonstrated in your 'main.cpp', should produce two declarations of "The previous node is full." and a single node '1' in the list printout. So, how about we update this question with a proper minimal reproducible example that actually produces the output you're claiming. Commented Apr 21, 2022 at 21:45
  • 1
    Adding an item to a singly linked list is ludicrously easy if you abstract away the difference between head and next with a pointer to a pointer. Here's a trick from WhozCraig showing how to remove an item from a list that you can steal ideas from. Commented Apr 21, 2022 at 21:54
  • 1
    @user4581301 lols. I remember updating that :-) Anyway for the OP, that same nuance adapted to the OPs code is here, including the odd announcement of "full" (whatever that means) on positional overreach. Commented Apr 21, 2022 at 22:00

1 Answer 1

1

The problem is this for loop

for (int i = 1; i < pos-1; ++i) {
  if (temp != NULL) {
    temp = temp->next;
  }
}

Let's assume that pos is equal to 2. In this case the loop will not iterate and temp will be equal to head. So the new node will be inserted before the second mode instead to be inserted before the third node.

You need to write

for (int i = 1; temp != nullptr && i < pos; ++i) {
    temp = temp->next;
}

Also instead of writing this message

 cout << " The previous node is full.";

(it is the caller of the function should decide whether to output a message) I would declare the function like

bool LinkedList::insert(int num, int pos) {

and return either true or false dependent on whether a node was inserted.

Sign up to request clarification or add additional context in comments.

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.