1+ // Circular Linked List
2+ #include <stdio.h>
3+ #include <stdlib.h>
4+
5+ struct Node
6+ {
7+ int data ;
8+ struct Node * next ;
9+ }* Head ;
10+
11+ void create (int A [],int n ) // To Create List
12+ {
13+ int i ;
14+ struct Node * t ,* last ;
15+ Head = (struct Node * )malloc (sizeof (struct Node ));
16+ Head -> data = A [0 ];
17+ Head -> next = Head ;
18+ last = Head ;
19+ for (i = 1 ;i < n ;i ++ )
20+ {
21+ t = (struct Node * )malloc (sizeof (struct Node ));
22+ t -> data = A [i ];
23+ t -> next = last -> next ;
24+ last -> next = t ;
25+ last = t ;
26+ }
27+ }
28+ void Display (struct Node * h ) //Print All Data
29+ {
30+ do
31+ {
32+ printf ("%d " ,h -> data );
33+ h = h -> next ;
34+ }
35+ while (h != Head );
36+ printf ("\n" );
37+ }
38+
39+ int Length (struct Node * p ) //Gives Length Of List
40+ {
41+ int len = 0 ;
42+ do
43+ {
44+ len ++ ;
45+ p = p -> next ;
46+ }
47+ while (p != Head );
48+ return len ;
49+ }
50+ void Insert (struct Node * p ,int index , int x ) //Insert New Element At Desired Index
51+ {
52+ struct Node * t ;
53+ int i ;
54+ if (index < 0 || index > Length (p )){
55+ printf ("Can't Insert, Invalid Index\n" );
56+ return ;
57+ }
58+ if (index == 0 )
59+ {
60+ t = (struct Node * )malloc (sizeof (struct Node ));
61+ t -> data = x ;
62+ if (Head == NULL )
63+ {
64+ Head = t ;
65+ Head -> next = Head ;
66+ }
67+ else
68+ {
69+ while (p -> next != Head )p = p -> next ;
70+ p -> next = t ;
71+ t -> next = Head ;
72+ Head = t ;
73+ }
74+ }
75+ else
76+ {
77+ for (i = 0 ;i < index - 1 ;i ++ )p = p -> next ;
78+ t = (struct Node * )malloc (sizeof (struct Node ));
79+ t -> data = x ;
80+ t -> next = p -> next ;
81+ p -> next = t ;
82+ }
83+ }
84+ int Delete (struct Node * p ,int index ) // Delete Element Of Desired Position (Index)
85+ {
86+ struct Node * q ;
87+ int i ,x ;
88+ if (index < 0 || index > Length (Head )){
89+ printf ("Can't Delete, Invalid Index\n" );
90+ return -1 ;
91+ }
92+ if (index == 1 )
93+ {
94+ while (p -> next != Head )
95+ p = p -> next ;
96+ x = Head -> data ;
97+ if (Head == p )
98+ {
99+ free (Head );
100+ Head = NULL ;
101+ }
102+ else
103+ {
104+ p -> next = Head -> next ;
105+ free (Head );
106+ Head = p -> next ;
107+ }
108+ }
109+ else
110+ {
111+ for (i = 0 ;i < index - 2 ;i ++ )
112+ p = p -> next ;
113+ q = p -> next ;
114+ p -> next = q -> next ;
115+ x = q -> data ;
116+ free (q );
117+ }
118+ return x ;
119+ }
120+ int main ()
121+ {
122+ int A []= {1 ,2 ,3 ,4 ,5 ,6 }; //Initializing With Declaration
123+ int n = 6 ;//Length Of List
124+
125+ create (A ,n );//Create List
126+ Display (Head );//OUTPUT 1 2 3 4 5 6
127+
128+ Delete (Head ,1 );//OUTPUT 0 2 3 4 5 6
129+
130+ Insert (Head ,0 ,9 );//A = {9, 2, 3, 4, 5, 6}
131+ Insert (Head ,1 ,8 );//A = {9, 8, 2, 3, 4, 5, 6}
132+ Insert (Head ,2 ,7 );//A = {9, 8, 7, 2, 3, 4, 5, 6}
133+ Insert (Head ,3 ,0 );//A = {9, 8, 7, 0, 2, 3, 4, 5, 6}
134+
135+ Delete (Head ,11 );//OUTPUT "Can't Delete, Invalid Index"
136+
137+ Display (Head );//OUTPUT 9 8 7 0 2 3 4 5 6
138+ return 0 ;
139+ }
0 commit comments