Question

In: Computer Science

Suppose a linked list of 20 nodes. The middle node has a data –250. Write the...

Suppose a linked list of 20 nodes. The middle node has a data –250. Write the pseudocode to replace the middle node of the linked list with a new node and new data. Assume that the list's head pointer is called head_ptr and the data for the new node is called entry

Solutions

Expert Solution

I am explaining the approach step by step because according to me this would be the easiest way to understand the code. First of all

Step 1:- Find the node which has to be replaced

bool search(Node* head, int x)
{
Node* current = head; // Initialize current
while (current != NULL)
{
if (current->key == x) // here the current key value or the value of x is given as 250
return true;
current = current->next;
}
return false;
}
Step 2:- Now after finding the node we need to replace it with a new node and a new value so at first of this step we need to delete the node that we have found out

// Find the middle and previous of middle.
// To store previous of slow_ptr   
struct Node* prev;
while (fast_ptr != NULL
&& fast_ptr->next != NULL) {
fast_ptr = fast_ptr->next->next;
prev = slow_ptr;
slow_ptr = slow_ptr->next;
}
  
// Delete the middle node
prev->next = slow_ptr->next;
delete slow_ptr;
  
return head;
}
  

Now this will delete the required node

Step 3:- in this step now we will make a new node and a new value as 'entry' in the node

// structure of a node
struct Node {
int entry; // as said to assume in the question
Node* next;
};
  
// function to create and return a node
Node* getNode(int entry)
{
// allocating space
Node* newNode = (Node*)malloc(sizeof(Node));
  
// inserting the required data
newNode->data = entry;
newNode->next = NULL;
return newNode;
}

Step 4:- now we have to just insert the new node that we have created

// function to insert node at the middle
// of the linked list
void insertAtMid(Node** head_ptr,intx)
{
// if list is empty
if (*head_ptr == NULL)
*head_ptr =getNode(x);
else {
  
// get a new node
Node* newNode = getNode(x);
  
Node* ptr = *head_ptr;
int len = 0;
  
// calculate length of the linked list
//, i.e, the number of nodes
while (ptr != NULL) {
len++;
ptr = ptr->next;
}
  
// 'count' the number of nodes after which
// the new node is to be inserted
int count = ((len % 2) == 0) ? (len / 2) :
(len + 1) / 2;
ptr = *head_ptr;
  
// 'ptr' points to the node after which
// the new node is to be inserted
while (count-- > 1)
ptr = ptr->next;
  
// insert the 'newNode' and adjust the
// required links
newNode->next = ptr->next;
ptr->next = newNode;
}
}
  

this will add a new node with a new value in the linked list that is it will replace the value 250 of the middle node and add a new node with a value assumed to be as 'entry'.Since u asked for just pseudocode I have just provided the code of the specific operations this is not the full code. I have tried to keep my approach as simple as possible, the code I have provided I have written in my code editor and tested it.Hope my work helps u

PLEASE PUT THE THUMBS UP BUTTON IF U LIKE IT. THANK U VERY MUCH.


Related Solutions

Consider a linked list whose nodes are objects of the class Node: class Node {    ...
Consider a linked list whose nodes are objects of the class Node: class Node {     public int data;     public Node next; } prev references a node n1 in the list and curr references the node n2 that is right after n1 in the list. Which of the following statements is used to insert a new node, referenced by newNodebetween prev and curr? Group of answer choices newNode.next = curr; prev.next = newNode; newNode.next = head; head = newNode;...
Data Structures on Java Basic Linked List exercises a. Suppose x is a linked-list node and...
Data Structures on Java Basic Linked List exercises a. Suppose x is a linked-list node and not the last node on the list. What is the effect of the following code fragment? x.next = x.next.next b. Singly Linked List has two private instance variables first and last as that point to the first and the last nodes in the list, respectively. Write a fragment of code that removes the last node in a linked list whose first node is first....
In C++, Write a function to reverse the nodes in a linked list. You should not...
In C++, Write a function to reverse the nodes in a linked list. You should not create new nodes when you reverse the the linked list. The function prototype:          void reverse(Node*& head); Use the following Node definition: struct Node {    int data;    Node *next; }
1a) Write the start of the class declaration for a node in a linked list (give...
1a) Write the start of the class declaration for a node in a linked list (give the name of the class and the instance variables). The name of the node should be SpecialNode. The data in each SpecialNode will include both a String and a Song object. b)Using the SpecialNode class you created in question above, write a constructor forSpecialNode that has three parameters and initializes all the SpecialNode instance variables. c) Write a line of code to instantiate a...
1.Please write a C++ program that counts the nodes in a linked list with the first...
1.Please write a C++ program that counts the nodes in a linked list with the first node pointed to by first. Also please explain. 2. Write a program to determine the average of a linked list of real numbers with the first node pointed to by first. 3. Determine the computing times of the algorithms in question 1 and 4. Write a program to insert a new node into a linked list with the first node pointed to by first...
Write a Java program to implement a double-linked list with addition of new nodes at the...
Write a Java program to implement a double-linked list with addition of new nodes at the end of the list. Add hard coded nodes 10, 20, 30, 40 and 50 in the program. Print the nodes of the doubly linked list.
Write down a C program which will create a list (simple linear linked list) of nodes....
Write down a C program which will create a list (simple linear linked list) of nodes. Each node consists of two fields. The first field is a pointer to a structure that contains a student id (integer) and a grade-point average (float). The second field is a link. The data are to be read from a text file. Your program should read a file of 10 students (with student id and grade point average) and test the function you wrote...
In C++, write a member method delete() that deletes a node from a linked list at...
In C++, write a member method delete() that deletes a node from a linked list at a random position. (It should first randomly generate that position. and then delete that node).
Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class in C++ that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value...
Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value x if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT