-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeleteCLL.c
More file actions
86 lines (84 loc) · 1.41 KB
/
DeleteCLL.c
File metadata and controls
86 lines (84 loc) · 1.41 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
#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\t",h->data);
h=h->next;
}while(h!=Head);
printf("\n");
}
int Length(struct Node *p)
{
int len=0;
do
{
len++;
p=p->next;
}while(p!=Head);
return len;
}
int Delete(struct Node *p, int index)
{
struct Node *q;
int i,x;
if(index<0 || index> Length(Head))
return -1;
if(index==1)
{
while(p->next!=Head)p=p->next;
x=Head->data;
if(Head==p)
{
free(Head);
Head=NULL;
}
else
{
p->next=Head->next;
free(Head);
Head=p->next;
}
}
else
{
for(i=0;i<index-2;i++)
p=p->next;
q=p->next;
p->next=q->next;
x=q->data;
free(q);
}
return x;
}
int main()
{
int A[]={3,5,7,10,15};
create(A,5);
Delete(Head,1);
Display(Head);
return 0;
}