-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDLLL2.C
106 lines (102 loc) · 2.22 KB
/
DLLL2.C
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
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node * next;
struct node * prev;
};
typedef struct node NODE;
void addNode( NODE ** startPtr, NODE ** endPtr, int input)
{
NODE * newnode=NULL,*tptr=NULL;
newnode=(NODE*)malloc(sizeof(NODE));
newnode->data = input;
newnode->next = newnode->prev= NULL;
if(*startPtr==NULL || *endPtr==NULL)
{
*startPtr=*endPtr=newnode;
}
else
{
for(tptr=*startPtr ; tptr && tptr->data < newnode->data ; tptr= tptr->next);
if(tptr==NULL) //at end
{
( *endPtr)->next = newnode;
newnode->prev = *endPtr;
*endPtr =newnode;
}
else if(tptr==*startPtr)//at begin
{
(*startPtr)->prev=newnode;
newnode->next = *startPtr;
*startPtr= newnode;
}
else
{
newnode->next = tptr;
newnode->prev = tptr->prev;
tptr->prev->next =newnode;
tptr->prev=newnode;
}
}
}
DLLdisplay(NODE * st , NODE *ed)
{
NODE* tptr=NULL;
for(printf("\n\tFOR : "),tptr=st;tptr ;
printf("%2d -> ",tptr->data), tptr=tptr->next);
for(printf("\n\tREV : "),tptr=ed; tptr ;
printf("%2d -> ",tptr->data), tptr=tptr->prev);
getch();
}
void deleteNode( NODE ** startPtr, NODE ** endPtr, int delData)
{
NODE * tptr=NULL;
for(tptr = *startPtr ;tptr&& tptr->data!= delData ; tptr = tptr->next);
if(tptr==NULL)
printf("\n\t Value not found");
else if(tptr==*startPtr)
{
tptr->next->prev=NULL;
*startPtr = tptr->next;
free(tptr);
}
else if(tptr==*endPtr)
{
tptr->prev->next=NULL;
*endPtr = tptr->prev;
free(tptr);
}
else
{
tptr->prev->next = tptr->next;
tptr->next->prev= tptr->prev;
free(tptr);
}
}
int main()
{
NODE * start=NULL,*end=NULL;
clrscr();
addNode(&start, & end , 30);
DLLdisplay(start,end);
addNode(&start, & end , 40);
DLLdisplay(start,end);
addNode(&start, & end , 10);
DLLdisplay(start,end);
addNode(&start, & end , 20);
DLLdisplay(start,end);
addNode(&start, & end , 50);
DLLdisplay(start,end);
deleteNode(&start, & end , 30);
DLLdisplay(start,end);
deleteNode(&start, & end , 10);
DLLdisplay(start,end);
deleteNode(&start, & end , 50);
DLLdisplay(start,end);
deleteNode(&start, & end ,70);
DLLdisplay(start,end);
getch();
return 0 ;
}