Question: Project 4 Improved Linked List Part A : Implementing a FlexibleLinkedList data structure. ( 6 0 % ) In class, we discussed how to use
Project
Improved Linked List
Part A : Implementing a FlexibleLinkedList data structure.
In class, we discussed how to use a Linked List to implement a Stack data structure with a few simple operations : push, pop, and peek.
For this project, we will build a "better" linked list, with more flexibility regarding the operations it can handle. You will implement a struct called FlexibleLinkedList that consists of two SinglyLinkedLists, like the ones we discussed in class. The 'top' of both singly linked lists will be in the "center" of our FlexibleLinkedList, and both of our SingleLinkedLists will grow outward; the left list will grow towards the left, and the right list will grow towards the right.
Your FlexibleLinkedList should be generic; it should be possible to contain any type T However, for the examples below, we will assume that it contains integers.
The data structure will look like this:
We push to the left list. Notice how the left list is a Stack, with value now on top.
Next, we push to the right list, the value
Now, we push to the right list, the value
Finally, we push to the right list the value Notice how the top of the right list is now The entire list has the values However, the cursor is still in the center.
In order to explore the list contents, from beginning to end, we must first move the cursor to the leftmost item. We can do this by moving the cursor to the left multiple times, one item at a time. Let's try this once:
We want to move the cursor left
First, we pop the from the left stack. We then hold the value temporarily
We then push the to the right stack. Notice how the order of all the items is still the same, but the cursor has now move to the left one slot. Now, we can insert or remove ie push or pop from this new location.
If we repeat this process two more times, then we would end up in this situation:
After moving the cursor left twice more, then we now have the left list stack empty, and the right stack contains all the items.
Now, if we try and push to the right, a new value of we get this:
In this case, the list contains and the cursor is at index
Similarly, we could push a new value to the left, and get this:
Now, the list contains the values in that order. The cursor is at index
Let's imagine that we need to now add an item to index First, we can move the cursor to that location, by moving it to the right times:
Now, the cursor is at index and we can insert at this index by either pushing to the left, or pushing to the right. The only difference between these two options would be the new location of the cursor. If we push left, then the cursor would then be located at index If we instead push to the right, the cursor would remain at the same index.
Similarly, we could pop from either the left or the right list. In this case though, the side we use matters more, since the removed item is dependent on the side we choose.
Overall, we want to implement the following operations.
pushleftlist item : Add the given item to the left stack, at the location of the cursor
pushrightlist item : Add the given item to the right stack, at the location of the cursor
popleftlist : Remove the item from the left stack, at the location of the cursor. If there is no item to the left of the cursor, return None. Otherwise, return the removed item. Use the Option enum type
poprightlist : Remove the item from the right stack, at the location of the cursor. If there is no item to the right of the cursor, return None. Otherwise, return the removed item. Use the Option enum type
peekleftlist : Return the item to the left of the cursor. If there is no item to the left of the cursor, return None. Use the Option enum type
peekrightlist : Return the item to the right of the cursor. If there is no item to the right of the cursor, return None. Use the Option enum type
moveleftlist : Move the cursor one space to the left. If the cursor is already as far left as it can go leave it where it is Return true if the cursor moved, and false otherwise.
moverightlist : Move the cursor one space to the right. If the cursor is already as far
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
