![]() ![]() The points (6) and (7) are a gentle introduction to what I will tell you here: you misunderstood the stack implementation: we can implement a stack using an array or a simple linked list.In a stack, we need to perform the push and pop operations at best, we would like to know its size, everything else does not belong to the stack notion. Why? Because you are coding a stack, and a stack follows the principle of LIFO, so do not care about the other end at all. Do not code what you do not need: it is unnecessary to use add_last() and remove_last().The head is the tail, and the tail is the head because that is where operations occur. You said you want to code a stack: by definition, in a stack, we need only a pointer to its head.I mean you can replace similar lines to simply: if not self._head Here, we can use more common sense: if not self._head and not self._tail: honestly, if the head is None, it must be obvious the tail is in the same condition.else statements instead because this makes sense and that is what the reader of your code may expect. While it apparently does the job, the return statement of your code in (3) is not in the right place.You can refactor this code into one function and call it when necessary: if not self._head and not self._head: In opposite, doing so condemns you not to reuse the Node() code and slaughters your code scalability (in case you will need to add functionality to your code by implementing a queue, for example) I do not see the interest of making Node() inner to LinkedList().You have a typo/mistake in if not self._head and not self._head, I think you wanted to write self._tail in one of them.""" This is depending on another method """ Is there anything I need to improve? whether it's style or logic.Ĭan you find any bugs? Whether in the linked list or the stack. This is because deleting the tail would require the traversal of the linked list to delete the last element. If adding and removing last were used, then remove last would O(n). Note: The reason add_first and remove_first are using for push and pop respectively is to keep them O(1). """ A stack implementation using a linked list """ Remove the first element in the linked list """ If not self._head and not self._tail: # if linked list is emptyĮlif self._head is self._tail: # if only one element """ Remove the last element in the linked list """ """ Add data to the end of the linked list """ ![]() """ Add data to the beginning of the linked list""" Super()._init_("Stack is empty: Invalid Operation!")ĭef _init_(self, data, next_node=None): """Insert a new node at the given position.The following is my implementation of a stack using a linked list. It was the insert method acting up so I changed it to def insert(self, new_element, position): """Get an element from a particular position. Then, use "Test Run" and "Submit" to run the test cases "delete" will delete the first element with that The "insert" function will add an element to a particular "get_position" returns the element at a certain position. My code looks like this: """The LinkedList code from before is provided below. ![]() ![]() I have been stuck on the delete function for the past few days and I can't see the (maybe obvious) solution. The exercise is to make get_position, insert and delete actions for a linked list class and then get the required results commented at the bottom of the script. I am ongoing an online course which includes actions with linked lists in Python. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |