answersLogoWhite

0


Best Answer
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 node
prev->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);
}
User Avatar

Wiki User

6y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

One in each element.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How many pointers will have to be changed if a node is deleted from a linear linked list?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is the minimum number of additional pointers will be needed to reverse a singly linked list?

3 pointers...


What is Linear and non linear list in data structure?

Linear : Traversal is linear .. ex: array,linked lists,stacks,queues NoN-linear: Traversal is not linear.. ex:trees,graphs imagine the situation of searching of particular element..in above scenarious..then u will understand easily.. Linear : Traversal is linear .. ex: array,linked lists,stacks,queues NoN-linear: Traversal is not linear.. ex:trees,graphs imagine the situation of searching of particular element..in above scenarious..then u will understand easily.. Linear : Traversal is linear .. ex: array,linked lists,stacks,queues NoN-linear: Traversal is not linear.. ex:trees,graphs imagine the situation of searching of particular element..in above scenarious..then u will understand easily..


Where linked list is used in computer?

Linked lists are dynamic linear data containers.They are linear because each item has one adjacent item in each direction (there is a predecessor, or none, and a successor, or none, to each item, but never more than one in each direction).They are dynamic because the size is not generally fixed and potentially infinite. To manage such a dynamic structure, memory for new items is allocated at runtime, and the different items are connected, or linked, through pointers.In a single linked list, a single pointer points from one item to the next item.In a double linked list, one pointer points from one item to the next item, and a second pointer points to the previous item in the list.


W difference between a linear linked list and a circular linked list?

I would say that there is no such thing as a circular queue. The point of a circular data structure is to allow the end to loop around to the beginning. Since you can only remove items from the beginning of a queue or add them to the front, having these two items linked has no purpose nor benefit.


What is the main reason behind linked-list?

Linear list for example is array, linked list, stack, ... It is called linear because all elements in those data structures are in linear sequence. We have first and the last elements in the list. If there is N elements in list, we know that all elements are in such sequence na, na+1. Non-linear structures would be trees and graphs.

Related questions

What is linked file organization?

The next record in a linked list is found at the address stored in the record. Records are added at any location in the DASD(Direct Access Storage Device) and pointers adjusted to include them. Deletions are not erased, but pointers changed to omit the deleted record. Just like a normal linked list has a value part in its structures, here the linked list structure can have multiple value parts.


What is linked file?

The next record in a linked list is found at the address stored in the record. Records are added at any location in the DASD(Direct Access Storage Device) and pointers adjusted to include them. Deletions are not erased, but pointers changed to omit the deleted record. Just like a normal linked list has a value part in its structures, here the linked list structure can have multiple value parts.


What kind of data is linked list useful for and what not very useful for?

A linked list is useful for data which will be changed very often. The linked nature of linked lists makes them ideal for inserting and removing elements, as it requires only changing one or two pointers.


What is the minimum number of additional pointers will be needed to reverse a singly linked list?

3 pointers...


Disadvantages of linked lists in c?

Because the C programming language leaves the responsibility for memory allocation and pointers entirely with the programmer, the disadvantage of linked lists over some other linear data structures (such as arrays) is that the bear a risk or memory leaks and invalid pointers. The fact that the size of a linked list is generally not deterministic is also commonly viewed a disadvantage over statically linked linear containers (e.g. arrays) in some systems, primarily in embedded systems. Compared to containers of higher order (such as trees or hash tables), search operations in a linked list are generally slower. Compared to a double linked list, removal and insertion of items (except head and tail) is generally more expensive.


How will you represent the linked list?

With pointers pointing to the next element.


Difference between linear linked list and circular linked list?

LINEAR STRAIGHT CIRCULAR CURVED


What are the differences between singly-linked doubly-linked and circularly-linked lists?

The difference is how many pointers each node has, and what they are pointing to. A linked list is comprised of "Nodes" each node contains data as well as 1 or more pointers. A singly linked list has one pointer per node, and a doubly linked list has 2 pointers per node. Some programs use several pointers per node. The purpose of these pointers is to hold the list together. In a singly linked list, you can view a node and can then move on to the next node that it is pointing to until you've passed through them all. A doubly-linked list would have a pointer to the next node as well as to the previous node. Thus you can move forward and backward through the list. A circularly-linked list doesn't necessarily have a set number of pointers because it simply means that the last node points to the first node creating a big circle. A non-circularly-linked list would not contain this last to first pointer and thus you would eventually reach the end of the list and stop.


What are the two basic ways in representing linear structures in memory?

Data structures are classified as either linear or non linear, a data structure is said to be linear if its elements form a sequence, or, in other words, a linear list, there are two basic ways of representing such linear structures in memory, one way is to have the linear relationship between the elements represented by means of sequential memory locations, these linear structures are called arrays, the other way is to have the linear relationship between the elements represented by means of pointers or links, these linear structures are called linked lists.


Is Linked list representation require an additional N words of memory for a list of pointers?

yes


What are amino acids linked in linear fashion is a?

Polypeptide


Why doubly linked list is a linear linked list?

Within the vast landscape of data structures, the doubly linked list stands out for its distinct architecture. It's a sequence of nodes, where each node has a data element and two pointers. One pointer gestures towards the next node, while the other points to the previous one. This bi-directionality permits traversal in both forward and backward directions, a feature that its cousin, the singly linked list, lacks. However, a question arises: Why term it 'linear'? In the world of data structures, 'linear' refers to a sequence where elements line up one after the other, like beads on a string. Arrays, queues, and all types of linked lists fall into this category. Even with its dual pointers, a doubly linked list remains linear. It has a clear start and end, and no node connects with multiple others simultaneously. While doubly linked lists radiate flexibility, especially with bidirectional traversal, they aren't without their trade-offs. The additional 'previous' pointer means extra memory consumption for each node. Plus, its implementation can be slightly more intricate than a singly linked list. Yet, when the need arises for efficient insertions and deletions at various points, doubly linked lists rise to the occasion. They strike a balance between flexibility and complexity, making them a valuable tool in a programmer's toolkit.