Last active
August 29, 2015 13:58
-
-
Save nureynisow/10012020 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include "sequence.h" | |
#include "assert.h" | |
struct sequence_t{ | |
void **t; | |
int size; | |
int nb_el; | |
}; | |
const int DEFAULT_SIZE = 2; | |
/* create an empty sequence */ | |
sequence sequence_create(){ | |
sequence s = malloc(sizeof(*s)); | |
s->t = malloc(sizeof(void *) * DEFAULT_SIZE); | |
s->size = 2; | |
s->nb_el = -1; | |
return s; | |
} | |
/* destroy the whole sequence (not objects)*/ | |
void sequence_destroy(sequence s){ | |
free(s->t); | |
free(s); | |
} | |
/* find the object at position pos and return it or NULL if not | |
found */ | |
void * sequence_find(sequence s, int pos){ | |
assert(pos > s->nb_el); | |
return s->t[pos]; | |
} | |
/* insert an object between objects at position pos-1 and pos */ | |
void sequence_insert(sequence s, void * object, int pos){ | |
if(s->size < pos){ | |
s->t = realloc(s->t, s->size*2); | |
s->size*=2; | |
} | |
for(int i=s->nb_el+1; i>pos;--i){ | |
s->t[i] = s->t[i-1]; | |
} | |
s->t[pos] = object; | |
s->nb_el++; | |
} | |
/* delete the object at position pos */ | |
void sequence_delete(sequence s, int pos){ | |
assert(pos > s->nb_el); | |
for(int i=pos;i<s->nb_el;i++) | |
s->t[i] = s->t[i+1]; | |
} | |
/* dump the sequence as integer object */ | |
void sequence_dump(sequence s){ | |
for(int i = 0; i < s->nb_el; ++i) | |
printf("|%d",(int)(s->t[i])); | |
printf("|\n"); | |
} | |
/* return the sequence length */ | |
int sequence_length(sequence s){ | |
return s->nb_el; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment