⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stringstack.c

📁 用于计算一个很长的式子
💻 C
字号:
/************************************************/
/*Name:stringstack                              */
/*This file is a example of operate stack.      */
/*Author:李士伟                                 */
/*Writed 2008.1.7                               */
/************************************************/

/*************************** main.h ************************************/

#include<stdio.h>
#include<stdlib.h>
#define EMPTY 0
#define FULL 100
typedef char data;
typedef enum{false,true} boolean;

/************************* end main.h **********************************/

/***************************strstk.h************************************/

struct elem
{
    data d;
    struct elem *next;
};
typedef struct elem elem;
struct stack
{
    int count;/*记录元素个数*/
    elem *top;
};
typedef struct stack stack;

/*********************** end strstk.h *************************************/

/****************************** strstk.c **********************************/
void StackPush(data d, stack *stk);
data StackPop(stack *stk);
void StackInit(stack *stk);
boolean StackEmpty(stack *stk);
boolean StackFull(stack *stk);

void StackPush(data d,stack *stk)
{
    elem *pelem=(elem *)malloc(sizeof(elem));

    pelem->d=d;
    pelem->next=stk->top;
    stk->top=pelem;
    stk->count++;
}

data StackPop(stack *stk)
{
    /*get stack top elem*/
    data d;
    elem *pelem;
    d=stk->top->d;      /* get top elem's data*/
    pelem=stk->top;
    stk->top=pelem->next;    /* top point the next elem*/
    stk->count--;   /* stack's elem decrease 1 */
    free(pelem);  /* realse elemptr memory */

    return(d);
}

void StackInit(stack *stk)
{
    /* initialize stack,
       set stack empty */
    stk->count=EMPTY;
    stk->top=NULL;
}

boolean StackEmpty(stack *stk)
{
    /* test stack is empty*/
    return((stk->count==EMPTY) ? true : false );
}

boolean StackFull(stack *stk)
{
    /* test stack is full */
    return((stk->count==FULL) ? true : false );
}

/******************************** end strstk.c ****************************/

/***********************************  main.c ******************************/
void main()
{
    char    str[]={" This is a example of operete stack.\nYou can get str from where!"};
    int i;
    stack stk;
    StackInit(&stk);/*initialize stack*/
    for(i=0;str[i]!='\0';i++)
        if(StackFull(&stk)==false) StackPush(str[i],&stk);
    printf("stack:");
    while(StackEmpty(&stk)==false)
        putchar(StackPop(&stk));

    getch();
}
/*********************************end main.c ********************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -