STACK
NAME – RUPAM TARAFDAR
WHAT IS A STACK?
 It is named stack as it behaves live a real-world like stack
 Stack is a linear data structure which follows a particular orderwhich the
operations are performed. The order may be LIFO(Last In First Out) .
 Stack is a sequence of items that can be accessed at only one end of the
sequence known as top.
REAL LIFE EXAMPLES OF STACK
WHAT IS A STACK?
OPERATIONS ON STACK
CREATION
INSERTION
DELETION
DISPLAYING
CREATION
#define MAX 5
int stack[MAX];
int Top = -1;
INSERTION
 Before inserting an element in a stack, we check whether the stack is full.
 If we try to insert the element in a stack, and the stack is full, then the overflow condition occurs.
 When we initialize a stack, we set the value of top as -1 to check that the stack is empty.
 When the new element is pushed in a stack, first, the value of the top gets incremented, i.e., top=top+1,
and the element will be placed at the new position of the top.
 The elements will be inserted until we reach the max size of the stack.
STACK EXAMPLES : PUSH
Insertion operation in stack called "Push"
void Push()
{
int x;
if(Top==Size-1)
{
printf("nOverflow!!");
}
else
{
printf("nEnter element to be inserted to the stack:");
scanf("%d",&x);
Top=Top+1;
inp_array[Top]=x;
}
}
STACK : PUSH OPERATION EXAMPLE
DELETION
 Before deleting the element from the stack, we check whether the stack is empty.
 If we try to delete the element from the empty stack, then the underflow condition occurs.
 If the stack is not empty, we first access the element which is pointed by the top
 Once the pop operation is performed, the top is decremented by 1, i.e., top=top-1.
STACK EXAMPLES : POP
Deletion operation is called as "Pop"
void Pop()
{
if(Top==-1)
{
printf("nUnderflow!!");
}
else
{
printf("nPopped element: %d",inp_array[Top]);
Top=Top-1;
}
}
STACK : POP OPERATION EXAMPLE
STACK EXAMPLES : DISPLAY
void display()
{
if(Top==-1)
{
printf("nUnderflow!!");
}
else
{
printf("nElements present in the stack: n");
for(int i=Top;i>=0;--i)
printf("%dn",inp_array[i]);
}
}
STACK.pptx

STACK.pptx

  • 1.
  • 2.
    WHAT IS ASTACK?  It is named stack as it behaves live a real-world like stack  Stack is a linear data structure which follows a particular orderwhich the operations are performed. The order may be LIFO(Last In First Out) .  Stack is a sequence of items that can be accessed at only one end of the sequence known as top.
  • 3.
  • 4.
    WHAT IS ASTACK?
  • 5.
  • 6.
    CREATION #define MAX 5 intstack[MAX]; int Top = -1;
  • 7.
    INSERTION  Before insertingan element in a stack, we check whether the stack is full.  If we try to insert the element in a stack, and the stack is full, then the overflow condition occurs.  When we initialize a stack, we set the value of top as -1 to check that the stack is empty.  When the new element is pushed in a stack, first, the value of the top gets incremented, i.e., top=top+1, and the element will be placed at the new position of the top.  The elements will be inserted until we reach the max size of the stack.
  • 8.
    STACK EXAMPLES :PUSH Insertion operation in stack called "Push" void Push() { int x; if(Top==Size-1) { printf("nOverflow!!"); } else { printf("nEnter element to be inserted to the stack:"); scanf("%d",&x); Top=Top+1; inp_array[Top]=x; } }
  • 9.
    STACK : PUSHOPERATION EXAMPLE
  • 10.
    DELETION  Before deletingthe element from the stack, we check whether the stack is empty.  If we try to delete the element from the empty stack, then the underflow condition occurs.  If the stack is not empty, we first access the element which is pointed by the top  Once the pop operation is performed, the top is decremented by 1, i.e., top=top-1.
  • 11.
    STACK EXAMPLES :POP Deletion operation is called as "Pop" void Pop() { if(Top==-1) { printf("nUnderflow!!"); } else { printf("nPopped element: %d",inp_array[Top]); Top=Top-1; } }
  • 12.
    STACK : POPOPERATION EXAMPLE
  • 13.
    STACK EXAMPLES :DISPLAY void display() { if(Top==-1) { printf("nUnderflow!!"); } else { printf("nElements present in the stack: n"); for(int i=Top;i>=0;--i) printf("%dn",inp_array[i]); } }