Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Question: Define the method peek for the sorted list implementation of the ADT priority queue. Hint: We have already discussed insert and remove methods implementation

Question: Define the method peek for the sorted list implementation of the ADT priority queue. Hint: We have already discussed insert and remove methods implementation in this module. c++ language

LinkedList merge( LinkedList l1 , LinkedList l2 )

{

// create a new Linked List

LinkedList ans = new LinkedList();

// get the length of list 1

int len1 = l1.getLength();

// get the length of list 2

int len2 = l2.getLength();

// store the index of current element in list 1

int i = 0;

// store the index of current element in list 2

int j = 0;

while( i <= len1 && j <= len2 )

{

// if the current node of list 1 is smaller than current node of list 2

if( l1.getEntry(i) <= l2.getEntry(j) )

{

// add the current element of list 1

ans.insertSorted( l1.getEntry(i) );

i++;

}

else

{

// add the current element of list 2

ans.insertSorted( l2.getEntry(j) );

j++;

}

}

// add the remaining elements of list 1 to ans

for( ; i <= len1 ; i++ )

// add the current element of list 1

ans.insertSorted( l1.getEntry(i) );

// add the remaining elements of list 2 to ans

for( ; j <= len2 ; j++ )

// add the current element of list 2

ans.insertSorted( l2.getEntry(j) );

return ans;

}

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