-
Notifications
You must be signed in to change notification settings - Fork 0
/
insertioncircularll.c
96 lines (91 loc) · 2.12 KB
/
insertioncircularll.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
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *link;
} * head;
// appending the c.l.l.
void append(int a[], int n)
{
struct node *temp, *last;
head = (struct node *)malloc(sizeof(struct node));
head->data = a[0];
head->link = head;
last = head;
for (int i = 1; i < n; i++)
{
temp = (struct node *)malloc(sizeof(struct node));
temp->data = a[i];
temp->link = last->link;
last->link = temp;
last = temp;
}
printf("\nThe C.L.L is successfully created!");
}
// Displaying the c.l.l.
void display()
{
struct node *p;
p = head;
do
{
printf("%d, ", p->data);
p = p->link;
} while (p != head);
}
// inserting at given position in c.l.l.
void insert(struct node *h, int pos, int ele)
{
struct node *temp, *p;
temp = (struct node *)malloc(sizeof(struct node));
temp->data = ele;
temp->link = temp;
p = h;
if (pos != 0)
{
for (int i = 0; i < pos - 1; i++)
{
p = p->link;
}
temp->link = p->link;
p->link = temp;
printf("\nThe element is successfully inserted!");
printf("\nThe c.l.l after insertion - ");
display();
}
else
{
do
{
p = p->link;
} while (p->link != h);
temp->link = p->link;
p->link = temp;
head = temp;
printf("\nThe c.l.l after insertion - ");
display();
}
}
int main()
{
int len = 5, n, ele;
char cho;
int a[] = {1, 2, 3, 4, 5};
append(a, len);
printf("\nThe c.l.l is - ");
display();
printf("\nPress 'y' is you wish to insert element: ");
scanf("%c", &cho);
if (cho == 'y' || cho == 'Y')
{
printf("\nTake a look at the c.l.l - ");
display();
printf("\nEnter the position (0-n) where you wish to insert: ");
scanf("%d", &n);
printf("\nEnter the element: ");
scanf("%d", &ele);
insert(head, n, ele);
}
return 0;
}