Algorithms_in_C  1.0.0
Set of algorithms implemented in C.
doubly_linked_list.c File Reference

Implementation of Doubly linked list More...

#include <stdio.h>
#include <stdlib.h>
Include dependency graph for doubly_linked_list.c:

Data Structures

struct  list
 Doubly linked list struct. More...
 

Typedefs

typedef struct list List
 Doubly linked list struct.
 

Functions

Listcreate (double value)
 Create list function, a new list containing one node will be created. More...
 
Listinsert (List *list, double value, int pos)
 Insertion by position into the list function. More...
 
Listdelete (List *list, int pos)
 Deletion by position into the list function. More...
 
int search (List *list, double value)
 Search value into the list function. More...
 
void print (List *list)
 Print list function. More...
 
void example ()
 Example function. More...
 
int main ()
 Main function. More...
 

Detailed Description

Implementation of Doubly linked list

A doubly linked list is a data structure with a sequence of components called nodes. Within that nodes there are three elements: a value recorded, a pointer to the next node, and a pointer to the previous node.

In this implementation, the functions of creating the list, inserting by position, deleting by position, searching for value, printing the list, and an example of how the list works were coded.

Author
Gabriel Mota Bromonschenkel Lima

Function Documentation

◆ create()

List * create ( double  value)

Create list function, a new list containing one node will be created.

Parameters
valuea value to be saved into the first list node
Returns
new_list the list created
93 {
94  List *new_list = (List *)malloc(sizeof(List));
95  new_list->value = value;
96  new_list->next = NULL;
97  new_list->prev = NULL;
98  return new_list;
99 }

◆ delete()

List * delete ( List list,
int  pos 
)

Deletion by position into the list function.

Parameters
lista doubly linked List
posa position into the list for value Deletion
Returns
list the input list with deleted values or the same list
180 {
181  // list NULL case
182  if (list == NULL)
183  return list;
184 
185  // position existing case
186  if (pos > 0)
187  {
188  List *cpy = list, *tmp = cpy;
189  int flag = 1, index = 1, size = 0;
190 
191  while (tmp != NULL)
192  {
193  size++;
194  tmp = tmp->next;
195  }
196 
197  // first position case
198  if (pos == 1)
199  {
200  if (size == 1)
201  return NULL;
202  cpy = cpy->next;
203  cpy->prev = NULL;
204  return cpy;
205  }
206 
207  // position existing in list range case
208  if (size + 2 > pos)
209  {
210  while (cpy->next != NULL && index < pos)
211  {
212  flag++;
213  index++;
214  cpy = cpy->next;
215  }
216 
217  if (flag == pos)
218  {
219  // position into list with no poiting for NULL
220  if (cpy->next != NULL)
221  {
222  cpy->prev->next = cpy->next;
223  cpy->next->prev = cpy->prev;
224  }
225 
226  // last position case
227  else
228  cpy->prev->next = NULL;
229  }
230  }
231  return list;
232  }
233 }

◆ example()

void example ( )

Example function.

Returns
void
270 {
271  List *my_list = NULL;
272  double node_value = 0;
273  int searching;
274 
275  my_list = create(node_value);
276  my_list = insert(my_list, 3, 1);
277  my_list = insert(my_list, 5, 3);
278  my_list = insert(my_list, 10, 3);
279  my_list = insert(my_list, 20, 3);
280 
281  print(my_list);
282  searching = search(my_list, 20);
283  printf("\n%d\n", searching);
284 
285  my_list = delete (my_list, 1);
286  my_list = delete (my_list, 1);
287  my_list = delete (my_list, 1);
288  my_list = delete (my_list, 1);
289 
290  print(my_list);
291  searching = search(my_list, 20);
292  printf("\n%d\n", searching);
293 }
Here is the call graph for this function:

◆ insert()

List * insert ( List list,
double  value,
int  pos 
)

Insertion by position into the list function.

Parameters
lista doubly linked List
valuea value to be inserted into the list
posa position into the list for value insertion
Returns
list the input list with a node more or the same list
109 {
110  // list NULL case
111  if (list == NULL)
112  {
113  list = create(value);
114  return list;
115  }
116 
117  // position existing case
118  if (pos > 0)
119  {
120  List *cpy = list, *tmp = cpy;
121  int flag = 1, index = 1, size = 0;
122 
123  while (tmp != NULL)
124  {
125  size++;
126  tmp = tmp->next;
127  }
128 
129  // first position case
130  if (pos == 1)
131  {
132  List *new_node = create(value);
133  new_node->next = cpy;
134  cpy->prev = new_node;
135  list = new_node;
136  return list;
137  }
138 
139  // position existing in list range case
140  if (size + 2 > pos)
141  {
142  while (cpy->next != NULL && index < pos)
143  {
144  flag++;
145  index++;
146  cpy = cpy->next;
147  }
148 
149  List *new_node = (List *)malloc(sizeof(List));
150  new_node->value = value;
151 
152  // position into list with no poiting for NULL
153  if (flag == pos)
154  {
155  cpy->prev->next = new_node;
156  new_node->next = cpy;
157  new_node->prev = cpy->prev;
158  cpy->prev = new_node;
159  }
160 
161  // last position case
162  if (flag < pos)
163  {
164  new_node->next = cpy->next;
165  new_node->prev = cpy;
166  cpy->next = new_node;
167  }
168  }
169  return list;
170  }
171 }
Here is the call graph for this function:

◆ main()

int main ( void  )

Main function.

Returns
0 on exit
80 {
81  // examples for better understanding
82  example();
83  // code here
84  return 0;
85 }
Here is the call graph for this function:

◆ print()

void print ( List list)

Print list function.

Parameters
lista doubly linked List
Returns
void
257 {
258  if (list != NULL)
259  {
260  printf("%f\t", list->value);
261  print(list->next);
262  }
263 }

◆ search()

int search ( List list,
double  value 
)

Search value into the list function.

Parameters
lista doubly linked list
valuea value to be looked for into the list
Returns
1 if the looked up value exists
0 if the looked up value doesn't exist
243 {
244  if (list == NULL)
245  return 0;
246  if (list->value == value)
247  return 1;
248  search(list->next, value);
249 }
create
List * create(double value)
Create list function, a new list containing one node will be created.
Definition: doubly_linked_list.c:92
list::prev
struct list * prev
directing to other nodes or NULL
Definition: doubly_linked_list.c:26
example
void example()
Example function.
Definition: doubly_linked_list.c:269
print
void print(List *list)
Print list function.
Definition: doubly_linked_list.c:256
list::value
double value
value saved on each node
Definition: doubly_linked_list.c:25
search
int search(List *list, double value)
Search value into the list function.
Definition: doubly_linked_list.c:242
insert
List * insert(List *list, double value, int pos)
Insertion by position into the list function.
Definition: doubly_linked_list.c:108
list
Doubly linked list struct.
Definition: doubly_linked_list.c:24