Friday, 5 July 2013

THIS PROGRAM DELETES FIRST NODE OF DOUBLY LINKED LIST




#include<stdio.h>
#include<malloc.h>

struct dlist
{
    char info[20];
    struct dlist *link;
    struct dlist *prev;
};

int i ;
struct dlist start;

void del_first (struct dlist *);
void create_dlist (struct dlist *);
void display (struct dlist *);

 /* Function to create doubly linked list */

 void create_dlist(struct dlist *ptr)
{
    int n;
    start.link = NULL;  /* Empty list */
    start.prev = NULL;
    ptr = &start;      /* Ptr points to the start pointer */
    printf("\n\n\n How many nodes you want to create: ");
    scanf("%d",&n);
    printf("\n\n ==============================================================================\n");
    printf("\n Note That:\n -----------\n Enter string values in nodes\n");
    printf("\n ==============================================================================\n");
    while( i!=n)
    {
        ptr->link = (struct dlist *) malloc(sizeof(struct dlist));
        ptr->link->prev = ptr;
        ptr = ptr->link;
        printf("\n Enter the values of the node : %d: ", (i+1));
        scanf("%s", ptr->info);
        ptr->link = NULL;
        i++;
    }
    printf("\n\n --------------------");
    printf("\n Total nodes = %d",i);
    printf("\n --------------------\n\n");
}
/* Function to delete first node */

void del_first (struct dlist *ptr)
{
    ptr = start.link;

    ptr->prev->link = ptr->link ;
    ptr->link->prev = ptr->prev ;
    free(ptr);

}

/* Function to display the list */

void display(struct dlist *ptr)
{
    ptr = start.link;

        while (ptr)
        {

            printf("\n\n\t\t %s", ptr->info);
            ptr = ptr->link;
        }
}

/* Main Function */

void main()
{
    struct dlist *ptr = (struct dlist *) malloc(sizeof(struct dlist));
    clrscr();
    create_dlist (ptr);
    printf("\n Doubly linked list is: ");
    printf("\n ----------------------\n");
    display (ptr);
    del_first (ptr);
    printf("\n ------------------------------------------------\n");
    printf("\n Doubly linked list after deletion of first node: \n");
    printf("\n ------------------------------------------------\n");
    display(ptr);
    getch();
}

No comments:

Post a Comment