-
Notifications
You must be signed in to change notification settings - Fork 4
/
..Double_Linked_List.c
154 lines (127 loc) · 3 KB
/
..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
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct node{
int data;
struct node *next;
struct node *prev;
};
struct node *add_head(struct node *,int);
struct node *add_last(struct node *,int);
struct node *delete_node(struct node *,int);
void print_list(struct node *);
int count_elements(struct node *);
int main(){
struct node *head;
head = NULL;
int secim, key, counter;
while(1){
printf("Press Any Key for Continue.. ");
getch();
system("CLS");
printf("1 ==> Add Head \n2 ==> Add Last\n");
printf("3 ==> Print List \n4 ==> Count Elements\n");
printf("5 ==> Delete Node \n6 ==> Exit\n");
printf("\nMake Your Choice : "); scanf("%d",&secim);
system("CLS");
switch(secim){
case 1:
printf("Please enter node's data : "); scanf("%d",&key);
head = add_head(head,key);
break;
case 2:
printf("Please enter node's data : "); scanf("%d",&key);
head = add_last(head,key);
break;
case 3:
if(head == NULL){
printf("List is empty..\n");
}
else{
print_list(head);
}
break;
case 4:
counter = count_elements(head);
printf("There are %d elements in your list\n",counter);
break;
case 5:
printf("Please enter node's data : "); scanf("%d",&key);
head = delete_node(head,key);
break;
case 6:
printf("Exited !!");
return 0;
default:
printf("Wrong choice !! Try Again !!");
break;
}
}
}
struct node *add_head(struct node *head,int key){
if (head == NULL){
head = (struct node *) malloc(sizeof(struct node));
head->data = key;
head->next = NULL;
head->prev = NULL;
}
else{
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
temp->data = key;
temp->next = head;
temp->prev = NULL;
head->prev = temp;
head = temp;
}
return head;
}
struct node *add_last(struct node *head,int key){
if (head == NULL){
head = (struct node *) malloc(sizeof(struct node));
head->data = key;
head->next = NULL;
head->prev = NULL;
}
else{
struct node *temp,*tail;
temp = (struct node *)malloc(sizeof(struct node));
tail = head;
while(tail->next != NULL)
tail = tail->next;
temp->data = key;
temp->next = NULL;
tail->next = temp;
temp->prev = tail;
}
return head;
}
void print_list(struct node *head){
if(head == NULL)
return;
printf("%d\n",head->data);
print_list(head->next);
}
int count_elements(struct node *head){
if(head == NULL)
return 0;
return 1+count_elements(head->next);
}
struct node *delete_node(struct node *head,int key){
if(head->data == key){
head = head->next;
free(head->prev);
head->prev = NULL;
}
else{
struct node *temp = head;
while(temp->data != key)
temp = temp->next;
if(temp->next->data == key){
temp->prev->next = temp->next;
temp->next->prev = temp->prev;
free(temp);
}
}
return head;
}