Compare commits
42 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 439457a903 | |||
| 6adb153201 | |||
| 30efd857c1 | |||
| a92c8a4ddf | |||
| 3c46959903 | |||
| 7ae0683809 | |||
| 40d224a8f8 | |||
| 72a13f7a19 | |||
| 95129eb608 | |||
| bcdfc07ee7 | |||
| 44746d970c | |||
| 8a194966d7 | |||
| 130ff44e16 | |||
| 7d5bb35f0b | |||
| d10807edb7 | |||
| a3142bf861 | |||
| 97fa4e5c8b | |||
| 8491a3aa7d | |||
| 6da27cbf48 | |||
| 4a720a3569 | |||
| d0c7a97131 | |||
| 2440002e28 | |||
| 3d0dd9e993 | |||
| 3ffa5c3bad | |||
| 472e8cc8ec | |||
| a87b015d99 | |||
| 6d932bfe28 | |||
| 3e87619a34 | |||
| f5c181b822 | |||
| 7b93a83737 | |||
| e56b334e85 | |||
| 5a3d3d1083 | |||
| 5829325c56 | |||
| cd32edb6fe | |||
| b6ad1eaafc | |||
| f135bcacf9 | |||
| c55ab6ab36 | |||
| 41c175cac4 | |||
| ccf8075da7 | |||
| db23925de1 | |||
| a731d3ebc4 | |||
| a62ec41abd |
@@ -1,46 +1,203 @@
|
|||||||
#include "basics.h"
|
#include "basics.h"
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
struct list *list_append(struct list *l, int e)
|
struct list *list_append(struct list *l, int e)
|
||||||
{
|
{
|
||||||
//FIXME
|
|
||||||
|
struct list *new_node = malloc(sizeof(struct list));
|
||||||
|
if(!new_node) return NULL;
|
||||||
|
|
||||||
|
new_node->data = e;
|
||||||
|
new_node->next = l;
|
||||||
|
|
||||||
|
return new_node;
|
||||||
|
/*
|
||||||
|
struct list *tmp = l;
|
||||||
|
|
||||||
|
struct list *new_node = calloc(1, sizeof(struct list));
|
||||||
|
if (!new_node) return NULL;
|
||||||
|
|
||||||
|
new_node->data = e;
|
||||||
|
if(!l) return new_node;
|
||||||
|
|
||||||
|
while (tmp->next != NULL) tmp = tmp->next;
|
||||||
|
tmp->next = new_node;
|
||||||
|
return l;
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
size_t list_count(struct list *l){
|
||||||
|
|
||||||
|
int nbr_nodes = 0;
|
||||||
|
//if (!l) return nbr_nodes;
|
||||||
|
|
||||||
|
struct list *tmp = l;
|
||||||
|
|
||||||
|
//nbr_nodes ++;
|
||||||
|
while(tmp != NULL) {
|
||||||
|
nbr_nodes ++;
|
||||||
|
tmp = tmp->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t list_count(struct list *l)
|
return nbr_nodes;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int list_insert(struct list **l, size_t index, int e){
|
||||||
|
|
||||||
|
if(!l) return 1;
|
||||||
|
|
||||||
|
struct list *new_node = malloc(sizeof(struct list));
|
||||||
|
if(!new_node) return 1;
|
||||||
|
|
||||||
|
if(index == 0){
|
||||||
|
|
||||||
|
*l = list_append(*l, e);
|
||||||
|
/*
|
||||||
|
new_node->data = e;
|
||||||
|
new_node->next = *l;
|
||||||
|
*l = new_node;
|
||||||
|
*/
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct list *tmp = *l;
|
||||||
|
size_t ind = 0;
|
||||||
|
|
||||||
|
while(tmp != NULL && ind <index - 1){
|
||||||
|
|
||||||
|
tmp = tmp->next;
|
||||||
|
ind ++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!tmp) return 1;
|
||||||
|
|
||||||
|
new_node->data = e;
|
||||||
|
new_node->next = tmp->next;
|
||||||
|
tmp->next = new_node;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
struct list *list_get(struct list *l, size_t index){
|
||||||
|
|
||||||
|
struct list *tmp = l;
|
||||||
|
size_t ind = 0;
|
||||||
|
|
||||||
|
while(tmp != NULL && ind < index){
|
||||||
|
tmp = tmp->next;
|
||||||
|
ind ++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return tmp;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void list_destroy(struct list *l){
|
||||||
|
|
||||||
|
if (!l) return;
|
||||||
|
|
||||||
|
list_destroy(l->next);
|
||||||
|
|
||||||
|
free(l);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct list *list_find(struct list *l, int e){
|
||||||
|
|
||||||
|
struct list *tmp = l;
|
||||||
|
|
||||||
|
while (tmp != NULL && tmp->data != e) tmp = tmp->next;
|
||||||
|
|
||||||
|
return tmp;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
struct list *list_delete_at(struct list **l, size_t index){
|
||||||
|
|
||||||
|
if (!l || !*l) return NULL;
|
||||||
|
|
||||||
|
struct list *old = *l;
|
||||||
|
|
||||||
|
if ( index == 0){
|
||||||
|
*l = old->next;
|
||||||
|
return old;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct list *tmp = *l;
|
||||||
|
|
||||||
|
size_t ind = 0;
|
||||||
|
while(tmp != NULL && ind < index - 1){
|
||||||
|
|
||||||
|
tmp = tmp->next;
|
||||||
|
ind ++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ind == index - 1) {
|
||||||
|
old = tmp->next;
|
||||||
|
tmp->next = old->next;
|
||||||
|
return old;
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int list_remove(struct list **l, int e){
|
||||||
|
|
||||||
|
struct list *old = NULL;
|
||||||
|
struct list *tmp = *l;
|
||||||
|
|
||||||
|
while (tmp != NULL && tmp->data != e){
|
||||||
|
|
||||||
|
old = tmp;
|
||||||
|
tmp = tmp->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!tmp) return 0;
|
||||||
|
|
||||||
|
if(!old) *l = tmp->next;
|
||||||
|
else old->next = tmp->next;
|
||||||
|
|
||||||
|
free(tmp);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void list_print(struct list *l){
|
||||||
|
|
||||||
|
struct list *tmp = l;
|
||||||
|
|
||||||
|
while (tmp != NULL){
|
||||||
|
|
||||||
|
printf("%i",tmp->data);
|
||||||
|
|
||||||
|
if (tmp->next != NULL) printf(" -> ");
|
||||||
|
|
||||||
|
tmp = tmp->next;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
int main(void)
|
||||||
{
|
{
|
||||||
//FIXME
|
struct list *l = NULL;
|
||||||
}
|
|
||||||
|
|
||||||
int list_insert(struct list **l, size_t index, int e)
|
l = list_append(l, 3);
|
||||||
{
|
l = list_append(l, 2);
|
||||||
}
|
l = list_append(l, 1);
|
||||||
|
|
||||||
struct list *list_get(struct list *l, size_t index)
|
// l = [1] -> [2] -> [3]
|
||||||
{
|
list_insert(&l, 1, 99); // l = [1] -> [99] -> [2] -> [3], and list_insert returns 0
|
||||||
|
list_insert(&l, 9, 99); // list unchanged, returns 1
|
||||||
|
|
||||||
//FIXME
|
list_print(l);
|
||||||
}
|
|
||||||
|
|
||||||
struct list *list_find(struct list *l, int e)
|
list_destroy(l);
|
||||||
{
|
|
||||||
//FIXME
|
|
||||||
}
|
|
||||||
|
|
||||||
struct list *list_delete_at(struct list **l, size_t index)
|
return 0;
|
||||||
{
|
|
||||||
//FIXME
|
|
||||||
}
|
|
||||||
|
|
||||||
int list_remove(struct list **l, int e)
|
|
||||||
{
|
|
||||||
//FIXME
|
|
||||||
}
|
|
||||||
|
|
||||||
void list_destroy(struct list *l)
|
|
||||||
{
|
|
||||||
//FIXME
|
|
||||||
}
|
|
||||||
|
|
||||||
void list_print(struct list *l)
|
|
||||||
{
|
|
||||||
//FIXME
|
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|||||||
BIN
Binary file not shown.
@@ -1,26 +1,140 @@
|
|||||||
#include "double.h"
|
#include "double.h"
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
struct dlist *dlist_append(struct dlist *l, int e)
|
struct dlist *dlist_append(struct dlist *l, int e)
|
||||||
{
|
{
|
||||||
//FIXME
|
struct dlist *new_node = calloc(1, sizeof(struct dlist));
|
||||||
|
if(!new_node) return NULL;
|
||||||
|
|
||||||
|
new_node->prev = NULL;
|
||||||
|
new_node->next = l;
|
||||||
|
new_node->data = e;
|
||||||
|
|
||||||
|
if (l) l->prev = new_node;
|
||||||
|
|
||||||
|
return new_node;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int dlist_insert(struct dlist **l, size_t index, int e)
|
int dlist_insert(struct dlist **l, size_t index, int e)
|
||||||
{
|
{
|
||||||
//FIXME
|
if(!l) return 1;
|
||||||
|
|
||||||
|
//struct dlist *new_node = malloc(sizeof(struct dlist));
|
||||||
|
//if(!new_node) return 1;
|
||||||
|
|
||||||
|
//struct dlist *tmp = *l;
|
||||||
|
|
||||||
|
if(index == 0){
|
||||||
|
|
||||||
|
*l = dlist_append(*l, e);
|
||||||
|
if(!l) return 1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct dlist *tmp = *l;
|
||||||
|
size_t ind = 0;
|
||||||
|
while(tmp != NULL && ind < index - 1){
|
||||||
|
|
||||||
|
tmp = tmp->next;
|
||||||
|
ind ++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!tmp) return 1;
|
||||||
|
|
||||||
|
struct dlist *new_node = malloc(sizeof(struct dlist));
|
||||||
|
if(!new_node) return 1;
|
||||||
|
|
||||||
|
new_node->data = e;
|
||||||
|
new_node->next = tmp->next;
|
||||||
|
new_node->prev = tmp;
|
||||||
|
|
||||||
|
if (tmp->next != NULL) tmp->next->prev = new_node;
|
||||||
|
tmp->next = new_node;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct dlist *dlist_delete_at(struct dlist **l, size_t index)
|
struct dlist *dlist_delete_at(struct dlist **l, size_t index)
|
||||||
{
|
{
|
||||||
//FIXME
|
if(!l || *l) return NULL;
|
||||||
|
|
||||||
|
struct dlist *old = NULL;
|
||||||
|
if(index == 0){
|
||||||
|
|
||||||
|
old = *l;
|
||||||
|
*l = old->next;
|
||||||
|
|
||||||
|
return old;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct dlist *tmp = *l;
|
||||||
|
size_t ind = 0;
|
||||||
|
while(tmp != NULL && ind < index -1){
|
||||||
|
|
||||||
|
tmp = tmp->next;
|
||||||
|
ind ++;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if(ind == index -1){
|
||||||
|
|
||||||
|
old = tmp->next;
|
||||||
|
tmp->prev = old->prev;
|
||||||
|
tmp->next = old->next;
|
||||||
|
|
||||||
|
return old;
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int dlist_remove(struct dlist **l, int e)
|
int dlist_remove(struct dlist **l, int e)
|
||||||
{
|
{
|
||||||
//FIXME
|
struct dlist *old = NULL;
|
||||||
|
struct dlist *tmp = *l;
|
||||||
|
|
||||||
|
while(tmp != NULL && tmp->data != e){
|
||||||
|
|
||||||
|
old = tmp;
|
||||||
|
tmp = tmp->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!tmp) return 0;
|
||||||
|
|
||||||
|
if(!old){
|
||||||
|
|
||||||
|
*l = tmp->next;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
|
||||||
|
old->next = tmp->next;
|
||||||
|
old->prev = tmp->prev;
|
||||||
|
}
|
||||||
|
|
||||||
|
free(tmp);
|
||||||
|
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void dlist_print(struct dlist *l)
|
void dlist_print(struct dlist *l)
|
||||||
{
|
{
|
||||||
//FIXME
|
|
||||||
|
struct dlist *tmp = l;
|
||||||
|
|
||||||
|
while (tmp != NULL){
|
||||||
|
|
||||||
|
printf("%i", tmp->data);
|
||||||
|
|
||||||
|
if (tmp->next != NULL) printf(" <-> ");
|
||||||
|
|
||||||
|
tmp = tmp->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,47 @@
|
|||||||
#include "practice.h"
|
#include "practice.h"
|
||||||
|
#include "../../utils/lists.h"
|
||||||
|
#include "../basics/basics.h"
|
||||||
|
|
||||||
int is_palindrome(struct dlist *l)
|
int is_palindrome(struct dlist *l)
|
||||||
{
|
{
|
||||||
//FIXME
|
if (!l || l->next != NULL) return 1;
|
||||||
|
|
||||||
|
struct dlist *tmp = l;
|
||||||
|
int indexb = 0;
|
||||||
|
|
||||||
|
while (tmp->next != NULL) {
|
||||||
|
|
||||||
|
indexb ++;
|
||||||
|
tmp = tmp->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
struct dlist *back = tmp;
|
||||||
|
tmp = l;
|
||||||
|
int indexp = 0;
|
||||||
|
|
||||||
|
while (indexp < indexb){
|
||||||
|
|
||||||
|
if (tmp->data != back->data) return 0;
|
||||||
|
|
||||||
|
indexp ++;
|
||||||
|
indexb --;
|
||||||
|
|
||||||
|
tmp = tmp->next;
|
||||||
|
back = back->prev;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct list *reverse(struct list *l)
|
struct list *reverse(struct list *l)
|
||||||
{
|
{
|
||||||
//FIXME
|
struct list *tmp =l;
|
||||||
|
struct list *rslt = NULL;
|
||||||
|
while(l != NULL){
|
||||||
|
rslt = list_append(rslt, l->data);
|
||||||
|
l = l->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
return rslt;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user