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.

Recommended Answers

All 7 Replies

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

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

hurry and edit that! highlight it and hit the [code] button...

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

@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

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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.