Chinaunix首页 | 论坛 | 博客
  • 博客访问: 613324
  • 博文数量: 144
  • 博客积分: 5037
  • 博客等级: 大校
  • 技术积分: 1581
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-30 21:49
文章存档

2010年(16)

2009年(128)

分类: WINDOWS

2009-04-10 20:21:39

#include "stdio.h"
#include "stdlib.h"

typedef struct node
{
    int data;
    int n;
    struct node *link;
}NODE;

NODE *top, *tail;

void push(int x)
{
    NODE *temp = (NODE *) malloc ( sizeof ( NODE ) );    //满栈插入
    temp->data = x;                //将数据插入节点
    //temp->link=NULL;
    temp->link = top;        //调整节点指针
    top = temp;
}

int pop( int *py )
{
    NODE *temp = top;
    if (top == tail)                //判断是否栈空
    {
        printf("stach empty!\n");
        return 0;
    }
    else
    {
        *py = top->data;        //将栈中数据读出
        top = top->link;        //调整栈指针
        free (temp);            //释放空间
        temp = NULL;
        return 1;
    }    
}

int main()
{
    int i, b, y;
    //head=top=(NODE *)malloc(sizeof(NODE));
    tail = top = NULL; //全局变量的初始值为0、空;
  
    push(12); //将11压进栈中
    push(13); //将11压进栈中
    push(14); //将11压进栈中
    pop(&y);
    printf("%d\n", y);
    for( i= 0; i < 4; i++)
    {
        if ( (b = pop(&y) )!= 0 ) //将11从栈中弹出
            printf("%d\n", y);
        else
            break;
    }    
    getchar(); 
    return 0;
}

 

 

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