To implement the STACK using Linked List, the following PUSH and POP algorithms may be used:
Algorithm for PUSH operation in Linked List implemented STACK:
PUSHLL(TOP, ITEM)
[TOP is address of top node of STACK and ITEM is the item to PUSH.[In case of Linked List no overflow, but may be memory allocation error]
If AVAIL=NULL then:
Write: ’Memory Allocation Error !!!’; Exit.
[End of If]
NEW<--AVAIL
AVAIL<--AVAIL-->LINK
NEW-->INFO<--ITEM
NEW-->LINK<--NULL
If TOP=NULL Then:
TOP<--NEW
Else:
NEW-->LINK<--TOP
TOP<--NEW
[End of If]
Exit.
For Linked List implemented STACK in C, a self-referential structure, a user defined data type STACK is used, it is as follows:
struct STACK
{
int i:
STACK *link:
};
Algorithm for POP operation in Linked List implemented STACK.
POPLL(TOP)
[TOP is the address of TOP node of STACK]
If TOP=NULL Then:
Write: ‘Underflow’
Exit.
[End of If]
ITEM <--TOP-->INFO
TOP<--TOP-->LINK
Return ITEM
Exit.
Algorithm for PUSH operation in Linked List implemented STACK:
PUSHLL(TOP, ITEM)
[TOP is address of top node of STACK and ITEM is the item to PUSH.[In case of Linked List no overflow, but may be memory allocation error]
If AVAIL=NULL then:
Write: ’Memory Allocation Error !!!’; Exit.
[End of If]
NEW<--AVAIL
AVAIL<--AVAIL-->LINK
NEW-->INFO<--ITEM
NEW-->LINK<--NULL
If TOP=NULL Then:
TOP<--NEW
Else:
NEW-->LINK<--TOP
TOP<--NEW
[End of If]
Exit.
For Linked List implemented STACK in C, a self-referential structure, a user defined data type STACK is used, it is as follows:
struct STACK
{
int i:
STACK *link:
};
Algorithm for POP operation in Linked List implemented STACK.
POPLL(TOP)
[TOP is the address of TOP node of STACK]
If TOP=NULL Then:
Write: ‘Underflow’
Exit.
[End of If]
ITEM <--TOP-->INFO
TOP<--TOP-->LINK
Return ITEM
Exit.