Data Structures: Linked List with Python
If you’re just starting out in the Computer Science world, you may have started learning about data structures. Or maybe you’re trying to brush up on the basics so you be prepared to take on the l33t coding interview for your dream job. Whatever your reason may be, I’ve taken the liberty to write up one of the most basic data structures for you in python: a linked list
Simply put, a linked list is a sequence of nodes that contain data and a reference point to the next node in memory. There are many different variations of linked lists with different capabilities that may better fit different use cases, but in this blog post I’ve created a doubly linked list. My doubly linked list has the following functions/feats:
- Size: Contains a property that tracks the number of elements in the list
- AddNode(data: int): Add a new node at the end of the list with the provided data
- SetValueAt(index: int, data: int): Change the value of an existing node with the provided data
- RemoveNodeAtIndex(index: int): Remove a node at the provided index from the linked list
- GetValueAtIndex(index: int): Returns the value of the node that exists at the provided index.
- Clear(): Clears the linked list of all nodes
Code
Running Code
Python3 LinkedList.py
Conclusion
As I previously mentioned, there are many different ways to create a linked list. My provided example is just one of many that you can find on the internet. I just hope that somebody out there can find my example useful in some way.
If you found this post interesting, please consider following my blog over on WordPress where I have many other posts like:
- VulnHub: Jangow 1.0.1 Writeup
- Hosting a Website with Github Pages
- Creating a Python Bot with Selenium
- Asynchronous Server/Client with Python
- Hack The Box: Impossible Password
Feel free to also follow me via my other social media accounts: Instagram, Twitter, Facebook, and Medium!
Originally published at http://testingonprod.com on December 12, 2021.