Use the Node class. Create a tester module that contains the following function definitions and code for testing them. The function definitions are:
1. A function length (not len) that expects a single linked structure as argument. The function returns the number of items in the structure.
2. Define a function named insert that inserts an item into a single linked structure at a given position. The function expects three arguments: the item, the position and the linked structure (the latter may be empty). The function returns the modified linked structure. If the position is greater than or equal to the structure’s length, the function inserts the item at its end. An example call of a structure where head is length is a variable that either is an empty link or refers to the first node of a structure, is head = insert (1, data, head).
3. Define a function named remove that removes the item at a given position from singly linked structure. The function expects a position as a first argument, with the precondition 0<= position < length of structure. Its second argument is the linked structure, which, of course, cannot be empty. The function returns a tuple containing the modified linked structure and the item that was removed. An example call is (head, item) = remove (1, head).

Need help writing this program-been working on it for more than four days.

Edited 5 Years Ago by nw240x: n/a

Show us some effort, if you've been working on it for three days you must have some code, post it. And use the code tags when you do. Also read the forum rules.

import sys
sys.path.append
from node import Node

class None(length)

class Node(object):
    def __init__(self, data, next =None):
        self.data = data
        self.data = next
removedItem = head.data
head = head.next
return removedItem

if head is None or index <0:
    head = None(newItem, head)
else:
    probe = probe
    while index >1 and probe.next!=None:
        probe = probe.next,
        index -= 1
    probe.next = None(newItem, probe.next)
while probe != None:
    print probe.data
    probe = probe.next

newNode = Node
if head is None:
    head = newNode
else:
    probe = head
    while probe.next.next != None:
        probe = probe.next
    probe.next = newNode
probe = head
print "linked structure after inserting end"
while probe != None:
    print probe.data
    probe = probe.next   
if index <=0 or head.next is None
    removedItem = head.data
    head = head.next
    return removedItem
else:
    probe = head
    while index >1 aand  probe.next.next != None:
        probe = probe.next
        index -= 1
    removedItem = probe.next.data
    probe.next = probe.next.next
    return removedItem

Edited 5 Years Ago by peter_budo: Keep It Clear - Do use code tags when posting any code

you need to use the code tags, note the (CODE) button when posting, hit that and paste your code in the tags it generates...

Edited 3 Years Ago by mike_2000_17: Fixed formatting

import sys
sys.path.append # does nothing
from node import Node # ??

class None(length) #????????


class Node(object): ## if we had module node this overwrited Node

Edited 5 Years Ago by pyTony: n/a

@pyguy62 instead of advising to add code tags and by this causing reposting it is better if you hit "Flag Bad Post" as you actually consequently did

Edited 5 Years Ago by peter_budo: n/a

import sys
sys.path.append
from node import Node
 
class None(length)
 
class Node(object):
    def __init__(self, data, next =None):
        self.data = data
        self.data = next
removedItem = head.data
head = head.next
return removedItem
 
if head is None or index <0:
    head = None(newItem, head)
else:
    probe = probe
    while index >1 and probe.next!=None:
        probe = probe.next,
        index -= 1
    probe.next = None(newItem, probe.next)
while probe != None:
    print probe.data
    probe = probe.next
 
newNode = Node
if head is None:
    head = newNode
else:
    probe = head
    while probe.next.next != None:
        probe = probe.next
    probe.next = newNode
probe = head
print "linked structure after inserting end"
while probe != None:
    print probe.data
    probe = probe.next   
if index <=0 or head.next is None
    removedItem = head.data
    head = head.next
    return removedItem
else:
    probe = head
    while index >1 aand  probe.next.next != None:
        probe = probe.next
        index -= 1
    removedItem = probe.next.data
    probe.next = probe.next.next
    return removedItem

Edited 5 Years Ago by peter_budo: Keep It Clear - Do use code tags [code]YOUR CODE HERE[/code]

This article has been dead for over six months. Start a new discussion instead.