-
Notifications
You must be signed in to change notification settings - Fork 0
/
circular_double_linked_list.c
171 lines (154 loc) · 5.28 KB
/
circular_double_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
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
166
167
168
169
170
171
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *prev;
struct node *next;
};
struct node *head, *tail = NULL;
void create(int data){
struct node *newnode = (struct node *)malloc(sizeof(newnode));
newnode -> data = data;
newnode -> prev = NULL;
newnode -> next = NULL;
if (head == NULL)
{
head = newnode;
tail = newnode;
head -> prev = tail;
head -> next = tail;
}
else
{
newnode -> prev = tail;
tail->next = newnode;
newnode->next = head;
tail = newnode;
head -> prev = tail;
}
}
void display(){
struct node *current = head;
if (head == NULL){
printf("Empty Linked List");
}
printf("Elements of Linked list are:\n");
do{
printf("%5d\t", current->data);
printf("%5ld\t",(long)current->prev);
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);
if (pos == 1){
newnode->next = head;
head -> prev = newnode;
head = newnode;
head -> prev = tail;
tail -> next = head;
}
else{
struct node *current = head;
int i;
for (i= 1; i< pos-1; i++)
{
current = current -> next ;
}
newnode -> next = current -> next;
newnode -> prev = current;
current -> next = newnode;
current -> next ->next -> prev = 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);
printf("length: %d\n",length);
if (pos == 1){
head = head -> next;
head -> prev = tail;
tail -> next = head;
return ;
}
else if (pos == length){
tail = tail -> prev;
tail -> next -> prev = NULL;
tail -> next = head;
}
else{
temp = head;
for(i= 1; i< pos-1; i++)
{
temp = temp -> next;
}
temp->next-> next -> prev = temp ;
temp->next = temp -> next -> next;
}
}
void linear_search(int key){
struct node *current = head;
int length=0;
do{
length++;
current = current -> next ;
}while (current != head);
current = head;
int i ;
for (i = 1; i<=length; i++){
if (current -> data == key){
printf("element found at %d pos\n",i);
return;
}
else{
current = current->next;
}
}
if (i > length) {
printf("Element not found in the linked list.\n");
}
}
int main(){
int ch=0;
int item;
int pos;
printf("1.Create 2.Insert 3.Delete 4.Display 5.Searching 6.Exit\n");
while (ch != 6){
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;
}