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, Web Developer, Student and Mechanical Engineer. He Enjoys writing articles, Blogging, Solving Errors and Social Networking. Feel Free to let me know any of your concerns about hacking or let me know if you need any more methods on hacking anything. Enjoy Learning

No comments:

Post a Comment