Esempio n. 1
0
void free_product(product_t * p)
{
   if (p == NULL)
      return;
   free_ll(p->factors_lg);
   free_ll(p->factors_sm);
   free(p);
}
Esempio n. 2
0
void testLL() {

  int i;
  int storeUs[6] = {5,10,15,7,2,2};

	struct list_head *head = NULL;

	head=create_new_ll();

	printf("Is the list empty? %s\n",is_list_empty(head) ? "Yes" : "No");

  for(i=0; i<6; i++) {
    add_new_node_ll(head,storeUs[i]);
  }
	printf("The number 2 occurs: %d time(s)\n",find_elements_ll(head,2));
  print_out_ll(head);
  remove_node_ll(head,5);
  print_out_ll(head);
  remove_node_ll(head,7);
  print_out_ll(head);
	printf("Total Elements %d\n",total_elements_ll(head));
  remove_node_ll(head,2);
  print_out_ll(head);
	printf("Total Elements %d\n",total_elements_ll(head));
	printf("The number 2 occurs: %d time(s)\n",find_elements_ll(head,2));
	printf("Does the element 10 exist in the list: %s\n", \
					does_element_exist_ll(head,10) ? "Yes!" : "No");
	printf("Does the element 100 exist in the list: %s\n", \
					does_element_exist_ll(head,100) ? "Yes!" : "No");
	printf("Is the list empty? %s\n",is_list_empty(head) ? "Yes" : "No");

	free_ll(head);
}
Esempio n. 3
0
static void free_ll(struct factors *p)
{
   if (p == NULL)
      return;
   if (p->next != NULL)
      free_ll(p->next);
   free(p);
}
/*
 * Computes the longest path on a directed graph
 */
ll_node* longest_path_da(graph* g,int* len_dest) {

   graph* f = flip(g);
   ll_node* cyc = cycles(g);
   cycle_counter counter = build_cycle_counter(cyc,g,f);
   init_cut_cycles(counter,g,f);



   int best_len = -1;
   ll_node* best_path = NULL;

   do {
      printf("g: \n");
      print_graph(g);
      printf("\nf: \n");
      print_graph(f);
      printf("\ncycle_counter: \n");
      print_cycle_counter(counter);
      printf("\n\n");

      int new_len;
      ll_node* new_path = longest_path_dag(g,f,&new_len);

      if(new_len > best_len) {
         free_ll(best_path);
         best_len = new_len;
         best_path = new_path;
      }
   } while(inc_cycles(counter,g,f));

   *len_dest = best_len;

   free_cycle_counter(counter);
   free_ll(cyc);
   free_graph(f);

   return best_path;
}
Esempio n. 5
0
static int addfactors(factor_t ** p, int factor_one, int factor_two)
{
   int n = factor_one * factor_two;
   if ((*p == NULL) || (((*p)->factor_a) * (*p)->factor_b != n)) {
      free_ll(*p);
      *p = NULL;
   }

   factor_t *tmp = malloc(sizeof(factor_t));
   if (tmp == NULL) {
      fprintf(stderr, "Memory error!\n");
      return 1;
   }

   tmp->next = *p;
   tmp->factor_a = factor_one;
   tmp->factor_b = factor_two;
   *p = tmp;
   return 0;
}
Esempio n. 6
0
File: code.cpp Progetto: samrrr/Labs
int main()
{
	setlocale(0, "");
	SetConsoleCP(1251);
	SetConsoleOutputCP(1251);

	NODE* list = NULL; //список
	int uc;//выбор пользователя
	int uc2;//выбор пользователя - 2

	do
	{
		uc = menu(list); //вывод меню

		switch (uc)
		{
		case 1: //тут будет справка
			break;
		case 2: //добавление узлов
			do
			{
				system("cls");
				printf_s("\n ЗАПОЛНЕНИЕ СПИСКА\n\n");
				puts(" 1 - Добавить данные вручную");
				puts(" 2 - Добавить данные из файла");
				puts(" 0 - Вернуться в главное меню");

				uc2 = userchoice();

				switch (uc2)
				{
				case 1: //меню добавления записей с клавиатуры
					list = add(list);
					break;
				case 2: //добавление из файла
				{
					int cont = file_read_help(); //справка по добавлению записей из файла
					if (cont) //если пользоватеь все же хочет добавить записи из файла
					{
						char* fn = get_file_name("ДОБАВЛЕНИЕ ЗАПИСЕЙ ИЗ ФАЙЛА"); //ввод имени файла
						list = read_from_file(list, fn); //добавление записей
						system("pause");
					}
					break;
				}

				case 0: //отмена
					break;
				default:
					system("cls");
					printf_s("\n ОШИБКА\n");
					printf_s("\n Введите существующий пункт\n\n ");
					system("pause");
				}
			} while (uc2 && uc2 < 0 || uc2 > 2);
			break;
		case 3: //вывод списка
			if (list)
			{
				do
				{
					system("cls");

					printf_s("\n ВЫВОД СПИСКА\n\n");
					puts(" 1 - На экран");
					puts(" 2 - Сохранить в файл");
					puts(" 0 - Вернуться в главное меню");

					uc2 = userchoice();

					switch (uc2)
					{
					case 1: //вывод в консоль
						cnsl_output(list);
						break;
					case 2: //вывод в файл
					{
						char* fn = get_file_name("СОХРАНЕНИЕ СПИСКА В ФАЙЛ"); //ввод имени файла
						write_to_file(list, fn); //вывод в файл
						system("pause");
						break;
					}
					case 0: //отмена
						break;
					default:
						system("cls");
						printf_s("\n ОШИБКА\n");
						printf_s("\n Введите существующий пункт\n\n ");
						system("pause");
						break;
					}
				} while (uc2 && uc2 < 0 || uc2 > 2);
			}
			else
			{
				system("cls");
				printf_s("\n ОШИБКА\n");
				printf_s("\n Введите существующий пункт\n\n ");
				system("pause");
			}
			break;
		case 4://удаление списка
			if (list)
				list = free_ll(list);
			else
			{
				system("cls");
				printf_s("\n ОШИБКА\n");
				printf_s("\n Введите существующий пункт\n\n ");
				system("pause");
			}
			break;
		case 0: //выход
			if (list)
				list = free_ll(list);
			break;
		default:
			system("cls");
			printf_s("\n ОШИБКА\n");
			printf_s("\n Введите существующий пункт\n\n ");
			system("pause");
		}
	} while (uc);
	return 0;
}