-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcllist.cpp
140 lines (137 loc) · 2.3 KB
/
cllist.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
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*head,*tail;
void CreateCLL()
{
struct node*temp,*newnode;
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(head==0)
{
head = temp=newnode;
}
else
{
temp->next=newnode;
temp=newnode;
}
temp->next=head;
printf("Do U want to continue 1/0 : ");
scanf("%d",&choice);
}
printf("\nFor Conformation list starts From : (%d",temp->next->data);
printf(")\n");
}
Display()
{
struct node*temp,*newnode;
int count=1;
if(head==0)
{
printf("\nList is Empty *~* ");
}
else
{
temp=head;
printf("\nCreated Circular Linklist : ");
while(temp->next!=head)
{
printf("%d->",temp->data);
temp = temp->next;
count++;
}
printf("%d",temp->data);
}
}
InsertatBeg()
{
struct node*temp,*newnode;
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;
}
Display();
}
InsertatEnd()
{
struct node*temp,*newnode;
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;
}
Display();
}
InsertatPos()
{
struct node*temp,*newnode;
int pos,i=1,l;
printf("Enter the position : ");
scanf("%d",&pos);
l=10;
if(pos<0||pos>l)
{
printf("Invalid Position *~* ");
}
else if(pos==1)
{
InsertatBeg();
}
else
{
newnode=(struct node*)malloc(sizeof(struct node));
printf("\nEnter data : ");
scanf("%d",&newnode->data);
newnode->next=0;
temp=temp->next;
while(i<pos-1)
{
temp=temp->next;
i++;
}
newnode->next=temp->next;
temp->next=newnode;
}
}
int main()
{
CreateCLL();
Display();
InsertatBeg();
Display();
InsertatEnd();
Display();
InsertatPos();
Display();
return 0;
}