-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoublyLinkedList.c
181 lines (159 loc) · 3.77 KB
/
doublyLinkedList.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
172
173
174
175
176
177
178
179
180
181
#include <stdio.h>
#include <stdlib.h>
struct node *head;
struct node *tail;
struct node{
int data;
struct node *next;
struct node *prev;
};
//display the values of linked-List
void displayForward(struct node *flag){
int count = 0;
while(flag->next!=NULL){
printf("%d->", flag->data);
flag = flag->next;
count++;
}
printf("%d\n", flag->data);
printf("Total node is : %d.\n", count+1);
}
void displayBackward(struct node *flag){
int count = 0;
while(flag->prev!=NULL){
printf("%d->", flag->data);
flag = flag->prev;
count++;
}
printf("%d\n", flag->data);
printf("Total node is : %d.\n", count+1);
}
//search an Item in the linked list
void searchItemF(int val){
struct node *p = head;
int count = 1;
while(p->data != val){
if(p->next == NULL){
printf("Sorry! Didn't find any data as your input.\n");
return;
}
p = p->next;
count++;
}
printf("Data found at %d no. node from front.\n", count);
}
void searchItemB(int val){
struct node *p = tail;
int count = 1;
while(p->data != val){
if(p->prev == NULL){
printf("Sorry! Didn't find any data as your input.\n");
return;
}
p = p->prev;
count++;
}
printf("Data found at %d no. node from back.\n", count);
}
//adding elements in the first position.
void addFirst(int val){
struct node *newN;
newN = malloc(sizeof(struct node));
newN->data = val;
newN->next = head;
newN->prev = NULL;
if(head != NULL){
head->prev = newN;
}
if(head == NULL){
tail = newN;
}
head = newN;
}
//adding element in the last position
void addLast(int val){
struct node *newN, *flag = tail;
newN = malloc(sizeof(struct node));
newN->data = val;
newN->next = NULL;
newN->prev = flag;
flag->next = newN;
tail = newN;
}
//add node at random place
void addItem(int pVal, int val){
struct node *p = head, *newN;
while(p->data!=pVal){
p = p->next;
}
newN = malloc(sizeof(struct node));
newN->data = val;
newN->next = p->next;
newN->prev = p;
p->next->prev = newN;
p->next = newN;
}
//remove node from the first
void deleteFirst(){
struct node *flag;
flag = head;
head->next->prev = NULL;
head = head->next;
printf("%d has been removed.\n", flag->data);
free(flag);
}
//remove node from the last
void deleteLast(){
struct node *flag = tail;
tail->prev->next = NULL;
tail = tail->prev;
flag->next = NULL;
flag->prev = NULL;
printf("%d has been removed.\n", flag->data);
free(flag);
}
//remove node from anywhere of that node
void deleteItem(int val){
//if head is the suspect node
if(head->data == val){
deleteFirst();
return;
}
if(tail->data == val){
deleteLast();
return;
}
struct node *temp = head, *tempO;
while(temp->next->data !=val){
temp = temp->next;
}
tempO = temp->next;
tempO->next->prev = temp;
temp->next = tempO->next;
tempO->next = NULL;
tempO->prev = NULL;
printf("%d has been removed.\n", tempO->data);
free(tempO);
}
int main(void){
head = NULL;
tail = NULL;
addFirst(1);
addFirst(2);
addFirst(3);
addFirst(5);
addFirst(6);
addFirst(7);
addLast(8);
addLast(9);
addLast(10);
addItem(5,4);
deleteFirst();
deleteLast();
deleteItem(3);
displayForward(head);
displayBackward(tail);
searchItemF(6);
searchItemB(6);
return 0;
}