Question

I need help Writing a Python code!!! Implement an ordered list using doubly linked list Implement...

I need help Writing a Python code!!!

Implement an ordered list using doubly linked list

Implement the following operations for an ordered list of integers ordered in ascending order using a doubly
linked list. The “head” of the list be where the “smallest items are and let “tail” be where the largest items are.
You may use whatever mechanism you like to keep track of the head and tail of the list. E.g. references or
sentinel nodes.
• OrderedList () creates a new ordered list that is empty. It needs no parameters and returns an empty list.
• add (item) adds a new item to the list making sure that the order is preserved. It needs the item and
returns nothing. Assume the item is not already in the list.
• remove (item) removes the item from the list. It needs the item and modifies the list. Return the position
of removed item if it is in the list, otherwise return -1 (as not found).
• search_forward (item) searches for the item in the list. It needs the item and returns the boolean value
True if the item is in the list and False if the item is not in the list.
• search_backward (item) searches for the item in the list starting from the tail of the list. It needs the
item and returns the boolean value True if the item is in the list and False if the item is not in the list.
• is_empty () tests to see whether the list is empty. It needs no parameters and returns a boolean value.
True if the list is empty and False if any items are in the list.
• size () returns the number of items in the list. It needs no parameters and returns an integer.
• index (item) returns the position of item in the list. It needs the item and returns the index. If it is not in
the item it returns -1(as not found).
• pop () removes and returns the last item in the list. It needs nothing and returns an item.
• pop (pos) removes and returns the item at position pos. It needs the position and returns the item. If it is
not in the item it returns -1(as not found). pop(pos) should compare pos to the size of the list and
search from the head if pos <= size/2 and from the rear if pos > size/2.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
class Node(object):
def __init__(self, initdata):
self._data = initdata
self._next = None
self._prev = None
@property
def data(self):
return self._data
@property
def next(self):
return self._next
@property
def prev(self):
return self._prev
def set_data(self, newdata):
self._data = newdata
return self.data
def set_next(self, newnext):
self._next = newnext
return self.next
def set_prev(self, newprev):
self._prev = newprev
return self.prev
class DoublyLinkedList(object):
def __init__(self):
self.head = None
def isEmpty(self):
return self.head is None
def add(self, item):
new = Node(item)
if self.head is None:
self.head = new
elif self.head.data > new.data:
new.set_next(self.head)
self.head.set_prev(new)
self.head = new
else:
prev = self.head
cur = self.head.next
while cur is not None:
if cur.data > new.data:
prev.set_next(new)
new.set_prev(prev)
new.set_next(cur)
cur.set_prev(new)
return new
prev = cur
cur = cur.next
prev.set_next(new)
new.set_prev(prev)
return new
@property
def size(self):
count = 0
cur = self.head
while cur is not None:
count += 1
cur = cur.next
return count
def search(self, item):
cur = self.head
while cur is not None:
if cur.data == item:
return True
elif cur.data > item:
return False
cur = cur.next
return False
def remove(self, item):
if self.head.data == item:
self.head = self.head.next
else:
prev = self.head
cur = prev.next
while cur is not None:
if cur.data == item:
if cur.next is None:
prev.set_next(None)
else:
prev.set_next(cur.next)
cur.next.set_prev(prev)
return True
prev = cur
cur = cur.next
return False
def index(self, item):
if self.search(item) == False:
raise ValueError("Item not in list")
index = 0
cur = self.head
while cur is not None:
if cur.data == item:
return index
index += 1
cur = cur.next
return index
def pop(self, index=None):
if self.isEmpty():
raise ValueError("The list is already empty")
prev = self.head
cur = prev.next
if index is None:
while cur is not None:
if cur.next is None:
prev.set_next(None)
return cur
prev = cur
cur = cur.next
self.head = None
return prev
if index == 0:
self.head = cur
return prev
cur_index = 0
while cur is not None:
if cur_index == index - 1:
try:
prev.set_next(cur.next)
cur.next.set_prev(prev)
except:
prev.set_next(None)
return cur
prev = cur
cur = cur.next
cur_index += 1
new_list = DoublyLinkedList()
print "isEmpty:", new_list.isEmpty()
new_list.add(10)
print "isEmpty:", new_list.isEmpty()
new_list.add(20)
new_list.add(30)
new_list.add(40)
new_list.add(25)
print "search(15):", new_list.search(15)
print "search(10):", new_list.search(10)
print "size:", new_list.size
print "remove(30):", new_list.remove(30)
print "size:", new_list.size
print "index(25):", new_list.index(25)
print "pop(2):", new_list.pop(2).data
print "size:", new_list.size
print "pop(1):", new_list.pop(1).data
print "size:", new_list.size
print "pop(1):", new_list.pop(1).data
print "pop():", new_list.pop().data
Add a comment
Know the answer?
Add Answer to:
I need help Writing a Python code!!! Implement an ordered list using doubly linked list Implement...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Part I: Create a doubly linked circular list class named LinkedItemList that implements the following interface:...

    Part I: Create a doubly linked circular list class named LinkedItemList that implements the following interface: /** * An ordered list of items. */ public interface ItemList<E> {      /**       * Append an item to the end of the list       *       * @param item – item to be appended       */ public void append(E item);      /**       * Insert an item at a specified index position       *       * @param item – item to be...

  • Linked Lists: Suppose you have a doubly linked list with both head and tail pointers, that...

    Linked Lists: Suppose you have a doubly linked list with both head and tail pointers, that stores integers. Implement a non-recursive function that takes a linked list, searches for an integer, and removes the node with the first occurrence of that integer and also removes the node directly after it regardless of value . This function will return to address of the resulting list. You ca n assume that there will be at least three nodes, and if there is...

  • Using a doubly linked list as the underlying data structure, implement a list ADT that implements...

    Using a doubly linked list as the underlying data structure, implement a list ADT that implements the ListInterface.java found in the ProgProjTwo Eclipse project starting point for this assignment. In addition to the forward iterator defined by resetIterator( ) and getNextItem( ) in ListInterface.java, implement a backwards iterator by providing resetBackIterator( ) and getPreviousItem( ) methods. As noted in the syllabus addendum, you are encouraged to develop a find( ) helper method that can support various list ADT operations. A...

  • C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the...

    C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the sortedList class. Call your new class doublyLinkedList. Convert the baseline code into a doubly linkedlist, and thoroughly test all existing operations (make sure to check all edge conditions), and then implement the new operations below. The class should have the following additional class methods: • A reverse method: this method will reverse the order of the doubly linked list. This method takes no parameters,...

  • I need help with the Implementation of an Ordered List (Template Files) public interface Ordered...

    I need help with the Implementation of an Ordered List (Template Files) public interface OrderedStructure { public abstract int size(); public abstract boolean add( Comparable obj ) throws IllegalArgumentException; public abstract Object get( int pos ) throws IndexOutOfBoundsException; public abstract void remove( int pos ) throws IndexOutOfBoundsException; public abstract void merge( OrderedList other ); } import java.util.NoSuchElementException; public class OrderedList implements OrderedStructure { // Implementation of the doubly linked nodes (nested-class) private static class Node { private Comparable value; private...

  • Implement the EasyStack interface with the MyStack class. You can use either a linked list or...

    Implement the EasyStack interface with the MyStack class. You can use either a linked list or a dynamic array to implement the data structure. A stack is a specialised form of list in which you can only get and remove the element most recently added to the stack. The class should be able to work with the following code: EasyStack stack = new MyStack(); NB: You cannot import anything from the standard library for this task. The data structure must...

  • Instructions Part 1 - Implementation of a Doubly Linked List Attached you will find the code for ...

    Instructions Part 1 - Implementation of a Doubly Linked List Attached you will find the code for an implementation of a Singly Linked List. There are 3 files: Driver.java -- testing the List functions in a main() method. LinkNode.java -- Class definition for a Node, which is the underlying entity that stores the items for the linked list. SinglyLinkedList.java -- Class definition for the Singly Linked List. All the heavy lifting happens here. Task 1 - Review & Testing: Create...

  • An ordered stack is a data structure that stores a sequence of items and supports the...

    An ordered stack is a data structure that stores a sequence of items and supports the following operations. ORDERPUSH(x) removes all items smaller than x from the beginning of the sequence and then adds x to the beginning of the sequence. Pop deletes and returns the first item in the sequence (or NULL if the sequence is empty). Suppose we implement an ordered stack with a simple linked list, using the obvious ORDERPUSH and Pop algorithms. Prove that if we...

  • Using the provided table interface table.h and the sample linked list code linkedList.c, complete an implementation...

    Using the provided table interface table.h and the sample linked list code linkedList.c, complete an implementation of the Table ADT. Make sure that you apply the concepts of design by contract (DbC) to your implementation. Once you have fully implemented the table, create a main.c file that implements a testing framework for your table. Your table implementation must ensure that values inserted are unique, and internally sorted within a linked list. table.h #ifndef _TABLE_H #define _TABLE_H //----------------------------------------------------------------------------- // CONSTANTS AND...

  • starter code To write a program using the starter code which is TestLinkedList to see if...

    starter code To write a program using the starter code which is TestLinkedList to see if the LinkedList program has bugs. It will produce ether a pass or fail.More information is in the first two pictures. LinkedList.java /** * @author someone * * Implements a double-linked list with four errors */ public class LinkedList<E> { // The first and last nodes in the list private Node<E> head, tail; // Number of items stored in the list private int size; //...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT