forked from sayantant01/cobraa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircular_linked_list.cpp
More file actions
103 lines (95 loc) · 1.66 KB
/
circular_linked_list.cpp
File metadata and controls
103 lines (95 loc) · 1.66 KB
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
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *next;
}*Head;
void Create(int A[], int n)
{
int i;
struct Node *t,*last;
Head = (struct Node*)malloc(sizeof(struct Node));
Head->data=A[0];
Head->next=Head;
last=Head;
for(i=1;i<n;i++)
{
t=(struct Node*)malloc(sizeof(struct Node));
t->data=A[i];
t->next=last->next;
last->next=t;
last=t;
}
}
void Display(struct Node *h)
{
do
{
printf("%d ",h->data);
h=h->next;
} while (h!=Head);
printf("\n");
}
void RDisplay(struct Node *h)
{
static int flag=0;
if(h!=Head || flag==0)
{
flag=1;
printf("%d ",h->data);
RDisplay(h->next);
}
flag=0;
}
int Length(struct Node *p)
{
int len=0;
do
{
len++;
p=p->next;
} while (p!=Head);
return len;
}
void Insert(struct Node *p,int index,int x)
{
struct Node *t;
int i;
if(index<0 || index>Length(Head))
{
return;
}
if(index==0)
{
t = (struct Node*)malloc(sizeof(struct Node));
t->data=x;
if(Head==NULL)
{
Head=t;
Head->next=Head;
}
else
{
while (p->next!=Head)
{
p=p->next;
}
p->next=t;
t->next=Head;
Head=t;
}
}
else
{
for(i=0;i<index-1;i++)
{
p=p->next;
}
t=(struct Node*)malloc(sizeof(struct Node));
t->data=x;
t->next=p->next;
p->next=t;
}
}
int Delete(struct Node *p,int index)