Stack in Data Structures
Stack in Data Structures
STRUCTURES
STACK :DEFINITION
Stack is a linear data structure which follows a particular order in
which the operations are performed. The order may be LIFO(Last
In First Out) or FILO(First In Last Out).
WHY STACK?
Stack in PL:
• The return address (when the function is complete it returns back to
the function call)
• Arguments passed to the function
• Local variables of the function
Hardware Implementation of stack:
consists of reserved contiguous region of memory with a stack pointer into
that memory.
The stack has a fixed location in memory at which it begins.
The stack pointer is a hardware register that points to the current extents of
the
stack.
There are stacks that grow downwards and ones that grow upwards.
The stack can technically be located anywhere in memory, depending on the
system.
Implementation of stack
The stack implementation can be done using
(i)Array :Array is a static data structure so the collection of data must be
fixed in size.
The only problem with this implementation is array size must be
specified initially.
struct stack
{
int
stk[MAXSIZE];
int top;
};
• Implementation using Linked List :Linked List is a dynamic data
structure. So collection of data can be used which are variable in size
and structure. The program executes can grow and shrink to
accommodate the data being stored.
struct Node {
int data;
struct Node* link;
};
struct Node* top;
Drawback of Linked List implementation
• All the operations take constant time
• Calls to malloc and free are expensive especially in comparison to the
pointer manipulation routines.