-2
void New_list ( struct el * *start, struct el * *end )
{

    struct el *g;
    int x;
    puts (Insert elements, to finish insert 0:" );

    *start = NULL;
    *end = NULL;

    scanf("%d", &x);

    while( x != 0 )
    {
        if ( *start == NULL)
        {
            g = (struct el *) malloc(sizeof(struct el));
            g->elem = x;
            g->next = NULL;
            *end = g;
            *start = g;
        }
        else
        {
            g = (struct el *) malloc(sizeof(struct el));
            g ->elem = x;
            g ->next = NULL;
            (*end)->next = g;
            *end = g;
        }
        scanf( "%d", &x );
    }
}

I have this list and i need to insert a new element after a k element (k is from keyboard). I'm new to the arrows so i can't figure it how to do it.

Mokosha
  • 2,737
  • 16
  • 33
B.G.
  • 19
  • 8
  • 1
    I suggest to study some basics. http://www.amazon.com/dp/0131103628/?tag=stackoverfl08-20 – Jeyaram Feb 13 '14 at 08:58
  • When pasting code, please make sure you paste actual (and compilable) code. Oh, and [don't cast the result of `malloc` in C](http://stackoverflow.com/a/605858/440558). – Some programmer dude Feb 13 '14 at 08:59
  • [Please don't cast the return value of `malloc()` in C](http://stackoverflow.com/a/605858/28169). – unwind Feb 13 '14 at 09:00
  • 2
    It would also be helpful to show the code you *tried* to make work, not some unrelated function. Please read [the Stack Overflow question checklist](http://meta.stackexchange.com/questions/156810/stack-overflow-question-checklist). – Some programmer dude Feb 13 '14 at 09:02
  • 1
    If you are "new to arrows", you should make sure you understand "arrows" (i.e., pointers), before asking for "how to make my list handling work"... – DevSolar Feb 13 '14 at 09:06

1 Answers1

4

Assuming your structure has elem and next

When you insert a new element in the middle what you need to do is

  +------+-------+         +-------+-------+
  |      |       |         |       |       |
  |      |       |         |       |       |
  |   1  |   +------------>|   2   |   +------------>
  |      |       |         |       |       |
  +------ -------+         +------- -------+
   elem    next              elem    next


  +------+-------+                                  +-------+-------+
  |      |       |                                  |       |       |
  |      |       |                                  |       |       |
  |   1  |   +-------+                +------------>|   2   |   +------------>
  |      |       |   |                |             |       |       |
  +------ -------+   |                |             +------- -------+
   elem    next      |                |               elem    next
                     |   +-------+----|---+
                     |   |       |    |   |
                     |   |       |    |   |
                     |   |   3   |    |   |
                     +-->|       |    +   |
                         |       |        |
                         +------- --------+

First you need to traverse to the block where you want to insert the new block. say k is 1

You need to save the value of the next of this block.

You create a new block and assign this next to the next of the new block.

The next of the block preceeding the new block will have the address of the new block

Suvarna Pattayil
  • 5,136
  • 5
  • 32
  • 59