-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.91DoubleLinkedLists
99 lines (98 loc) · 1.57 KB
/
1.91DoubleLinkedLists
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
/*
Double Linked Lists
*/
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
struct node *pre;
int data;
struct node *nex;
};
void append(struct node **,int);
void Delete(struct node **,int);
void main()
{
clrscr();
struct node *n,*q;
n=NULL; int i=0;
int a[5]={2,5,4,6,1};
while(i<5)
{
append(&n,a[i]);
i++;
}
q=n;
printf("Before Deleting : ");
while(n!=NULL)
{
printf("%d ",(n->data));
n=n->nex;
}
n=q;
Delete(&n,6);
printf("\nAfter deleting : ");
while(n!=NULL)
{
printf("%d ",(n->data));
n=n->nex;
}
free(n);
getch();
}
/*any element we want to insert to double linked lists will append */
void append(struct node **a,int num)
{
struct node *temp,*p;
if(*a==NULL)
{
temp=(struct node*)malloc(sizeof(struct node));
temp->pre=NULL;
temp->data=num;
temp->nex=NULL;
*a=temp;
}
else
{
temp=*a; // *a passes the address of first node
while(temp->nex!=NULL)
temp=temp->nex;
p=(struct node*)malloc(sizeof(struct node));
p->pre=temp;
p->data=num;
p->nex=NULL;
temp->nex=p;
}
}
void Delete(struct node **a,int num)
{
struct node *temp;
temp=*a;
while(temp->nex!=NULL)
{
if(temp->data==num)
{
if(temp==*a) //for first node
{
(*a)=(*a)->nex;
(*a)->pre=NULL;
}
else //for any other node
{
if(temp->nex==NULL) //for last node
{
temp->pre->nex=NULL; //list ends at previous node
}
else //for any node in between
{
temp->pre->nex=temp->nex;
temp->nex->pre=temp->pre;
}
}
return;
}
temp=temp->nex;
}
printf(".!..");
}