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

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....
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...
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...
Write a subroutine named swap in C thatswaps two nodes in a linked list. The first...
Write a subroutine named swap in C thatswaps two nodes in a linked list. The first node should not be able to change places. The nodes are given by: Struct nodeEl { int el; struct nodeEl * next; }; typdef struct nodeEl node; The list header (of type node *) is the first parameter of the subroutine. The second and third parameters consist of integers and are the places in the list where the nodes are to change places. The...
Given a linked list of integers, remove any nodes from the linked list that have values...
Given a linked list of integers, remove any nodes from the linked list that have values that have previously occurred in the linked list. Your function should return a reference to the head of the updated linked list. (In Python)
What is a linked data structure? What is a node? What are the benefits of linked...
What is a linked data structure? What is a node? What are the benefits of linked structure? What are the drawbacks of linked structure? What are the differences between singly linked and doubly linked structures? Give examples of when a linked structure could be used.
I've provided a Node class that implements a node of a simple singly-linked list (with .value...
I've provided a Node class that implements a node of a simple singly-linked list (with .value and .next fields), and an empty LinkedList class. Your task is to implement LinkedList.sort(l), where given the node l as the head of a singly-linked list, LinkedList.sort(l) sorts the nodes in the list into ascending order according to the values in the .value field of each node. Your implementation should do an in-place update of the list. It is ok to use a simple...
Write PSEUDOCODE to insert a node at position 2 in a doubly-linked list (assume position follows...
Write PSEUDOCODE to insert a node at position 2 in a doubly-linked list (assume position follows classic indexing from 0 to item_count - 1)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT