Insert

int insert(node **head, int data)
{
    node *new;
    new = (node*)malloc(sizeof(node));
 
    new->value = data;
    new->next = *head;
    *head = new;
 
    return new->value;
}
 
Node* Insert_Sorted(Node *head, int value)
{
    Node *p0, *p1, *new;
    new = (Node*)malloc(sizeof(Node));
    new->data = value;
 
    p1 = head;
    while (p1 != NULL && p1->data <= value) {
        p0 = p1;
        p1 = p1->next;
    }
 
    if (p1 == head) {               // be careful! may stop at the head!
        head = new;
        new->next = p1;
    }
    else {                                 // otherwise
        p0->next = new;
        new->next = p1;
    }    
 
    return head;
}
 
int insert_circular(node **head, int data)
{
    node *p1;
    node *new = (node*)malloc(sizeof(node));
    new->value = data;
 
    if (*head == NULL) { 
        *head = new;
        *head->next = *head;   // for circular linked list 
        return new->value;
    } 
 
    p1 = *head;
    while (p1->next != *head) {         // find the node before head
        p1 = p1->next;
    }
 
    p1->next = new;                 // insert head
    new->next = *head;
    *head = new;
 
    return new->value;
}
 
int insert_double(node** head, int num) 
{
 
        node *p0, *p1, *p2;
    p2 = (node*)malloc(sizeof(node));
    p2 -> data = num;         
    if (*head == null) {
        *head = p2;
        p2->prev = null;
        return num;
    }
    p1 = *head;
    while (p1 != NULL && p1->data > num) {
        p0 = p1;
        p1 = p1->next;
    }
    if (p1 == *head) {
        p2->next = p1;
        p2->prev = NULL;
        *head = p2;
    }
    else {
        p0->next = p2;
        p2->prev = p0;
        p2->next = p1;
        if (p1 != NULL)
            p1->prev = p2;
    }
 
    return num;
}

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License