Skip to content

LinkinLearning coding practice of data structures and algorithms in python

Notifications You must be signed in to change notification settings

Bansikah/Python-Data-Structures-and-Algorithms

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python-Data-Structures-and-Algorithms

Stack class

This is a Python class that implements a stack data structure. A stack is a linear data structure in which elements are added and removed from one end, called the top. The stack follows the last-in, first-out (LIFO) principle, which means that the last element added to the stack is the first element to be removed.

The Stack class has the following methods:

  • __init__(self): Initializes the stack.
  • is_empty(self): Returns True if the stack is empty, False otherwise.
  • push(self, item): Adds an item to the stack.
  • pop(self): Removes an item from the stack and returns it.
  • peek(self): Returns the top item on the stack without removing it.
  • size(self): Returns the number of items in the stack.
  • __str__(self): Returns a string representation of the stack.

Here is an example of how to use the Stack class: python

s = Stack() print(s) [] print(s.is_empty()) True s.push(1) s.push(2) s.push(3) print(s) [1, 2, 3] print(s.pop()) 3 print(s.peek()) 2 print(s.size()) 2 The Stack class is a useful data structure for implementing a variety of algorithms, such as the infix-to-postfix conversion algorithm and the binary search algorithm. You can view code here [stack class](/Starting Files/a_star.py)

About

LinkinLearning coding practice of data structures and algorithms in python

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 93.9%
  • JavaScript 2.4%
  • HTML 2.3%
  • CSS 1.4%