0% found this document useful (0 votes)
6 views

Stack

Uploaded by

ananyaprakash005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Stack

Uploaded by

ananyaprakash005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Stack Data Structure

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).
Mainly the following three basic operations are performed in the
stack:
• Push: Adds an item in the stack. If the stack is full, then it is said
to be an Overflow condition.
• Pop: Removes an item from the stack. The items are popped in
the reversed order in which they are pushed. If the stack is empty,
then it is said to be an Underflow condition.

Applications of stack:
• Balancing of symbols
• Infix to Postfix /Prefix conversion
• Redo-undo features at many places like editors, photoshop.
• Forward and backward feature in web browsers
• Used in many algorithms like Tower of Hanoi, tree
traversals, stock span problem, histogram problem.
• Other applications can be Backtracking, Knight tour problem, rat
in a maze, N queen problem and sudoku solver
• In Graph Algorithms like Topological Sorting and Strongly
Connected Components
Implementation:
There are two ways to implement a stack:
• Using array
• Using linked list

Array implementation of Stack


In array implementation, the stack is formed by using the array. All
the operations regarding the stack are performed using arrays. Lets
see how each operation can be implemented on the stack using array
data structure.

Adding an element onto the stack (push operation)

Adding an element into the top of the stack is referred to as push


operation. Push operation involves following two steps.

1. Increment the variable Top so that it can now refere to the next
memory location.
2. Add element at the position of incremented top. This is referred
to as adding new element at the top of the stack.

Stack is overflown when we try to insert an element into a completely


filled stack therefore, our main function must always avoid stack
overflow condition.

Algorithm:

1. begin
2. if top = n then stack full
3. top = top + 1
4. stack (top) : = item;
5. end

Time Complexity : o(1)


Deletion of an element from a stack (Pop operation)

Deletion of an element from the top of the stack is called pop


operation. The value of the variable top will be incremented by 1
whenever an item is deleted from the stack. The top most element of
the stack is stored in an another variable and then the top is
decremented by 1. the operation returns the deleted value that was
stored in another variable as the result.

The underflow condition occurs when we try to delete an element


from an already empty stack.

Algorithm :
1. begin
2. if top = 0 then stack empty;
3. item := stack(top);
4. top = top - 1;
5. end;

Time Complexity : o(1)


next →← prev
Linked list implementation of stack

Instead of using array, we can also use linked list to implement


stack. Linked list allocates the memory dynamically. However,
time complexity in both the scenario is same for all the operations
i.e. push, pop and peek.

In linked list implementation of stack, the nodes are maintained non-


contiguously in the memory. Each node contains a pointer to its
immediate successor node in the stack. Stack is said to be overflown
if the space left in the memory heap is not enough to create a node.
next →← prev
Linked list implementation of stack

Instead of using array, we can also use linked list to implement


stack. Linked list allocates the memory dynamically. However,
time complexity in both the scenario is same for all the operations
i.e. push, pop and peek.

In linked list implementation of stack, the nodes are maintained


non-contiguously in the memory. Each node contains a pointer to
its immediate successor node in the stack. Stack is said to be
overflown if the space left in the memory heap is not enough to
create a node.
The top most node in the stack always contains null in its address
field. Lets discuss the way in which, each operation is performed in
linked list implementation of stack.

Adding a node to the stack (Push operation)

Adding a node to the stack is referred to as push operation. Pushing


an element to a stack in linked list implementation is different from
that of an array implementation. In order to push an element onto
the stack, the following steps are involved.

1. Create a node first and allocate memory to it.


2. If the list is empty then the item is to be pushed as the start
node of the list. This includes assigning value to the data part
of the node and assign null to the address part of the node.
3. If there are some nodes in the list already, then we have to add
the new element in the beginning of the list (to not violate the
property of the stack). For this purpose, assign the address of
the starting element to the address field of the new node and
make the new node, the starting node of the list.

Time Complexity : o(1)

You might also like