int main() { struct linkedlist *l; struct node *n, *before; printf("testing list create..."); if ((l = linkedlist_create()) == NULL) goto fail; printf("[ok]\n"); printf("testing node create..."); if ((n = node_create('a')) == NULL) goto fail; printf("[ok]\n"); printf("testing insert of 1st node..."); linkedlist_insert_before(l, NULL, n); if (l->head != n) goto fail; printf("[ok]\n"); printf("testing insert at head..."); n = node_create('c'); linkedlist_insert_before(l, l->head, n); if (l->head != n) goto fail; printf("[ok]\n"); printf("testing get node..."); before = linkedlist_get_node(l, 1); if (before->c != 'a') goto fail; printf("[ok]\n"); printf("testing insert in middle..."); n = node_create('b'); linkedlist_insert_before(l, before, n); n = linkedlist_get_node(l, 1); if (n->c != 'b') goto fail; printf("[ok]\n"); printf("testing count..."); if (linkedlist_count(l) != 3) goto fail; printf("[ok]\n"); printf("testing del..."); linkedlist_del(l, n); printf("[ok]\n"); printf("testing free..."); linkedlist_free(l); printf("[ok]\n"); return 0; fail: printf("[failed]\n"); return -1; }
void adapt_width_show(N *head) { struct winsize size; int max_col_width = 0; int row = 1; N ** linkedlist_array = NULL; int count = 0; int col = 0; N * p = NULL; int i = 0,j = 0,k = 0; int is_success = 1; int sum_length = 0; file_info * temp = NULL; int *maxsize_array = NULL; ioctl(STDIN_FILENO,TIOCGWINSZ,&size); count = linkedlist_count(head); linkedlist_array = (N **)malloc(sizeof(N * )*(count)); if(linkedlist_array != NULL) { for(p = head->next,i = 0;p !=NULL;p = p->next,i++) { linkedlist_array[i] = (N *)p; } while(1) { for(i = 0;i < count;i += row) { for(k = i;(k < (i+row)) && (k < count);k ++) { temp = ((file_info *)linkedlist_array[k]->datapointer); if(max_col_width < temp->file_name_length) { max_col_width = temp->file_name_length; } } sum_length += max_col_width+2; max_col_width = 0; } if(sum_length <= size.ws_col) { break; } row ++; sum_length = 0; } if(count%row == 0) { col = count/row; } else { col = count/row+1; } maxsize_array = (int *)malloc(sizeof(int)*col); for(i = 0,j = 0;i < count;i += row,j++) { for(k = i;(k < (i + row)) && k < count;k ++) { temp = ((file_info *)linkedlist_array[k]->datapointer); if(max_col_width < temp->file_name_length) { max_col_width = temp->file_name_length; } } maxsize_array[j] = max_col_width; max_col_width = 0; } for(i = 0;i < row ;i++) { for(j = i,k = 0;j < count;j += row,k++) { temp = ((file_info *)linkedlist_array[j]->datapointer); printf("%*s",-(maxsize_array[k]+2),temp->file_name); } printf("\n"); } } }