0

I have a problem in this piece of code with linked lists. It gives me the error:

 Segmentation Fault (Core Dumped)

Can anyone see what seems to be the problem? Thanks in advance.

pos stands for position and root for the first element

typedef struct
{
  element *root;
  int size;
} list;

typedef struct _element
{
  char *str;
  struct _element *next;
} element;

int
insert_list (list * lst, const char *value, int pos)
{
  element *new;

  int k;
  new = malloc (sizeof (element));

  for (k = 0; k < pos; k++)
    {
      new = lst->root;
      lst->root = lst->root->next;

      if (k == pos - 1)
        {
          lst->root = NULL;
          new->str = value;
        }
    }

  for (k = 0; k <= lst->size; k++)
    {
      new = lst->root;
      lst->root = lst->root->next;

      if (k == lst->size)
        {
          lst->root = NULL;
          new->str = value;
        }
      if (pos < 0 || pos >= lst->size)
        return -1;
      else
        return pos;
    }
}
12
  • 2
    What is first? You dereference it before any assignment! Commented Mar 14, 2014 at 17:15
  • Show how you call the function. Commented Mar 14, 2014 at 17:15
  • Hello, you are right, i forgot to declare the variable, but the problem still maintains, any idea? Commented Mar 14, 2014 at 17:17
  • The function is called correctly, since it was already given to us, we only need to develop the functions Commented Mar 14, 2014 at 17:18
  • 1
    lst == NULL ? and new = malloc(sizeof(element)); for(k=0;k<pos;k++) { new = lst->root; is weird Commented Mar 14, 2014 at 17:18

1 Answer 1

1

Let's see how to debug this.

First of all, compile your code with warnings, and read the warnings:

$ gcc -Wall x.c -o xx.c:6:3: error: unknown type name ‘element’
x.c: In function ‘insert_list’:
x.c:26:11: warning: assignment from incompatible pointer type [enabled by default]
x.c:27:28: error: request for member ‘next’ in something not a structure or union
x.c:32:20: warning: assignment discards ‘const’ qualifier from pointer target type [enabled by default]
x.c:38:11: warning: assignment from incompatible pointer type [enabled by default]
x.c:39:28: error: request for member ‘next’ in something not a structure or union
x.c:44:20: warning: assignment discards ‘const’ qualifier from pointer target type [enabled by default]
x.c:51:1: warning: control reaches end of non-void function [-Wreturn-type]

All of those things are problems, and 2 (the errors) indicate the code you posted would never have compiled

So, you have problems beyond a segmentation fault. The first one is this line:

 lst->root = lst->root->next;

This is because you need to define the element struct before the list struct.

Apart from this, your insert routine is broken:

new = malloc (sizeof (element));

for (k = 0; k < pos; k++)
  {
    new = lst->root;

Here you are overwriting your newly allocated element many times.

On the next line you overwrite the root:

  lst->root = lst->root->next;

I'm afraid I can't even figure out what you are trying to do here. If the goal is to insert an element at position pos in a singly linked list, what you want to do is:

  1. Allocate a new element n.

  2. If pos is zero, make your new element n the root, and make n->next point to the current root, and you are done.

  3. Else iterate pos-1 times along the list, call that x.

  4. Make n->next->next = x->next (if n->next exists)

  5. Make n->next = x

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.