#include
#include
typedef struct node
{
void *elem;
struct node *next;
} node_t ,* stack;
typedef struct abc
{
int num;
} abc_t;
stack init()
{
stack top = (stack)malloc(sizeof(node_t));
top->elem = NULL;
top->next = top;
return top;
}
node_t *push(stack top, void *elem)
{
node_t *new_node = (node_t *)malloc(sizeof(node_t));
new_node->elem = elem;
new_node->next = top->next;
top->next = new_node;
return new_node;
}
node_t *pop(stack top)
{
node_t *dest = top->next;
if (top->next == top)
{
return NULL;
}
top->next = top->next->next;
return dest;
}
int length(stack top)
{
int len = 0;
node_t *cur = top;
while ((cur = cur->next) != top)
{
len++;
}
return len;
}
void print(node_t *cur)
{
printf("%d\t", ((abc_t *)(cur->elem))->num);
}
int main()
{
abc_t arr[100] = {0};
int i;
stack top = init();
for (i = 0; i < 100; i++)
{
arr[i].num = i;
push(top, &arr[i]);
}
printf("This stack has %d numbers!\n", length(top));
for (i = 0; i < 20; i++)
{
print(pop(top));
}
return 0;
}
阅读(532) | 评论(0) | 转发(0) |