Created
July 19, 2014 09:20
-
-
Save 314maro/75531f3eefd55ce11cf6 to your computer and use it in GitHub Desktop.
雑な実装のスタックとキュー 下手なこと(空に対しての操作とか)するとバグるはず
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> | |
typedef long item; | |
typedef struct { | |
int max_size; | |
int head, length; | |
item *items; | |
} queue; | |
int q_index(queue *q, int i) { | |
return i & ((1<<q->max_size)-1); | |
} | |
item front(queue *q) { | |
return q->items[q_index(q, q->head)]; | |
} | |
item back(queue *q) { | |
return q->items[q_index(q, q->head+q->length-1)]; | |
} | |
void pop(queue *q) { | |
q->head++; | |
q->length--; | |
} | |
void push(queue *q, item v) { | |
q->length++; | |
if (q->length > (1<<q->max_size)) { | |
q->max_size++; | |
q->items = realloc(q->items, sizeof(item)*(1<<q->max_size)); | |
} | |
q->items[q_index(q, q->head+q->length-1)] = v; | |
} | |
/// new_size(size).max_size == 2**size | |
queue new_size(int size) { | |
queue q = { 1<<size, 0, 0, NULL }; | |
q.items = malloc(sizeof(item)*(1<<q.max_size)); | |
return q; | |
} | |
queue new(void) { | |
return new_size(4); | |
} | |
void print_queue(queue *q) { | |
printf("(max_size = %d, head = %d, length = %d){" | |
, q->max_size, q->head, q->length); | |
if (q->length) | |
printf(" %ld", front(q)); | |
for (int i = q->head+1; i < q->head+q->length; i++) | |
printf(", %ld", q->items[q_index(q, i)]); | |
printf(" }\n"); | |
} | |
int main(void) { | |
queue q = new(); | |
print_queue(&q); | |
for (int i = 0; i < 16; ++i) | |
push(&q,i); | |
print_queue(&q); | |
for (int i = 0; i < 8; ++i) | |
pop(&q); | |
print_queue(&q); | |
push(&q,0); | |
push(&q,1); | |
print_queue(&q); | |
} |
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> | |
typedef long item; | |
typedef struct { | |
int max_size; | |
int size; | |
item *items; | |
} stack; | |
item top(stack *s) { | |
return s->items[s->size-1]; | |
} | |
void pop(stack *s) { | |
s->size--; | |
} | |
void push(stack *s, item v) { | |
s->size++; | |
if (s->size > s->max_size) { | |
s->max_size *= 2; | |
s->items = realloc(s->items, sizeof(item)*s->max_size); | |
} | |
s->items[s->size-1] = v; | |
} | |
stack new_size(int size) { | |
stack s = { size, 0, NULL }; | |
s.items = malloc(sizeof(item)*size); | |
return s; | |
} | |
stack new(void) { | |
return new_size(16); | |
} | |
void print_stack(stack *s) { | |
printf("(max_size = %d, size = %d){", s->max_size, s->size); | |
if (s->size >= 1) | |
printf(" %ld", s->items[0]); | |
for (int i = 1; i < s->size; ++i) { | |
printf(", %ld", s->items[i]); | |
} | |
printf(" }\n"); | |
} | |
int main(void) { | |
stack s = new(); | |
print_stack(&s); | |
for (int i = 0; i < 256; ++i) | |
push(&s,i); | |
print_stack(&s); | |
printf("top = %ld\n", top(&s)); | |
for (int i = 0; i < 240; ++i) | |
pop(&s); | |
print_stack(&s); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment