Stack Operations Using Array

C Program to Implement Stack Operations Using Array.



C Program to Implement Stack Operations Using Array Images-Pics-Pictures-Photos-Basic C Programs-C Language Programs-C Program to Perform Stack Operations-Stack Operations using Array-General C Programs-Stack Program-Linked List Programs-Push Pop Operations.

⦁ Stack is a liner data structure in which the operations are performed based on LIFO principle.

⦁ In a Stack, adding and removing of elements are performed at single position which is known as "top".

⦁ The insertion operation is performed using a function called push and deletion operation is performed using a function called pop.

⦁ An array is a variable which can store multiple values of same data type at a time.


C Program to Implement Stack Operations Using Array Images-Pics-Pictures-Photos-Basic C Programs-C Language Programs-C Program to Perform Stack Operations-Stack Operations using Array-General C Programs-Stack Program-Linked List Programs-Push Pop Operations.




--------------------------------------------------------------------------------------------------------------------------


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define size 5
struct stack
{
    int s[size];
    int top;
} st;
int stfull( )
{
   if(st.top>=size-1)
   return 1;
   else
   return 0;
}
void push(int item)
{
    st.top++;
    st.s[st.top] = item;
 }
int stempty( )
{

   if(st.top == -1)
   return 1;
   else
   return 0;
}
int pop( )
{
    int item;
    item = st.s[st.top];
    st.top--;
    return(item);
}
void display( )
{
    int i;
    if(stempty( ))
    printf("\n Stack is Empty!");
    else
    {
         for(i=st.top;i>=0;i--)
         printf("\n %d", st.s[i]);
    }

}

int main( )
{
    int item,choice;
    char ans;
    st.top = -1;
    printf("\n\t Implementation of Stack");
    do
    {
         printf("\n Main Menu");
         printf("\n1.Push \n2.Pop\n3. Display\n4.exit");
         printf("\n Enter your choice");
         scanf("%d", &choice);
         switch(choice)
         {
                case 1:
                printf("\n Enter the item to be pushed");
                scanf("%d", &item);
                if(stfull( ))
                printf("\n Stack is Full!");
                else
                push(item);
                break;
                case 2:
                if(stempty( ))
                printf("\n Empty stack! Underflow!!");
                else
                {
                     item = pop( );
                     printf("\n The popped element is %d",item);
                }
               break;
               case 3:
               display( );
               break;
               case 4:
               exit(0);
    }
    printf("\n Do You want to Continue?");
    ans = getche( );
}
while(ans =='Y' || ans == 'Y');
return 0;
}

--------------------------------------------------------------------------------------------------------------------------

Explanation

--------------------------------------------------------------------------------------------------------------------------


⦁ We have created stack structure and we have array of elements having size size.

⦁ We have declared top as structure member to keep track of Topmost element.

⦁ We are incrementing the top and then adding element.
                   
  #define size 5
                      struct stack
                     {
                         int s[size];
                         int top;
                     }st;

⦁ While removing or poping element, we are firstly removing the element and then decrementing the top.
     
void push(int item)
      {
          st.top++;
          st.s[st.top] = item;
      }




EmoticonEmoticon