Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
basic linked list class - say if anything else is needed
- Loading branch information
Showing
2 changed files
with
84 additions
and
0 deletions.
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <malloc.h> | ||
#include "linkedlist.h" | ||
|
||
void add(linkedListHolder list, void * data, int key) { | ||
linkedList insert = (linkedList)malloc(sizeof(struct ll)); | ||
insert->key = key; | ||
insert->data = data; | ||
if (list->head == 0) { | ||
insert->next = insert; | ||
insert->prev = insert; | ||
list->head = insert; | ||
return; | ||
} | ||
insert->prev = head; | ||
insert->next = head->next; | ||
head->next->prev = insert; | ||
return; | ||
} | ||
void * get(linkedListHolder list, int key) { | ||
linkedList pointer = list->head; | ||
do { | ||
if (pointer->key == key) { | ||
return pointer->data; | ||
} | ||
pointer = pointer->next; | ||
} while (pointer != list->head); | ||
return 0; | ||
} | ||
|
||
int remove(linkedListHolder list, int key) { | ||
if (list->head->key == key) { | ||
if (list->head->next == list->head) { | ||
free(list->head); | ||
list->head = 0; | ||
return 1; | ||
} | ||
else { | ||
list->head->next->prev = list->head->prev; | ||
list->head->prev->next = list->head->next; | ||
linkedList temp = list->head; | ||
list->head = list->head->next; | ||
free(temp); | ||
return 1; | ||
} | ||
} | ||
linkedList pointer = list->head->next; | ||
while (pointer != list->head) { | ||
if (pointer->key == key) { | ||
pointer->prev->next = pointer->next; | ||
pointer->next->prev = pointer->prev; | ||
free(pointer); | ||
return 1; | ||
} | ||
pointer = pointer->next; | ||
} | ||
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,23 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <malloc.h> | ||
|
||
struct ll { | ||
struct ll * prev; | ||
struct ll * next; | ||
int key; | ||
void * data; | ||
}; | ||
|
||
typedef struct ll * linkedList; | ||
|
||
struct llholder { | ||
linkedList head; | ||
}; | ||
typedef struct llholder * linkedListHolder; | ||
|
||
void add(linkedListHolder list, void * data, int key); | ||
|
||
void * get(linkedListHolder list, int key); | ||
|
||
int remove(linkedListHolder list, int key); |