-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoubly_linked_list.c
189 lines (165 loc) · 3.74 KB
/
doubly_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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
filename : doubly_linked_list.c
author : Nishant Ghai
*/
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
int data;
struct Node* prev;
struct Node* next;
}Node;
void print_linked_list( Node *ptrHead )
{
if( ptrHead == NULL )
{
printf("\nThe linked list is empty.\n");
}
else
{
Node *temp = ptrHead;
while( temp != NULL )
{
printf("%d\t",temp->data);
temp = temp->next;
}
}
}
void print_linked_list_reverse( Node* ptrHead )
{
if( ptrHead == NULL )
{
printf("\nThe linked list is empty.\n");
}
else
{
Node* temp = ptrHead;
while( temp->next != NULL )
{
temp = temp->next;
}
//now "temp" points to last node, now traverse backwards
while( temp != NULL )
{
printf("%d\t",temp->data);
temp = temp->prev;
}
}
}
void add_node_to_start( Node** ptrHead, int data )
{
Node *temp = (Node*)malloc(sizeof(Node));
temp->data = data;
temp->prev = NULL;
if( *ptrHead == NULL )
{
//list is currently empty
temp->next = NULL;
}
else
{
temp->next = *ptrHead;
(*ptrHead)->prev = temp;
}
(*ptrHead) = temp;
}
void add_node_to_end( Node** ptrHead, int data )
{
Node *temp = (Node*)malloc(sizeof(Node));
temp->data = data;
temp->prev = NULL;
temp->next = NULL;
if( *ptrHead == NULL )
{
*ptrHead = temp;
}
else
{
Node* curr = *ptrHead;
while( curr->next != NULL )
{
curr = curr->next;
}
temp->prev = curr;
curr->next = temp;
}
}
void add_node_to_pos( Node** ptrHead, int data, int pos )
{
if( pos < 0 )
{
printf("\nThe position %d is invalid.\n", pos);
}
else if( pos == 0 )
{
//same as add_node_to_start()
Node *temp = (Node*)malloc(sizeof(Node));
temp->data = data;
temp->prev = NULL;
if( *ptrHead == NULL )
{
//list is currently empty
temp->next = NULL;
}
else
{
temp->next = *ptrHead;
(*ptrHead)->prev = temp;
}
(*ptrHead) = temp;
}
else
{
int i = 0;
Node *temp = NULL , *new_node = NULL;
if( *ptrHead == NULL )
{
printf("\nInvalid pos %d for empty list.\n",pos);
return;
}
temp = (*ptrHead);
for( i = 0; i < pos-1 ; i++)
{
if( temp->next == NULL ) //if we have reached the end of the list
{
if( pos-i > 1 )
{
printf("\nInvalid position %d for the given list.\n",pos);
return;
}
else
{
break;//temp
}
}
temp = temp->next;
}
//temp is pointing to node after which we have to insert the new node
new_node = (Node*)malloc(sizeof(Node));
new_node->data = data;
new_node->prev = temp;
new_node->next = temp->next;
if( temp->next != NULL )
{
temp->next->prev = new_node;
}
temp->next = new_node;
}
}
int main(void)
{
Node *head = NULL;
add_node_to_start( &head , 33 );
add_node_to_start( &head , 22 );
add_node_to_start( &head , 11 );
add_node_to_end( &head, 100 );
add_node_to_pos( &head, 200, 1 );
printf("\nThe linked list is:\n");
print_linked_list( head );
printf("\n\nThe linked list printed in reverse is:\n");
print_linked_list_reverse( head );
printf("\n\n");
return 0;
}
//end of file