python - Linked List and manipulation -


i need constructing linkedlist no constructor parameters in python 3.3; resulting in empty linkedlist object. want able add , remove elements in front , in end of list. far can prepend , add things front. i'm little iffy on how else works. here's have far:

class node:     def __init__(self, d, n):         self.data = d         self.next = n   class linkedlist:     def __init__(self):         self._head = none         self._size = 0      def __len__(self):         return self._size      def append(self, item):         pass      def prepend(self, item):         self._head = node(item, self._head)         self._size += 1      def remove_first(self):         pass      def remove_last(self):         pass 

does have clue? bunch!

you can use deque object collections module in standard library. prepend method called appendleft in deque object.

in other way, can modify linkedlist class reference last node of list:

class linkedlist:     def __init__(self):         self._head = none         self._tail = none         self._size = 0      def append(self, item):         if self._tail none:             self.prepend(item)         else:             self._tail.next = node(item, none)             self._tail = self._tail.next      def prepend(self, item):         if self._head none:             self._tail = node(item, none)             self._head = self._tail         else:             self._head = node(item, self._head)         self._size += 1 

Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -