-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from dcross23/tads
Tads to merge
- Loading branch information
Showing
29 changed files
with
1,512 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
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,... |
This file contains 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
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(""); | ||
} | ||
} | ||
|
||
|
This file contains 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
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 |
This file contains 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
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; | ||
} | ||
|
||
|
||
|
||
|
This file contains 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
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) | ||
|
Oops, something went wrong.