Make file more readable?
This commit is contained in:
parent
cf905eb686
commit
d3631c4e7a
1 changed files with 45 additions and 28 deletions
|
@ -2,8 +2,6 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Sa se genereze un fisier binar cu 100 de numere reale(float/double) arbitrar.
|
// Sa se genereze un fisier binar cu 100 de numere reale(float/double) arbitrar.
|
||||||
void generate_file(const char * filename) {
|
void generate_file(const char * filename) {
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
|
@ -43,44 +41,63 @@ List init() {
|
||||||
return new_list;
|
return new_list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _insert_middle(List list, double data) {
|
||||||
|
// head ... prev node | wwwti | cur node | next node ... last node
|
||||||
|
// ^
|
||||||
|
// wwwti = where we want to insert.
|
||||||
|
List cur_node = list;
|
||||||
|
List node = malloc(sizeof(struct _dl_list));
|
||||||
|
node->data = data;
|
||||||
|
node->next = cur_node;
|
||||||
|
node->prev = cur_node->prev;
|
||||||
|
|
||||||
|
cur_node->prev->next = node;
|
||||||
|
cur_node->next->prev = cur_node;
|
||||||
|
cur_node->prev = node;
|
||||||
|
}
|
||||||
|
|
||||||
|
void _insert_first(List list, double data) {
|
||||||
|
// head | wwwti
|
||||||
|
// ^
|
||||||
|
List node = malloc(sizeof(struct _dl_list));
|
||||||
|
List head = list;
|
||||||
|
node->data = data;
|
||||||
|
node->next = head;
|
||||||
|
node->prev = head;
|
||||||
|
list->next = node;
|
||||||
|
list->prev = node;
|
||||||
|
}
|
||||||
|
|
||||||
|
void _insert_last(List list, List head, double data) {
|
||||||
|
// cur_node | wwwti | head
|
||||||
|
// ^
|
||||||
|
List node = malloc(sizeof(struct _dl_list));
|
||||||
|
node->data = data;
|
||||||
|
node->next = head;
|
||||||
|
node->prev = list;
|
||||||
|
list->next = node;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* The statement _insert_middle(...) can be replaced by the actual
|
||||||
|
* code from the function _insert_middle. I've chosed to move the
|
||||||
|
* code in a different function in order to keep this one small.
|
||||||
|
* Same goes for the other two: _insert_first and _insert_last
|
||||||
|
*/
|
||||||
void add(List list, double data) {
|
void add(List list, double data) {
|
||||||
List head = list;
|
List head = list;
|
||||||
int flag = 0;
|
int flag = 0;
|
||||||
while( list->next != head ) { // middle insertion
|
while( list->next != head ) { // middle insertion
|
||||||
list = list->next;
|
list = list->next;
|
||||||
if ( data < list->data && !flag) {
|
if ( data < list->data && !flag) {
|
||||||
List cur_node = list;
|
_insert_middle(list, data);
|
||||||
List node = malloc(sizeof(struct _dl_list));
|
|
||||||
node->data = data;
|
|
||||||
node->next = cur_node;
|
|
||||||
node->prev = cur_node->prev;
|
|
||||||
|
|
||||||
cur_node->prev->next = node;
|
|
||||||
cur_node->next->prev = cur_node;
|
|
||||||
cur_node->prev = node;
|
|
||||||
// head ... prev node | wwwti | cur node | next node ... last node
|
|
||||||
// ^
|
|
||||||
// wwwti = where we want to insert.
|
|
||||||
flag = 1;
|
flag = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ( list == head ) { // first insertion
|
if ( list == head ) { // first insertion
|
||||||
List node = malloc(sizeof(struct _dl_list));
|
_insert_first(list, data);
|
||||||
node->data = data;
|
|
||||||
node->next = head;
|
|
||||||
node->prev = head;
|
|
||||||
list->next = node;
|
|
||||||
list->prev = node;
|
|
||||||
list = list->next;
|
list = list->next;
|
||||||
} if ( list->next == head && data > list->data) { // last insertion
|
} if ( list->next == head && data > list->data) { // last insertion
|
||||||
// cur_node | wwwti | head
|
_insert_last(list, head, data);
|
||||||
// ^
|
|
||||||
List node = malloc(sizeof(struct _dl_list));
|
|
||||||
node->data = data;
|
|
||||||
node->next = head;
|
|
||||||
node->prev = list;
|
|
||||||
|
|
||||||
list->next = node;
|
|
||||||
list = list->next; // move to wwwti
|
list = list->next; // move to wwwti
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue