Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Declaration 

:
Struct node
{
    int val;
    struct node* next;
    struct node* prev;
};

typedef struct {
struct node* head;
struct node* tail;
} DLL;
Initialization:
L.head = NULL;
L.tail = NULL;

Display DLL in the ordinary order:


void displayDLL (DLL D)
{
    node* P=D.head;
    int i=1;
    if ((D.head == NULL ) && (D.tail==NULL)) {printf("List is empty.\n");}
    else { while (P!=NULL)
    {printf("Number %d is = %d.\n", i, P->val);
        P = P->next;
        i++;
    }
    }
}
Display DLL backwards:
void displaybackwards (DLL D)
{
    node* P=D.tail;
    int i=1;
    if ((D.head == NULL ) && (D.tail==NULL)) {printf("List is empty.\n");}
    else { while (P!=NULL)
    {printf("Number %d is = %d.\n", i, P->val);
        P = P->prev;
        i++;
    }
    }
}
Search for an element in DLL:
node* searchstation (DLL L, int x)
{
    node* P=L.head;
    if ((L.head!=NULL)&&(L.tail!=NULL))
    {
        while ((P!=NULL) && (P->sta.number!=x))
        {
            P = P->next;
        }
    }
    return P;
}

int findelement (DLL D, int x)


{
    node* P=D.head;
    int found=0;
    while ((P!=NULL) && (found == 0))
        {
            if (P->val ==x) {found++;}
            P= P->next;
        }
    return found;
}

Delete an element from a DLL:


void deleteval (DLL* L)
{
    int found1, found2, found, x;
    node* S;
    node* P;
    printf("Please enter the value to delete:\n");
    scanf("%d", &x);
    found = searchvalue(*L, x);
    found1 = searchbegin(*L, x);
    found2 = searchend(*L, x);
    if (found == 0) {printf("Unable to delete because this value doesn't
exist.\n");}
    else if (found1 == 1) {S= L->head;
                if (L->head == L->tail) {
                L->head = NULL;
                L->tail = NULL;}
            else {
            L->head = (L->head)->next;
            (L->head)->prev = NULL;}
            free (S);}
        else if (found2 == 1) { S = L->tail;
            (L->tail) = (L->tail)->prev;
            (L->tail) ->next = NULL;
            free (S);  }
        else {
            P = L->head;
            while ((P!=NULL) && (P->val !=x))
            {
                P = P->next;
            }
            S = P;
            (P->prev)->next = P->next;
            (P->next)->prev = P->prev;
            free(S);
        }
}

Add elements in ascending order in DLL:


void addascending (DLL* L)
{
    int found, x;
    node* neww;
    node* P;
    printf("Please enter the value to add in ascending order: \n");
    scanf("%d", &x);
    neww = malloc(sizeof(node));
    if (neww == NULL) {printf("ERROR.\n");}
    else { found = searchvalue(*L, x);
    if (found == 1) {printf("Unable to add because it already exists.\n");}
    else {
    neww->val = x;
    neww->next = NULL;
    neww->prev = NULL;
    P = L->head;
    while ((P!=NULL) && (P->val < x))
    {
        P = P->next;
    }
    if (P==L->head) {if ((L->head == NULL) && (L->tail == NULL)) {
        L->head = neww;
        L->tail = neww;}
        else { P->prev = neww;
        neww->next = P;
        L->head = neww;}
    }
    else if (P==NULL) {
        neww->prev = L->tail;
        (L->tail) ->next = neww;
        L->tail = neww;
    }
    else {
        neww->next = P;
        neww->prev = P->prev;
        (P->prev) ->next = neww;
        P->prev = neww;
    }
    }
    }
}

You might also like