-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCLLdeletion.cpp
167 lines (161 loc) · 2.77 KB
/
CLLdeletion.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
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*tail,*temp,*newnode;
void CreateCLL()
{
int choice = 1;
printf("Create a circular linklist ^~^\n");
while(choice)
{
newnode=(struct node*)malloc(sizeof(struct node));
printf("Enter data : ");
scanf("%d",&newnode->data);
newnode->next=0;
if(tail==0)
{
tail=newnode;
tail->next=newnode;
}
else
{
newnode->next=tail->next;
tail->next=newnode;
tail=newnode;
}
printf("Do U want to continue 1/0 : ");
scanf("%d",&choice);
}
printf("\nFor Conformation list starts From : (%d",tail->next->data);
printf(")\n");
}
Display()
{
int count=1;
if(tail==0)
{
printf("\nList is Empty *~* ");
}
else
{
temp=tail->next;
printf("\nCreated Circular Linklist : ");
while(temp->next!=tail->next)
{
printf("%d->",temp->data);
temp = temp->next;
count++;
}
printf("%d",temp->data);
printf("\nCount Value : %d",count);
}
}
DeleteFromBeg()
{
temp=temp->next;
if(tail==0)
{
printf("\nList is Empty '~' ");
}
else if(temp->next==temp)
{
tail=0;
free(temp);
}
else
{
tail->next=temp->next;
free(temp);
}
}
DeleteFromEnd()
{
struct node *current,*previous;
current=tail->next;
if(tail==0)
{
printf("\nList is Empty '~' ");
}
else if(current->next==current)
{
tail=0;
free(current);
}
else
{
while(current->next!=tail->next)
{
previous=current;
current=current->next;
}
previous->next=tail->next;
tail=previous;
free(current);
}
}
DeleteAtPos()
{
struct node*current,*nextnode;
int pos,i=1,l;
printf("Enter the position : ");
scanf("%d",&pos);
l=20;
if(pos<1||pos>l)
{
printf("Invalid Position *~* ");
}
else if(pos==1)
{
DeleteFromBeg();
}
else
{
current=tail->next;
while(i<pos-1)
{
current=current->next;
i++;
}
nextnode=current->next;
current->next=nextnode->next;
free(nextnode);
}
Display();
}
DeleteNode()
{
int choice1=1,choice=1;
while(choice)
{
printf("\nWant do Delete something ^~^ ");
printf("\n1. Delete at beg : \n2.Delete at end : \n3. Delete at specified Position : \n");
scanf("%d",&choice1);
switch(choice1)
{
case 1:
DeleteFromBeg();
Display();
break;
case 2:
DeleteFromEnd();
Display();
break;
case 3:
DeleteAtPos();
break;
}
printf("\nDo U want to continue 1/0 : ");
scanf("%d",&choice);
}
}
int main()
{
CreateCLL();
Display();
DeleteNode();
Display();
return 0;
}