[ create a new paste ] login | about

Link: http://codepad.org/1jgRw59V    [ raw code | output | fork ]

C++, pasted on Oct 28:
#include "stdio.h"
#include "stdlib.h"

#define SIZE_MAX 100

typedef int TStackKey;

struct TStack{
      int depth;
      TStackKey keys[SIZE_MAX];
    };

void createstack(TStack *s);          // creates a stack empty s
bool emptystack(TStack s);            // returns T if s stack is empty or F, otherwise
void push(TStackKey k, TStack *s);    // stacking on the stack key k s
TStackKey pop(TStack *s);             // returns the top element of the stack s, removing it from this
TStackKey top(TStack s);              // Returns the top element from the stack S
int depth(TStack s);                  // returns the size of the stack s
void printstack(TStack *s, char *t);  // prints a stack of text preceded s t

void createstack(TStack *s){
  s->depth = 0;
}

bool emptystack(TStack s){
  return s.depth == 0;
}

void push(TStackKey k, TStack *s){
  if ((*s).depth < SIZE_MAX){
    (*s).keys[(*s).depth] = k;
    (*s).depth++;
  }
}

TStackKey pop(TStack *s){
  if (!emptystack(*s)){
    TStackKey k;

    k = (*s).keys[(*s).depth - 1];
    (*s).depth--;

    return k;
  }
}

TStackKey top(TStack s){
  if (!emptystack(s))
    return s.keys[s.depth - 1];
}

int depth(TStack s){
  return s.depth;
}

void printstack(TStack *s, char *t){
  TStack tmp;
  TStackKey k;

  printf("%s ", t);

  if (emptystack(*s))
    printf("Vazia!\n");
  else{
    createstack(&tmp);

    while (!emptystack(*s)){
      k = pop(s);
      printf("%d ", k);
      push(k, &tmp);
    }

    printf("\n");

    while (!emptystack(tmp))
      push(pop(&tmp), s);
  }
}


Output:
1
2
3
4
5
cc1plus: warnings being treated as errors
In function 'TStackKey top(TStack)':
Line 50: warning: control reaches end of non-void function
In function 'TStackKey pop(TStack*)':
Line 45: warning: control reaches end of non-void function


Create a new paste based on this one


Comments: