公 告

欢迎各位网友添加友情链接,在您添加本博客:http://linux0818.blogspot.com/ 做为链接后, E-mail:linux0818@gmail.com给我,我将将您的网址添加到本博客。

2008年10月14日星期二

堆栈使用实例

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define Maxsize 100
#define true 1
#define false 0
int main(int argc, char **argv)
{
    int i, num;
    typedef int elemtype;
    typedef struct {
        elemtype stack[Maxsize];
        int top;
    } sqstack;
    sqstack *s;
/**********************************************/
  /*initial stack */
    s=(sqstack *)malloc(sizeof(sqstack));//给结构体变量分配空间
    s->top = -1;
    printf("Initial Stack Sucess!\n");
    printf("\tStack top = %d\n", s->top);
/**********************************************/
    /*Push Stack */
    printf("Start Push Stack......\n");
    if (s->top > Maxsize - 1) {
        printf("\tPush Stack Fail!");
    } else {
        for (i = 0; i < 20; i++) {
            s->top++;
            s->stack[s->top] = i;
            printf("\tStack[%d] = %d\n", s->top, s->stack[i]);
        }
    }
    printf("Push End!\n");
/************************************************/
    /*Pop Stack */
    printf("Start Pop Stack......\n");
    if (s->top < 0) {
        printf("\tPop Stack Fail!");
    } else {
        //s->top--;
        printf("\tStack[%d] = %d\n", s->top, s->stack[s->top]);
    }
/**************************************************/
    /*Null Stack */
    printf("Testing Stack Null......\n");
    if (s->top < 0) {
        printf("The Stack is Null!\n");
    } else {
        printf("The Stack is not Null!\n");
    }
/**************************************************/
    /*Get Stack Top */
    printf("Start Get Stack Top Count......\n");
    if (s->top < 0) {
        printf("Get Stack Top Count Fail!\n");
    } else {
        num = s->stack[s->top];
        printf("The Stack Top Count is: %d\n", num);
    }
    return 0;
}


--
/**************************************/
Name: Xiong Feng
E-mail:linux0818@gmail.com
MSN:linux0818@hotmail.com
QQ:23562033
Address: GuangZhou.China
/**************************************/

没有评论:

发表评论