You are on page 1of 19

DATA STRUCTURES

STACK
Ghazanfar Latif
Stack
A list for which Insert and Delete are allowed
only at one end of the list (the top)
– LIFO – Last in, First out

Pop Pop
Push
What is this good for ?
• Page-visited history in a Web browser
• Undo sequence in a text editor
• Saving local variables when one function
calls another, and this one calls another
Applications of Stack

• The simplest application of a stack is to


reverse a word. You push a given word to stack
- letter by letter - and then pop letters from
the stack.
• There are other uses also like:
• Parsing
• Expression Conversion(Infix to Postfix, Postfix
to Prefix etc)
Basic features of Stack

• Stack is an ordered list of similar data type.


• Stack is a LIFO(Last in First out) structure or we
can say FILO(First in Last out).
• push() function is used to insert new elements
into the Stack and pop() function is used to
remove an element from the stack. Both insertion
and removal are allowed at only one end of Stack
called Top.
• Stack is said to be in Overflow state when it is
completely full and is said to be
in Underflow state if it is completely empty.
Stack ADT
Objects:
A finite sequence of nodes
Operations:
• Create
• Push: Insert element at top
• Top: Return top element
• Pop: Remove and return top element
• IsEmpty: test for emptyness
Abstract Data Type: Stack

• A finite number of objects


– Not necessarily distinct
– Having the same data type
– Ordered by when they were added
• Operations
– isEmpty()
– push(newEntry)
– pop()
– peek()
Data Structures and Problem Solving with
C++: Walls and Mirrors, Carrano and Henry,
© 2013
Implementation of Stack Data Structure

• Stack can be easily implemented using an


Array or a Linked List. Arrays are quick, but are
limited in size and Linked List requires
overhead to allocate, link, unlink, and
deallocate, but is not limited in size. Here we
will implement Stack using array.
Push (ItemType newItem)
• Function: Adds newItem to the top of the
stack.
• Preconditions: Stack has been initialized
and is not full.
• Postconditions: newItem is at the top of
the stack.
Pop (ItemType& item)
• Function: Removes topItem from stack and
returns it in item.
• Preconditions: Stack has been initialized and is
not empty.
• Postconditions: Top element has been
removed from stack and item is a copy of
the removed element.
Stack overflow
• The condition resulting from trying to push
an element onto a full stack.
if(!stack.IsFull())
stack.Push(item);
Stack underflow
• The condition resulting from trying to pop
an empty stack.
if(!stack.IsEmpty())
stack.Pop(item);
Algorithm for PUSH operation

• Check if the stack is full or not.


• If the stack is full, then print error of overflow
and exit the program.
• If the stack is not full, then increment the top
and add the element.
Algorithm for POP operation

• Check if the stack is empty or not.


• If the stack is empty, then print error of
underflow and exit the program.
• If the stack is not empty, then print the
element at the top and decrement the top.
Stack Implementation (cont.)
StackType::StackType()
{
top = -1;
}
void StackType::MakeEmpty()
{
top = -1;
}
bool StackType::IsEmpty() const
{
return (top == -1);
}
Stack Implementation (cont.)
bool StackType::IsFull() const
{
return (top == MAX_ITEMS-1);
}

void StackType::Push(ItemType newItem)


{
top++;
items[top] = newItem;
}

void StackType::Pop(ItemType& item)


{
item = items[top];
top--;
}
Thank you

You might also like