-
Notifications
You must be signed in to change notification settings - Fork 0
/
DLLL1.C
68 lines (65 loc) · 1.41 KB
/
DLLL1.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
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node* prev;
struct node* next;
};
typedef struct node NODE;
void addNode(NODE**stptr,NODE**endptr,int input)
{
NODE * newnode=NULL,*tptr=NULL;
newnode=(NODE*)malloc(sizeof(NODE));
newnode->data= input;
newnode-> prev= newnode->next= NULL;
if(*stptr == NULL || *endptr== NULL)
*stptr = *endptr = newnode;
else
{
for(tptr=*stptr; tptr&&tptr->data< newnode->data; tptr= tptr->next);
if(tptr==*stptr)
{
newnode->next = *stptr;
(*stptr)->prev = newnode;
*stptr = newnode;
}
else if(tptr==NULL)
{
newnode->prev = *endptr;
(*endptr)->next = newnode;
*endptr = newnode;
}
else
{
newnode->next = tptr;
newnode->prev = tptr->prev;
tptr->prev->next = newnode;
tptr->prev = newnode;
}
}
}
void displayDLL(NODE * st, NODE *ed)
{
NODE * tptr=NULL;
for(printf("\n\t FOR : "),tptr=st; tptr ; printf("%2d ->",tptr->data),tptr=tptr->next);
for(printf("\n\t REV : "),tptr=ed; tptr ; printf("%2d ->",tptr->data),tptr=tptr->prev);
getch();
}
int main()
{
NODE *start=NULL,*end=NULL;
clrscr();
addNode(&start,&end,40);
displayDLL(start,end);
addNode(&start,&end,20);
displayDLL(start,end);
addNode(&start,&end,10);
displayDLL(start,end);
addNode(&start,&end,30);
displayDLL(start,end);
addNode(&start,&end,50);
displayDLL(start,end);
getch();
return 0 ;
}