-
Notifications
You must be signed in to change notification settings - Fork 368
/
Circular Linked List.c
110 lines (104 loc) · 2.45 KB
/
Circular Linked List.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
110
/* A circular linked list is a type of linked list in which the first and the last nodes are also connected to each other to form a circle.
In this the address of the last node consists of the address of the first node.*/
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
}*first=NULL;
//For Creation of Linked List
void create(int A[],int n)
{
struct node* last=NULL;
first=(struct node*)malloc(sizeof(struct node));
first->data=A[0];
first->next=NULL;
last=first;
first->next=first;//To form a circle
for(int i=1;i<n;i++)//To add further nodes
{
struct node* temp=(struct node*)malloc(sizeof(struct node));
temp->data=A[i];
temp->next=NULL;
last->next=temp;
last=temp;
last->next=first;
}
}
//For Insertion Of Element in Linked List
void insert(int pos,int key)
{
struct node* p=first;
struct node* temp=(struct node*)malloc(sizeof(struct node));
temp->data=key;
temp->next=NULL;
if(pos==0)//If we want to insert node in the beginning
{
while(p->next!=first)
{
p=p->next;
}
p->next=temp;
temp->next=first;
first=temp;
}
else
{
for(int i=1;i<pos;i++)
{
p=p->next;
}
temp->next=p->next;
p->next=temp;
}
}
//For performing Deletion In Linked List
int deletei(int pos)
{
struct node* p=first;
struct node* q;
if(pos==0)//If we want to delete the node from the beginning
{
while(p->next!=first)
{
p=p->next;
}
q=first;
p->next=first->next;
first=first->next;
free(q);
}
else
{
for(int i=1;i<pos;i++)
{
p=p->next;
}
q=p->next;
p->next=p->next->next;
free(q);
}
}
//For printing the elements in Linked List
void display(struct node* p)
{
do
{
printf("%d ",p->data);//for printing p's data
p=p->next;//for moving p ahead
}while(p!=first);
}
int main()
{
int A[]={2,3,4,5,6};
create(A,5);//Calling create function
printf("Displaying the Circular Linked List after Creation\n");
display(first);
insert(5,12);//calling insert function
printf("\nDisplaying the Circular Linked List after Insertion at a particular position \n");
display(first);
deletei(4);//calling delete function
printf("\nDisplaying the Circular Linked List after Deletion from a particular position \n");
display(first);
}