Assume you have a linked list of integer data.
Q1: Write a method to count the data in the linked list
Q2: Assume that the data in the linked list are sorted. Write a method to search for an element x and calculate its complexity Big‐Oh
Q1 -
The algorithm to count data in a linked list
CountNode(Node PTR)
1. Count = 0
2. Node temp_ptr = PTR
3. While temp_ptr is not
null
4. do
5. Count
= Count + 1
6. temp_ptr
= temp_ptr->next
7. end
8. Return Count
C Language equivalent code to implement the method
int countNodes(Node **head)
{
int count = 0;
Node* ptr = *head;
while(ptr)
{
count++;
ptr
= ptr->next;
}
return count;
}
Java Language equivalent code to implement the method
public int getCount()
{
Node temp = head;
int count = 0;
while (temp != null)
{
count++;
temp = temp.next;
}
return count;
}
/*******************************************************/
Q2 -
The algorithm to count data in a linked list
SearchNode(Node PTR, Key)
1. Node Result
2. Node temp_ptr = PTR
3. While temp_ptr is not
null
4. do
5. if
temp_ptr is key
6. Result
= temp_ptr
7. end
if
8. temp_ptr
= temp_ptr->next
9. end loop
10. Return Count
C Language equivalent code to implement the method
Node* searchNode(Node **head, int key)
{
Node* result =
NULL;
Node* ptr = *head;
while(ptr)
{
if(ptr->data
== key)
{
result
= ptr;
}
ptr
= ptr->next;
}
return result;
}
Java Language equivalent code to implement the method
public boolean search(Node head, int x)
{
Node current = head; //Initialize current
while (current != null)
{
if (current.data == x)
return true; //data found
current = current.next;
}
return false; //data not found
}
Since Linked list are unable to caluclate array index by simple
performing arithmetic opeartions, hence it is not possible to get
an array index for binary search.
Thus the only searching technique that can be used in the case of a
linked list is linear search which has a time complexity of
O(n).
Assume you have a linked list of integer data. Q1: Write a method to count the...
Assume you have a linked list of integer data pointed by head (p-head). head e2 next nextー next next Qu: Write the recursive method that duplicates (make a separate copy) the linked list (2 Points) Q2: Write the method that sort the linked list in increasing order (from smallest to the largest) (2 Points) Q3: Write the method that returns true if all the elements of the linked list are different. It return false otherwise (1 Point)
Write a JAVA contains method for a linked implementation of a sorted list. #This method is written from the implementation perspective, meaning it would go inside of the LinkedSortedList class. This means we have access to firstNode and numberOfEntries. #write an efficient solution. This will involve directly accessing the linked structure rather than only invoking existing methods. You can assume T is Comparable. #The method header is: public boolean contains(T anEntry)
Q2 [15 pts] Linked list: For this question use class Node.java Assume this file implements a node in a linked list. It has the following public data members: ➢ int data ➢ Node next Write class Node and Class single linked list then write the following functions: 2.2 Remove duplicates from an unsorted linked list Write a Java method removeDuplicates() which takes a list and deletes any duplicate nodes from the list. The list is not sorted. Example: if the...
*********************Java recursion********************** Plz help me write a method in java that traverse Through a integer linked list recursively . The method returns a string with every element in the list and a space in between each element in reverse order. THE METHOD DOES NOT REVERSE THE LINKED LIST. IT JUST RETURNS A STRING CONTAINING THE ELEMENTS OF THE LIST IN REVERSE ORDER. ASSUME ALL THE ELEMENTS IN THE LINKED LIST ARE int... method signature is something like : public String...
Java 1. Write a method to search for and remove an element from a linked list, thereby returning the data portion of the node. 2. Making use of both Stacks and Queues, write a function that will determine whether or not a String is a palindrome
Given a linked list of integers, write a method that computes and prints the pairwise sums which is illustrated below with an example. If the linked list has values 5,3,2,9,3,15,22 from head to tail, the pairwise sums would be 8,5,11,12,18,37 which is the sum of two consecutive values from left to right. Notice the output will be of size one less than the original. You will output the pairwise sums exactly as above on a single line, comma separated. If...
In JAVA Recursive Methods For exercises 16 to 18 assume we have a sorted linked list of Integer. The start of the linked list is referenced by values which, of course, is of type LLNode. For example purposes, assume values points to a list containing 3, 6, 6, 9, 12, 15, 18, 19, 19, and 20. You can and should assume the list in question is sorted in nondecreasing order. For each of these exercises you should also create a...
C# please Write method to search a node in linked list using ID recursively, Write method to search a node in linked list using NAME recursively
Write a method max() which take a generic singly linked list as an argument and returns the maximum element reference in the list. If the list is empty, please throw an empty collection exception. The method prototype is defined as follows. Please write down your code in the method body enclosed in braces: public static <T extends Comparable<T>> T max(SingleLinkedList<T> list) throws EmptyCollectionException { } 2) What is the big O notation of the max method? a) O(N)...
Assume you are working with a stack implementation of the linked list definition pasted below, write a member method “pop”. The method should return a value (in the “popped” node). Assume the existence of the node references called “TheStack” and “Top”. These references point to the start (or bottom) and top of the stack (or back of the list). ------- Definition: class node { boolean data; node link; }