栈的表示与实现及栈的应用

转载 2007年09月21日 17:38:00

<iframe style="WIDTH: 89.81%; HEIGHT: 64px" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-4577827332549849&amp;dt=1192819750500&amp;lmt=1192819750&amp;prev_fmts=468x60_as&amp;format=468x15_0ads_al_s&amp;output=html&amp;correlator=1192819750437&amp;channel=1741427766&amp;pv_ch=1741427766%2B&amp;url=http%3A%2F%2Fyzkzoo.5d6d.com%2Fthread-68-1-1.html&amp;color_bg=FFFFFF&amp;color_text=000000&amp;color_link=0000FF&amp;color_url=008000&amp;color_border=336699&amp;ref=http%3A%2F%2Fyzkzoo.5d6d.com%2Fforum-16-1.html&amp;cc=100&amp;ga_vid=1025434795.1192631677&amp;ga_sid=1192817968&amp;ga_hid=1871659779&amp;ga_fc=true&amp;flash=8&amp;u_h=768&amp;u_w=1024&amp;u_ah=738&amp;u_aw=1024&amp;u_cd=32&amp;u_tz=480&amp;u_his=2&amp;u_java=true"></iframe>

<iframe style="WIDTH: 44.33%; HEIGHT: 259px" src="http://pagead2.googlesyndication.com/cpa/ads?client=ca-pub-4577827332549849&amp;cpa_choice=CAEaCB94-nvUZWENUB9QugJQtwRQTVAgULcCUB4&amp;oe=gb2312&amp;dt=1192720966468&amp;lmt=1192720966&amp;format=250x250_as&amp;output=html&amp;correlator=1192720966453&amp;channel=2735220158&amp;url=http%3A%2F%2Fyzkzoo.5d6d.com%2Fthread-67-1-1.html&amp;color_bg=FFFFFF&amp;color_text=000000&amp;color_link=0000FF&amp;color_url=008000&amp;color_border=336699&amp;ad_type=text_image&amp;region=_google_cpa_region_&amp;ref=http%3A%2F%2Fyzkzoo.5d6d.com%2Fforum-16-1.html&amp;cc=100&amp;ga_vid=1025434795.1192631677&amp;ga_sid=1192720940&amp;ga_hid=1155529757&amp;ga_fc=true&amp;flash=8&amp;u_h=768&amp;u_w=1024&amp;u_ah=738&amp;u_aw=1024&amp;u_cd=32&amp;u_tz=480&amp;u_his=2&amp;u_java=true"></iframe>实现栈的顺序存储。

栈实现示例
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OK 1
#define EQUAL 1
#define OVERFLOW -1
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef int Status ;
struct STU{
  char name[20];
  char stuno[10];
  int age;
  int score;
};
typedef struct STU SElemType;
struct STACK
{
  SElemType *base;
  SElemType *top;
  int stacksize;
};
typedef struct STACK SqStack;
typedef struct STACK *pSqstack;
Status InitStack(SqStack  **S);
Status DestroyStack(SqStack *S);
Status ClearStack(SqStack *S);
Status StackEmpty(SqStack S);
int StackLength(SqStack S);
Status GetTop(SqStack S,SElemType *e);
Status Push(SqStack *S,SElemType e);
Status Pop(SqStack *S,SElemType *e);
Status StackTraverse(SqStack S,Status (*visit)());
Status InitStack(SqStack **S)
{
  (*S)=(SqStack *) malloc(sizeof(SqStack));
  (*S)->base=(SElemType *)malloc(STACK_INIT_SIZE *sizeof(SElemType));
  if(!(*S)->base)exit(OVERFLOW);
  (*S)->top=(*S)->base;
  (*S)->stacksize=STACK_INIT_SIZE;
  return OK;
}
Status DestroyStack(SqStack *S)
{
free(S->base);
free(S);
}
Status ClearStack(SqStack *S)
{
  S->top=S->base;
}
Status StackEmpty(SqStack S)
{
  if(S.top==S.base) return TRUE;
  else
    return FALSE;
}
int StackLength(SqStack S)
{
  int i;
  SElemType *p;
  i=0;
  p=S.top;
  while(p!=S.base)
    {p++;
     i++;
    }
}
Status GetTop(SqStack S,SElemType *e)
{
  if(S.top==S.base) return ERROR;
  *e=*(S.top-1);
  return OK;
}
Status Push(SqStack *S,SElemType e)
{
/*
  if(S->top - S->base>=S->stacksize)
   {
     S->base=(SElemType *) realloc(S->base,
            (S->stacksize + STACKINCREMENT) * sizeof(SElemType));
     if(!S->base)exit(OVERFLOW);
     S->top=S->base+S->stacksize;
     S->stacksize += STACKINCREMENT;
   }
  */
  *(S->top++)=e;
  return OK;
}
Status Pop(SqStack *S,SElemType *e)
{
  if(S->top==S->base) return ERROR;
  *e=*--S->top;
  return OK;
}
Status StackPrintElem(SElemType * e)
{
  printf("%s  %s  %d  %d/n",e->name,e->stuno,e->age,e->score);
}
Status StackTraverse(SqStack S,Status (*visit)())
{
  while(S.top!=S.base)
     visit(--S.top);
}
main()
{
  SElemType e;
  SqStack *Sa;
  clrscr();
  printf("/n/n-------------------SqStack Demo is running...----------------/n/n");
  printf("First is Push function./n");
  InitStack(&Sa);
  strcpy(e.name,"stu1");
  strcpy(e.stuno,"100001");
  e.age=80;
  e.score=1000;
  printf("   Now Stack is Empty./n");
  StackTraverse(*Sa,StackPrintElem);
  Push(Sa,e);
  printf("   Now Stack has one element./n");
  StackTraverse(*Sa,StackPrintElem);
  strcpy(e.name,"stu3");
  strcpy(e.stuno,"100002");
  e.age=80;
  e.score=1000;
  Push(Sa,e);
  printf("   Now Stack has another element./n");
  StackTraverse(*Sa,StackPrintElem);
  printf("   Now Pop Stack,the top elem put into variable e./n");
  Pop(Sa,&e);
  printf("%s/n%s/n%d/n%d/n",e.name,e.stuno,e.age,e.score);
  printf("   Let's see the left of Stack's elem:/n");
  StackTraverse(*Sa,StackPrintElem);
  getch();
  printf("/n/n/nWelcom to visit http://zmofun.topcool.net/n/n");
}

二、栈的应用
1、利用栈实现数制转换 2、利用栈实现单行编辑
以上任选一题。
数制转换示例
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
typedef int SElemType;
#include "stack.h"
Status visit(SElemType * e)
{
  printf(" %d  ", *e);
}
void conversion()
{
pSqStack S;
SElemType e;
int n;
InitStack(&S);
printf("Input a number to convert to OCT:/n");
scanf("%d",&n);
if(n<0)
   {
     printf("/nThe number must be over 0.");
     return;
   }
if(!n) Push(S,0);
while(n){
   Push(S,n%8);
   n=n/8;
}
printf("the result is:        ");
while(!StackEmpty(*S)){
   Pop(S,&e);
   printf("%d",e);
}
}
main()
{
  printf("/n/n/n/n");
  conversion();
  getch();
  printf("/n/nWelcome to visit http://zmofun.yeah.net !");
}
单行编辑示例
#include<string.h>
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
#define EOFILE '`'
typedef char SElemType;
#include "stack.h"
Status visit(SElemType * e)
{
  printf("%c", *e);
}
char OP[10]={'+','-','*','/','(',')','#'};
int precede[7][7]={
      1,1,2,2,2,1,1,
      1,1,2,2,2,1,1,
      1,1,1,1,2,1,1,
      1,1,1,1,2,1,1,
      2,2,2,2,2,3,0,
      1,1,1,1,0,1,1,
      2,2,2,2,2,0,3};
int In(char c,char *op)
{
  int i=0;
  while(i<7)
     if(c==op[i++])
       return 1;
  return 0;
}
char Precede(char op,char c)
{
  int pos_op;
  int pos_c;
  int i;
  for(i=0;i<7;i++)
    {
      if(op==OP) pos_op=i;
      if(c==OP) pos_c=i;
    }
  switch(precede[pos_op][pos_c])
    {
      case 1:  return '>';
      case 2:  return '<';
      case 3:  return '=';
    }
}
char Operate(int a,char theta,int b)
{
  switch(theta)
    {
      case '+':return a+b-'0';
      case '-':return a-b+'0';
      case '*':return (a-'0')*(b-'0')+'0';
      case '/':return (a-'0')/(b-'0')+'0';
    }
}
char EvaluateExpression()
{
  SqStack *OPND,*OPTR;
  char c,x,theta;
  char a,b;
  InitStack(&OPTR);  Push(OPTR,'#');
  InitStack(&OPND);
  c=getchar();
  while(c!='#'||GetTop(*OPTR)!='#')
    {
      if(!In(c,OP))
        {Push(OPND,c);c=getchar();}
      else
        switch(Precede(GetTop(*OPTR),c))
          {
            case '<':
              Push(OPTR,c);
              c=getchar();
              break;
            case '=':
              Pop(OPTR,&x);
              c=getchar();
              break;
            case '>':
              Pop(OPTR,&theta);
              Pop(OPND,&b);  Pop(OPND,&a);
              Push(OPND,Operate(a,theta,b));
              break;
          }
    }
  c=GetTop(*OPND);
  DestroyStack(OPTR);
  DestroyStack(OPND);
  return c;
}

main()
{
  char i;
  printf("/n/n/n/nOnly within 0..9 evaluation,input a expression end with symbol #:/n");
  i=EvaluateExpression();
  printf("/nThis expression's result is:   ");
  printf("%d/n/n/n/n",i-'0');
  printf("/n/nWelcome to visit http://zmofun.yeah.net !");
}

这里是实现栈的头文件
#include "simuc.h"

#define OVERFLOW -1
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef int Status;
struct STACK
{
  SElemType *base;
  SElemType *top;
  int stacksize;
};
typedef struct STACK SqStack;
typedef struct STACK *pSqStack;
Status InitStack(SqStack  **S);
Status DestroyStack(SqStack *S);
Status ClearStack(SqStack *S);
Status StackEmpty(SqStack S);
int StackLength(SqStack S);
SElemType GetTop(SqStack S);
Status Push(SqStack *S,SElemType e);
Status Pop(SqStack *S,SElemType *e);
Status StackTraverse(SqStack S,Status (*visit)());
Status InitStack(SqStack **S)
{
  (*S)=(SqStack *)malloc(sizeof(SqStack));
  (*S)->base=(SElemType *)malloc(STACK_INIT_SIZE *sizeof(SElemType));
  if(!(*S)->base)exit(OVERFLOW);
  (*S)->top=(*S)->base;
  (*S)->stacksize=STACK_INIT_SIZE;
  return OK;
}
Status DestroyStack(SqStack *S)
{
free(S->base);
free(S);
}
Status ClearStack(SqStack *S)
{
  S->top=S->base;
}
Status StackEmpty(SqStack S)
{
  if(S.top==S.base) return TRUE;
  else
    return FALSE;
}
int StackLength(SqStack S)
{
  int i;
  SElemType *p;
  i=0;
  p=S.top;
  while(p!=S.base)
    {p++;
     i++;
    }
}
SElemType GetTop(SqStack S)
{
  if(S.top==S.base) return ERROR;
  return *(S.top-1);
}
Status Push(SqStack *S,SElemType e)
{
/*
  if(S->top - S->base>=S->stacksize)
   {
     S->base=(SElemType *) realloc(S->base,
            (S->stacksize + STACKINCREMENT) * sizeof(SElemType));
     if(!S->base)exit(OVERFLOW);
     S->top=S->base+S->stacksize;
     S->stacksize += STACKINCREMENT;
   }
  */
  *(S->top++)=e;
  return OK;
}
Status Pop(SqStack *S,SElemType *e)
{
  if(S->top==S->base) return ERROR;
  *e=*(--(S->top));
  return OK;
}

Status StackTraverse(SqStack S,Status (*visit)())
{
  while(S.top>S.base)
     visit(--S.top);
}

<iframe style="WIDTH: 52.39%; HEIGHT: 263px" src="http://pagead2.googlesyndication.com/cpa/ads?client=ca-pub-4577827332549849&amp;cpa_choice=CAEaCKcC4yuPlq5lUDRQDVAtUK4BUENQCA&amp;oe=gb2312&amp;dt=1192819388296&amp;lmt=1192819388&amp;format=300x250_as&amp;output=html&amp;correlator=1192819388281&amp;channel=2735220158&amp;url=http%3A%2F%2Fyzkzoo.5d6d.com%2Fviewthread.php%3Ftid%3D70%26page%3D1%26extra%3Dpage%253D1&amp;color_bg=FFFFFF&amp;color_text=000000&amp;color_link=0000FF&amp;color_url=008000&amp;color_border=336699&amp;ad_type=text_image&amp;region=_google_cpa_region_&amp;ref=http%3A%2F%2Fyzkzoo.5d6d.com%2Fpost.php%3Faction%3Dedit%26fid%3D16%26tid%3D70%26pid%3D70%26page%3D1%26extra%3Dpage%253D1&amp;cc=100&amp;ga_vid=1025434795.1192631677&amp;ga_sid=1192817968&amp;ga_hid=1527165455&amp;ga_fc=true&amp;flash=8&amp;u_h=768&amp;u_w=1024&amp;u_ah=738&amp;u_aw=1024&amp;u_cd=32&amp;u_tz=480&amp;u_his=5&amp;u_java=true"></iframe>

<iframe style="WIDTH: 82.23%; HEIGHT: 74px" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-4577827332549849&amp;dt=1192818497343&amp;lmt=1192818497&amp;format=468x60_as&amp;output=html&amp;correlator=1192818497343&amp;channel=1741427766&amp;url=http%3A%2F%2Fyzkzoo.5d6d.com%2Fthread-68-1-1.html&amp;color_bg=FFFFFF&amp;color_text=000000&amp;color_link=0000FF&amp;color_url=008000&amp;color_border=336699&amp;ad_type=text_image&amp;ref=http%3A%2F%2Fyzkzoo.5d6d.com%2Fforumdisplay.php%3Ffid%3D16%26page%3D1&amp;ui=rc%3A6&amp;cc=100&amp;ga_vid=1025434795.1192631677&amp;ga_sid=1192817968&amp;ga_hid=1000484876&amp;ga_fc=true&amp;flash=8&amp;u_h=768&amp;u_w=1024&amp;u_ah=738&amp;u_aw=1024&amp;u_cd=32&amp;u_tz=480&amp;u_his=6&amp;u_java=true"></iframe>

<iframe style="WIDTH: 58.41%; HEIGHT: 156px" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-4577827332549849&amp;dt=1192819750656&amp;lmt=1192819750&amp;prev_fmts=468x60_as%2C468x15_0ads_al_s%2C234x60_as&amp;format=200x90_0ads_al_s&amp;output=html&amp;correlator=1192819750437&amp;channel=1741427766&amp;pv_ch=1741427766%2B&amp;url=http%3A%2F%2Fyzkzoo.5d6d.com%2Fthread-68-1-1.html&amp;color_bg=FFFFFF&amp;color_text=000000&amp;color_link=0000FF&amp;color_url=008000&amp;color_border=336699&amp;ref=http%3A%2F%2Fyzkzoo.5d6d.com%2Fforum-16-1.html&amp;cc=100&amp;ga_vid=1025434795.1192631677&amp;ga_sid=1192817968&amp;ga_hid=1871659779&amp;ga_fc=true&amp;flash=8&amp;u_h=768&amp;u_w=1024&amp;u_ah=738&amp;u_aw=1024&amp;u_cd=32&amp;u_tz=480&amp;u_his=2&amp;u_java=true"></iframe> 

栈的应用二--四则运算表达式求值(逆波兰表示:后缀表达式)

栈的应用一–斐波那契(Fibonacci)数列的实现(代码)中缀表达式,我们把平时所用的标准的四则运算表达式“9+()3-1)*3+10/2”叫做中缀表达式。 后缀表达式,所有的符号都是在要运算的数...

java 栈的实现和应用

  • 2017年07月05日 13:58
  • 366KB
  • 下载

zigbee2006协议栈的实现和应用

  • 2014年03月03日 10:14
  • 5.97MB
  • 下载

实验4:栈和队列的基本操作实现及其应用之《循环队列》

循环队列

栈的应用,实现,与学习

  • 2011年07月27日 01:20
  • 2KB
  • 下载

栈的应用--计算器实现四则运算

学习一个知识点并不是那么难,而要运用所学的知识点去实现一个功能或者做一个项目却总不是那么简单。就像这个标题,如何用栈来做出一个计算器呢? 我们先来思考一番,比如有个式子:9 + (3 - 1...

栈的应用:C实现简单计算器

  • 2013年08月02日 10:37
  • 6KB
  • 下载

实验3:栈和队列的基本操作实现及其应用——顺序队列和链队列

实验4:栈和队列的基本操作实现及其应用 一、实验目的 1、   熟练掌栈和队列的结构特点,掌握栈和队列的顺序存储和链式存储结构和实现。 2、      学会使用栈和队列解决实际问题。 二、实验内容 1...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:栈的表示与实现及栈的应用
举报原因:
原因补充:

(最多只允许输入30个字)