Add a method to the DoubleLinkedList class built in class to reverse every set of values
For example:
1, 2, 3, 4, 5, 6
Reverse 3: 3,2,1,6,5,4
Reverse 2: 2,1,4,3,6,5
Reverse 6: 6,5,4,3,2,1
Method header:
public void reverseSegments(int setSize)
outcome should be like this:
Input:
3
1 2 3 4 5 6
output:
3 2 1 6 5 4
Input:
2
1 2 3 4 5 6
output:
2 1 6 5 4 3
============================================code======================================================================
public class MyDoubleLinkedList<E> {
private Node start, end;
private int currentCount;
public MyDoubleLinkedList()
{
start = null;
end = null;
currentCount = 0;
}
public void printList()
{
Node current = start;
while(current != null)
{
System.out.println(current.value);
current = current.next;
}
}
public void printListRev()
{
Node current = end;
while(current != null)
{
System.out.println(current.value);
current = current.prev;
}
}
public void add(E val)//O(1)
{
Node newItem = new Node(val);
//if list is empty
if(start == null)
{
start = newItem;
end = start;//only item in list means end = start
currentCount++;
}
//if list has items
else
{
end.next = newItem;//end -> newItem
newItem.prev = end;//end <- newItem
end = newItem;
currentCount++;
}
}
public void insert(E val, int index)
{
if(index < 0)
{
index = 0;
}
if(index >= currentCount)//insert at end is same as add
{
this.add(val);
}
else
{
Node newItem = new Node(val);
if(index == 0)//special case, changing start variable
{
newItem.next = start;//current list comes after new item
start.prev = newItem;
start = newItem;//new item is first in list
}
else
{
Node current = start;
for(int i = 1; i < index; i++)
{
current = current.next;
}
//System.out.println(current.value);
//current == before at this point
//current/before <-> index <-> after
//1 <-> 2 <-> 3
//goal
//before <-> new <-> index <-> after
//1 <-> new <-> 2 <-> 3
newItem.next = current.next;//new -> index
current.next.prev = newItem;//new <- index
current.next = newItem;//before -> new
newItem.prev = current;//before <- new
}
currentCount++;
}
}
public void delete(int index)
{
if(index >= 0 && index < currentCount)
{
if(index==0)//deal with special case
{
start = start.next;
if(start != null)//in case list just became empty
{
start.prev = null;
}
else
{
end = null;
}
}
else if(index == currentCount -1)
{
end = end.prev;
if(end != null)
{
end.next = null;
}
else
{
start = null;
}
}
else
{
Node current = start;
for(int i = 1; i < index; i++)//find item before the one being deleted
{
current = current.next;
}
current.next = current.next.next;
//current <-> deleteMe <-> restoflist
//current -> restoflist
//current <- deleteMe <- restoflist
if(current.next != null)//incase we deleted the last item
{
current.next.prev = current;//current <- restoflist
}
}
currentCount--;
}
}
public E get(int index)//O(N)//could be improved to O(N/2) by starting from start/end depending on index
{
if(index >= 0 && index < currentCount)
{
Node current = start;
for(int i = 0; i < index; i++)
{
current = current.next;
}
//current = node at the index
return current.value;
}
else
{
return null;
}
}
//Problem 1 swap method
public void swap(int index)
{
Node temp = start;
int startindex = 0;
while (temp != null && temp.next != null ) {
if(startindex++ == index) {
E k = temp.value;
temp.value = temp.next.value;
temp.next.value = k;
temp = temp.next.next;
break;
}
temp = temp.next;
}
}
private class Node
{
E value;
Node next, prev;
public Node(E v)
{
value = v;
next = null;//no node after this one
prev = null;
}
}
}
import java.util.ArrayList;
import java.util.Collections;
public class MyDoubleLinkedList<E> {
private Node start, end;
private int currentCount;
public MyDoubleLinkedList()
{
start = null;
end = null;
currentCount = 0;
}
//
public void reverseSegments(int setSize){
/*
* N: nodeArray length
* S: setSize
* Time Complexity (Worst Case): -2S+12N+((4N^2N)/S)+20
* Big(O): N^2
*/
long opCnt = 0;
// Special Case: setSize = 0 or 1
if (setSize == 0 || setSize == 1) {
opCnt++;
return; // 1: Comparison
}
// Turn double linked list into an ArrayList // 4N + 2
ArrayList<E> nodeArray = new ArrayList<E>(currentCount); // 1: Assignment
opCnt++;
for (int i = 0; i < currentCount; i++){ // 5N + 1
nodeArray.add(start.value); // N: Run method
start = start.next; // N: Assignment
opCnt = 5 * opCnt + 1;
}
start = end = null; // 2: Assignment
opCnt += 2;
System.out.println("nodeArray: " + nodeArray);
// Special Case: setSize is not a factor of nodeArray length.
ArrayList<E> tail = null; // 1: Assignment
int remainder = nodeArray.size() % setSize; // 2: Assignment and Math
opCnt += 3;
if (remainder != 0) { // 1 + 3 + N - setSize + N - (N - setSize)
tail = new ArrayList<E>(nodeArray.subList(nodeArray.size() - remainder, nodeArray.size())); // 3
opCnt += 3;
Collections.reverse(tail); // N - setSize: Worst Case
opCnt += tail.size();
nodeArray = new ArrayList<E>(nodeArray.subList(0, nodeArray.size() - remainder)); // 1 + (N - (N - setSize))
opCnt += 2;
}
opCnt++;
System.out.println("After Special Case: ");
System.out.println("nodeArray: " + nodeArray);
System.out.println("tail: " + tail);
int nodeArrayMinusOne = nodeArray.size() - 1; // 2 Assignment and Math
opCnt += 2;
// Chop up nodeArray into setSize long sections, reverse those small sections, and stitch it back to get
for (int i = 0; i < nodeArrayMinusOne; i += setSize) { // 4N^2N / setSize + 1
ArrayList<E> reverseMe = new ArrayList<E>(nodeArray.subList(i, i + setSize)); // 3
opCnt += 3;
Collections.reverse(reverseMe); // N / setSize
opCnt += reverseMe.size();
System.out.println(i + ": " + reverseMe);
for (E obj : reverseMe) { // 2N / setSize
this.add(obj); // N
opCnt += 4;
}
opCnt++;
}
opCnt++;
if (remainder != 0) // 2(N - setSize) + 2: Worst Case
for (E obj : tail) { // N - setSize + 1
this.add(obj); // N - setSize
opCnt += 4;
}
opCnt++;
opCnt++;
System.out.println("Operation Count: " + opCnt);
}
public void printList()
{
Node current = start;
while(current != null)
{
System.out.println(current.value);
current = current.next;
}
}
public void printListRev()
{
Node current = end;
while(current != null)
{
System.out.println(current.value);
current = current.prev;
}
}
public void add(E val)//O(1)
{
Node newItem = new Node(val);
//if list is empty
if(start == null)
{
start = newItem;
end = start;//only item in list means end = start
currentCount++;
}
//if list has items
else
{
end.next = newItem;//end -> newItem
newItem.prev = end;//end <- newItem
end = newItem;
currentCount++;
}
}
public void insert(E val, int index)
{
if(index < 0)
{
index = 0;
}
if(index >= currentCount)//insert at end is same as add
{
this.add(val);
}
else
{
Node newItem = new Node(val);
if(index == 0)//special case, changing start variable
{
newItem.next = start;//current list comes after new item
start.prev = newItem;
start = newItem;//new item is first in list
}
else
{
Node current = start;
for(int i = 1; i < index; i++)
{
current = current.next;
}
//System.out.println(current.value);
//current == before at this point
//current/before <-> index <-> after
//1 <-> 2 <-> 3
//goal
//before <-> new <-> index <-> after
//1 <-> new <-> 2 <-> 3
newItem.next = current.next;//new -> index
current.next.prev = newItem;//new <- index
current.next = newItem;//before -> new
newItem.prev = current;//before <- new
}
currentCount++;
}
}
public void delete(int index)
{
if(index >= 0 && index < currentCount)
{
if(index==0)//deal with special case
{
start = start.next;
if(start != null)//in case list just became empty
{
start.prev = null;
}
else
{
end = null;
}
}
else if(index == currentCount -1)
{
end = end.prev;
if(end != null)
{
end.next = null;
}
else
{
start = null;
}
}
else
{
Node current = start;
for(int i = 1; i < index; i++)//find item before the one being deleted
{
current = current.next;
}
current.next = current.next.next;
//current <-> deleteMe <-> restoflist
//current -> restoflist
//current <- deleteMe <- restoflist
if(current.next != null)//incase we deleted the last item
{
current.next.prev = current;//current <- restoflist
}
}
currentCount--;
}
}
public E get(int index)//O(N)//could be improved to O(N/2) by starting from start/end depending on index
{
if(index >= 0 && index < currentCount)
{
Node current = start;
for(int i = 0; i < index; i++)
{
current = current.next;
}
//current = node at the index
return current.value;
}
else
{
return null;
}
}
private class Node
{
E value;
Node next, prev;
public Node(E v)
{
value = v;
next = null;//no node after this one
prev = null;
}
}
}
Add a method to the DoubleLinkedList class built in class to reverse every set of values...
Improve the speed of public T get(int i) by having it work backward from the end of the array if you attempt to get a member which is closer to the end than the start. This improvement will be tested through timing tests on large lists. The method should still return null if i is not a valid index. Code import java.util.Iterator; public class DLList<T> implements Iterable<T> { private static class Node<T> { public Node<T> prev, next;...
Create a MyQueue class in Java, similar to what we did when we created the MyStack class. Include a test class to show everything works correctly. Include all the methods mentioned in the power points that are normally included in a Queue. This is MyStack class we did in a class. Have to make Queue class. public class MyStack<E> { private Node start; private int currentCount; public MyStack() { start = null; currentCount = 0; } public void printStack()//available for...
The add(index, e) method inserts an element into the list at the specified index. It can be implemented as follows: public void add(int index, E e) { if (index == 0) addFirst(e); // Insert first else if (index >= size) addLast(e);// Insert last else { // Insert in the middle Node<E> current = head; for (int i = 1; i < index; i++) current = current.next; Node<E> temp = current.next; current.next...
P1 is below
package p6_linkedList;
import java.util.*;
public class LinkedList
{
public Node header;
public LinkedList()
{
header = null;
}
public final Node Search(int key)
{
Node current = header;
while (current != null && current.item != key)
{
current = current.link;
}
return current;
}
public final void Append(int newItem)
{
Node newNode = new Node(newItem);
newNode.link = header;
header = newNode;
}
public final Node Remove()
{
Node x = header;
if (header != null)
{
header...
Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time complexity is omitted. Any methods/functions below could be changed into something different. I was thinking of changing the method of getting size of list and maybe change from numbers to letters for nodes. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null;...
Java Programming: The following is my code: public class KWSingleLinkedList<E> { public void setSize(int size) { this.size = size; } /** Reference to list head. */ private Node<E> head = null; /** The number of items in the list */ private int size = 0; /** Add an item to the front of the list. @param item The item to be added */ public void addFirst(E...
public void add(linked list, int data){ Node node = new Node(data); node.next=null; node.prev=null; if(list.head==null){ list.head=node; } else{ Node n = list.head; Node temp = list.head; while(n.next!=null){ n=n.next; n.prev=temp; temp=temp.next; } n.next=node; node.prev=n; System.out.println(node.prev + " " + n.data); } } How can i turn this doubly linked list into a circular doubly linkedlist, using java
public void add(linked list, int data){ Node node = new Node(data); node.next=null; node.prev=null; if(list.head==null){ list.head=node; } else{ Node n = list.head; Node temp = list.head; while(n.next!=null){ n=n.next; n.prev=temp; temp=temp.next; } n.next=node; node.prev=n; System.out.println(node.prev + " " + n.data); } } How can i turn this doubly linked list into a circular doubly linkedlist, using java
Currently working on a Java Assignment. I have written most codes for swap, reverse and insert. Just need a. itemCount receives a value and returns a count of the number of times this item is found in the list. c. sublist receives two indexes and returns an ArrayList of node values from the first index to the second index, provided the indexes are valid. d. select receives a variable number of indexes, and returns an ArrayList of node values corresponding...
Problem 2: based on java.util.LinkedList class, create MyStack class that should have the methods: push(), pop(), peek(), size(), isEmpty(). my linkedlist class is: public class Lab8_problem1 { private Node head, tail; private int size; public Lab8_problem1() { this.head = null; this.tail = null; this.size = 0; } //Insert a node at specifc Location public void insertAt(int value, int index) { Node newNode = new Node(); newNode.data = value; if(head == null){ head = newNode; tail = newNode; head.next...