C Program For Array Implementation of a stack using Structures and Pointers

/* Program array implementation of a stack using Structures and Pointers. */
#include <stdio.h>
#define MAX 10
struct stack
{
 int arr[MAX] ;
 int top ;
 } ;
void push ( struct stack *s, int item ) /* adds an element to the stack */
{
 if ( s -> top == MAX - 1 )
 {
  printf ( "\nStack is full." ) ;
  return ;
 }
 s -> arr[++s ->top] = item ;
}
int pop ( struct stack *s ) /* removes an element from the stack */
{
 if ( s -> top == -1 )
 {
  printf ( "\nStack is empty." ) ;
  return ;
 }
 return s -> arr[s -> top--]  ;
}
int main( )
{
 struct stack s ;
 s.top=-1;
 int i,j,k;
    for(j=0; j<13; j++)
    {
  push ( &s, j ) ;
    }
    while(s.top!=-1)
    {
  k = pop ( &s ) ;
  printf ( "\nItem popped: %d", k ) ;
    }
    return 0;
}
Bhanu Namikaze

Bhanu Namikaze is an Ethical Hacker, Security Analyst, Blogger, Web Developer and a Mechanical Engineer. He Enjoys writing articles, Blogging, Debugging Errors and Capture the Flags. Enjoy Learning; There is Nothing Like Absolute Defeat - Try and try until you Succeed.

No comments:

Post a Comment