Skip to content

Commit

Permalink
Merge pull request #5 from dcross23/tads
Browse files Browse the repository at this point in the history
Tads to merge
  • Loading branch information
dcross23 authored Oct 23, 2020
2 parents d994d64 + 8973246 commit ebc26f1
Show file tree
Hide file tree
Showing 29 changed files with 1,512 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Algorithms and Data Structures (EDA)
Collection of algorithms (search, sorting, backtracking, divide and conquer,... algorithms) and data structures such as stacks, queues, trees, heaps,...
Collection of algorithms (search, sorting, backtracking, divide and conquer,... algorithms) and data structures such as stacks, queues, trees, heaps,...
122 changes: 122 additions & 0 deletions TADS/Lists/Array Imp/arrayList.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "arrayList.h"

int createEmptyList(ArrayList *list){
if(list == NULL) return -1;

list->last = -1;
return 0;
}


int isEmpty(ArrayList *list){
if(list == NULL) return -1;

return (list->last == -1);
}

listPosition first(ArrayList *list){
if(list == NULL) return -1;

if(!isEmpty(list)){
return 0;
}else{
return end(list);
}
}


listPosition next(listPosition p, ArrayList *list){
if(list == NULL || p<first(list) || p>end(list)) return -1;

if(p == end(list)){
return -1;
}else{
return (p+1);
}
}

listPosition prev(listPosition p, ArrayList *list){
if(list == NULL || p<first(list) || p>end(list)) return -1;

if(p == first(list)){
return -1;
}else{
return (p-1);
}
}

listPosition end(ArrayList *list){
if(list == NULL) return -1;

return list->last+1;
}

int listAdd(listElement x, listPosition p, ArrayList *list){
if(list == NULL || p<first(list) || p>end(list) || end(list) == MAX_LIST_SIZE) return -1;
if(list->last >= MAX_LIST_SIZE) return -2;

listPosition q;
for(q = list->last; q >= p; q--){
list->elements[q+1] = list->elements[q];
}
(list->last)++;
list->elements[p] = x;
return 0;
}

int listRemove(listPosition p, ArrayList *list){
if(list == NULL || p<first(list) || p>=end(list)) return -1;

listPosition q;
for(q = p; q < list->last; q++){
list->elements[q] = list->elements[q+1];
}
(list->last)--;
return 0;
}

listPosition find(listElement x, ArrayList *list){
if(list == NULL) return -1;

listPosition i;
for(i=0; i<=list->last; i++){
if(list->elements[i] == x)
return i;
}
return -1;
}

listElement get(listPosition p, ArrayList *list){
if(list == NULL || p<first(list) || p>=end(list)) return -1;

return list->elements[p];
}


int clear(ArrayList *list){
if(list == NULL) return -1;

list->last = -1;
return 0;
}

void printList(ArrayList *list){
if(list != NULL){
printf("\nLIST\n");
if(!isEmpty(list)){
listPosition i;
for(i=0; i<=list->last; i++){
printf("%d ",list->elements[i]);
}
}else{
printf("The list is empty");
}
puts("");
}
}


34 changes: 34 additions & 0 deletions TADS/Lists/Array Imp/arrayList.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef __ARRAY_LIST_H__
#define __ARRAY_LIST_H__

#define MAX_LIST_SIZE 10

//List element
typedef int listElement;
typedef int listPosition;

//List structure with array implementation
typedef struct arrayList {
listElement elements[MAX_LIST_SIZE];
int last;
} ArrayList;

//Array list methods
int createEmptyList(ArrayList *list);
int isEmpty(ArrayList *list);

listPosition first(ArrayList *list);
listPosition next(listPosition p, ArrayList *list);
listPosition prev(listPosition p, ArrayList *list);
listPosition end(ArrayList *list);

int listAdd(listElement x, listPosition p, ArrayList *list);
int listRemove(listPosition p, ArrayList *list);

listPosition find(listElement x, ArrayList *list);
listElement get(listPosition p, ArrayList *list);

int clear(ArrayList *list);
void printList(ArrayList *list);

#endif
45 changes: 45 additions & 0 deletions TADS/Lists/Array Imp/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <stdio.h>
#include <stdlib.h>

#include "arrayList.h"


int main(int argc, char **argv){
ArrayList *list;

if(NULL == (list = malloc(sizeof(ArrayList)))) return -1;


createEmptyList(list);
printList(list);

listAdd(5,end(list),list);
listAdd(8,end(list),list);
listAdd(3,end(list),list);

listAdd(4,first(list),list);
printList(list);

listAdd(7, prev(prev(end(list),list),list), list);
printList(list);

listRemove(first(list),list);
printList(list);

listElement element = 7;
printf("\n\nElement %d finded at position %d\n",element, find(element,list));

listPosition posToGet = prev(end(list), list);
printf("\nElement %d goted\n",get(posToGet, list));


clear(list);
printList(list);

puts("\n");
return 0;
}




21 changes: 21 additions & 0 deletions TADS/Lists/Array Imp/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
CC=gcc
#CFLAGS=-c
CFLAGS=-c -g
EXECUTABLE_NAME=arrayList

all: $(EXECUTABLE_NAME)

$(EXECUTABLE_NAME): main.o arrayList.o
$(CC) $^ -o $@ -lm

main.o: main.c
$(CC) $(CFLAGS) main.c

arrayList.o: arrayList.c arrayList.h
$(CC) $(CFLAGS) arrayList.c


clean:
rm *.o
rm $(EXECUTABLE_NAME)

Loading

0 comments on commit ebc26f1

Please sign in to comment.