Chinaunix首页 | 论坛 | 博客
  • 博客访问: 510917
  • 博文数量: 81
  • 博客积分: 7010
  • 博客等级: 少将
  • 技术积分: 1500
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-15 10:51
文章分类

全部博文(81)

文章存档

2011年(1)

2009年(22)

2008年(58)

我的朋友

分类: C/C++

2009-06-01 10:38:02

 

#include <stdio.h>

typedef char Element;

typedef struct Node {
    Element data;
    struct Node *next;    
} StackNode;

typedef struct {
    StackNode *top;
} LinkStack;

void initstack(LinkStack *ls)
{
    ls->top = NULL;
}

int isempty(LinkStack *ls)
{
    return ls->top == NULL;
}

void push(LinkStack *ls, Element el)
{
    StackNode *p;
    p = (StackNode *)malloc( sizeof(StackNode) );
    p->data = el;
    p->next = ls->top;
    ls->top = p;
}

Element pop(LinkStack *ls)
{
    StackNode *p = ls->top;
    Element popdata = ls->top->data;
    ls->top = ls->top->next;
    free(p);
    return popdata;
}

void traverse(LinkStack *ls)
{
    int i = 0;
    StackNode *node = ls->top;
    while(node->next != NULL) {
        node = node->next;
        printf("LinkStack Node %d is %c\n", i, node->data);
        i++;        
    }
}

int main()
{
    LinkStack *ls;
    char indata;
    ls = (LinkStack *)malloc( sizeof(LinkStack) );
    printf("Please input data\n");
    while(indata != '\n') {
        scanf("%c", &indata);
        push(ls, indata);
        printf("push %c to stack\n", indata);
    }
    printf("print linkStack:\n");
    traverse(ls);
    printf("pop stack\n");
    pop(ls);
    traverse(ls);
    printf("pop stack\n");
    pop(ls);
    traverse(ls);    
}

阅读(1800) | 评论(0) | 转发(0) |
0

上一篇:sigsetjmp和siglongjmp

下一篇:struct 与 typedef

给主人留下些什么吧!~~