int main(){

	struct node *head = NULL;

	add_node(&head, 1);
	add_node(&head, 9);
	add_node(&head, 1);
	add_node(&head, 1);
	add_node(&head, 7);
	add_node(&head, 4);
	add_node(&head, 4);
	add_node(&head, 7);
	add_node(&head, 6);

	print_list(head);
	printf("\n");

	merge_sort(&head);
	printf("List after Sorting\n");

	print_list(head);
	printf("\n");

	remove_duplicates(head);
	printf("List after removing duplicates\n");
	print_list(head);
	printf("\n");
	return 0;
}
Пример #2
0
void concept_indexer::index_concept(concept& c, model& m,
    std::unordered_set<qname>& processed_qnames) {
    BOOST_LOG_SEV(lg, debug) << "Indexing concept: "
                             << string_converter::convert(c.name());

    if (processed_qnames.find(c.name()) != processed_qnames.end()) {
        BOOST_LOG_SEV(lg, debug) << "Concept already processed.";
        return;
    }

    if (c.refines().empty()) {
        BOOST_LOG_SEV(lg, debug) << "Concept refines no concepts.";
        processed_qnames.insert(c.name());
        return;
    }

    std::list<qname> expanded_refines;
    for (auto& qn : c.refines()) {
        auto& parent(find_concept(qn, m));
        index_concept(parent, m, processed_qnames);

        expanded_refines.push_back(qn);

        expanded_refines.insert(expanded_refines.end(),
            parent.refines().begin(), parent.refines().end());
    }

    BOOST_LOG_SEV(lg, debug) << "Computing reduced set for concept.";
    remove_duplicates(expanded_refines);
    c.refines(expanded_refines);
    processed_qnames.insert(c.name());
}
Пример #3
0
void gfx_gradient_table::build_table(void)
{
    quick_sort(m_color_profile, offset_less);
    m_color_profile.cut_at(remove_duplicates(m_color_profile, offset_equal));

    if (m_color_profile.size() >= 2) {
        unsigned int i;
        unsigned int start = uround(m_color_profile[0].offset * color_table_size);
        unsigned int end = 0;
        color_type c = m_color_profile[0].color;

        for (i = 0; i < start; i++) {
            m_color_table[i] = c;
        }

        for (i = 1; i < m_color_profile.size(); i++) {
            end  = uround(m_color_profile[i].offset * color_table_size);
            color_interpolator ci(m_color_profile[i-1].color,
                                  m_color_profile[i].color, end - start + 1);
            while (start < end) {
                m_color_table[start] = ci.color();
                ++ci;
                ++start;
            }
        }

        c = m_color_profile.last().color;

        for (; end < m_color_table.size(); end++) {
            m_color_table[end] = c;
        }
    }
}
Пример #4
0
int main() {
	node* head = new node(0);
	node* app = head;
	for(int i = 0; i < 10; ++i) {
		append(app, i);
		app = app->next;
	}
	remove_duplicates(head);
	node* curr = head;
	while(curr) {
		std::cout << curr->data << " ";
		curr = curr->next;
	}
	std::cout << std::endl;
	delete_list(head);

	head = new node(0);
	app = head;
	for(int i = 0; i < 10; ++i) {
		append(app, i);
		app = app->next;
	}
	remove_dups_hash(head);
	curr = head;
	while(curr) {
		std::cout << curr->data << " ";
		curr = curr->next;
	}
	std::cout << std::endl;
	delete_list(head);
}
Пример #5
0
struct ref *fetch_pack(struct fetch_pack_args *my_args,
		       int fd[], struct child_process *conn,
		       const struct ref *ref,
		const char *dest,
		int nr_heads,
		char **heads,
		char **pack_lockfile)
{
	struct stat st;
	struct ref *ref_cpy;

	fetch_pack_setup();
	if (&args != my_args)
		memcpy(&args, my_args, sizeof(args));
	if (args.depth > 0) {
		if (stat(git_path("shallow"), &st))
			st.st_mtime = 0;
	}

	if (heads && nr_heads)
		nr_heads = remove_duplicates(nr_heads, heads);
	if (!ref) {
		packet_flush(fd[1]);
		die("no matching remote head");
	}
	ref_cpy = do_fetch_pack(fd, ref, nr_heads, heads, pack_lockfile);

	if (args.depth > 0) {
		struct cache_time mtime;
		struct strbuf sb = STRBUF_INIT;
		char *shallow = git_path("shallow");
		int fd;

		mtime.sec = st.st_mtime;
		mtime.nsec = ST_MTIME_NSEC(st);
		if (stat(shallow, &st)) {
			if (mtime.sec)
				die("shallow file was removed during fetch");
		} else if (st.st_mtime != mtime.sec
#ifdef USE_NSEC
				|| ST_MTIME_NSEC(st) != mtime.nsec
#endif
			  )
			die("shallow file was changed during fetch");

		fd = hold_lock_file_for_update(&lock, shallow,
					       LOCK_DIE_ON_ERROR);
		if (!write_shallow_commits(&sb, 0)
		 || write_in_full(fd, sb.buf, sb.len) != sb.len) {
			unlink_or_warn(shallow);
			rollback_lock_file(&lock);
		} else {
			commit_lock_file(&lock);
		}
		strbuf_release(&sb);
	}

	reprepare_packed_git();
	return ref_cpy;
}
Пример #6
0
void testRemoveDuplicateFunctionality()
{
    // define sample data
    std::vector<int> sampleDataInt = { 10,11,10,13,14,15,15,15,18,19,12,11,17 };
    std::vector<std::string> sampleDataString = { "Die", "___", "eines", "ist", "Gebrauch", "der", "Sprache", "in", "sein", "Wortes", "Bedeutung", "Die", "ist", "blabla" };
    
    // test for integer vector
    printContainer(sampleDataInt);
    //remove_duplicates(sampleDataInt, std::less<int>() );
    remove_duplicates(sampleDataInt);
    printContainer(sampleDataInt);
    
    // test for string vector
    printContainer(sampleDataString);
    remove_duplicates(sampleDataString);
    printContainer(sampleDataString);
}
Пример #7
0
Datum
tsq_mcontains(PG_FUNCTION_ARGS)
{
	TSQuery		query = PG_GETARG_TSQUERY(0);
	TSQuery		ex = PG_GETARG_TSQUERY(1);
	char	  **query_values;
	int			query_nvalues;
	char	  **ex_values;
	int			ex_nvalues;
	bool		result = true;

	/* Extract the query terms into arrays */
	query_values = collectTSQueryValues(query, &query_nvalues);
	ex_values = collectTSQueryValues(ex, &ex_nvalues);

	/* Sort and remove duplicates from both arrays */
	qsort(query_values, query_nvalues, sizeof(char *), cmp_string);
	query_nvalues = remove_duplicates(query_values, query_nvalues);
	qsort(ex_values, ex_nvalues, sizeof(char *), cmp_string);
	ex_nvalues = remove_duplicates(ex_values, ex_nvalues);

	if (ex_nvalues > query_nvalues)
		result = false;
	else
	{
		int			i;
		int			j = 0;

		for (i = 0; i < ex_nvalues; i++)
		{
			for (; j < query_nvalues; j++)
			{
				if (strcmp(ex_values[i], query_values[j]) == 0)
					break;
			}
			if (j == query_nvalues)
			{
				result = false;
				break;
			}
		}
	}

	PG_RETURN_BOOL(result);
}
Пример #8
0
static void
merge_include_chains (cpp_reader *pfile, int verbose)
{
  /* Join the SYSTEM and AFTER chains.  Remove duplicates in the
     resulting SYSTEM chain.  */
  if (heads[SYSTEM])
    tails[SYSTEM]->next = heads[AFTER];
  else
    heads[SYSTEM] = heads[AFTER];
  heads[SYSTEM] = remove_duplicates (pfile, heads[SYSTEM], 0, 0, verbose);

  /* Remove duplicates from BRACKET that are in itself or SYSTEM, and
     join it to SYSTEM.  */
  heads[BRACKET] = remove_duplicates (pfile, heads[BRACKET], heads[SYSTEM],
				      heads[SYSTEM], verbose);

  /* Remove duplicates from QUOTE that are in itself or SYSTEM, and
     join it to BRACKET.  */
  heads[QUOTE] = remove_duplicates (pfile, heads[QUOTE], heads[SYSTEM],
				    heads[BRACKET], verbose);

  /* If verbose, print the list of dirs to search.  */
  if (verbose)
    {
      struct cpp_dir *p;

      fprintf (stderr, _("#include \"...\" search starts here:\n"));
      for (p = heads[QUOTE];; p = p->next)
	{
	  if (p == heads[BRACKET])
	    fprintf (stderr, _("#include <...> search starts here:\n"));
	  if (!p)
	    break;
	  /* APPLE LOCAL begin 5033355 */
	  if (p->construct == 0)
	    fprintf (stderr, " %s\n", p->name);
	  else if (p->construct == hmap_construct_pathname)
	    fprintf (stderr, " %s (headermap)\n", p->name);
	  else
	    fprintf (stderr, " %s (framework directory)\n", p->name);
	  /* APPLE LOCAL end 5033355 */
	}
      fprintf (stderr, _("End of search list.\n"));
    }
}
Пример #9
0
struct ref *fetch_pack(struct fetch_pack_args *my_args,
		       int fd[], struct child_process *conn,
		       const struct ref *ref,
		const char *dest,
		int nr_heads,
		char **heads,
		char **pack_lockfile)
{
	struct stat st;
	struct ref *ref_cpy;

	fetch_pack_setup();
	memcpy(&args, my_args, sizeof(args));
	if (args.depth > 0) {
		if (stat(git_path("shallow"), &st))
			st.st_mtime = 0;
	}

	if (heads && nr_heads)
		nr_heads = remove_duplicates(nr_heads, heads);
	if (!ref) {
		packet_flush(fd[1]);
		die("no matching remote head");
	}
	ref_cpy = do_fetch_pack(fd, ref, nr_heads, heads, pack_lockfile);

	if (args.depth > 0) {
		struct cache_time mtime;
		char *shallow = git_path("shallow");
		int fd;

		mtime.sec = st.st_mtime;
#ifdef USE_NSEC
		mtime.usec = st.st_mtim.usec;
#endif
		if (stat(shallow, &st)) {
			if (mtime.sec)
				die("shallow file was removed during fetch");
		} else if (st.st_mtime != mtime.sec
#ifdef USE_NSEC
				|| st.st_mtim.usec != mtime.usec
#endif
			  )
			die("shallow file was changed during fetch");

		fd = hold_lock_file_for_update(&lock, shallow, 1);
		if (!write_shallow_commits(fd, 0)) {
			unlink(shallow);
			rollback_lock_file(&lock);
		} else {
			commit_lock_file(&lock);
		}
	}

	reprepare_packed_git();
	return ref_cpy;
}
Пример #10
0
int main(int argc, const char *argv[]) {
  char string[] = "abcbde";

  remove_duplicates(string);

  printf("%s", string);

  return 0;
}
int main()
{
    std::vector<int> vec{1, 1, 2, 2, 3, 4, 4, 4, 5, 5, 6, 7, 7};
    std::cout << "Original vector:";
    print_vector(vec, vec.size());
    unsigned int size = remove_duplicates(vec);
    std::cout << "Output vector:";
    print_vector(vec, size);
    return 0;
}
void * removeArrayDuplicates(int *Arr, int len)
{

	if (Arr == NULL || len <= 0)return NULL;
	else
	{
		 remove_duplicates(Arr, len);

	}
	
	return NULL;
}
Пример #13
0
int main(int argc, char** argv) {
  node* head = add(1, NULL);
  node* next = add(2, head);
  next = add(2, next);
  next = add(4, next);
  next = add(3, next);
  next = add(1, next);
  next = add(3, next);
  add(4, next);
  output(head);
  remove_duplicates(head);
  output(head);
  release(head);
  return 0;
}
Пример #14
0
int main(void)
{
    list_t *list;
    int a[] = { 1, 2, 1, 3, 3, 5, 8, 5 };
    int count;

    list = make_list(a, 8);
    print_list(list);

    count = remove_duplicates(list);
    printf("Deleted %d list elements\n", count);
    print_list(list);

    return 0;
}
Пример #15
0
void generate_new_generation(void *arg)
{
    struct thread_arguments_info *info = arg;
    int population_size = info->population_size;
    GeneWrapper * population = info->population;
    GeneWrapper * next_population = info->next_population;
    int i, num_iterations = NUM_ITERATIONS;

    GeneWrapper combined_populaton[population_size * 2];

    for (i = 0; i < population_size * 2; i++) {
        GeneWrapper * unitialized_gene = &combined_populaton[i];
        unitialized_gene->gene = malloc(sizeof(PacGene));
        unitialized_gene->score = 0;
    }

    char buffer[51];
    bzero(buffer, 51);
    while (num_iterations--  > 0) {

        for (i = 0; i < population_size * 2; i++) {
            NewStringFromGene(population[i % population_size].gene, buffer);
            SetGeneFromString(buffer, combined_populaton[i].gene);
        }
        printf("crossover_population \n");
        crossover_population(combined_populaton + population_size, population_size, population, population_size);
        mutate_population(combined_populaton, population_size, 0.1); // high mutation

        printf("reduce_population_through_ranking \n");
        int reduced_size = population_size * 2;
        remove_duplicates(combined_populaton, &reduced_size);
        reduce_population_through_ranking(combined_populaton, population_size, combined_populaton, max(reduced_size, population_size),
                                          population, population_size);
        for (i = 0; i < population_size; i++) {
            NewStringFromGene(combined_populaton[i].gene, buffer);
            SetGeneFromString(buffer, population[i].gene);
        }
    }
    
    for (i = 0; i < population_size * 2; i++) {
        free(combined_populaton[i].gene);
    }
    
    for (i = 0; i < population_size; i++) {
        NewStringFromGene(population[i].gene, buffer);
        SetGeneFromString(buffer, next_population[i].gene);
    }
}
int main(int argc, char const *argv[]) {
  char *line = NULL;
  size_t len = 0;
  size_t read;

  while ((read = getline(&line, &len, stdin)) != -1) {
    if(read > 0) {
      // Remove last '\n'
      line[read - 1] = '\0';
      remove_duplicates(line);
      printf("%s\n", line);
    }
  }

  return 0;
}
Пример #17
0
int
main()
{
    node_t *root = get_node(10);
    root->next = get_node(20);
    root->next->next = get_node(30);
    root->next->next->next = get_node(40);
    root->next->next->next->next = get_node(30);
    root->next->next->next->next->next = get_node(50);
    root->next->next->next->next->next->next= get_node(10);

    print_list("BEFORE", root);

    root = remove_duplicates(root);
    print_list("AFTER", root);

    return 0;
}
Пример #18
0
/**
   \brief External interface for the simplifier. 
   A client will invoke operator()(s, r, p) to simplify s.
   The result is stored in r.
   When proof generation is enabled, a proof for the equivalence (or equisatisfiability)
   of s and r is stored in p.
   When proof generation is disabled, this method stores the "undefined proof" object in p.
*/
void simplifier::operator()(expr * s, expr_ref & r, proof_ref & p) {
    m_need_reset = true;
    reinitialize();
    expr  * s_orig = s;
    expr  * old_s;
    expr  * result;
    proof * result_proof;
    switch (m.proof_mode()) {
    case PGM_DISABLED: // proof generation is disabled.
        reduce_core(s); 
        // after executing reduce_core, the result of the simplification is in the cache
        get_cached(s, result, result_proof);
        r = result;
        p = m.mk_undef_proof();
        break;
    case PGM_COARSE: // coarse proofs... in this case, we do not produce a step by step (fine grain) proof to show the equivalence (or equisatisfiability) of s an r.
        m_subst_proofs.reset(); // m_subst_proofs is an auxiliary vector that is used to justify substitutions. See comment on method get_subst. 
        reduce_core(s);
        get_cached(s, result, result_proof);
        r = result;
        if (result == s)
            p = m.mk_reflexivity(s);
        else {
            remove_duplicates(m_subst_proofs);
            p = m.mk_rewrite_star(s, result, m_subst_proofs.size(), m_subst_proofs.c_ptr());
        }
        break;
    case PGM_FINE: // fine grain proofs... in this mode, every proof step (or most of them) is described.
        m_proofs.reset();
        old_s = 0;
        // keep simplyfing until no further simplifications are possible.
        while (s != old_s) {
            TRACE("simplifier", tout << "simplification pass... " << s->get_id() << "\n";);
            TRACE("simplifier_loop", tout << mk_ll_pp(s, m) << "\n";);
            reduce_core(s);
            get_cached(s, result, result_proof);
            SASSERT(is_rewrite_proof(s, result, result_proof));
            if (result_proof != 0) {
                m_proofs.push_back(result_proof);
            }
            old_s = s;
            s     = result;
        }
Пример #19
0
void test_remove_duplicates()
{
	int input_array[5][30] = {
								{1,2,2,5,2,6,6,6,3,2,6,9},
								{1,1,1,2,5,6,9,2,1,3,2,1,2,5,2,6,6,6,6,6,6,3,2,6,9},
								{1,2,3,5,4,3,1,2,4,5,4,4,2,1,3,4},
								{1,2,3,4,5,6},
								{1,1,1,1,1,1}
						     };
	int output_array[5][7] = {
								{1,2,5,6,3,9},
								{1,2,5,6,9,3},
								{1,2,3,5,4},
								{1,2,3,4,5,6},
								{1}
							 };
	int iter_loop,iter_loop2;
	for(iter_loop=0;iter_loop<5;iter_loop++)
	{
		remove_duplicates(input_array[iter_loop]);
		iter_loop2 = 0;
		while(input_array[iter_loop2] !='\0')
		{
			if(input_array[iter_loop][iter_loop2] != output_array[iter_loop][iter_loop2])
			{
				break;
			}
			else if(input_array[iter_loop][iter_loop2] == input_array[iter_loop][iter_loop2])
			{
				iter_loop2++;
				continue;
			}
		}
		if(input_array[iter_loop][iter_loop2] =='\0')
		{
			printf("ACCEPTED\n");
		}
		else if(input_array[iter_loop][iter_loop2] !='\0')
		{
			printf("REJECTED\n");
		}
	}
}
Пример #20
0
string 
get_word( istream & i )
{
	string word;
	
	while ( !i.eof() && !is_non_text(i.peek()) )
	{
		word += i.get();
	}
	
	// lowercase
	transform(
		word.begin(), word.end(),
		word.begin(),
		tolower
	);
	
	return stem( strip_specials( remove_duplicates(word) ) );
}
Пример #21
0
int main(void)
{
    
    
    push(&np,13,0);
    push(&np,21,0);
    push(&np,36,0);
    push(&np,28,0);
    push(&np,21,0);
    push(&np,89,0);
    push(&np,21,0);
    push(&np,45,0);
    push(&np,89,0);
    printlist(&np);
    nodeptr  n=get_duplicates(&np);
    remove_duplicates(&n);
    
    printf("the new list without duplicates is\n");
    printlist(&np);
    return 0;
}
Пример #22
0
int             main(int argc, char **argv)
{
    optparse(argc, argv); /* set g_conf options */

    if (isatty(STDIN_FILENO))
        watch_user_input();

    update_status(FCOPY_START);
    init_file(g_conf.infile_name, g_conf.outfile_name);
    config(); /* configure g_conf options */

    init_hmap(g_conf.hmap_size);
    update_status(TAGDUP_START);
    tag_duplicates();
    destroy_hmap();

    update_status(FCLEAN_START);
    remove_duplicates();
    destroy_file();
    return (0);
}
Пример #23
0
int main (int argc, char **argv) 
{
  if(argc <= 1) {
    printf ("Usage: ./three_sum <Num_elem>\n");
    exit(1);
  }

  int num_elem = atoi(argv[1]);
  bool has_negative = false;

  printf("Three sum over %d elements\n", num_elem);

  int *elements = (int*)malloc(sizeof(int) * num_elem);
  // Note: Assumptions for data set
  // 1. May contain duplicates
  // 2. May contain zero
  // 3. May be negative

  srand(0xDEADBEEF);
  for (int i=0; i<num_elem; i++) {
    elements[i] = rand()%num_elem;
    if (elements[i] < 0)  has_negative = true;
  }
  print_elem (elements, num_elem, "Init");

  clock_t startTime = clock();
  // sort the array using quick sort
  sort (elements, num_elem);
  print_elem(elements, num_elem, "Sorted");
  int new_size = remove_duplicates(elements, num_elem);

  print_elem(elements, new_size, "New array");
  three_sum_hash (elements, new_size, has_negative);
  clock_t endTime = clock();
	clock_t time = (endTime - startTime)*1000.0/CLOCKS_PER_SEC;

  printf("Total time %0.2f ms\n", (double)time);
  exit(0);
}
Пример #24
0
void crossover_population(GeneWrapper * population, int population_size,
                          GeneWrapper * base_population, int base_population_size)
{
    int i,j;
    int first_unitialized_gene = 0;
    int cross_over_size = population_size * (population_size-1) /2 * (int) (pow(2.0, 1.0 * NUM_CROSS_OVERS))  ;
    GeneWrapper * combined_populaton = (GeneWrapper *) malloc(sizeof(GeneWrapper) * cross_over_size);
    for (i = 0; i < population_size; i++) {
        for (j = i+1; j < population_size; j++) {
            GeneWrapper * p1 = &population[i];
            GeneWrapper * p2 = &population[j];
            bool flags[NUM_CROSS_OVERS];
            crossover_population_helper(flags, 0, NUM_CROSS_OVERS, combined_populaton, p1, p2, &first_unitialized_gene);
        }
    }
    
    int reduced_size = cross_over_size;
    remove_duplicates(combined_populaton, &reduced_size);
    reduce_population_through_ranking(population, population_size, combined_populaton, max(reduced_size,population_size), base_population, base_population_size);
    for (i = 0; i < cross_over_size; i++) {
        free(combined_populaton[i].gene);
    }
    free(combined_populaton);
}
Пример #25
0
static void collect_global_symbols() {
  uint32_t count = count_symbols();
  GlobalSymbol* all_symbols =
      safe_malloc(sizeof(GlobalSymbol)*count);
  GlobalSymbolSet* set;
  GlobalSymbolSet* next;
  uint32_t i;

  count = 0;
  for (set = global_symbol_sets; set; set = next) {
    for (i = 0; i < set->num_symbols; ++i) {
      GlobalSymbol* g = &all_symbols[count + i];
      CH_DbgGlobalSymbol* sym = &set->symbols[i];
      g->name = sym->name;
      g->kind = sym->kind;
      g->is_partial = sym->is_partial;
      g->defining_object = set->defining_object - debug_objects;
      g->defining_object_offset = set->symbols[i].defining_object_offset;
      g->address = sym->address;
    }
    count += set->num_symbols;
    next = set->next;
    safe_free(set->symbols);
    safe_free(set);
  }
  
  qsort(all_symbols, count, sizeof(GlobalSymbol), compare_global_symbols);
  count = remove_duplicates(count, all_symbols);
  all_symbols = safe_realloc(all_symbols, count*sizeof(GlobalSymbol));

  pthread_mutex_lock(&global_symbol_mutex);
  global_symbols = all_symbols;
  global_symbol_count = count;
  pthread_cond_broadcast(&global_symbol_condition);
  pthread_mutex_unlock(&global_symbol_mutex);
}
Пример #26
0
static gboolean
create_codec_lists (FsMediaType media_type,
    GList *recv_list, GList *send_list)
{
  GList *duplex_list = NULL;
  list_codec_blueprints[media_type] = NULL;

  /* TODO we should support non duplex as well, as in have some caps that are
   * only sendable or only receivable */
  duplex_list = codec_cap_list_intersect (recv_list, send_list);

  if (!duplex_list) {
    GST_WARNING ("There are no send/recv codecs");
    return FALSE;
  }

  GST_LOG ("*******Intersection of send_list and recv_list");
  debug_codec_cap_list (duplex_list);

  duplex_list = remove_dynamic_duplicates (duplex_list);
  duplex_list = remove_duplicates (duplex_list);

  if (!duplex_list) {
    GST_WARNING ("Dynamic duplicate removal left us with nothing");
    return FALSE;
  }

  parse_codec_cap_list (duplex_list, media_type);

  codec_cap_list_free (duplex_list);

  list_codec_blueprints[media_type] =
    fs_rtp_special_sources_add_blueprints (list_codec_blueprints[media_type]);

  return TRUE;
}
Пример #27
0
int main()
{
	node_t head = NULL;
	node_t first = NULL;
	node_t second = NULL;
	node_t merged = NULL;
	int choice = 0;
	int ele;
	int pos;
	node_t node_pointer = NULL;
	int n;
	int is_cycle = 0;
	node_t list_cycle = NULL;
	int isPalindrome = 0;
	int intersection = 0;
	node_t resultant_of_addition = NULL;
	int k = 0;
	int m;


	while(1)
	{
		printf("--------------------------------------------------------------------------------------------------------\n");
		printf("1. Add node in the front end \n");
		printf("2. Display the list \n");
		printf("3. Exit \n");
		printf("4. Add node at the rear end of the list \n");
		printf("5. Delete a node at the front end of the list \n");
		printf("6. Delete a node from the rear end of the list \n");
		printf("7. Insert a node in order to the list \n");
		printf("8. Merge two ordered linked lists \n");
		printf("9. Search for an item in the list \n");
		printf("10. Delete a node whose value is specified \n");
		printf("11. Delete a node at the specified position \n");
		printf("12. Reverse  list wihtout creating extra nodes \n");
		printf("13. Delete a node given only a pointer to the node \n");
		printf("14. Print middle element of the list \n");
		printf("15. Print the nth last element in the list \n");
		printf("16. Delete the entire list \n");
		printf("17. Detect a loop in the list \n");
		printf("18. Check whether a list is a palindrome \n");
		printf("19. Find the intersection of two lists \n");
		printf("20. Print reverse recursively \n");
		printf("21. Remove duplicates in a sorted linked list \n");
		printf("22. Move the last node in the list to the first node \n");
		printf("23. Reverse the list pairwise \n");
		printf("24. Find the intersection of two lists recursively \n");
		printf("25. Delete alternate nodes in the list \n");
		printf("26. Alternating split of the list \n");
		printf("27. Delete nodes whose neighbours value is greater \n");
		printf("28. Sepearate into even and odd in that order \n");
		printf("29. Add two lists and give the resultant list \n");
		printf("30. Rotate the list by k elements \n");
		printf("31. Separate into 0s and 1s \n");
		printf("32. Delete n nodes after the first m nodes \n");
		printf("-------------------------------------------------------------------------------------------------------\n");

		printf("Enter the choice\n");
		scanf("%d", &choice);

		switch(choice)
		{
			case 1:

					printf("Enter the element to enter to the front end of the list \n");
					scanf("%d", &ele);
					head = add_front(head, ele);
					break;

			case 2:
					display(head);
					break;

			case 3:
					exit(0);

			case 4:
					printf("Enter an element to be added to the end of the list \n");
					scanf("%d", &ele);
					head = add_end(head, ele);
					break;

			case 5:
					head = delete_front(head);
					break;

			case 6:
					head = delete_rear(head);
					break;

			case 7:
					printf("Enter the element to be inserted \n");
					scanf("%d", &ele);
					head = insert_in_order(head, ele);
					break;

			case 8:

					first = insert_in_order(first, 92);
					first = insert_in_order(first, 42);
					first = insert_in_order(first, 35);

					second = insert_in_order(second, 100);
					second = insert_in_order(second, 432);
					second = insert_in_order(second, 90);
					second = insert_in_order(second, 10);


					printf("The elements of the first list are: \n");
					display(first);

					printf("The elements of the second list are \n");
					display(second);

					merged = merge_ordered_lists(first, second);
					printf("The ordered list is: \n");
					display(merged);

			case 9:
					printf("Enter the element of the list to be searched for \n");
					scanf("%d", &ele);
					int pos = search(head, ele);
					if(pos != -1)
					{
						printf("The element is found at %d: \n", pos);
					}
					else
					{
						printf("The element is not found in the list \n");
					}

					break;

			case 10:
					printf("Enter the element of the list to be deleted: \n");
					scanf("%d", &ele);
					head = delete_with_info(head, ele);

					if(head == (node_t)NULL)
					{
						printf("The list is empty or the element u specified is not found: \n");
					}
					break;

			case 11:
					printf("Enter the position with the first node as position 1 \n");
					scanf("%d", &ele);
					head = delete_with_pos(head, ele);

					if(head == (node_t)NULL)
					{
						printf("Either the list is empty or the position specified is not appropriate \n");
					}

					break;

			case 12:

					head = reverse(head);
					break;

			case 13:
					node_pointer = head -> link -> link;
					delete_node_given_pointer(node_pointer);
					break;

			case 14:
					 print_middle(head);
					 break;

			case 15:
					printf("Enter the value of n \n");
					scanf("%d",&n);
					print_nth_last(head, n);
					break;

			case 16:
					head = delete_list(head);
					break;

			case 17:

					list_cycle = add_end(list_cycle,1);
					list_cycle = add_end(list_cycle,2);
					list_cycle = add_end(list_cycle,3);
					list_cycle = add_end(list_cycle,4);
					list_cycle = add_end(list_cycle,5);
					list_cycle -> link -> link -> link -> link -> link = list_cycle -> link;
					is_cycle = find_cycle(list_cycle);
					if(is_cycle)
					{
						printf("There is a cycle in the list \n");

					}

					else
					{
						printf("There is no cycle in the list \n");
					}



					break;

			case 18:

					isPalindrome = is_palindrome(&head, head);
					if(isPalindrome)
					{
						printf("The list is a palindrome \n");
					}

					else
					{
						printf("The list is not a palindrome \n");
					}
					break;

			case 19:

					first = add_end(first,10);
					first = add_end(first,20);
					


					second = add_end(second,43);
					second = add_end(second,3);
					second = add_end(second,34);
					second = add_end(second,44);

					first -> link -> link = second -> link;


					intersection = find_intersection(first, second);
					printf("The intersection point of the two lists are %d \n", intersection);
					break;

			case 20:
					print_reverse_recursively(head);
					printf("\n");
					break;

			case 21:

					remove_duplicates(head);
					break;

			case 22:

					head = move_last_to_first(head);
					break;

			case 23:

					head = pairwise_reverse(head);
					break;

			case 24:

					first = add_end(first, 10);
					first = add_end(first, 30);
					first = add_end(first, 40);
					first = add_end(first, 50);
					first = add_end(first, 60);


					second = add_end(second, 10);
					second = add_end(second, 20);
					second = add_end(second, 30);


					find_common_recursively(first, second);
					break;

			case 25:
					head = delete_alternate(head);
					break;

			case 26:
					 alternating_split_v2(head);
					 break;

			case 27:
					head = delete_node_when_neigbour_higher(head);
					break;

			case 28:
					head = separate_into_even_odd_v2(head);
					break;

			case 29:
						first = add_front(first, 2);
						first = add_front(first, 4);
						first = add_front(first, 8);


						second = add_front(second,2);
						second = add_front(second,4);
						second = add_front(second,5);
						second = add_front(second,3);

						resultant_of_addition = add_two_lists(first, second);

						printf("The resultant list is as follows \n");
						display(resultant_of_addition);

						break;	

			case 30:
					printf("Enter the value of k \n");
					scanf("%d",&k);
					head = rotate_by_k(head, k);
					break;

			case 31:
					head = separate_into_zeroes_ones(head);
					break;

			case 32:
					printf("Enter the value of m \n");
					scanf("%d", &m);
					printf("Enter the value of n \n");
					scanf("%d", &n);
					head = retain_m_delte_n(head, m , n);
					break;



			default:
					printf("Invalid choice... Please try again \n");
					break;

		}

	}
}
Пример #28
0
int main(void)
{
    FILE* f;
    char str[MS];
    int k;
    struct mylist* head=NULL, *head1 = NULL, *headr=NULL, *headr1=NULL, *kwn, *cir, *cir1;
    struct anime* mas=NULL, *mas1=NULL, *masr = NULL, *masr1 = NULL;
    setlocale(0, "russian");

    printf("Тестирование функции readline из mystruct.c\n");
    f = fopen("testread/test1.txt","r");
    k = freadline(f, str, MS);
    if (strcmp(str, "bbbb") == 0 && k == 4)
        printf("Test 1 - OK!");
    else
        printf("Test 1 - FAILED!");
    fclose(f);

    f = fopen("testread/test2.txt","r");
    k = freadline(f, str, MS);
    if (strcmp(str, "bbbbb") == 0 && k == 5)
        printf("\nTest 2 - OK!");
    else
        printf("\nTest 2 - FAILED!");
    fclose(f);

    f = fopen("testread/test3.txt","r");
    k = freadline(f, str, MS);
    if (strcmp(str, "aba aba b") == 0 && k == 9)
        printf("\nTest 3 - OK!");
    else
        printf("\nTest 3 - FAILED!");
    fclose(f);

    f = fopen("testread/test4.txt","r");
    k = freadline(f, str, MS);
    if (strcmp(str, "trata trata") == 0 && k == 11)
        printf("\nTest 4 - OK!");
    else
        printf("\nTest 4 - FAILED!");
    fclose(f);

    f = fopen("testread/test5.txt","r");
    k = freadline(f, str, MS);
    if (strcmp(str, "") == 0 && k == 0)
        printf("\nTest 5 - OK!");
    else
        printf("\nTest 5 - FAILED!");
    fclose(f);

    f = fopen("testread/test6.txt","r");
    k = freadline(f, str, 3);
    if (strcmp(str, "AA") == 0 && k == 2)
        printf("\nTest 6 - OK!");
    else
        printf("\nTest 6 - FAILED!");
    fclose(f);



    printf("\n\nТестирование функции readdate из mystruct.c\n");
    mas = malloc(sizeof(struct anime));
    f = fopen("testread/test_d.txt","r");
    readdate(f, mas);
    if (strcmp(mas->name, "One Punch Man") == 0 && strcmp(mas->stud, "Madhouse") == 0 && mas->year==2015)
        printf("Test 1 - OK!");
    else
        printf("Test 1 - FAILED!");
    readdate(f, mas);
    if (strcmp(mas->name, "Overlord") == 0 && strcmp(mas->stud, "Mad House") == 0 && mas->year==2014)
        printf("\nTest 2 - OK!");
    else
        printf("\nTest 2 - FAILED!");
    fclose(f);
    free(mas);  mas = NULL;


    printf("\n\nТестирование функции readfile из mystruct.c\n");
    mas = NULL;
    f = fopen("testread/test_d.txt","r");
    mas = readfile(f, &k);
    if (strcmp(mas->name, "One Punch Man") == 0 && strcmp(mas->stud, "Madhouse") == 0 && mas->year==2015 && k == 2 &&
        strcmp((mas+1)->name, "Overlord") == 0 && strcmp((mas+1)->stud, "Mad House") == 0 && (mas+1)->year==2014)
        printf("Test 1 - OK!");
    else
        printf("Test 1 - FAILED!");
    free(mas);  mas = NULL;
    fclose(f);
    mas = NULL;
    f = fopen("testread/test_de.txt","r");
    mas = readfile(f, &k);
    if (mas == NULL && k == 0)
        printf("\nTest 2 - OK!");
    else
        printf("\nTest 2 - FAILED!");
    free(mas);  mas = NULL;
    fclose(f);


    printf("\n\nТестирование функции formlist из mystruct.c\n");
    mas = NULL;
    f = fopen("testread/test_d.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    if (strcmp(((struct anime*) head->date)->name, "One Punch Man") == 0 && strcmp(((struct anime*) head->date)->stud, "Madhouse") == 0 && ((struct anime*) head->date)->year==2015 && k == 2 &&
        strcmp(((struct anime*) head->next->date)->name, "Overlord") == 0 && strcmp(((struct anime*) head->next->date)->stud, "Mad House") == 0 && ((struct anime*) head->next->date)->year==2014)
        printf("Test 1 - OK!");
    else
        printf("Test 1 - FAILED!");
    free_list(head); head = NULL;
    free(mas);  mas = NULL;
    fclose(f);

    mas = NULL;
    head = NULL;
    f = fopen("testread/test_de.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    if (head == NULL)
        printf("\nTest 2 - OK!");
    else
        printf("\nTest 2 - FAILED!");
    free_list(head); head = NULL;
    free(mas);  mas = NULL;
    fclose(f);

    printf("\n\nТестирование функции namecmp из mystruct.c\n");
    mas = malloc(2*sizeof(struct anime));
    snprintf(mas->name, MS, "AAA");
    snprintf(mas->stud, MS, "A");
    mas->year = 0;
    snprintf((mas+1)->name, MS, "BBB");
    snprintf((mas+1)->stud, MS, "A");
    mas->year = 0;
    head = create_mylist(mas);
    head1 = create_mylist(mas+1);
    if (namecmp(head, head1) < 0)
        printf("Test 1 - OK!");
    else
        printf("Test 1 - FAILED!");
    snprintf(mas->name, MS, "CCC");
    if (namecmp(head, head1) > 0)
        printf("\nTest 2 - OK!");
    else
        printf("\nTest 2 - FAILED!");
    snprintf((mas+1)->name, MS, "CCC");
    if (namecmp(head, head1) == 0)
        printf("\nTest 3 - OK!");
    else
        printf("\nTest 3 - FAILED!");
    free(mas);  mas = NULL;
    mas = NULL;
    free(head);
    head = NULL;
    free(head1);
    head1 = NULL;

    printf("\n\nТестирование функции lookup из exfunclo.c\n");
    mas1 = malloc(sizeof(struct anime));
    snprintf(mas1->name, MS, "AAABBB");
    snprintf(mas1->stud, MS, "AAA");
    mas1->year = 24;

    head1 = create_mylist(mas1);
    f = fopen("testlook/test1.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    if (lookup(head, head1, namecmp) != NULL)
        printf("Test 1 - OK!");
    else
        printf("Test 1 - FAILED!");
    free_list(head); head = NULL;
    head = NULL;
    free(mas);  mas = NULL;
    mas = NULL;
    fclose(f);
    free(head1);
    head1 = create_mylist(mas1);
    f = fopen("testlook/test2.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    if (lookup(head, head1, namecmp) != NULL)
        printf("\nTest 2 - OK!");
    else
        printf("\nTest 2 - FAILED!");
    free_list(head); head = NULL;
    head = NULL;
    free(mas);  mas = NULL;
    mas = NULL;
    fclose(f);
    f = fopen("testlook/test3.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    if (lookup(head, head1, namecmp) != NULL)
        printf("\nTest 3 - OK!");
    else
        printf("\nTest 3 - FAILED!");
    free_list(head); head = NULL;
    head = NULL;
    free(mas);  mas = NULL;
    mas = NULL;
    fclose(f);
    f = fopen("testlook/test4.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    if (lookup(head, head1, namecmp) == NULL)
        printf("\nTest 4 - OK!");
    else
        printf("\nTest 4 - FAILED!");
    free_list(head); head = NULL;
    head = NULL;
    free(mas);  mas = NULL;
    mas = NULL;
    fclose(f);
    f = fopen("testlook/test5.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    if (lookup(head, head1, namecmp) == NULL)
        printf("\nTest 5 - OK!");
    else
        printf("\nTest 5 - FAILED!");
    free_list(head); head = NULL;
    head = NULL;
    free(mas);  mas = NULL;
    mas = NULL;
    fclose(f);

    printf("\n\nТестирование функции insert из exfunclo.c\n");
    f = fopen("testins/test1.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testins/test11.txt", "r");
    masr = readfile(f, &k);
    headr = formlist(headr, masr, k);
    fclose(f);
    insert(&head, head1);
    int s = 0;
    for (cir = head, cir1 = headr; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    if (s == 0 && cir == NULL && cir1 == NULL)
        printf("Test 1 - OK!");
    else
        printf("Test 1 - FAILED!");
    free_list(head); head = NULL;
    free(mas);  mas = NULL;
    free_list(headr); headr = NULL;
    free(masr);  masr = NULL;
    free(mas1);  mas1 = NULL;
    mas1 = malloc(sizeof(struct anime));
    snprintf(mas1->name, MS, "AAABBB");
    snprintf(mas1->stud, MS, "AAA");
    mas1->year = 24;
    head1 = create_mylist(mas1);
    f = fopen("testins/test1.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testins/test12.txt", "r");
    masr = readfile(f, &k);
    headr = formlist(headr, masr, k);
    fclose(f);
    kwn = head->next->next->next;
    insert(&kwn, head1);
    s = 0;
    for (cir = head, cir1 = headr; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    if (s == 0 && cir == NULL && cir1 == NULL)
        printf("\nTest 2 - OK!");
    else
        printf("\nTest 2 - FAILED!");
    free_list(head); head = NULL;
    free(mas);  mas = NULL;
    free_list(headr); headr = NULL;
    free(masr);  masr = NULL;
    free(mas1);  mas1 = NULL;

    f = fopen("testins/test2.txt","r");
    mas1 = malloc(sizeof(struct anime));
    snprintf(mas1->name, MS, "AAABBB");
    snprintf(mas1->stud, MS, "AAA");
    mas1->year = 24;
    head1 = create_mylist(mas1);
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    insert(&head, head1);
    if (allcmp(head, head1) == 0 && head->next == NULL)
        printf("\nTest 3 - OK!");
    else
        printf("\nTest 3 - FAILED!");
    free(mas1);  mas1 = NULL;
    free(mas);  mas = NULL;
    free(head); head = NULL;

    printf("\n\nТестирование функции remove_duplicates из exfunclo.c\n");
    f = fopen("testrmd/test1.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testrmd/test1r.txt", "r");
    masr = readfile(f, &k);
    headr = formlist(headr, masr, k);
    fclose(f);
    remove_duplicates(head, namecmp);
    s=0;
    if (head == headr && head == NULL)
        printf("Test 1 - OK!");
    else
        printf("Test 1 - FAILED!");
    free(mas);  mas = NULL;
    free_list(head); head = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;

    f = fopen("testrmd/test2.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testrmd/test2r.txt", "r");
    masr = readfile(f, &k);
    headr = formlist(headr, masr, k);
    fclose(f);
    remove_duplicates(head, namecmp);
    s=0;
    for (cir = head, cir1 = headr; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    if (s == 0 && cir == NULL && cir1 == NULL)
        printf("\nTest 2 - OK!");
    else
        printf("\nTest 2 - FAILED!");
    free(mas);  mas = NULL;
    free_list(head); head = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;

    f = fopen("testrmd/test3.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testrmd/test3r.txt", "r");
    masr = readfile(f, &k);
    headr = formlist(headr, masr, k);
    fclose(f);
    remove_duplicates(head, namecmp);
    s=0;
    for (cir = head, cir1 = headr; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    if (s == 0 && cir == NULL && cir1 == NULL)
        printf("\nTest 3 - OK!");
    else
        printf("\nTest 3 - FAILED!");
    free(mas);  mas = NULL;
    free_list(head); head = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;

    f = fopen("testrmd/test4.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testrmd/test4r.txt", "r");
    masr = readfile(f, &k);
    headr = formlist(headr, masr, k);
    fclose(f);
    remove_duplicates(head, namecmp);
    s=0;
    for (cir = head, cir1 = headr; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    if (s == 0 && cir == NULL && cir1 == NULL)
        printf("\nTest 4 - OK!");
    else
        printf("\nTest 4 - FAILED!");
    free(mas);  mas = NULL;
    free_list(head); head = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;

    printf("\n\nТестирование функции front_back_split из exfunclo.c\n");
    headr = front_back_split(head);
    if (head == NULL && headr == NULL)
        printf("Test 1 - OK!");
    else
        printf("Test 1 - FAILED!");

    f = fopen("testfbs/test1.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testfbs/test1r1.txt", "r");
    masr = readfile(f, &k);
    headr = formlist(headr, masr, k);
    fclose(f);
    f = fopen("testfbs/test1r2.txt", "r");
    masr1 = readfile(f, &k);
    headr1 = formlist(headr1, masr1, k);
    fclose(f);
    head1 = front_back_split(head);
    s=0;
    for (cir = head, cir1 = headr; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    s += cir || cir1;
    for (cir = head1, cir1 = headr1; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    s += cir || cir1;
    if (s == 0)
        printf("\nTest 2 - OK!");
    else
        printf("\nTest 2 - FAILED");
    free(mas);  mas = NULL;
    free_list(head); head = NULL;
    free(mas1); mas1 = NULL;
    free_list(head1); head1 = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;
    free(masr1);  masr1 = NULL;
    free_list(headr1); headr1 = NULL;

    f = fopen("testfbs/test2.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testfbs/test2r1.txt", "r");
    masr = readfile(f, &k);
    headr = formlist(headr, masr, k);
    fclose(f);
    f = fopen("testfbs/test2r2.txt", "r");
    masr1 = readfile(f, &k);
    headr1 = formlist(headr1, masr1, k);
    fclose(f);
    head1 = front_back_split(head);
    s=0;
    for (cir = head, cir1 = headr; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    s += cir || cir1;
    for (cir = head1, cir1 = headr1; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    s += cir || cir1;
    if (s == 0)
        printf("\nTest 3 - OK!");
    else
        printf("\nTest 3 - FAILED");
    free(mas);  mas = NULL;
    free_list(head); head = NULL;
    free(mas1); mas1 = NULL;
    free_list(head1); head1 = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;
    free(masr1);  masr1 = NULL;
    free_list(headr1); headr1 = NULL;

    f = fopen("testfbs/test3.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testfbs/test3r1.txt", "r");
    masr = readfile(f, &k);
    headr = formlist(headr, masr, k);
    fclose(f);
    f = fopen("testfbs/test3r2.txt", "r");
    masr1 = readfile(f, &k);
    headr1 = formlist(headr1, masr1, k);
    fclose(f);
    head1 = front_back_split(head);
    s=0;
    for (cir = head, cir1 = headr; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    s += cir || cir1;
    for (cir = head1, cir1 = headr1; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    s += cir || cir1;
    if (s == 0)
        printf("\nTest 4 - OK!");
    else
        printf("\nTest 4 - FAILED");
    free(mas);  mas = NULL;
    free_list(head); head = NULL;
    free(mas1); mas1 = NULL;
    free_list(head1); head1 = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;
    free(masr1);  masr1 = NULL;
    free_list(headr1); headr1 = NULL;


    printf("\n\nТестирование функции sorted_merge из exfunclo.c");
    f = fopen("testmerge/test1.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testmerge/test1a.txt", "r");
    mas1 = readfile(f, &k);
    head1 = formlist(head1, mas1, k);
    fclose(f);
    f = fopen("testmerge/test1r.txt", "r");
    masr1 = readfile(f, &k);
    headr1 = formlist(headr1, masr1, k);
    fclose(f);
    headr = sorted_merge(head, head1, namecmp);
    s=0;
    for (cir = headr, cir1 = headr1; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    s += cir || cir1;
    if (s == 0)
        printf("\nTest 1 - OK!");
    else
        printf("\nTest 1 - FAILED");
    free(mas);  mas = NULL;
    free_list(head); head = NULL;
    free(mas1); mas1 = NULL;
    free_list(head1); head1 = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;
    free(masr1);  masr1 = NULL;
    free_list(headr1); headr1 = NULL;

    f = fopen("testmerge/test2.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testmerge/test2a.txt", "r");
    mas1 = readfile(f, &k);
    head1 = formlist(head1, mas1, k);
    fclose(f);
    f = fopen("testmerge/test2r.txt", "r");
    masr1 = readfile(f, &k);
    headr1 = formlist(headr1, masr1, k);
    fclose(f);
    headr = sorted_merge(head, head1, namecmp);
    s=0;
    for (cir = headr, cir1 = headr1; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    s += cir || cir1;
    if (s == 0)
        printf("\nTest 2 - OK!");
    else
        printf("\nTest 2 - FAILED");
    free(mas);  mas = NULL;
    free_list(head); head = NULL;
    free(mas1); mas1 = NULL;
    free_list(head1); head1 = NULL;
    free(masr);  masr = NULL;
    //free_list(headr);
    headr = NULL;
    free(masr1);  masr1 = NULL;
    free_list(headr1); headr1 = NULL;

    f = fopen("testmerge/test3.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testmerge/test3a.txt", "r");
    mas1 = readfile(f, &k);
    head1 = formlist(head1, mas1, k);
    fclose(f);
    f = fopen("testmerge/test3r.txt", "r");
    masr1 = readfile(f, &k);
    headr1 = formlist(headr1, masr1, k);
    fclose(f);
    headr = sorted_merge(head, head1, namecmp);
    s=0;
    for (cir = headr, cir1 = headr1; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    s += cir || cir1;
    if (s == 0)
        printf("\nTest 3 - OK!");
    else
        printf("\nTest 3 - FAILED");
    free(mas);  mas = NULL;
    free_list(head); head = NULL;
    free(mas1); mas1 = NULL;
    //free_list(head1);
    head1 = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;
    free(masr1);  masr1 = NULL;
    free_list(headr1); headr1 = NULL;

    f = fopen("testmerge/test4.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testmerge/test4a.txt", "r");
    mas1 = readfile(f, &k);
    head1 = formlist(head1, mas1, k);
    fclose(f);
    f = fopen("testmerge/test4r.txt", "r");
    masr1 = readfile(f, &k);
    headr1 = formlist(headr1, masr1, k);
    fclose(f);
    headr = sorted_merge(head, head1, namecmp);
    s=0;
    for (cir = headr, cir1 = headr1; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    s += cir || cir1;
    if (s == 0)
        printf("\nTest 4 - OK!");
    else
        printf("\nTest 4 - FAILED");
    free(mas);  mas = NULL;
    //free_list(head);
    head = NULL;
    free(mas1); mas1 = NULL;
    //free_list(head1);
    head1 = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;
    free(masr1);  masr1 = NULL;
    free_list(headr1); headr1 = NULL;

    f = fopen("testmerge/test5.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testmerge/test5a.txt", "r");
    mas1 = readfile(f, &k);
    head1 = formlist(head1, mas1, k);
    fclose(f);
    f = fopen("testmerge/test5r.txt", "r");
    masr1 = readfile(f, &k);
    headr1 = formlist(headr1, masr1, k);
    fclose(f);
    headr = sorted_merge(head, head1, namecmp);
    s=0;
    for (cir = headr, cir1 = headr1; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    s += cir || cir1;
    if (s == 0)
        printf("\nTest 5 - OK!");
    else
        printf("\nTest 5 - FAILED");
    free(mas);  mas = NULL;
    //free_list(head);
    head = NULL;
    free(mas1); mas1 = NULL;
    //free_list(head1);
    head1 = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;
    free(masr1);  masr1 = NULL;
    free_list(headr1); headr1 = NULL;

    f = fopen("testmerge/test6.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testmerge/test6a.txt", "r");
    mas1 = readfile(f, &k);
    head1 = formlist(head1, mas1, k);
    fclose(f);
    f = fopen("testmerge/test6r.txt", "r");
    masr1 = readfile(f, &k);
    headr1 = formlist(headr1, masr1, k);
    fclose(f);
    headr = sorted_merge(head, head1, namecmp);
    s=0;
    for (cir = headr, cir1 = headr1; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    s += cir || cir1;
    if (s == 0)
        printf("\nTest 6 - OK!");
    else
        printf("\nTest 6 - FAILED");
    free(mas);  mas = NULL;
    //free_list(head);
    head = NULL;
    free(mas1); mas1 = NULL;
    //free_list(head1);
    head1 = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;
    free(masr1);  masr1 = NULL;
    free_list(headr1); headr1 = NULL;

    f = fopen("testmerge/test7.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testmerge/test7a.txt", "r");
    mas1 = readfile(f, &k);
    head1 = formlist(head1, mas1, k);
    fclose(f);
    f = fopen("testmerge/test7r.txt", "r");
    masr1 = readfile(f, &k);
    headr1 = formlist(headr1, masr1, k);
    fclose(f);
    headr = sorted_merge(head, head1, namecmp);
    s=0;
    for (cir = headr, cir1 = headr1; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    s += cir || cir1;
    if (s == 0)
        printf("\nTest 7 - OK!");
    else
        printf("\nTest 7 - FAILED");
    free(mas);  mas = NULL;
    //free_list(head);
    head = NULL;
    free(mas1); mas1 = NULL;
    //free_list(head1);
    head1 = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;
    free(masr1);  masr1 = NULL;
    free_list(headr1); headr1 = NULL;

    f = fopen("testmerge/test8.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testmerge/test8a.txt", "r");
    mas1 = readfile(f, &k);
    head1 = formlist(head1, mas1, k);
    fclose(f);
    f = fopen("testmerge/test8r.txt", "r");
    masr1 = readfile(f, &k);
    headr1 = formlist(headr1, masr1, k);
    fclose(f);
    headr = sorted_merge(head, head1, namecmp);
    s=0;
    for (cir = headr, cir1 = headr1; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    s += cir || cir1;
    if (s == 0)
        printf("\nTest 8 - OK!");
    else
        printf("\nTest 8 - FAILED");
    free(mas);  mas = NULL;
    //free_list(head);
    head = NULL;
    free(mas1); mas1 = NULL;
    //free_list(head1);
    head1 = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;
    free(masr1);  masr1 = NULL;
    free_list(headr1); headr1 = NULL;

    printf("\n\nТестирование функции merge_sort из exfunclo.c\n");
    f = fopen("testbacisort/test1.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testbacisort/test1r.txt", "r");
    masr = readfile(f, &k);
    headr = formlist(headr, masr, k);
    fclose(f);
    head =  merge_sort(head, namecmp);
    s=0;
    for (cir = head, cir1 = headr; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    if (s == 0 && cir == NULL && cir1 == NULL)
        printf("Test 1 - OK!");
    else
        printf("Test 1 - FAILED!");
    free(mas);  mas = NULL;
    free_list(head); head = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;

    f = fopen("testbacisort/test2.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testbacisort/test2r.txt", "r");
    masr = readfile(f, &k);
    headr = formlist(headr, masr, k);
    fclose(f);
    head =  merge_sort(head, namecmp);
    s=0;
    for (cir = head, cir1 = headr; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    if (s == 0 && cir == NULL && cir1 == NULL)
        printf("\nTest 2 - OK!");
    else
        printf("\nTest 2 - FAILED!");
    free(mas);  mas = NULL;
    free_list(head); head = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;

    f = fopen("testbacisort/test3.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testbacisort/test3r.txt", "r");
    masr = readfile(f, &k);
    headr = formlist(headr, masr, k);
    fclose(f);
    head =  merge_sort(head, namecmp);
    s=0;
    for (cir = head, cir1 = headr; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    if (s == 0 && cir == NULL && cir1 == NULL)
        printf("\nTest 3 - OK!");
    else
        printf("\nTest 3 - FAILED!");
    free(mas);  mas = NULL;
    free_list(head); head = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;

    f = fopen("testbacisort/test4.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testbacisort/test4r.txt", "r");
    masr = readfile(f, &k);
    headr = formlist(headr, masr, k);
    fclose(f);
    head = merge_sort(head, namecmp);
    s=0;
    if (head == headr && head == NULL)
        printf("\nTest 4 - OK!");
    else
        printf("\nTest 4 - FAILED!");
    free(mas);  mas = NULL;
    free_list(head); head = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;

    f = fopen("testbacisort/test5.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testbacisort/test5r.txt", "r");
    masr = readfile(f, &k);
    headr = formlist(headr, masr, k);
    fclose(f);
    head =  merge_sort(head, namecmp);
    s=0;
    for (cir = head, cir1 = headr; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    if (s == 0 && cir == NULL && cir1 == NULL)
        printf("\nTest 5 - OK!");
    else
        printf("\nTest 5 - FAILED!");
    free(mas);  mas = NULL;
    free_list(head); head = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;
    return 0;
}
Пример #29
0
struct node *parse_config_file(char *config_file)
{
	FILE *fp;
	char *line = NULL;
	size_t len = 0;
	ssize_t read;

	fp = fopen(config_file, "r");
	if (fp == NULL)
		exit(EXIT_FAILURE);

	struct node *head = malloc(sizeof(struct node));
	head->data = NULL;
	struct node *tail = head;
	struct boot_option *boot;


	while ((read = getline(&line, &len, fp)) != -1) {

		char *line_copy;

		line_copy = strdup(line);
		char *token = strtok(line_copy, " \t\n");

		if (token) {
			if (strcmp(token, "LABEL") == 0) {
				token = strtok(NULL, " \t\n");
				boot = new_boot_option();
				boot->label = strdup(token);
				tail = add_node(tail, BOOT_OPTION, boot);
			} else if (strcmp(token, "MENU") == 0) {
				token = strtok(NULL, " \t\n");
				if (strcmp(token, "LABEL") == 0) {
					token = strtok(NULL, " \t\n");
					boot->menu_label = add_to_string(boot->menu_label, 1, token);
					token = strtok(NULL, " \t\n");
					while (token) {
						boot->menu_label = add_to_string(boot->menu_label, 2, " ", token);
						token = strtok(NULL, " \t\n");
					}
				} else {
					tail = add_node(tail, TEXT_BLOCK, strdup(line));
				}
			} else if (strcmp(token, "LINUX") == 0) {
				token = strtok(NULL, " \t\n");
				boot->image = add_to_string(boot->image, 1, token);
			} else if (strcmp(token, "APPEND") == 0) {
				token = strtok(NULL, " \t\n");
				boot->root = add_to_string(boot->root, 1, token);
				token = strtok(NULL, " \t\n");
				while (token) {
					boot->root = add_to_string(boot->root, 2, " ", token);
					token = strtok(NULL, " \t\n");
				}
			} else if (strcmp(token, "INITRD") == 0) {
				token = strtok(NULL, " \t\n");
				boot->initrd = add_to_string(boot->initrd, 1, token);
			} else if (strcmp(token, "COM32") == 0) {
				token = strtok(NULL, " \t\n");
				boot->com32 = add_to_string(boot->com32, 1, token);
			} else {
				tail = add_node(tail, TEXT_BLOCK, strdup(line));
			}
		} else {
			tail = add_node(tail, TEXT_BLOCK, strdup(line));
		}
		free(line_copy);
	}

	fclose(fp);
	if (line)
		free(line);
	head = remove_duplicates(&head);
	head = remove_duplicates(&head);
	return head;
}
        br_status reduce_app(func_decl * f, unsigned num, expr * const * args, expr_ref & result, 
            proof_ref & result_pr)
        {
            if (m.is_not(f) && (m.is_and(args[0]) || m.is_or(args[0]))) {
                SASSERT(num == 1);
                expr_ref tmp(m);
                app* a = to_app(args[0]);
                m_app_args.reset();
                for (expr* arg : *a) {
                    m_brwr.mk_not(arg, tmp);
                    m_app_args.push_back(tmp);
                }
                if (m.is_and(args[0])) {
                    result = mk_or(m_app_args); 
                }
                else {
                    result = mk_and(m_app_args); 
                }
                return BR_REWRITE2;
            }
            if (!m.is_and(f) && !m.is_or(f)) { 
                return BR_FAILED; 
            }
            if (num == 0) {
                if (m.is_and(f)) {
                    result = m.mk_true();
                }
                else {
                    result = m.mk_false();
                }
                return BR_DONE;
            }
            if (num == 1) {
                result = args[0];
                return BR_DONE;
            }

            m_app_args.reset();
            m_app_args.append(num, args);

            std::sort(m_app_args.c_ptr(), m_app_args.c_ptr()+m_app_args.size(), m_expr_cmp);

            remove_duplicates(m_app_args);

            bool have_rewritten_args = false;

            have_rewritten_args = detect_equivalences(m_app_args, m.is_or(f));

            if (m_app_args.size()==1) {
                result = m_app_args[0].get();
            }
            else {
                if (m.is_and(f)) {
                    result = m.mk_and(m_app_args.size(), m_app_args.c_ptr());
                }
                else {
                    SASSERT(m.is_or(f));
                    result = m.mk_or(m_app_args.size(), m_app_args.c_ptr());
                }
            }

            if (have_rewritten_args) {
                return BR_REWRITE1;
            }
            return BR_DONE;
        }