Stack
Stack
STACK
The stack data structure is a linear data structure
that accompanies a principle known as LIFO (Last In
First Out) or FILO (First In Last Out).
Real-life examples of a stack are a deck of cards, piles
of books, piles of money, and many more.
STACK
This example allows you to perform operations from
one end only, like when you insert and remove new
books from the top of the stack. It means insertion and
deletion in the stack data structure can be done only
from the top of the stack. You can access only the top
of the stack at any given point in time.
Inserting a new element in the stack is termed a push
operation.
Removing or deleting elements from the stack is termed
pop operation.
STACK REPRESENTATION
WORKING OF STACK
Now, assume that you have a stack of books.
You can only see the top, i.e., the top-most book,
namely 40, which is kept top of the stack.
If you want to insert a new book first, namely 50,
you must update the top and then insert a new
text.
And if you want to access any other book other
than the topmost book that is 40, you first
remove the topmost book from the stack, and
then the top will point to the next topmost book
WORKING OF STACK
After working on the representation of stacks in
data structures, you will see some basic
operations performed on the stacks in data
structures.
WORKING OF STACK
BASIC OPERATIONS ON STACK:PUSH
Push Operation
Push operation involves inserting new elements in
the stack. Since you have only one end to insert a
unique element on top of the stack, it inserts the new
element at the top of the stack.
BASIC OPERATIONS ON STACK:PUSH
Push operation includes various steps, which are
as follows :
Step 1: First, check whether or not the stack is
full
Step 2: If the stack is complete, then exit
Step 5: Success
BASIC OPERATIONS ON STACK:POP
begin
if top = 0 then stack empty;
item := stack(top);
top = top - 1;
end;
BASIC OPERATIONS ON STACK:PEEK
Peek Operation
Peek operation refers to retrieving the topmost
element in the stack without removing it from the
collections of data elements.
Begin
if top = -1 then stack empty
item = stack[top]
return item
End
BASIC OPERATIONS ON STACK:ISFULL()
isFull()
isFull function is used to check whether or not a
stack is empty.
begin
if
top equals to maxsize
return true
else
return false
else if
end
BASIC OPERATIONS ON STACK:ISEMPTY
isEmpty()
begin
if
top less than 1
return true
else
return false
else if
end
IMPLEMENTATION OF STACK
You can perform the implementation of stacks in
data structures using two data structures that
are an array and a linked list.
1. Array: In array implementation, the stack is
formed using an array. All the operations are
performed using arrays. You will see how all
operations can be implemented on the stack in
data structures using an arraydata structure.
IMPLEMENTATION OF STACK USING ARRAY
IMPLEMENTATION OF STACK USING ARRAY
IMPLEMENTATION OF STACK USING ARRAY
IMPLEMENTATION OF STACK USING ARRAY
IMPLEMENTATION OF STACK USING ARRAY
IMPLEMENTATION OF STACK USING ARRAY
IMPLEMENTATION OF STACK USING ARRAY
IMPLEMENTATION OF STACK USING ARRAY
IMPLEMENTATION OF STACK USING ARRAY
IMPLEMENTATION OF STACK
2. Linked-List: Every new element is inserted as a
top element in the linked list implementation of
stacks in data structures. That means every
newly inserted element is pointed to the top.
Whenever you want to remove an element from
the stack, remove the node indicated by the top,
by moving the top to its previous node in the
list.
IMPLEMENTATION OF STACK USING
LINKED LIST
IMPLEMENTATION OF STACK USING
LINKED LIST
int isEmpty()
{
if(top==NULL)
return 1;
else
return 0;
}
IMPLEMENTATION OF STACK USING
LINKED LIST
Prefix Notation
Postfix Notation
STACK APPLICATION-ARITHMETIC
EXPRESSION EVALUATION
Infix Notation
The infix notation is a convenient way of writing
an expression in which each operator is placed
between the operands. Infix expressions can be
parenthesized or unparenthesized depending
upon the problem requirement.
Example: A + B, (C - D) etc.
Prefix Notation
The prefix notation places the operator before the
operands. This notation was introduced by the
Polish mathematician and hence often referred to
as polish notation.
Example: + A B, -CD etc.
Postfix Notation
The postfix notation places the operator after the
operands. This notation is just the reverse of
Polish notation and also known as Reverse Polish
notation.
Example: AB +, CD+, etc.
STACK APPLICATION-ARITHMETIC
EXPRESSION EVALUATION