Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- long ListCreate()
- {
- long list_ptr = malloc(sizeof(long) * 2);
- ((long*)list_ptr)[0] = list_ptr;
- ((long*)list_ptr)[1] = 0;
- }
- long ListCount(long list_ptr)
- {
- return ((long*)list_ptr)[1];
- }
- long ListGetEntry(long list_ptr, long index)
- {
- //TODO: Get Current List Object without iterating; dynamic number of dereference brackets frog code
- long current_list_entry_ptr = list_ptr;
- for (int i = 0; i <= index; ++i)
- {
- current_list_entry_ptr = ((long*)current_list_entry_ptr)[0];
- }
- return current_list_entry_ptr;
- }
- long ListRemove(long list_ptr, long index)
- {
- long list_count = ((long*)list_ptr)[1];
- if (index == 0)
- {
- long current_list_entry_ptr = ((long*)list_ptr)[0];
- ((long*)list_ptr)[0] = ((long*)current_list_entry_ptr)[0];
- --((long*)list_ptr)[1];
- free(current_list_entry_ptr);
- }
- else
- {
- long current_list_entry_ptr = ListGetEntry(list_ptr, index);
- if (index <= list_count - 1)
- {
- long previous_list_entry_ptr = ListGetEntry(list_ptr, index -1);
- long next_list_entry_ptr = ListGetEntry(list_ptr, index + 1);
- ((long*)previous_list_entry_ptr)[0] = next_list_entry_ptr;
- --((long*)list_ptr)[1];
- free(current_list_entry_ptr);
- }
- else
- {
- --((long*)list_ptr)[1];
- free(current_list_entry_ptr);
- }
- }
- }
- void ListDelete(long list_ptr)
- {
- //TODO: Delete all list objects at same time with frog code
- long list_count = ((long*)list_ptr)[1];
- long next_list_entry_ptr = list_ptr;
- while (ListCount(list_ptr) > 0)
- {
- ListRemove(list_ptr, 0);
- }
- free(list_ptr);
- }
- void ListAdd(long list_ptr, long obj_ptr)
- {
- long list_count = ((long*)list_ptr)[1];
- long current_list_entry_ptr = malloc(sizeof(long) * 2);
- long last_list_entry_ptr = ListGetEntry(list_ptr, list_count - 1);
- ((long*)last_list_entry_ptr)[0] = current_list_entry_ptr;
- ((long*)current_list_entry_ptr)[1] = obj_ptr;
- ++((long*)list_ptr)[1];
- }
- long ListInsert(long list_ptr, long obj_ptr, long index)
- {
- }
- long ListGet(long list_ptr, long index)
- {
- return ((long*)ListGetEntry(list_ptr, index))[1];
- }
Advertisement
Comments
-
- struct List {
- long *list_entry;
- long count;
- };
- struct ListEntry {
- long *list_entry;
- long object_ptr;
- };
- struct List *ListCreate();
- long ListCount();
- void ListDelete(struct List *list_ptr);
- struct ListEntry *ListGetEntry(struct List *list_ptr, long index);
- void ListRemove(struct List *list_ptr, long index);
- void ListAdd(struct List *list_ptr, long obj_ptr);
- long ListGet(struct List *list_ptr, long index);
-
- #include "List.h"
- #include <stdlib.h>
- struct List * ListCreate() {
- struct List *list_ptr = malloc(sizeof(struct List));
- list_ptr->list_entry = list_ptr;
- list_ptr->count = 0;
- }
- long ListCount(struct List *list_ptr) { return list_ptr->count; }
- struct ListEntry *ListGetEntry(struct List *list_ptr, long index) {
- // TODO: Get Current List Object without iterating; dynamic number of
- // dereference brackets frog code
- struct ListEntry *current_list_entry_ptr = (struct ListEntry *)list_ptr;
- for (int i = 0; i <= index; ++i) {
- current_list_entry_ptr = current_list_entry_ptr->list_entry;
- }
- return current_list_entry_ptr;
- }
- void ListRemove(struct List *list_ptr, long index) {
- long list_count = list_ptr->count;
- if (index == 0) {
- struct ListEntry *current_list_entry_ptr = list_ptr->list_entry;
- list_ptr->list_entry = current_list_entry_ptr->list_entry;
- --list_ptr->count;
- free(current_list_entry_ptr);
- } else {
- struct ListEntry *current_list_entry_ptr = ListGetEntry(list_ptr, index);
- if (index <= list_count - 1) {
- struct ListEntry *previous_list_entry_ptr =
- ListGetEntry(list_ptr, index - 1);
- struct ListEntry *next_list_entry_ptr = ListGetEntry(list_ptr, index + 1);
- previous_list_entry_ptr->list_entry = next_list_entry_ptr;
- --list_ptr->count;
- free(current_list_entry_ptr);
- } else {
- --list_ptr->count;
- free(current_list_entry_ptr);
- }
- }
- }
- void ListDelete(struct List *list_ptr) {
- // TODO: Delete all list objects at same time with frog code
- long list_count = list_ptr->count;
- struct ListEntry *next_list_entry_ptr = (struct ListEntry *)list_ptr;
- while (ListCount(list_ptr) > 0) {
- ListRemove(list_ptr, 0);
- }
- free(list_ptr);
- }
- void ListAdd(struct List *list_ptr, long obj_ptr) {
- long list_count = list_ptr->count;
- struct ListEntry *current_list_entry_ptr = malloc(sizeof(struct ListEntry));
- struct ListEntry *last_list_entry_ptr =
- ListGetEntry(list_ptr, list_count - 1);
- last_list_entry_ptr->list_entry = current_list_entry_ptr;
- current_list_entry_ptr->object_ptr = obj_ptr;
- ++list_ptr->count;
- }
- long ListGet(struct List *list_ptr, long index) {
- return ListGetEntry(list_ptr, index)->object_ptr;
- }
Add Comment
Please, Sign In to add comment
Advertisement