Last active
May 6, 2016 19:30
-
-
Save kylecorry31/67fa8cb50f00e0afcc5cba3b1ca49a8d to your computer and use it in GitHub Desktop.
Data Structures in C
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
#include "arraylist.h" | |
#include <stdlib.h> | |
void ArrayList_Add(ArrayList* list, int value){ | |
if((list)->logicalSize == (list)->actualSize){ | |
ArrayList_IncreaseSize(list, 10); | |
} | |
(list)->values[(list)->logicalSize] = value; | |
(list)->logicalSize++; | |
} | |
void ArrayList_Insert(ArrayList* list, int value, int index){ | |
if((list)->logicalSize == (list)->actualSize){ | |
ArrayList_IncreaseSize(list, 10); | |
} | |
int i; | |
int temp = value; | |
for(i = index; i <= (list)->logicalSize; i++){ | |
int prevValue = (list)->values[i]; | |
(list)->values[i] = temp; | |
temp = prevValue; | |
} | |
} | |
void ArrayList_IncreaseSize(ArrayList* list, int increment){ | |
int* values = malloc(sizeof(int) * ((list)->actualSize + increment)); | |
int i; | |
for(i = 0; i < (list)->actualSize; i++){ | |
values[i] = (list)->values[i]; | |
} | |
(list)->values = malloc(sizeof(int) * ((list)->actualSize + increment)); | |
for(i = 0; i < (list)->actualSize; i++){ | |
(list)->values[i] = values[i]; | |
} | |
(list)->actualSize += 10; | |
free(values); | |
} | |
int ArrayList_Get(ArrayList* list, int index){ | |
return (list)->values[index]; | |
} | |
int ArrayList_Length(ArrayList* list){ | |
return (list)->logicalSize; | |
} | |
void ArrayList_Set(ArrayList* list, int index, int value){ | |
(list)->values[index] = value; | |
} | |
int ArrayList_Remove(ArrayList* list, int index){ | |
int value = ArrayList_Get(list, index); | |
int* values = malloc(sizeof(int) * (list)->actualSize); | |
int i; | |
for(i = 0; i < index; i++){ | |
values[i] = (list)->values[i]; | |
} | |
for(i = index; i < (list)->actualSize - 1; i++){ | |
values[i] = (list)->values[i + 1]; | |
} | |
(list)->values = malloc(sizeof(int) * (list)->actualSize); | |
for(i = 0; i < (list)->actualSize; i++){ | |
(list)->values[i] = values[i]; | |
} | |
free(values); | |
(list)->logicalSize--; | |
return value; | |
} | |
int ArrayList_Pop(ArrayList* list){ | |
(list)->logicalSize--; | |
return (list)->values[(list)->logicalSize]; | |
} |
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
typedef struct { | |
int* values; | |
int logicalSize; | |
int actualSize; | |
} ArrayList; | |
void ArrayList_Add(ArrayList* list, int value); | |
void ArrayList_IncreaseSize(ArrayList* list, int increment); | |
int ArrayList_Get(ArrayList* list, int index); | |
int ArrayList_Length(ArrayList* list); | |
int ArrayList_Remove(ArrayList* list, int index); | |
int ArrayList_Pop(ArrayList* list); | |
void ArrayList_Insert(ArrayList* list, int value, int index); | |
void ArrayList_Set(ArrayList* list, int index, int value); |
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
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include "stack.h" | |
void Stack_Push(Stack* stack, int value){ | |
ArrayList_Add(&(stack)->values, value); | |
} | |
int Stack_Pop(Stack* stack){ | |
return ArrayList_Pop(&(stack)->values); | |
} | |
int Stack_Length(Stack* stack){ | |
return ArrayList_Length(&(stack)->values); | |
} | |
int Stack_Peek(Stack* stack){ | |
return ArrayList_Get(&(stack)->values, ArrayList_Length(&(stack)->values-1)); | |
} |
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
#include "arraylist.h" | |
typedef struct { | |
ArrayList values; | |
} Stack; | |
void Stack_push(Stack* stack, int value); | |
int Stack_Pop(Stack* stack); | |
int Stack_Length(Stack* stack); | |
int Stack_Peek(Stack* stack); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment