Hello, I am creating a doubly linked list class but I am unsure how to make it so I get the current node. The concept behind this is that I am making a carousel of images and if I add an image/node then that node becomes the current node and I either add to the left or right of that node. If I decide to remove from that node then it'll remove the left/previous of the current node and then the current becomes the previous. I'll figure out how the add and remove methods but I'm just unsure of how I can start the get current node method.
#๐ Circular Doubly Linked List
44 messages ยท Page 1 of 1 (latest)
@lunar grotto
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
Hello, to make it easier for others to help you, please remember to always share text as text.
!code
the instructions say that the size of the cyclic linked list is fixed; if you need to update an element, wouldn't it be easier to just update the value for an existing node, rather than try to replace the node?
I think I have a general idea, so basically when I am initializing the list I have a head, tail and current, and whenever I make changes I can use current and update the next and previous according to that current
when you create a doubly linked list, there is no "current node" that is an inherent part of the list. the concept of a "current node" only applies when you're iterating over the list, or doing an operation on it.
though it looks like your assignment wants you to pretend that there is
oh well.
@lunar grotto can you show the definition for the doubly linked list class? you've only shown the node class. please post it as text--not as a screenshot.
!code
you have to follow the instructions in the message. you do not need to say "!code" again
this message contains the instructions.
Hey @lunar grotto!
It looks like you are trying to paste code into this channel.
You seem to be using the wrong symbols to indicate where the code block should start. The correct symbols would be ```, not '''.
Furthermore, it looks like you pasted Python code without syntax highlighting. Please use syntax highlighting to improve the legibility of your code and make it easier for us to help you.
To do this, use the following method:
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
You can **edit your original message** to correct your code block.
class CircularDoublyLinkedList:
def __init__(self, capacity):
self.head = None
self.tail = None
self.size = 0
self.capacity = capacity
self.current = None
def isFull(self):
return self.size >= self.capacity
def isEmpty(self):
return self.size == 0
def add(self, item):
if self.isFull():
raise Exception("Carousel is Full.")
new_node = DLinkedListNode(item, self.head, None)
if self.head is None:
self.head = new_node
self.tail = new_node
self.current = new_node
self.size = 1
Okay got it
you forgot the py, but that's fine. please do not delete it again.
So far this is what I have
do the instructions contain any other references to the "current node"?
but the instructions say you need a method for getting the "active/current node"
by doing current.setPrevious and current.setNext
how does a node become the current node?
Yes and I can do that by adding another method that just does self.current.getData()
the node becomes the current node if I make self.current = new_node
because when I add I have to create a new_node
okay. do you want to focus on just getting the add method to work?
What I am planning on doing was just to make 2 other methods, addLeft and addRight, then once those are done I can do the remove method, which is only removing the current node, I don't have to specify what position or item.
The current will change when I call the methods for the object outside of the class
in the class I am only trying to set its position and where it points to
That was my general idea on the problem
Okay. What issues are you facing currently?
When I sent the code I didn't know how to start so I was just wondering if my approach to the problem was something that would work or if I need to change anything so that I won't have issues later.
I don't think having separate addLeft and addRight methods will necessarily make things any easier.
Later on I have to have to prompt the user to either add left or right so I thought it would be easier to implement that when I have 2 methods for each case and just add for when the list is empty
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.