Stack and Array Implementation with Python and NodeJs | by Chamal Pradeep | Towards Data Science

We were discussed basic definitions of the Data structures and algorithms in the previous article. in this article, let’s dig deeper into the Data structure world, and especially, let’s get our hands dirty with a little bit for coding as well.
Objectives of this article:
Discuss Data types, built-in, and derived data types.Introduce Stack Derived Data StructureImplement and use Stack in Python {code} and NodeJs {code}Introduce Array Derived Data StructureImplement and use Stack in Python {code} and NodeJs {code}
Discuss Data types, built-in, and derived data types.
Introduce Stack Derived Data Structure
Implement and use Stack in Python {code} and NodeJs {code}
Introduce Array Derived Data Structure
Implement and use Stack in Python {code} and NodeJs {code}
Data structures are made from one or more Data objects. data objects represent the data that we are going to store using carefully designed data structures. Data types are identified as primary ways of classifying several types of data in a data structure such as string, character, integer, etc. There are two major data types in the programming world, namely build-in data types and Derived data types.
These are the basic data types that programming languages are supporting. Mainly known as the primary data types in a particular programming language.
These data types are implemented using one or more built-in (primary) data types. All data structures are developed based on such kind of derived data types. In this article, we will discuss Stack’s an Array's data structures with the implementation examples.
The stack is an Abstract data type (ADT is a type for objects whose behavior is defined by a set of values and a set of operations) which is one of the main Data structures in programming languages, and for beginners, this is an easily understandable structure. LIFO (Last In First Out) is the main specialty of a stack.
Like most of the data structures, the stack also represent real-world objects. For instance, a stack of coins, a stack of boxes, etc.
A stack can be implemented using an array, a list, a pointer, etc. When it comes to a stack, there is a set of functions defined to use the stack efficiently in the programming context.
Python implementation
In python, we can use a list data type as a built-in data type to implement the stack data structure.
{code}Please find the attached codebase in this Github link.
NodeJs implementation
In NodeJs, we can implement a stack data structure using the Array data type.
{code} Please find the attached codebase in this Github link.
Once you defined the stack class one of the main functionalities is push function. Here you will input an item to the top of the array.
Algorithm to implement
We can define an algorithm to implement the push operation in the stack class.
Step 1 − Checks if the stack is full(assume that the list is implemented based on a  dynamic array) for the given size or not.Step 2 − If the stack is full, print an errorStep 3 − If the stack is not full for the given maximum size, increase the top by one and point the pointer to the next empty space.Step 4 − Add a new element to the new empty space which is in the top of the stackStep 5 − Return success.
The pop function will remove the topmost element from the stack, and the stack item count will be reduced by one. Even though it seems like the topmost element removed from the stack, still that element will not be completely removed, only the pointer will move to the below position.
Algorithm to implement
We can define an algorithm to implement the pop operation in the stack class.
Step 1 − Checks if the stack is empty by looking at the array lengthStep 2 − If the stack is empty, print an error, exitStep 3 − If the stack is not empty, get the element which is pointing at the top of the stack.Step 4 − Decreases the size of the stack by 1, automatically the pointer of the top most item  will changed to the below item.Step 5 − Returns success.
The peek function will display the topmost element from the stack, and this operation will not remove the item from the stack like in the pop operation.
Algorithm to implement
We can define an algorithm to implement the peek operation in the stack class. the peek operation just returns the value at the top of the stack.
Step 1 − Checks if the stack is empty by looking at the array lengthStep 2 − If the stack is empty, print an error, exitStep 3 − If the stack is not empty, get the element which is pointing at the top of the stack.Step 4 −  Returns success.
There are other functions like isEmpty(), isFull(), and printStackItems() which can be used as supportive functions that will help you to use the stack efficiently.
Stack implementation and all supportive functional implementations will be found in this codebase. stack data structure
Get your hands dirty with a stack data structure because we will use these data structures in the future when we are solving real-life problems in the algorithms section.
The array is one of the most used data structures when programmers implementing their algorithms. One specialty of an array is that the array container should be in fixed size and all the elements should be in the same type.
Apart from all the elements in the array should be in the same data type, array always starts from the 0th element (zero-indexed), and the size of the array means, how many elements can be stored in itself.
In many data types, there are major operations(functionalities) that exist for the effective and efficient usage of that data type. In the array data type, there are five major operations exist.
Add a new element to the given index is called the array insertion. We were able to implement inserting an element to the given index via python and nodejs programming language.
Implementation of the array insert operation in nodejs
Implementation of the array insert operation in python
We can perform search operations on an array element based on either the value or an index.
Search by index means, return the corresponding array element to the given index, and the search by value means to return the corresponding value the given index in the array.
Implementation of the array insert operation in nodejs
Implementation of the array insert operation in python
Since the array is a fixed size data structure, delete an element from the given position in an array is a little bit tricky. You should adjust the new array by reducing the size of the array while you are deleting the given element. Please refer following code samples which explain how to delete a given element from an array with basic programming techniques.
Implementation of the array insert operation in python
Implementation of the array insert operation in Nodejs
Array update is quite an easy operation that you just need to traverse through the given array until you find the required element to update. Please follow the following code samples to get more familiar with array update functionality.
Implementation of the array update operation in nodejs — code
Implementation of the array update operation in python — code
Array traversal is nothing but print all the array elements in a sequence. Since the array is a zero-indexed data type, we can start to print array elements by traversal through the array structure from the zero position.
Implementation of the array traversal operation in nodejs — code
Implementation of the array traversal operation in python — code
In conclusion, we have learned how to use the Stack and Array efficiently. Please find the following code segments to see how we can implement a stack and an array in python and nodejs.
You will only need node installed in your machine and you are good to go. (node installation link)
Start coding to get your hands dirty for the following lessons.
FYI:  when you trying to implement data structure operations, always remember on “Algorithm to implement” and think in that direction. This approach will help you to get a deep understanding of the entire workflow of data structure usage.
In the next lesson, we will implement other derived data structures like a queue, linked list, doubly link list, and circular linked lists which are considered as highly important data structures to get a hand on before diving into algorithmic usage.
For the Git repository cloning please click here
See you with more lessons, happy coding!