answersLogoWhite

0


Best Answer

To insert a new node between two lists, append the new node to the first list, then insert the head node of the second list after the new node.

User Avatar

Wiki User

6y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Program for inserting a new node in between two list in c?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Coding of inserting a node at the begning of the list in C?

newnode->next = first; first= newnode;


Explain any two advantages using Single linked list over Doubly linked list and vice-versa?

Advantages of single linked list: # Decrease in storage space per linked list node # Simpler implementation Advantages of double linked list # Decrease in work when accessing a random node # Decrease in work when inserting or deleting a node


How many pointers will have to be changed if a node is deleted from a linear linked list?

For a singly-linked list, only one pointer must be changed. If the node about to be deleted (let's call it node for the sake of argument) is the head of the list, then the head node pointer must be changed to node->next. Otherwise, the node that comes before the deleted node must change its next pointer to node->next. Note that given a singly-linked node has no knowledge of its previous node, we must traverse the list from the head in order to locate that particular node, unless the node is the head of the list: void remove (List* list, Node* node) { if (!list !node) return; // sanity check!if (list->head == node) {list->head = node->next;} else {Node* prev = list->head;while (prev->next != node) prev = prev->next; // locate the node's previous nodeprev->next = node->next;}} Note that the remove function only removes the node from the list, it does not delete it. This allows us to restore the node to its original position, because the node itself was never modified (and thus still refers to its next node in the list). So long as we restore all removed nodes in the reverse order they were removed, we can easily restore the list. In order to delete a node completely, we simply remove it and then free it:void delete (List* list, Node* node) {if (!list !node) return; // sanity check!remove (list, node);free (node);} For a doubly-linked list, either two or four pointers must be changed. If the node about to be deleted is the head node, then the head node pointer must be changed to n->next and n->next->prev must be changed to NULL, otherwise, n->prev->next becomes n->next. In addition, if the node about to be deleted is the tail node, then the tail node pointer must be changed to n->prev and n->prev->next must be changed to NULL, otherwise, n->next->prev becomes n->prev. Deletion from a doubly-linked list is generally quicker than deletion from a singly linked list because a node in a doubly-linked list knows both its previous node and its next node, so there's no need to traverse the list to locate the previous node to the one being deleted. void remove (List* list, Node* node) {if (!list !node) return; // sanity check!if (list->head == node) {list->head = node->next;node->next->prev = NULL;} else {node->prev->next = node->next; }if (list->tail == node) {list->tail = node->prev;node->prev->next = NULL;} else {node->next->prev = node->prev; }} Again, to physically delete the node we simply remove and then free the node:void delete (List* list, Node* node) {if (!list !node) return; // sanity check!remove (list, node); free (node); }


Difference between circular and single linklist?

A regular linked list will have a pointer to the start of the list, with each node pointing to the next node, and finally the last node points to NULL. In a circular linked-link, the last node will point to the first node, making the list circular. This requires extra checks to ensure that you don't end up going into an infinite loop while traversing the list.


What is a sentinel in programming?

A real-world sentinel is a soldier or guard, someone who keeps watch. In programming, a sentinel is typically a "dummy" node that is used specifically to mark the beginning or end of a data sequence. If we consider a singly-linked list (also known as a forward list), each node points forwards to the next node in the sequence. When we insert a new node into a list, we typically specify the node that will come immediately before it in the sequence because that node also points to the node that will (ultimately) come after it. Thus our algorithm can be encoded as follows: void insert_after (Node* node, Node* prev) { assert (node); assert (prev); node->next = prev->next; prev->next = node; } This is fine until we try and insert a new node at the start of the sequence or try to insert into an empty list. In both cases, p will be a null pointer because there can be no node that can come before the new node, n. Thus the assertion that p is non-null will fail. To cater for this, we must over-complicate the algorithm by testing for each of these special cases and, in order to do so, pass another argument to the function, namely the list itself: void insert_after (List* list, Node* node, Node* prev) { assert (list); assert (node); if (!list->head) { /* cater for empty list */ assert (!prev); list->head = node; node->next = null; } else if (!prev) { /* insert at head of list */ node->next = list->head; list->head = node; } else { node->next = prev->next; prev->next = node; } } However, if we place a sentinel at the beginning of the list, we guarantee that even an empty list will always have at least one node (the sentinel) and that whenever we insert a new node into the list there will always be a node that can come before that new node. Thus our original function works efficiently without having to deal with any special cases. The sentinel node is similar to any other node except that it does not store any data, it simply points to the first node in the sequence or to null if the list is empty.

Related questions

Coding of inserting a node at the begning of the list in C?

newnode->next = first; first= newnode;


Explain any two advantages using Single linked list over Doubly linked list and vice-versa?

Advantages of single linked list: # Decrease in storage space per linked list node # Simpler implementation Advantages of double linked list # Decrease in work when accessing a random node # Decrease in work when inserting or deleting a node


How do you write a algorithm of inserting an element at end of a linked list?

Algorithm to insert an element at the end of a linked listSpecial case: the list is empty.Create a new node for the element, assigning nullptr (0) to its next node.Assign the new node to the head of the list.Return a pointer to the new node and exit.All other cases: the list is not empty.Start at the head node (make it current).Repeat: while the current node has a next node, traverse to that node (make it current).Create a new node for the element, assigning nullptr (0) to its next node.Assign the new node as the current node's next node.Return a pointer to the new node and exit.


How do you use push operation in stack give the exampale of the program?

A stack can be implemented as an array or a list. If an array, simply push the new element onto the end of the array. If a list, point the new node at the head node then make the new node the new head of the list.


How many pointers will have to be changed if a node is deleted from a linear linked list?

For a singly-linked list, only one pointer must be changed. If the node about to be deleted (let's call it node for the sake of argument) is the head of the list, then the head node pointer must be changed to node->next. Otherwise, the node that comes before the deleted node must change its next pointer to node->next. Note that given a singly-linked node has no knowledge of its previous node, we must traverse the list from the head in order to locate that particular node, unless the node is the head of the list: void remove (List* list, Node* node) { if (!list !node) return; // sanity check!if (list->head == node) {list->head = node->next;} else {Node* prev = list->head;while (prev->next != node) prev = prev->next; // locate the node's previous nodeprev->next = node->next;}} Note that the remove function only removes the node from the list, it does not delete it. This allows us to restore the node to its original position, because the node itself was never modified (and thus still refers to its next node in the list). So long as we restore all removed nodes in the reverse order they were removed, we can easily restore the list. In order to delete a node completely, we simply remove it and then free it:void delete (List* list, Node* node) {if (!list !node) return; // sanity check!remove (list, node);free (node);} For a doubly-linked list, either two or four pointers must be changed. If the node about to be deleted is the head node, then the head node pointer must be changed to n->next and n->next->prev must be changed to NULL, otherwise, n->prev->next becomes n->next. In addition, if the node about to be deleted is the tail node, then the tail node pointer must be changed to n->prev and n->prev->next must be changed to NULL, otherwise, n->next->prev becomes n->prev. Deletion from a doubly-linked list is generally quicker than deletion from a singly linked list because a node in a doubly-linked list knows both its previous node and its next node, so there's no need to traverse the list to locate the previous node to the one being deleted. void remove (List* list, Node* node) {if (!list !node) return; // sanity check!if (list->head == node) {list->head = node->next;node->next->prev = NULL;} else {node->prev->next = node->next; }if (list->tail == node) {list->tail = node->prev;node->prev->next = NULL;} else {node->next->prev = node->prev; }} Again, to physically delete the node we simply remove and then free the node:void delete (List* list, Node* node) {if (!list !node) return; // sanity check!remove (list, node); free (node); }


How to write an actual algorithm for inserting an element in a linked list?

Insert newNode into a linked list after targetNode Node currentNode = root while currentNode != targetNode currentNode = currentNode.next newNode.next = currentNode.next currentNode.next = newNode


Difference between circular and single linklist?

A regular linked list will have a pointer to the start of the list, with each node pointing to the next node, and finally the last node points to NULL. In a circular linked-link, the last node will point to the first node, making the list circular. This requires extra checks to ensure that you don't end up going into an infinite loop while traversing the list.


Can we use doubly linked list as a circular linked list?

Yes. The tail node's next node is the head node, while the head node's previous node is the tail node.


What does the word 'node' mean in c plus plus?

A node is an object in some kind of linked structure, such as a linked list or tree. Nodes can be allocated off of the free list and added to the linked list or tree at hand, and then filled in with the data needed to represent some piece of information. Conversely, they can be unlinked from the linked list or tree, and then returned to the free list when they are no longer needed. Often, there is no direct variable of pointer in the program's scope that represents the node; rather, the node can be found by traversing a list of linkages, list or tree, in the applicable data structure. There are other definitions of "node", but this is the most common.


How do you write a C program to add a node in a linked list after a given node?

Given the 'given node' G, after which the 'new node' N is to be added: 1. For a single-linked list, where the node structure contains a pointer to the next node in the list (or a dedicate invalid pointer value, such as NULL) in a field named 'next', you set N.next = G.next; G.next = &N; 2. For a double-linked list of the same characteristic as above, but with an added 'prev' pointer to point to the previous node, you'd set N.next = G.next; G.next = &N; N.prev = &G;if (N.next) N.next.prev = &N;


What is a simple node?

In computer programming, a simple node is a structure that holds two pointers. The first pointer refers to the data that is indirectly associated with the node, while the other refers to the next node in the sequence. Simple nodes are typically used in forward lists (singly-linked lists) where we keep track of the head node only. The last node in the list refers to NULL as there can be no next node after the last node. We can insert new data into the list knowing only the address of the data. A new node is constructed which then refers to the data and to the current head of the list. The new node then becomes the head of the list. Insertions at the head are performed in constant time. Inserting anywhere else in the list requires that we traverse the nodes recursively, following each node's next pointer, starting from the head, stopping at the node that should come after the new node. That node then becomes the new node's next node (if the node reaches the end of the list, the next node is NULL). Since we traversed recursively, we can return the newly inserted node to the caller, which can then be assigned to the caller's next node. All other recursions simply return the node we traversed to, thus leaving the caller's next node unchanged. The following example shows a simple node implementation for an ordered list of type int: typedef struct Node_t { // a simple node int* data; Node_t* next; } Node; Node* insert (int* data, Node* node) { // insert data if (!node *node->data > *data ) { // insertion point reached (end recursions) Node* pnode = malloc (sizeof (Node)); // create a new node pnode->data = data; // attach the data pnode->next = node; // refer to the next node (may be NULL) return pnode; // return the new node so the previous node can be updated } // if we get this far, the given node comes before the new data // delegate to the next node, the return value is the new or existing next node node->next = insert (data, node->next); return node; // return the node (it remains the previous node's next node) } void print_all (Node* node) { // print the given node and all that follow it while (node) { printf ("%d ", *node->data); node = node->next; } } Node* destroy_all (Node* node) // destroy the given node and all that follow it while (node) { Node* next = node->next; // refer to the next node (may be NULL) free (node->data); // release the current node's data free (node); // release the node itself node = next; // make the next node current (may be NULL) } return node; // always NULL } int main(void) { // The test program... srand ((unsigned) time (NULL)); // seed a random number generator Node* head = NULL; // the head node of the list (initially NULL for an empty list) for (int i=0; i<10; ++i) { // insert 10 random integers int* pint = malloc (sizeof (int)); // allocate a new integer on the free store if (!pint) break; // allocation failed, stop inserting! *pint = rand () % 10; // assign a random integer value (range 0:9) head = insert (pint, head); // insert the data (the return value is the new or existing head) } print_all (head); // print all the nodes starting from the head node head = delete_all (head); // destroy all the nodes starting from the head (returns NULL) return 0; }


What is a sentinel in programming?

A real-world sentinel is a soldier or guard, someone who keeps watch. In programming, a sentinel is typically a "dummy" node that is used specifically to mark the beginning or end of a data sequence. If we consider a singly-linked list (also known as a forward list), each node points forwards to the next node in the sequence. When we insert a new node into a list, we typically specify the node that will come immediately before it in the sequence because that node also points to the node that will (ultimately) come after it. Thus our algorithm can be encoded as follows: void insert_after (Node* node, Node* prev) { assert (node); assert (prev); node->next = prev->next; prev->next = node; } This is fine until we try and insert a new node at the start of the sequence or try to insert into an empty list. In both cases, p will be a null pointer because there can be no node that can come before the new node, n. Thus the assertion that p is non-null will fail. To cater for this, we must over-complicate the algorithm by testing for each of these special cases and, in order to do so, pass another argument to the function, namely the list itself: void insert_after (List* list, Node* node, Node* prev) { assert (list); assert (node); if (!list->head) { /* cater for empty list */ assert (!prev); list->head = node; node->next = null; } else if (!prev) { /* insert at head of list */ node->next = list->head; list->head = node; } else { node->next = prev->next; prev->next = node; } } However, if we place a sentinel at the beginning of the list, we guarantee that even an empty list will always have at least one node (the sentinel) and that whenever we insert a new node into the list there will always be a node that can come before that new node. Thus our original function works efficiently without having to deal with any special cases. The sentinel node is similar to any other node except that it does not store any data, it simply points to the first node in the sequence or to null if the list is empty.