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
What is 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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 here on WordPress where I have many other posts like:
As always, if you liked what you read and you’d like to support me, please consider buying me some coffee! Every cup of coffee sent my way helps me stay awake after my full-time job so that I can produce more high quality blog posts! Any and all support would be greatly appreciated!
2 thoughts on “Data Structures: Linked List with Python”