简体   繁体   中英

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

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.

malloc()没有零内存,你没有设置你的第一个节点next / prev,所以你的while循环可以永远继续,如果第二个节点> =第一个节点值,即退出条件p-> next!= NULL不满足。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM