Blog Moved

This website completely moved to new domain. For latest content, visit www.programmingposts.com

Search This Blog

4 Nov 2012

C PROGRAM TO IMPLEMENT STACK OPERATIONS


/*program for implementing Stack Operations*/
#include<stdio.h>
#include<conio.h>
int stack[10],top=-1; //global declaration
void push();  //declaring functions
void pop();
void display();
void main()
{
  int ch;
  printf(">>> StackOperations <<<\n\n");
  do
  {
      printf("\n 1.Push");
      printf("\n 2.Pop");
      printf("\n 3.Display");
      printf("\n 4.Exit");
      printf("\n Enter your choice: ");
      scanf("%d",&ch);
      if(ch==1)
        push();
      else if(ch==2)
        pop();
      else if(ch==3)
        display();
      else if(ch==4)
       printf("\n\nEnd Of program");
 
  }while(ch!=4);
}
//to push elements in a stack
void push()
{
  if(top>9)
    printf("\n Stack Overflow");
  else
  {
      top=top+1;
      printf("\n Enter a value: ");
      scanf("%d",&stack[top]);
  }
 
}
 
//to pop/delete stack elements
void pop()
{
  if(top==-1)
   printf("\n Stack Overflow. No Elements to pop");
  else
  {
      printf("%d is deleted..",stack[top]);
      top--;  
  }
}
 
//displaying stack elements
void display()
{
    int i;
    if(top==-1)
      printf("\n Stack underflow. No elements to display..");
    else
    {
        printf("\n The stack elements are: ");
        for(i=0;i<=top;i++)
        {
          printf("\t %d",stack[i]);
        }
 
    }
}

Output:


3 comments:

  1. The implimentation of stack using linkedlist is below :)


    Stack using Linked List>

    ReplyDelete
  2. @jayesh : thanks for sharing link

    ReplyDelete
  3. Good
    Here is a link for C/C++ programs and pointer programs. This may be useful for you.

    C/C++ Programs

    ReplyDelete