Stack
A stack is a linear structure that allows data to be inserted and removed from the same end thus follows a last in first out(LIFO) system. Insertion and deletion is known as push() and pop() respectively.
Program:
Python3
stack = [ 'first' , 'second' , 'third' ] print (stack) print () # pushing elements stack.append( 'fourth' ) stack.append( 'fifth' ) print (stack) print () # printing top n = len (stack) print (stack[n - 1 ]) print () # popping element stack.pop() print (stack) |
Output:
[‘first’, ‘second’, ‘third’]
[‘first’, ‘second’, ‘third’, ‘fourth’, ‘fifth’]
fifth
[‘first’, ‘second’, ‘third’, ‘fourth’]
User Defined Data Structures in Python
In computer science, a data structure is a logical way of organizing data in computer memory so that it can be used effectively. A data structure allows data to be added, removed, stored and maintained in a structured manner. Python supports two types of data structures:
- Non-primitive data types: Python has list, set, and dictionary as its non-primitive data types which can also be considered its in-built data structures.
- User-defined data structures: Data structures that aren’t supported by python but can be programmed to reflect the same functionality using concepts supported by python are user-defined data structures. There are many data structure that can be implemented this way:
Contact Us