static node *instantiate_permutation(unsigned seed, struct coefficients *usable)
{
    srand(seed);

    //instantiate a permutation of DCT coefficients, using Fisher-Yates algorithm
    size_t permutationArray[usable->size];
    
    for (size_t index = 0; index < usable->size; index++) {
        permutationArray[index]=index;
    }
    
    printf("running permutation\n");
    size_t randomIndex, temp;
    size_t maxRandom = usable->size;
    for (size_t index = 0; index < usable->size; index++) {

        randomIndex = ((unsigned int) rand()) % maxRandom--;
        temp = permutationArray[randomIndex];
        permutationArray[randomIndex] = permutationArray[index];
        permutationArray[index] = temp;
    }
    
    printf("done permuting\n");
    
    node *root = malloc(sizeof(node));
    node *current_node = root;
    
    printf("creating linked list...\n");

    for (size_t linked_list_index = 0; linked_list_index < usable->size; linked_list_index++) {

        //creates linked list of the coefficients, as shuffled by the permutation
        node *new_node = malloc(sizeof(node));
        new_node->coeff_struct = usable->array[permutationArray[linked_list_index]];
        add_to_linked_list(new_node, current_node);
        current_node = current_node->next;

    }
    
    printf("done creating linked list\n");
    
    current_node = root->next;
    for (int j = 0; j < 30; j++) {
        printf("%i ", current_node->coeff_struct.coefficient);
        current_node = current_node->next;
    }
    
    //print_linked_list(root);
    return root;
}
Beispiel #2
0
int main(int argc, const char * argv[]) {
    while (END_OF_FILE != t_type) {
	getToken();
	
	if (NUM == t_type) {
	    add_to_linked_list();
	    
	} else if (ID == t_type) {
	    if ((0 == strncmp(current_token, "cse340", strlen("cse340"))) ||
		(0 == strncmp(current_token, "programming", strlen("programming"))) ||
		(0 == strncmp(current_token, "language", strlen("language")))) {
		
		if ((strlen("cse340") == strlen(current_token)) ||
		    (strlen("programming") == strlen(current_token)) ||
		    (strlen("language") == strlen(current_token))) {
		    
		    add_to_linked_list();
		}
	    }
	}
    }
    print_and_free_linked_list();
    return 0;
}
int main(int argc, const char * argv[])
{
    //first, we create the linked list header
    node *root = malloc(sizeof(node));
    node *end = malloc(sizeof(node));
    
    root->next = end;
    end->prev = root;
    end->next = NULL;
    node *current_node = root;
    
    int index;
    int list_size = 100;
    
    //then make linked list of 350
    //this will be our usable coefficient buffer
    for (index = 0; index<list_size; index++){
        int random_number = arc4random() % 150;
        random_number =  (random_number%2)? random_number*-1: random_number;
        
        node *new_node = malloc(sizeof(node));
        new_node->coeff_struct.coefficient = random_number;
        
        add_to_linked_list(new_node, current_node);
        current_node = current_node->next;
    }
    
    printf("\n");
    printf("loop coefficients are: ");
    //print_linked_list(root);
    printf("\n");
    
    char *message = "Hello";/* that whirl me I know not whither\nYour schemes, politics, fail, lines give way, substances mock and elude me,\nOnly the theme I sing, the great and strong-possess'd soul, eludes not,\nOne's-self must never give way--that is the final substance--that\nout of all is sure,\nOut of politics, triumphs, battles, life, what at last finally remains?\nWhen shows break up what but One's-Self is sure?";*/
    
    embedMessageIntoCoefficients(message, root, list_size);
    
    printf("\n\nour stego coefficients: \n");
    print_linked_list(root);
    
    printf("\n");
    int message_size = (int)strlen(message)*8; //getting size of message in bits
    char *extractedMessage = malloc(sizeof(char) *message_size);
    extractMessageFromCoefficients(root, list_size, message_size, extractedMessage);
    
    return 0;
}
int main() {
    int n, m;
    scanf( "%d %d", &n, &m );
    // the array of nodes to mark;
    graph_vertex* nodes = (graph_vertex*)calloc( n, sizeof(graph_vertex) );
    if( !nodes ) {
        perror( "malloc" ); 
        exit(1);
    }

    for( int i = 0; i < m; ++i ) {
        int n1, n2;
        scanf("%d %d", &n1, &n2 );
        // allocate a new node on the heap
        link_node* ln = new_node(n2);

        // add the linked nodes to the linked lists
        graph_vertex* v = &nodes[n1];
        add_to_linked_list( v, ln );
    }

    // create space for the topological sort to be stored
    topo_sort = (int*)calloc(n, sizeof(int));

    // perform a topological sort on the graph:
    for( int i = 0; i < n; ++i ) {
        // connected component!
        if( !nodes[i].marked ) {
            // do a topological sorting DFS
            dfs(nodes, i);
        } 
    }

    // construct the heart and run the dynamic algorithm
    int* heart = (int*)calloc(n,sizeof(int));
    for( int i = 0; i < n; ++i ) {
        link_node* l = nodes[topo_sort[i]].first;
        if( l ) {
            int max = 0;
            while( l ) {
                if( max <= heart[l->v] ) 
                    max = 1 + heart[l->v];
                l = l->next;
            }
            heart[topo_sort[i]] = max;
        }
    }

    // find the max value in the heart and print it out
    int max = 0;
    for( int i = 0; i < n; ++i ) {
        if( max < heart[i] ) {
            max = heart[i];
        }
    }
    printf( "%d\n", max );

    // Cleanup memory
    for( int i = 0; i < n; ++i ) {
        link_node* n = nodes[i].first;
        while( n ) {
            link_node* next = n->next;
            free(n);
            n = next;
        }
    }
    free(nodes);
    free(topo_sort);
    free(heart);
}