最初のノードからのカウントとしてリスト内の場所にあるノードを削除するdelete_node関数を作成したいとします。リンクされたリストからのノードの削除
class node:
def __init__(self):
self.data = None # contains the data
self.next = None # contains the reference to the next node
class linked_list:
def __init__(self):
self.cur_node = None
def add_node(self, data):
new_node = node() # create a new node
new_node.data = data
new_node.next = self.cur_node # link the new node to the 'previous' node.
self.cur_node = new_node # set the current node to the new one.
def list_print(self):
node = ll.cur_node
while node:
print node.data
node = node.next
def delete_node(self,location):
node = ll.cur_node
count = 0
while count != location:
node = node.next
count+=1
delete node
ll = linked_list()
ll.add_node(1)
ll.add_node(2)
ll.add_node(3)
ll.list_print()
ご質問ありがとうございます。具体的な質問をStackOverflowにお願いしてください。ちょうど私たちにいくつかのコードを書いて、「ここに私がしたいことがある」と言わないでください。たとえば、あなたはどんな問題に取り組んでいますか? – Amber
さて、私はこのアルゴリズムをC++で理解しています。問題はどうしたらノードオブジェクトを削除できますか?基本的にnew_nodeを作成することの反対 – pandoragami
@lost_with_coding「C++で」アルゴリズムを理解していれば、そのアルゴリズムを理解できません。あなたはC++でそれを実装する方法を知っています。アルゴリズムを理解していれば、Pythonの構文について特定の質問をするでしょう。 – aaronasterling