Write the method check List Ends Symmetry that receives a double linked list and
ID: 3864309 • Letter: W
Question
Write the method check List Ends Symmetry that receives a double linked list and an integer number k. the method checks if the double linked list has identical k elements going forward from the first element and backwards from the last one. the method returns true if they are identical, and false otherwise. the method signature is: public Boolean check List Ends SimCity (Double Linked Listdl, int k) If dl = A doubleheadarrow B doubleheadarrow C doubleheadarrow D doubleheadarrow B doubleheadarrow A and k = 2, then the method should return true. If k = 3, it should return false, since C dues not equal D.Explanation / Answer
/*
First traverse linked list and store values in stack
Then again traverse in stack and compare value with stack entry
if there is match increase count variable by 1
and finally compare count with given K , if they are equal function will return true else flase
*/
public <T> boolean checkListendsSymmetry(DoubleLinkedList<T>dl,int k)
{
//calculating size of link list and storing in integer value
int count ;
int x = dl.size();
Stack <T> st = new Stack<T>(x);
ListIterator <T>lt = dl.listIterator()
//copying value to stack
while(lt.hasNext())
{
st.push(lt.next());
}
for (int i =0 ; i <=x;i++)
{
while(lt.hasNext())
{
if(lt.next().equals(st.peek())
{
count++;
}//if
}//while
st.pop();
}//for
return count;
}//method
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.