Chinaunix首页 | 论坛 | 博客
  • 博客访问: 597588
  • 博文数量: 239
  • 博客积分: 7941
  • 博客等级: 准将
  • 技术积分: 2467
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-10 12:14
个人简介

及时当勉励

文章分类

全部博文(239)

文章存档

2013年(29)

2011年(22)

2010年(188)

分类:

2010-06-04 16:13:39

#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;
}

阅读(506) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~