Ryan
Ryan

Reputation: 362

insert sort with doubly linked list

I've been working on a set of functions for doubly linked lists, one that I've had trouble with is inserting elements into the list but keeping the list in sorted order. So if I have a list of {3, 4, 6} and insert 5 then the list will become {3, 4, 5, 6}

I just finished the latest code after rewriting it last night, please comment and tell me if there is a better way, I am posting both the header file and the c file. One thing I want to point out is that I do not use a pointer to the current node and only create one pointer in the insert function that creates a new node with a temp placement.

LIST.H

/* custom types */

typedef struct node
{
    int val;
    struct node * next;
    struct node * prev;
}Node;

typedef struct list
{
    Node * head;
    Node * tail;
}List;

/* function prototypes */

/* operation: creates a list */
/* pre: set equal to a pointer to a list*/
/* post: list is initialized to empty */
List* NewList();

/* operation: Insert a number into a list sorted */
/* pre: plist points to a list, num is an int */
/* post: number inserted and the list is sorted */
void Insert(List * plist, int x);

LIST.C

/* c file for implentation of functions for the custome type list */
/* specifically made for dueling lists by, Ryan Foreman */

#include "List.h"
#include <stdlib.h> /* for exit and malloc */
#include <stdio.h>

List* NewList()
{
    List * plist = (List *) malloc(sizeof(List));
    plist->head = NULL;
    plist->tail = NULL;
    return plist;
}

void Insert(List * plist, int x)
{
    /* create temp Node p then point to head to start traversing */
    Node * p = (Node *) malloc(sizeof(Node));
    p->val = x;

    /* if the first element */
    if ( plist->head == NULL) {
        plist->head = p;
        plist->tail = p;
    }
    /* if inserting into begining */
    else if ( p->val < plist->head->val ) {
        p->next = plist->head;
        plist->head->prev = p;
        plist->head = p;
    }

    else {
        p->next = plist->head;
        int found = 0;
        /* find if there is a number bigger than passed val */
        while((p->next != NULL) && ( found == 0)) {
            if(p->val < p->next->val)
                found = 1;
            else {
                p->next = p->next->next;
            }
        }
        /* if in the middle of the list */
        if(found == 1)
        {
            p->prev = p->next->prev;
            p->next->prev = p;
        }
        /* if tail */
        else {
            plist->tail->next = p;
            p->prev = plist->tail;
            plist->tail = p;
        }
    }
}

Thank you for any input on the code, any comments are appreciated

Upvotes: 0

Views: 4491

Answers (2)

X Tian
X Tian

Reputation: 772

malloc() does not zero memory, you don't set your first nodes next/prev, so your while loop could go on forever if second node >= first node value, ie exit condition p->next != NULL is not met.

Upvotes: 1

md5
md5

Reputation: 23699

Some comments on your C' utilisation.

  • In C, cast from pointer to void to pointer to object is unecessary.
  • It could be a good idea to check malloc return in such library.

Upvotes: 1

Related Questions