Skip to content
Snippets Groups Projects
Commit f1076df7 authored by poulpe's avatar poulpe
Browse files

[Update] Rename library + Add new file for array library

parent c24c1f27
No related branches found
No related tags found
No related merge requests found
Exe_list 0 → 100755
File added
CC = gcc -Wall -Wextra
CFLAGS=-Wall -Wextra -std=c11
LINKS=-lm -lSDL2
LINKS=-lm
SANIT = -g -pedantic -fsanitize=address -fsanitize=leak -fsanitize=undefined -fsanitize-address-use-after-scope
Exe: queue.o main.o
Exe_list: queue_ll.o main_linked_list.o
$(CC) $(CFLAGS) -o $@ $^ $(LINKS) $(SANIT)
Exe_array: queue_a.o main_array.o
$(CC) $(CFLAGS) -o $@ $^ $(LINKS) $(SANIT)
main_array.o: main_array.c
$(CC) $(CFLAGS) -o $@ $^ $(LINKS) $(SANIT)
queue_a.o: Queue_a.c
$(CC) $(CFLAGS) -o $@ $^ $(LINKS) $(SANIT)
main.o: main.c
$(CC) $(CFLAGS) -o $@ -c $^
queue.o: Queue.c
queue_ll.o: Queue_ll.c
$(CC) $(CFLAGS) -o $@ -c $^
clean:
rm -rf *.o
rm -rf Exe
rm -rf Exe*
rebuild: clean Exe
\ No newline at end of file
rebuild: clean Exe_list Exe_array
Queue_a.c 0 → 100644
#include "Queue_a.h"
queue_array queue_create()
{
queue_array q;
q.head = NULL;
q.tail = NULL;
return q;
}
bool queue_is_empty(queue_array *q)
{
if(q->head == NULL && q->tail == NULL)
{
return true;
}
return false;
}
void queue_print_item(queue_array *q)
{
element* tmp = q->head;
T i = 0;
while (tmp != NULL)
{
printf("Data[%d] : %d\n",i,tmp->data);
tmp = tmp->next;
i+=1;
}
}
void queue_item_tail_insert_value(queue_array *q, T value)
{
element elem = {.data = value,.next = NULL};
queue_item_tail_insert_element(q,&elem);
}
void queue_item_tail_insert_element(queue_array *q, element* elem)
{
if(q->tail == NULL)
{
q->tail = malloc(sizeof(element));
q->tail->data = elem->data;
q->tail->next = NULL;
q->head = q->tail;
}else
{
q->tail->next = malloc(sizeof(element));
q->tail = q->tail->next;
q->tail->data = elem->data;
q->tail->next = NULL;
}
}
T queue_item_head_extract(queue_array *q)
{
assert(!queue_is_empty(q));
T elem = q->head->data;
free(q->head);
q->head = q->head->next;
return elem;
}
T queue_item_head_consult(queue_array *q)
{
return q->head->data;
}
T queue_item_tail_extract(queue_array *q)
{
assert(!queue_is_empty(q));
T res;
element* tmp = q->head;
while (tmp->next != NULL)
{
if(tmp->next != q->tail)
{
tmp = tmp->next;
}else
{
tmp->next = NULL;
res = q->tail->data;
free(q->tail);
q->tail = tmp;
}
}
return res;
}
void queue_destroy(queue_array *q)
{
while(q->tail != NULL)
{
if(q->head == q->tail)
{
free(q->head);
q->head = NULL;
q->tail = NULL;
break;
}
element* tmp = q->head;
q->head = tmp->next;
free(tmp);
}
}
int queue_item_count(queue_array *q)
{
assert(!queue_is_empty(q));
int count = 0;
element* tmp = q->head;
while (tmp != NULL)
{
tmp = tmp->next;
count += 1;
}
return count;
}
bool queue_is_equal(queue_array *q1,queue_array *q2)
{
assert(!queue_is_empty(q1));assert(!queue_is_empty(q2));
bool res = true;
if(queue_item_count(q1) != queue_item_count(q2)){return false;}
element* tmp_q1 = q1->head;
element* tmp_q2 = q2->head;
while(tmp_q1 != NULL && tmp_q2 != NULL)
{
if(tmp_q1->data != tmp_q2->data)
{
return false;
}
else{
tmp_q1 = tmp_q1->next;
tmp_q2 = tmp_q2->next;
}
}
return res;
}
bool queue_search_value(queue_array *q,T value)
{
assert(!queue_is_empty(q));
element* tmp = q->head;
while (tmp != NULL)
{
if(tmp->data == value){return true;}
tmp = tmp->next;
}
return false;
}
\ No newline at end of file
#ifndef HEADER_QUEUE_ARRAY
#define HEADER_QUEUE_ARRAY
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <assert.h>
#include <math.h>
typedef int32_t T;
typedef struct _element
{
T data;
struct _element *next;
} element;
// typedef element* queue_array;
typedef struct _queue_array {
element* head;
element* tail;
} queue_array;
queue_array queue_create();
void queue_destroy(queue_array *q);
bool queue_is_empty(queue_array *q);
int queue_item_count(queue_array *q);
void queue_print_item(queue_array *q);
T queue_item_head_extract(queue_array *q);
T queue_item_head_consult(queue_array *q);
T queue_item_tail_extract(queue_array *q);
bool queue_is_equal(queue_array *q1,queue_array *q2);
void queue_item_tail_insert_element(queue_array *q, element* elem);
void queue_item_tail_insert_value(queue_array *q, int32_t value);
#endif
\ No newline at end of file
#include "Queue.h"
#include "Queue_ll.h"
queue queue_create()
queue_ll queue_create()
{
queue q;
queue_ll q;
q.head = NULL;
q.tail = NULL;
return q;
}
bool queue_is_empty(queue *q)
bool queue_is_empty(queue_ll *q)
{
if(q->head == NULL && q->tail == NULL)
{
......@@ -17,7 +17,7 @@ bool queue_is_empty(queue *q)
return false;
}
void queue_print_item(queue *q)
void queue_print_item(queue_ll *q)
{
element* tmp = q->head;
T i = 0;
......@@ -29,14 +29,14 @@ void queue_print_item(queue *q)
}
}
void queue_item_tail_insert_value(queue *q, T value)
void queue_item_tail_insert_value(queue_ll *q, T value)
{
element elem = {.data = value,.next = NULL};
queue_item_tail_insert_element(q,&elem);
}
void queue_item_tail_insert_element(queue *q, element* elem)
void queue_item_tail_insert_element(queue_ll *q, element* elem)
{
if(q->tail == NULL)
{
......@@ -53,7 +53,7 @@ void queue_item_tail_insert_element(queue *q, element* elem)
}
}
T queue_item_head_extract(queue *q)
T queue_item_head_extract(queue_ll *q)
{
assert(!queue_is_empty(q));
T elem = q->head->data;
......@@ -62,12 +62,12 @@ T queue_item_head_extract(queue *q)
return elem;
}
T queue_item_head_consult(queue *q)
T queue_item_head_consult(queue_ll *q)
{
return q->head->data;
}
T queue_item_tail_extract(queue *q)
T queue_item_tail_extract(queue_ll *q)
{
assert(!queue_is_empty(q));
......@@ -89,7 +89,7 @@ T queue_item_tail_extract(queue *q)
return res;
}
void queue_destroy(queue *q)
void queue_destroy(queue_ll *q)
{
while(q->tail != NULL)
{
......@@ -106,7 +106,7 @@ void queue_destroy(queue *q)
}
}
int queue_item_count(queue *q)
int queue_item_count(queue_ll *q)
{
assert(!queue_is_empty(q));
int count = 0;
......@@ -119,7 +119,7 @@ int queue_item_count(queue *q)
return count;
}
bool queue_is_equal(queue *q1,queue *q2)
bool queue_is_equal(queue_ll *q1,queue_ll *q2)
{
assert(!queue_is_empty(q1));assert(!queue_is_empty(q2));
bool res = true;
......@@ -140,7 +140,7 @@ bool queue_is_equal(queue *q1,queue *q2)
return res;
}
bool queue_search_value(queue *q,T value)
bool queue_search_value(queue_ll *q,T value)
{
assert(!queue_is_empty(q));
......
#ifndef HEADER_QUEUE
#define HEADER_QUEUE
#ifndef HEADER_QUEUE_LL
#define HEADER_QUEUE_LL
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
......@@ -15,24 +15,24 @@ typedef struct _element
struct _element *next;
} element;
// typedef element* queue;
// typedef element* queue_ll;
typedef struct _queue {
typedef struct _queue_ll {
element* head;
element* tail;
} queue;
} queue_ll;
queue queue_create();
void queue_destroy(queue *q);
bool queue_is_empty(queue *q);
int queue_item_count(queue *q);
void queue_print_item(queue *q);
T queue_item_head_extract(queue *q);
T queue_item_head_consult(queue *q);
T queue_item_tail_extract(queue *q);
bool queue_is_equal(queue *q1,queue *q2);
void queue_item_tail_insert_element(queue *q, element* elem);
void queue_item_tail_insert_value(queue *q, int32_t value);
queue_ll queue_create();
void queue_destroy(queue_ll *q);
bool queue_is_empty(queue_ll *q);
int queue_item_count(queue_ll *q);
void queue_print_item(queue_ll *q);
T queue_item_head_extract(queue_ll *q);
T queue_item_head_consult(queue_ll *q);
T queue_item_tail_extract(queue_ll *q);
bool queue_is_equal(queue_ll *q1,queue_ll *q2);
void queue_item_tail_insert_element(queue_ll *q, element* elem);
void queue_item_tail_insert_value(queue_ll *q, int32_t value);
#endif
\ No newline at end of file
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int main()
{
queue_array q;
return 0;
}
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include "Queue.h"
#include "Queue_ll.h"
int main()
{
queue q = queue_create();
queue_ll q = queue_create();
queue_item_tail_insert_value(&q,10);
queue_item_tail_insert_value(&q,20);
......@@ -15,7 +15,7 @@ int main()
queue_print_item(&q);
queue q1 = queue_create();
queue_ll q1 = queue_create();
queue_item_tail_insert_value(&q1,10);
queue_item_tail_insert_value(&q1,20);
queue_item_tail_insert_value(&q1,30);
......@@ -38,5 +38,7 @@ int main()
queue_destroy(&q);
queue_destroy(&q1);
return 0;
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment