-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclist.c~
More file actions
executable file
·138 lines (138 loc) · 1.68 KB
/
clist.c~
File metadata and controls
executable file
·138 lines (138 loc) · 1.68 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
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
#include<stdio.h>
#include<stdlib.h>
void create(clist *,int);
clist * insert(clist *,int,int);
clist * delete(clist *,int);
void print(clist *);
int count(clist *);
void freeall(clist *);
void create(clist *p,int n)
{
int x,i;
clist *q;
for(i=1;i<=n;i++)
{
printf("Enter value ");
scanf("%d",&x);
p->data=x;
if(i!=n)
{
q=(clist *)malloc(sizeof(clist));
if(q==NULL)
{
printf("Sorry!!");
exit(0);
}
q->next=p->next;
p->next=q;
p=q;
}
}
}
clist * insert(clist *p,int x,int pos)
{
clist *q=(clist *)malloc(sizeof(clist)),*r=p;
if(q==NULL)
{
printf("Sory!!");
exit(0);
}
q->data=x;
if(pos==1)
{
q->next=p;
while(p->next!=r)
p=p->next;
p->next=q;
r=q;
}
else if(pos==(count(p)+1))
{
while(p->next!=r)
p=p->next;
q->next=r;
p->next=q;
}
else
{
int ct=2;
while(ct!=pos)
{
ct++;
p=p->next;
}
q->next=p->next;
p->next=q;
}
return r;
}
clist * delete(clist *p,int pos)
{
clist *q=p,*r=p;
if(pos==1)
{
q=p->next;
while(r->next!=p)
r=r->next;
r->next=q;
free(p);
r=q;
}
else if(pos==count(p))
{
while(p->next->next!=r)
p=p->next;
q=p->next;
free(q);
p->next=r;
}
else
{
int ct=2;
while(ct!=pos)
{
p=p->next;
ct++;
}
q=p->next;
p->next=p->next->next;
free(q);
}
return r;
}
void print(clist *p)
{
clist *r=p;
printf("The list contains ");
printf("%d ",p->data);
p=p->next;
while(p!=r)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
int count(clist *p)
{
clist *r=p->next;
int ct=1;
while(p!=r)
{
ct++;
r=r->next;
}
return ct;
}
void freeall(clist *p)
{
clist *q,*r=p;
p=p->next;
while(p!=r)
{
q=p;
p=p->next;
free(q);
}
free(r);
}