0
2.1kviews
What is Stack? Explain the use & operation of Stack & Stack pointer.
1 Answer
0
21views

Stack : -

  1. A stack is one of the most commonly used data structure.

  2. A stack, also called Last In First Out (LIFO) system, is a linear list in which insertion and deletion can take place only at one end, called top.

  3. This structure operates in much the same way as stack of trays.

  4. If we want to remove a tray from stack of trays it can only be removed from the top only.

  5. The insertion and deletion operation in stack terminology are known as PUSH and POP operations.


enter image description here


Following operation can be performed on stack :

  1. Create stack (s) : To create an empty stack s.

  2. PUSH (s, i) : To push an element i into stack s.

  3. POP (s) : To access and remove the top element of the stack s.

  4. Peek (s) : To access the top element of stack s without removing it from the stack s.

  5. Overflow : To check whether the stack is full.

  6. Underflow : To check whether the stack is empty.


Applications of Stacks are : -

  1. Stacks can be used for expression evaluation.

  2. Stacks can be used to check parenthesis matching in an expression.

  3. Stacks can be used for Conversion from one form of expression to another.

  4. Stacks can be used for Memory Management.

  5. Stack data structures are used in backtracking problems.


Stack Pointer:

  1. It is used as a memory pointer.

  2. It points to a memory location in read/write memory, called the stack.

  3. It is always incremented / decremented by 2 during push and pop operation.

  4. A stack pointer is a small register that stores the address of the last program request in a stack.

  5. A stack is a specialized buffer which stores data from the top down. As new requests come in, they "push down" the older ones.


Array implementation of stack in C : -

#include<stdio.h>
#include<conio.h>
#define MAX 50
int stack [MAX + 1], top = 0 ;
void main( )
{
clrscr( );
void create( ), traverse( ), push( ), pop( );
create( );
printf(“\n stack is:\n”);
traverse( );
push( );
printf(“After Push the element in the stack is :\n”);
traverse( );
pop( );
printf(“After Pop the element in the stack is :\n”);
traverse( );
getch( );
}
void create( )
{
char ch;
do
{
top ++;
printf(“Input Element”);
scanf (“%d”, stack[top]);
printf(“Press<Y>for more element\n”);
ch = getch( );
}
while (ch == ‘Y’ )
}
void traverse( )
{
Data Structure 2–5 A (CS/IT-Sem-3)
int i ;
for(i = top; i > 0; – –i)
printf(“%d\n”, stack[i]);
}
void push( )
{
int m;
if(top == MAX)
{
printf(“Stack is overflow”);
return;
}
printf(“Input new element to insert”);
scanf(“%d”, &m) ;
top++;
stack[top] = m;
}
void pop( )
{
if(top == 0)
{
printf(“Stack is underflow\n”);
return;
}
stack[top] = ‘\0’ ;
top – – ;
}
Please log in to add an answer.