-
Notifications
You must be signed in to change notification settings - Fork 54
/
Circular_Singly_LinkedList.c
109 lines (102 loc) · 2.6 KB
/
Circular_Singly_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 <stdlib.h>
struct node {
int data;
struct node *link;
};
void insert_beg(struct node **head) {
int value;
struct node *temp = (struct node *)malloc(sizeof(struct node));
struct node *temp2 = *head;
printf("\nEnter the value : ");
scanf("%d",&value);
temp->data = value;
if(*head == NULL) {
*head = temp;
temp->link = *head;
}
else {
do {
temp2 = temp2->link;
}while(temp2 != *head);
temp2 = temp2->link;
temp->link = *head;
*head = temp;
temp2->link = temp;
}
printf("\nValue entered successfully....");
}
void insert_end(struct node **head) {
int value;
struct node *temp = (struct node *)malloc(sizeof(struct node));
struct node *temp2 = *head;
printf("\nEnter the value : ");
scanf("%d",&value);
temp->data = value;
if(*head == NULL) {
*head = temp;
temp->link = *head;
}
else {
do{
temp2 = temp2->link;
}while(temp2 != *head);
temp2 = temp2->link;
temp->link = temp2->link;
temp2->link = temp;
}
}
void delete_sp(struct node **head){
int index,count = 0,countb = 1;
struct node *temp = *head;
struct node *tempb = *head;
if(*head == NULL)
printf("\nList is empty....");
else {
printf("\nEnter the index of element to be deleted : ");
scanf("%d",&index);
while(count != index) {
temp = temp->link;
count++;
}
while(countb != index) {
tempb = tempb->link;
countb++;
}
tempb->link = temp->link;
free(temp);
printf("\nElement in index %d is deleted.",index);
}
}
void display(struct node **head) {
struct node *temp1 = *head;
if(*head == NULL)
printf("\nList is empty....");
else {
printf("\nThe elements in the list are : ");
do {
printf("%d ,",temp1->data);
temp1 = temp1->link;
}while(temp1 != *head);
}
}
int main() {
struct node *head = NULL;
int option;
printf("\n<--Circular queue implementation-->");
while(1) {
printf("\n1.insert at beginning\n2.insert at end\n3.delete at specified position\n4.display\n5.exit\nEnter an option : ");
scanf("%d",&option);
if(option == 1)
insert_beg(&head);
else if(option == 2)
insert_end(&head);
else if(option == 3)
delete_sp(&head);
else if(option == 4)
display(&head);
else
break;
}
return 0;
}