Program for Doubly Linked List Operations

Program for Doubly Linked List Operations – Insert (Front, Before), Delete, Display
Source: Dr. G T Raju, Professor & Head, Dept. of CSE, RNSIT

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <stdlib.h>
typedef struct dnode
{
    int data;
    struct dnode *next,*prev;
}DNODE;
 
DNODE *InsFront(DNODE *, int);
DNODE *InsBefore(DNODE *, int,int);
DNODE *DelNode(DNODE *, int);
void Display(DNODE *);
 
main()
{
    DNODE *start=NULL;		 /* Main Program */
    int opn,elem,info,n,i;
    do
    {
        clrscr();
        printf("\n ### Doubly Linked List Operations ### \n\n");
        printf("\n Press 1-Creation with front Insertion");
        printf("\n       2-Insert Before a Given Node");
        printf("\n       3-Delete a Given Node");
        printf("\n       4-Display");
        printf("\n       5-Exit\n");
        printf("\n       Your option ? ");
        scanf("%d",&opn);
        switch(opn)
        {
        case 1:
            printf("\n\nHow Many Nodes ?");
            scanf("%d",&n);
            for(i=1;i<=n;i++)
            {
                printf("\nRead the Data for Node %d ?",i);
                scanf("%d",&elem);
                start=InsFront(start,elem);
            }
            printf("\nDoubly Linked list with %d nodes is ready toUse!!\n",n);
            break;
        case 3: printf(" Read the Info of the Node to be deleted ? ");
            scanf("%d",&info);
            start=DelNode(start,info);
            break;
        case 2: printf(" Read the Data for New node\n");
            scanf("%d",&elem);
            printf(" Read the Info of the Node(to the left of which new node tobe inserted ? ");
            scanf("%d",&info);
            start=InsBefore(start,elem,info);
            break;
        case 4: printf(" Doubly Linked List is \n");
            Display(start); break;
        case 5: printf("\n\n Terminating \n\n"); break;
        default: printf("\n\nInvalid Option !!! Try Again !! \n\n");
            break;
        }
        printf("\n\n\n\n  Press a Key to Continue . . . ");
        getch();
    }while(opn != 5);
}
 
DNODE *InsFront(DNODE *start, int elem)
{
    DNODE *temp;
    temp=(DNODE *)malloc(sizeof(DNODE));
    if( temp == NULL)
    { printf(" Out of Memory !! Overflow !!!");
    return(start);
    }
    else
    {
        temp->data=elem;
        temp->prev=NULL;
        temp->next=start;
        start->prev=temp;
        printf(" New Node has been inserted at Front Successfully !!");
        return(temp);
    }
}
 
DNODE *InsBefore(DNODE *start, int elem,int info)
{
    DNODE *temp,*t;
    temp=(DNODE *)malloc(sizeof(DNODE));
    if( temp == NULL)
    { printf(" Out of Memory !! Overflow !!!");
    return(start);
    }
    else
    {
        temp->data=elem;
        temp->next=NULL;
        temp->prev=NULL;
 
        if(start->data == info)    /* Front Insertion */
        { temp->next=start;
        start->prev=temp;
        return(temp);
        }
        else
        {
            t=start;
            while( t != NULL && t->data != info)
                t=t->next;
            if(t->data == info) /* Node found */
            {
                temp->next=t;
                temp->prev=t->prev;
                t->prev->next=temp;
                t->prev=temp;
            }
            else
                printf(" Node not found,Invalid Info !!!");
            return(start);
        }
    }
}
 
DNODE *DelNode(DNODE *start, int info)
{
    DNODE *t;
    if( start ==  NULL) { printf(" Underflow!!!"); return(start); }
    else
    { t=start;
    if(start->data == info)    /* Front Deletion */
    { start=start->next;
    start->prev=NULL;
    t->next=NULL;
    free(t);
    return(start);
    }
    else
    {
        while( t != NULL && t->data != info)
            t=t->next;
        if(t->data == info) /* node to be deleted  found*/
        {
            t->prev->next=t->next;
            t->next->prev=t->prev;
            t->next=t->prev=NULL;
            free(t);
        }
        else
            printf("Node not found, Invalid Info !!");
        return(start);
    }
    }
}
void Display(DNODE *start)
{ 
	DNODE *t;
	if( start == NULL) printf("Empty List\n");
	else
	{
		t=start;
		printf("Forward Traversal \n\n Start->");
		while(t)
		{
			printf("[%d]->",t->data);
			t=t->next;
		}
		printf("Null\n");
	}
}

2 Responses to “Program for Doubly Linked List Operations”

Leave a Reply