-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDLLdeletion.cpp
163 lines (157 loc) · 2.61 KB
/
DLLdeletion.cpp
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
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
struct node *prev;
}*head,*tail;
void CreateDLL()
{
struct node*newnode;
int choice= 1;
head=0;
printf("Create a Doubly linklist ^~^\n");
while(choice)
{
newnode=(struct node*)malloc(sizeof(struct node));
printf("Enter data : ");
scanf("%d",&newnode->data);
newnode->next=0;
if(tail==0)
{
head=tail=newnode;
}
else
{
tail->next=newnode;
newnode->prev=tail;
tail=newnode;
}
printf("Do U want to continue 1/0 : ");
scanf("%d",&choice);
}
}
Display()
{
struct node*temp;
int count=1;
temp = head;
printf("\nCreated a Doubly linklist : ");
while(temp->next!=0)
{
printf("%d->",temp->data);
temp = temp->next;
count++;
}
printf("%d",temp->data);
printf("\nTotal no. of nodes : %d",count);
}
//count the no. of nodes
int Getcount(struct node *head)
{
// Base case
if(head==0)
return 0;
//count is 1 + count of remaining list
return 1+Getcount(head->next);
}
DeleteAtBeg()
{
struct node*temp;
if(head==0)
{
printf("List is Empty *~* ");
}
else
{
temp=head;
head=head->next;
head->prev=0;
free(temp);
}
}
DeleteAtEnd()
{
struct node*temp;
if(tail==0)
{
printf("List is Empty *~* ");
}
else
{
temp=tail;
tail->prev->next=0;
tail=tail->prev;
free(temp);
}
}
DeleteAtPos()
{
struct node*temp;
int pos,i=1,l;
temp=head;
printf("Enter the position : ");
scanf("%d",&pos);
l=Getcount(head);
if(pos<1||pos>l)
{
printf("Invalid Position *~* ");
}
else if(pos==1)
{
DeleteAtBeg();
}
else if(pos==l)
{
DeleteAtEnd();
}
else
{
while(i<pos)
{
temp=temp->next;
i++;
}
temp->prev->next=temp->next;
temp->next->prev=temp->prev;
free(temp);
}
}
DeleteNode()
{
int choice1=1,choice2 = 1;;
printf("\nWant do Insert something ^~^ ");
while(choice2)
{
printf("\n1. Delete at beg : \n2. Delete at end : \n3. Delete from specified Position : \n4. Not want to Delete anything : \n");
scanf("%d",&choice1);
switch(choice1)
{
case 1:
DeleteAtBeg();
Display();
break;
case 2:
DeleteAtEnd();
Display();
break;
case 3:
DeleteAtPos();
Display();
break;
case 4:
break;
}
printf("\nDo U want to continue 1/0 : ");
scanf("%d",&choice2);
}
}
int main()
{
CreateDLL();
Display();
DeleteNode();
Display();
return 0;
}