stack1.cpp

来自「STRUCTURI DE DATE SI ALGORITMI」· C++ 代码 · 共 68 行

CPP
68
字号
#include "Stack1.h"    /*De continuat!*/
#include<stdio.h>

void main(void)
{
  int l, i;
  Stack S;
  initStack(S);
  for(l=0;l<4;l++)
  {
	 printf("\nIntroduceti un element:");
	 scanf("%d",&i);
	 push(S,i);
  }
  printf("\nVarful stivei este : %d",top(S));
}

void initStack(Stack& S)
{
  int i;
  S.sp=0;
  for(i=0;i<100;i++)
    S.vect[i]=0;
}

void push(Stack& S, Atom val)
{
  if(S.sp>=100)
  {
    printf("EROARE!   stiva plina.\n");
  }
  else
  {
    S.sp+=1;
    S.vect[S.sp]=val;
  }
}

Atom pop(Stack& S)
{
  if(isEmpty(S))
  {
    printf("EROARE!   stiva goala.\n");
    return(0);
  }
  else
  {
    S.sp-=1;
    return(S.vect[S.sp+1]);
  }
}

Atom top(Stack& S)
{
  if(isEmpty(S))
  {
    printf("EROARE!   stiva goala.\n");
    return(0);
  }
  else
    return(S.vect[S.sp]);
}

int isEmpty(Stack& S)
{
  return(S.sp==0);
}

⌨️ 快捷键说明

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