-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
178 additions
and
30 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
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
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
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,119 @@ | ||
#ifndef SLIST_H_ | ||
#define SLIST_H_ | ||
|
||
#include <stdlib.h> | ||
|
||
#define SDECL(sname, elem_t) \ | ||
struct sname##_node; \ | ||
struct sname; \ | ||
struct sname##_node *sname##_node(elem_t value); \ | ||
struct sname *sname(); \ | ||
void sname##_del(struct sname *list); \ | ||
struct sname *sname##_append(struct sname *list, elem_t value); \ | ||
struct sname *sname##_prepend(struct sname *list, elem_t value); \ | ||
struct sname *sname##_dfirst(struct sname *list); \ | ||
struct sname *sname##_push(struct sname *list, elem_t elem); \ | ||
elem_t *sname##_top(struct sname *list); \ | ||
struct sname *sname##_pop(struct sname *list); \ | ||
struct sname *sname##_enque(struct sname *list, elem_t elem); \ | ||
elem_t *sname##_peek(struct sname *list); \ | ||
struct sname *sname##_deque(struct sname *list); \ | ||
int sname##_empty(struct sname *list); | ||
|
||
#define SIMPL(sname, elem_t) \ | ||
struct sname##_node { \ | ||
elem_t value; \ | ||
struct sname##_node *next; \ | ||
}; \ | ||
struct sname { \ | ||
struct sname##_node *first; \ | ||
struct sname##_node *last; \ | ||
int size; \ | ||
}; \ | ||
\ | ||
struct sname##_node *sname##_node(elem_t value) { \ | ||
struct sname##_node *tmp = malloc(sizeof(struct sname##_node)); \ | ||
tmp->value = value; \ | ||
tmp->next = NULL; \ | ||
return tmp; \ | ||
} \ | ||
struct sname *sname() { \ | ||
struct sname *tmp = malloc(sizeof(struct sname)); \ | ||
tmp->first = tmp->last = NULL; \ | ||
tmp->size = 0; \ | ||
return tmp; \ | ||
} \ | ||
void sname##_del(struct sname *list) { \ | ||
while (!sname##_empty(list)) { \ | ||
sname##_dfirst(list); \ | ||
} \ | ||
free(list); \ | ||
} \ | ||
struct sname *sname##_append(struct sname *list, elem_t value) {\ | ||
struct sname##_node *node = sname##_node(value); \ | ||
if (!node) { \ | ||
return NULL; \ | ||
} \ | ||
if (list->first == NULL) { \ | ||
list->first = list->last = node; \ | ||
} else { \ | ||
list->last->next = node; \ | ||
list->last = node; \ | ||
} \ | ||
++list->size; \ | ||
return list; \ | ||
} \ | ||
struct sname *sname##_prepend(struct sname *list, elem_t value) {\ | ||
struct sname##_node *node = sname##_node(value); \ | ||
if (!node) { \ | ||
return NULL; \ | ||
} \ | ||
if (list->first == NULL) { \ | ||
list->first = list->last = node; \ | ||
} else { \ | ||
node->next = list->first; \ | ||
list->first = node; \ | ||
} \ | ||
++list->size; \ | ||
return list; \ | ||
} \ | ||
struct sname *sname##_dfirst(struct sname *list) {\ | ||
if (!list || sname##_empty(list)) { \ | ||
return NULL; \ | ||
} \ | ||
if (list->first == list->last) { \ | ||
list->last = NULL; \ | ||
} \ | ||
struct sname##_node *tmp = list->first; \ | ||
list->first = tmp->next; \ | ||
free(tmp); \ | ||
--list->size; \ | ||
return list; \ | ||
} \ | ||
struct sname *sname##_push(struct sname *list, elem_t elem) { \ | ||
return sname##_prepend(list, elem); \ | ||
} \ | ||
elem_t *sname##_top(struct sname *list) { \ | ||
return &list->first->value; \ | ||
} \ | ||
struct sname *sname##_pop(struct sname *list) {\ | ||
return sname##_dfirst(list); \ | ||
} \ | ||
struct sname *sname##_enque(struct sname *list, elem_t elem) { \ | ||
return sname##_append(list, elem); \ | ||
} \ | ||
elem_t *sname##_peek(struct sname *list) {\ | ||
return &list->first->value; \ | ||
} \ | ||
struct sname *sname##_deque(struct sname *list) { \ | ||
return sname##_dfirst(list); \ | ||
} \ | ||
int sname##_empty(struct sname *list) { \ | ||
return list->first == NULL || list->last == NULL; \ | ||
} | ||
|
||
#define SDECL_IMPL(sname, elem_t) \ | ||
SDECL(sname, elem_t) \ | ||
SIMPL(sname, elem_t) | ||
|
||
#endif // SLIST_H_ |
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
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
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
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
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,27 @@ | ||
#include "slist.h" | ||
|
||
#include <stdio.h> | ||
|
||
SDECL_IMPL(slist, int) | ||
|
||
void print_lst(struct slist *lst) { | ||
printf("size = %d\n", lst->size); | ||
for (struct slist_node *n = lst->first; n; n = n->next) { | ||
printf("%d ", n->value); | ||
} | ||
printf("\n"); | ||
} | ||
|
||
int main() { | ||
struct slist *lst = slist(); | ||
slist_append(lst, 1); | ||
slist_append(lst, 3); | ||
slist_append(lst, 5); | ||
slist_prepend(lst, 2); | ||
slist_prepend(lst, 6); | ||
print_lst(lst); | ||
slist_dfirst(lst); | ||
slist_dfirst(lst); | ||
print_lst(lst); | ||
slist_del(lst); | ||
} |
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