Answer to Question #256115 in C for Jainab

Question #256115

Explain stacks with the help of an example(Example required).


1
Expert's answer
2021-10-27T00:47:51-0400

Stack is a data structure that follows first in last out principle. The first data entered in the stack will be the last one to be removed.

It consist of three main operations:

  1. Push - adds an element to the stack.
  2. Pop - removes the top element from the stack.
  3. Peek - returns the top element without modifying the stack.

Example

#include <stdio.h>


int MAXSIZE = 8;       
int stack[8];     
int top = -1;            


int isempty() {
   if(top == -1)
      return 1;
   else
      return 0;
}
   
int isfull() {
   if(top == MAXSIZE)
      return 1;
   else
      return 0;
}


int peek() {
   return stack[top];
}


int pop() {
   int d;
   if(!isempty()) {
      d = stack[top];
      top = top - 1;   
      return d;
   } else {
      printf("Stack is empty.\n");
   }
}
int push(int d) {


   if(!isfull()) {
      top = top + 1;   
      stack[top] = d;
   } else {
      printf("Stack is full.\n");
   }
}


int main() {
   push(1);
   push(2);
   push(3);
 
   return 0;
}

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 the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS