-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDCLLinsertion.cpp
184 lines (180 loc) · 3.33 KB
/
DCLLinsertion.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
struct node *prev;
}*head,*newnode,*tail,*temp;
void CreateDCLL()
{
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(head==0)
{
head=tail=newnode;
head->next=head;
head->prev=head;
}
else
{
tail->next=newnode;
newnode->prev=tail;
newnode->next=head;
head->prev=newnode;
tail=newnode;
}
printf("Do U want to continue 1/0 : ");
scanf("%d",&choice);
}
printf("\nFor Conformation list : \tStarts From : (%d",tail->next->data);
printf(")\tAnd End At : (%d",tail->data);
printf(")\n");
}
Display()
{
struct node*temp;
int count=1;
temp = head;
printf("\nCreated a Doubly Circular linklist : ");
while(temp!=tail)
{
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);
}
InsertatBeg()
{
struct node*temp;
newnode=(struct node*)malloc(sizeof(struct node));
printf("Enter data : ");
scanf("%d",&newnode->data);
newnode->next=0;
if(head==0)
{
head=tail=newnode;
newnode->prev=tail;
newnode->next=head;
}
else
{
newnode->next=head;
head->prev=newnode;
newnode->prev=tail;
tail->next=newnode;
head=newnode;
}
Display();
}
InsertatEnd()
{
newnode=(struct node*)malloc(sizeof(struct node));
printf("Enter data : ");
scanf("%d",&newnode->data);
newnode->next=0;
if(head==0)
{
head=tail=newnode;
newnode->prev=tail;
newnode->next=head;
}
else
{
newnode->prev=tail;
tail->next=newnode;
newnode->next=head;
head->prev=newnode;
tail=newnode;
}
Display();
}
InsertatPos()
{
struct node*temp;
temp=head;
int pos,l,i=1;
printf("Enter the Position : ");
scanf("%d",&pos);
l=Getcount(head);
if(pos<1||pos>l)
{
printf("\nInvalid Position *~*");
}
else if(pos==1)
{
InsertatBeg();
}
else if(pos==l)
{
InsertatEnd();
}
else
{
newnode->next=0;newnode->prev=0;
newnode=(struct node*)malloc(sizeof(struct node));
printf("Enter data : ");
scanf("%d",&newnode->data);
while(i<pos-1)
{
temp=temp->next;
i++;
}
newnode->prev=temp;
newnode->next=temp->next;
temp->next->prev=newnode;
temp->next=newnode;
}
}
InsertNode()
{
int choice1=1,choice2 = 1;;
printf("\nWant do Insert something ^~^ ");
while(choice2)
{
printf("\n1. Insert at beg : \n2. Insert at end : \n3. Insert at specified Position : \n4. Not want to Insert anything : \n");
scanf("%d",&choice1);
switch(choice1)
{
case 1:
InsertatBeg();
break;
case 2:
InsertatEnd();
break;
case 3:
InsertatPos();
break;
case 4:
break;
}
printf("\nDo U want to continue 1/0 : ");
scanf("%d",&choice2);
}
}
int main()
{
CreateDCLL();
Display();
InsertNode();
Display();
return 0;
}