-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircular_linkedlist.c
109 lines (105 loc) · 1.93 KB
/
circular_linkedlist.c
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
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*head=NULL;
void create(int a[],int n)
{
int i=0,j=0;
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 *p=head;
do
{
printf("%d ",p->data);
p=p->next;
} while (p!=head);
}
void insert(int position,int x)
{
int i,j;
struct node *p;
struct node *t=head;
if(position==1)
{
p=(struct node*)malloc(sizeof(struct node));
p->data=x;
if(head==NULL)
{
head=p;
head->next=head;
}
else
{
while(t->next!=head)
{ t=t->next;}
t->next=p;
p->next=head;
head=p;
}
}
else
{
p=(struct node*)malloc(sizeof(struct node));
p->data=x;
for(i=1;i<position-1;i++)
{t=t->next;}
p->next=t->next;
t->next=p;
}
}
int delete(int position)
{
struct node *p=head;
struct node *q;int x;
if(position==1)
{
x=head->data;
while(p->next!=head)
{
p=p->next;
}
p->next=head->next;
free(head);
head=p->next;
return x;
}
else
{
for(int i=0;i<position-2;i++)
{
p=p->next;
}
q=p->next;
x=q->data;
p->next=q->next;
free(q);
return x;
}
}
void main()
{
int a[]={2,3,4,5,6,7};
create(a,6);
display();
insert(3,9);
display();
printf("%d",delete(4));
}