-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircular_single_linked_list_operations.c
More file actions
165 lines (147 loc) · 5.03 KB
/
circular_single_linked_list_operations.c
File metadata and controls
165 lines (147 loc) · 5.03 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
struct node *head, *tail = NULL;
void create(int data){
struct node *newnode = (struct node *)malloc(sizeof(struct node));
newnode -> data = data;
newnode -> next = NULL;
if (head == NULL)
{
head = newnode;
tail = newnode;
}
else
{
tail->next = newnode;
newnode -> next = head;
tail = tail -> next;
}
}
void linear_search(int key){
struct node *current = head;
int length=0;
do{
length++;
current = current -> next ;
}while (current != head);
printf("length:%d\n",length);
current = head;
int i ;
for (i = 1; i<=length; i++){
if (current -> data == key){
printf("element found at %d pos\n",i);
break;
}
else{
current = current->next;
}
}
if (i > length) {
printf("Element not found in the linked list.\n");
}
}
void display(){
struct node *current = head;
if (head == NULL){ // list is null
printf("Empty Linked List");
}
else {//list is not null
printf("Elements of Linked list are:\n");
do{
printf("%5d\t", current->data);
printf("%5ld\t",(long)current->next);
printf("%5ld\t",(long)current);
printf("\n");
current = current->next;
}while(current!=head);
}
}
void insert(int pos){
struct node *newnode = (struct node *)malloc(sizeof(struct node));
printf("enter a new node value:");
scanf("%d",&newnode->data);
struct node *current = head;
if (pos == 1){
newnode->next = head;
head = newnode;
tail -> next = head;
}
else {
int i;
for (i= 1; i< pos-1; i++)
{
current = current -> next ;
}
newnode -> next = current -> next;
current -> next = newnode;
}
}
void delete(int pos)
{
struct node *temp = head;
struct node *current = head;
int length=0;
int i;
do{
length++;
current = current -> next ;
}while(current != head);
if (pos == 1){
head = head -> next;
tail -> next = head;
}
else if (pos == length){
current = head;
int i = 1;
while (i<pos-1) {
current = current -> next;
}
current->next = head;
tail = current;
return;
}
else{
temp = head;
for(i= 1; i< pos-1; i++)
{
temp = temp -> next;
}
temp->next = temp -> next -> next;
}
}
int main(){
int ch=0;
int item;
int pos;
while (ch != 6){
printf("1.Create 2.Insert 3.Delete 4.Display 5.Searching 6.Exit\n");
printf("enter choice:");
scanf("%d",&ch);
switch(ch){
case 1: printf("enter:");
scanf("%d",&item);
create(item);
break;
case 2: printf("enter pos:");
scanf("%d",&pos);
insert(pos);
break;
case 3: printf("enter pos:");
scanf("%d",&pos);
delete(pos);
break;
case 4: display();
break;
case 5: printf("enter key:");
scanf("%d",&item);
linear_search(item);
break;
case 6: exit(0);
}
}
return 0;
}