Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 {

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;

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Transaction Processing

Authors: Philip M. Lewis, Arthur Bernstein, Michael Kifer

1st Edition

0201708728, 978-0201708721

More Books

Students also viewed these Databases questions

Question

Effective Delivery Effective

Answered: 1 week ago