1

I'm having trouble figuring out how to insert an element into a sorted list. I am new to linked lists and I'm still having trouble.The following function takes an predefined list and an element as arguments. I have white boarded the whole thing but I still can't figure it out. Thank you for your help.

/*
 * function:  lst_insert_sorted
 *
 * description:  assumes given list is already in sorted order
 *     and inserts x into the appropriate position
 *     retaining sorted-ness.
 * Note 1:  duplicates are allowed.
 *
 * Note 2:  if given list not sorted, behavior is undefined/implementation
 *      dependent.  We blame the caller.    
 *      So... you don't need to check ahead of time if it is           sorted.
 */

void lst_insert_sorted(LIST *l, ElemType x) {
    NODE *p = l->front;
    NODE *temp;
    NODE *current = p;
    NODE *prev;
    NODE *next;

    if (p->val >= x) { // Base Case if
        p->val = x;
    }

    while (p !=NULL) {
        prev = current;
        temp = prev->next;
        next = current->next;

        if (next->val >= x) {
            temp->val = x;
        }

    }

    return 0;
}
John Bollinger
  • 160,171
  • 8
  • 81
  • 157
Rbutler93
  • 73
  • 1
  • 11

3 Answers3

0

Generally speaking, a linked list consists of "nodes" joined together in sequence by links pointing from each node to the next one (or to the previous one, or both). The nodes each point also (perhaps trivially) to one of the actual elements of the list.

To insert an element into a linked list at a given position, you just create a node pointing to that element, and update the other pointers as needed. With a doubly-linked list such as yours (where each node points both to the next one and to the previous one), you must

  • update the next pointer of the node immediately preceding the insertion position to point at the new node,
  • update the prev pointer of the node immediately following the insertion position to point at the new node, and
  • set the prev and next pointers of the new node to point to these other nodes.

There are typically special cases for inserting at the beginning or end of the list; details depend on your list and node implementation.

In your case, you must also find an appropriate insertion point. Since the list is sorted, you can just traverse it from the beginning, comparing each node's element to the one to be inserted, until you find the right spot. Such a "linear search" is not terribly efficient if the list is long, but you cannot do better with a generic linked list.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
0
if (p->val >= x) { // Base Case if
    p->val = x;
}

there is a loss data so you wrote the x with overwriting in to first data in the list. İf ı understood the problem you should create a node and insert this to list.

batuhan
  • 5
  • 3
0

You did not show how NODE is defined. So I suppose that the list is a single-linked list. In this case the function can look like

void lst_insert_sorted( LIST *l, ElemType x ) 
{
    NODE *current = l->front;
    NODE *prev = NULL;

    while ( ( current != NULL ) && !( x < current->val ) )
    {
        prev = current;
        current = current->next;
    }

    NODE *node = malloc( sizeof( NODE ) );
    node->val = x;

    node->next = current;
    if ( prev != NULL )
    {
        prev->next = node;
    }
    else
    {
        l->front = node;
    }
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335