63 155
Assignments Done
99,3%
Successfully Done
In July 2018

Answer to Question #46264 in C for erico fire

Question #46264
write a program to implement the stacks using the concept of arrays.
Expert's answer
#include <stdio.h>
#define MAX_STACK_SIZE 64
int stack[ MAX_STACK_SIZE ];
int sizeOfStack = 0;
void push( int val );
int pop();
/**
Realise int stack
constanct MAX_STACK_SIZE is max size of stack
*/
int main() {
printf("Hello, It's stack program.\n");
printf("push: 1, 2, 3\n");
push(1);
push(2);
push(3);
printf("Pop 4 elements:\n");
printf("%d\n", pop());
printf("%d\n", pop());
printf("%d\n", pop());
printf("%d\n", pop());
return 0;
}
/**
Push element to the stack.
If stack is not overflow
*/
void push( int val ) {
if ( sizeOfStack < MAX_STACK_SIZE ) {
  stack[sizeOfStack] = val;
  sizeOfStack++;
} else {
  fprintf( stderr, "ERROR: Stack is overflow" );
}
}
/**
Return element from the stack.
If stack is not empty
*/
int pop() {
if ( sizeOfStack > 0 ) {
  return stack[--sizeOfStack];
} else {
  fprintf( stderr, "ERROR: Stack is empty\n" );
}
return -1;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions