Skip to content
Snippets Groups Projects
Commit d51823ba authored by joachim.bach's avatar joachim.bach
Browse files

finished exa

parent 1b0d9c5b
No related branches found
No related tags found
No related merge requests found
......@@ -5,9 +5,211 @@
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include <malloc.h>
typedef struct _element
{
int data;
struct _element *next;
}element;
typedef struct _list
{
element *head;
element *pos;
}list;
void list_destroy(list *list)
{
list->pos = list->head;
while(list->pos->next != NULL)
{
element *tmp = list->pos;
list->pos = list->pos->next;
free(tmp);
}
free(list->pos);
}
int array_pop(element *array)
{
element *tmp = array;
array = array->next;
int value = tmp->data;
return value;
}
void print_list(list *list)
{
list->pos = list->head;
while (list->pos->next != NULL)
{
printf("%i ", list->pos->data);
list->pos = list->pos->next;
}
printf("%i ", list->pos->data);
}
int read_list(list *list)
{
list->pos = list->head;
if (list->pos->next != NULL)
{
list->pos = list->pos->next;
}
return list->pos->data;
}
int main()
{
printf("Entrez la liste \n");
int list_length1;
scanf("%i", &list_length1);
list list1;
for(int i = 0; i < list_length1; i++)
{
element *elem = malloc(sizeof(*elem));
if(list1.head != NULL)
{
elem->next = list1.head;
}
else
{
elem->next = NULL;
}
int value;
scanf("%i", &value);
elem->data = value;
list1.head = elem;
}
printf("Entrez la liste \n");
int list_length2;
scanf("%i", &list_length2);
list list2;
for(int i = 0; i < list_length2; i++)
{
element *elem = malloc(sizeof(*elem));
if(list2.head != NULL)
{
elem->next = list2.head;
}
else
{
elem->next = NULL;
}
int value;
scanf("%i", &value);
elem->data = value;
list2.head = elem;
}
int values[list_length1+list_length2];
for (int i = 0; i < list_length1; i++)
{
values[i] = read_list(&list1);
}
for (int i = list_length1; i < list_length2 + list_length1; i++)
{
values[i] = read_list(&list2);
}
printf("intersection : \n");
for(int i = 0; i< list_length1 + list_length2; i++)
{
printf("%i", values[i]);
}
printf("\n");
// list sorted_list_intersection;
// list1.pos = list1.head;
// for(int i = 0; i < list_length1; i++)
// {
// int value = list1.pos->data;
// if(list1.pos->next != NULL)
// {
// list1.pos = list1.pos->next;
// }
// element *elem = malloc(sizeof(*elem));
// if(sorted_list_intersection.head != NULL)
// {
// sorted_list_intersection.pos = sorted_list_intersection.head;
// while(sorted_list_intersection.pos->next != NULL && value > sorted_list_intersection.pos->data)
// {
// sorted_list_intersection.pos = sorted_list_intersection.pos->next;
// }
// if(sorted_list_intersection.pos->next != NULL)
// {
// element *tmp = sorted_list_intersection.pos->next;
// sorted_list_intersection.pos->next = elem;
// elem->next = tmp;
// }
// else
// {
// sorted_list_intersection.pos->next = elem;
// }
// }
// else
// {
// elem->next = NULL;
// sorted_list_intersection.head = elem;
// }
// elem->data = value;
// }
print_list(&list1);
print_list(&list2);
// print_list(&sorted_list_intersection);
list_destroy(&list1);
list_destroy(&list2);
// list_destroy(&sorted_list_intersection);
// for(int i = 0; i < array_length1; i++)
// {
// printf("%i", array_pop(array1));
// }
// printf("%i", array_length1);
// free_array(array1);
}
File deleted
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment