1. Create a node class and linked list class.
2. Add the following function from lecture to your linked list class
addFirst(e)
addLast (e)
add(index, e)
removeFirst
removeLast
remove(index)
3. Add the following methods to the linked list class.
a. # Return the size of the list
def getSize(self):
b. # Clear the list */
def clear(self):
c. # Return the element from this list at the specified index
def get(self, index):
#Python program
class Node:
def __init__(self, data):
self.data = data
self.next = None
# Linked List class
class LinkedList:
def __init__(self):
self.head = None
def addFirst(self , new_data):
new_node = Node(new_data)
new_node.next= self.head
self.head = new_node
def addLast(self, new_data):
new_node = Node(new_data)
if self.head is None:
self.head = new_node
return
last = self.head
while (last.next):
last = last.next
last.next = new_node
def add(self,index,new_data):
if(index<0 or index>self.getSize()):
return
new_node = Node(new_data)
if index==0:
new_node.next= self.head
self.head = new_node
return
last = self.head
i=1
while (last.next and i<index):
last = last.next
i=i+1
new_node.next = last.next
last.next = new_node
def removeFirst(self):
if(self.head is None):
return
temp = self.head
self.head = temp.next
temp=None
def removeLast(self):
new_node = Node(new_data)
if self.head is None:
self.head = new_node
return
last = self.head
if last.next is None:
last = None
return
prev = last
while (last.next):
prev = last
last = last.next
prev.next = last.next
last = None
def getSize(self):
count = 0
last = self.head
while last:
count = count+1
last = last.next
return count
def clear (self):
current = self.head
while current:
prev = current.next
del current.data
current = prev
def get(self , index):
if(index<0 or index>=self.getSize()):
return
current = self.head
i=0
while current and i<index:
i=i+1
current = current.next
return current.data
llist = LinkedList()
llist.addLast(7)
llist.addFirst(1)
llist.addLast(3)
llist.addFirst(2)
print(llist.getSize())
print(llist.get(2))
llist.add(2,5)
print(llist.get(2))
1. Create a node class and linked list class. 2. Add the following function from lecture...
PYTHON
--------------------------------------------------------
class LinkedList:
def __init__(self):
self.__head = None
self.__tail = None
self.__size = 0
# Return the head element in the list
def getFirst(self):
if self.__size ==
0:
return
None
else:
return
self.__head.element
# Return the last element in the list
def getLast(self):
if self.__size ==
0:
return
None
else:
return
self.__tail.element
# Add an element to the beginning of the
list
def addFirst(self, e):
newNode = Node(e) #
Create a new node
newNode.next =
self.__head # link...
Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...
Generic Linked Lists ( Program should be written in Java language). Modify the linked list class presented in this chapter so it works with generic types. Add the following methods drawn from the java.util.List interface: -void clear(): remove all elements from the list. -E get(int index): return the element at position index in the list. -E set(int index, E element): replace the element at the specified position with the specified element and return the previous element. Test your generic linked...
Python Program Only: Write the function definition only of the "get(self, index)" function. Plug your method in the code file shared with you and test it in the main to get the element at index 3 of mylist2 . class Node: def __init__(self, e): self.element = e self.next = None class LinkedList: def __init__(self): self.head = None self.tail = None self.size = 0 def addFirst(self, e): newNode = Node(e) newNode.next = self.head self.head = newNode self.size = self.size + 1...
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...
LinkedListBox Patterned after the ArrayBox assignment, please create your own implementation of LinkedList called LinkedListBox as explained in class. Refer to the class slides on linked lists. Specifically, implement your generic classes as: public class LinkedListNode { public E element; public LinkedListNode next; } public class LinkedListBox { public LinkedListNode head; public LinkedListNode tail; public int count = 0; } Within the LinkedListBox class, implement the following methods: public boolean add(E e). Returns true after adding an element to the...
1. Create a class MLL, a singly linked, non-circular list where each node only has one link next and the list has a head and a tail link (think about implementation of node). The MLL class also implements the Iterable interface. The following should be implemented as well: 2. Add the methods to the MLL class: a. iterator() to implement the Iterable interface. This method returns an instance of MLLI initialized appropriately. b. addFirst( value) that adds a value to...
On Java:
Problems Problem 1. (Deque) Hints: my Use a doubly-linked list Node to implement the API — each node in the list stores a generic item, and references next and prev to the next and previous nodes in the list null itemi → item2 item3 ... itemn null Instance variables wy Reference to the front of the deque, Node first my Reference to the back of the deque, Node last my Size of the deque, int n + LinkedDeque...
JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has you display a pessimistic poem from a list of phrases. Next, this program has you reverse the phrases to find another more optimistic poem. Use the following algorithm. 1. You are given a list of phrases each ending with a pound sign: ‘#’. 2. Create a single String object from this list. 3. Then, split the String of phrases into an array of phrases...
Write a generic array list class. Task Description Your goal for this lab is to write a generic ArrayList class similar to the one in the given lecture notes on array lists. The ArrayList Class The public constructors and methods required for the ArrayList class are listed here. The type E is the generic type of an element of the list. ArrayList() Construct an empty ArrayList object. int size() Return the size (number of items) in this ArrayList. boolean isEmpty()...