Linked List Implementation 2


 Stack ?

Stack is an important data structure which stores its elements in an ordered manner
Analogy:
  You must have seen a pile of plates where one plate is placed on top of another. When you want to remove a plate, you remove the topmost plate first. Hence, you can add and remove an element (i.e. the plate) only at/from one position which is the topmost position

 Array Representation
Stack has two variables:
TOP which is used to store the address of the topmost element of the stack
MAX which is used to store the maximum number of elements that the stack can hold
If TOP = NULL, then indicates that the stack is empty
If TOP = MAX – 1, then the stack is full
     0    1   2   3    4   5    6    7   8
TOP=4, insertion and deletion will be done at this position
Linked List Representation
Technique of creating a stack using array is easier, but the drawback is that the array must be declared to have some fixed size.
In a linked stack, every node has two parts:
One that stores data
One that stores the address of the next node
The START pointer of the linked list is used as TOP
All insertions and deletions are done at the node pointed by the TOP
If TOP = NULL, then it indicates that the stack is empty
 

Komentar

Postingan populer dari blog ini

Pointer, Array and Introduction to Data Structure-fernando-2101637812

Tree Concept

Binary Tree