answersLogoWhite

0


Best Answer

void delete_all (list* lst) {

if (!lst !lst->count) return; // sanity check!

while (lst->count) { // repeat until the list is empty

node* tail = lst->tail; // store the current tail

lst->tail = tail->prev; // set the new tail

if (lst->tail) lst->tail->next = NULL; // detach the old tail from the new tail (if there is one)

else (lst->head = NULL); // if there is no tail then here can be no head either

free (tail->data); // release the old tail's data

free (tail); // release the old tail

lst->count--; // update the count

}

}

The above function assumes your list uses the following C-style structures:

typedef struct node_t {void* data; node* next; node* prev; } node;

typedef struct list_t {node* head; node* tail; unsigned count; } list;

User Avatar

Wiki User

7y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write a void function to delete all nodes in a doubly linked list?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is difference between linked list and singly linked list?

Answersingly linked list has the node inserted only at one end. and the pointer corresponds to the next pointer.but in a doubly linked list, the node pointer points to the both previous and the next node.singly linked list has two nodesdoubly linked list has three nodesA doubly linked list makes sense when you need to traverse the list in both directions. You aren't able to do that with a singly linked list.


What is node class?

When creating linked lists (singly- or doubly-linked), the elements in the list are known as nodes. A node class defines how these nodes work. Generally, a node will contain a member pointer to the data it represents (which is typically an abstract data type rather than a concrete data class), as well as a pointer to the next node in the list, and the previous node if it is doubly-linked. The list itself maintains a member pointer to the head node in the list, and the tail node if doubly-linked. In this way, the list is only concerned with the nodes, not the data within those nodes. By the same token the nodes are only concerned with their nearest neighbouring nodes, not the list, nor the data they contain. Similarly, the data is only concerned with itself and other data, it is not concerned with the fact it may be part of a list or a node. All work concerning nodes can then be delegated to the nodes themselves, while any work relating to the data can be delegated to the data. Thus each class plays a small but clearly defined role within the list; no class does any more than it has to, which greatly simplifies the list interface.


Write an algorithm for the implementation of a circular doubly linked list?

Create a new node, making sure it is not allocated locally in the function and thus will not be destroyed when the function execution finishesFill in dataUse the "last node" pointer in the list and copy the "next" pointer location (pointing to the first node) into the new nodes "next" pointerSet the "last node" "next" pointer to point to the new nodeChange the list's "last node" pointer to point to the new nodeFor an example of implementation see: How_you_insert_a_newnode_in_singly_circular_link_list


How do you implement a doubly linked list using only one reference value instead of the usual next and previous references?

store the exor of the previous node address and next node address in each node of single linked list .further exor the nodes to proceed forward or backward as necessary


What is the advantage of doubly linked list over Doubly linked list?

A doubly linked list can be traversed in both directions (forward and backward). A singly linked list can only be traversed in one direction. A node on a doubly linked list may be deleted with little trouble, since we have pointers to the previous and next nodes. A node on a singly linked list cannot be removed unless we have the pointer to its predecessor. On the flip side however, a doubly linked list needs more operations while inserting or deleting and it needs more space (to store the extra pointer).

Related questions

What is difference between linked list and singly linked list?

Answersingly linked list has the node inserted only at one end. and the pointer corresponds to the next pointer.but in a doubly linked list, the node pointer points to the both previous and the next node.singly linked list has two nodesdoubly linked list has three nodesA doubly linked list makes sense when you need to traverse the list in both directions. You aren't able to do that with a singly linked list.


What is node class?

When creating linked lists (singly- or doubly-linked), the elements in the list are known as nodes. A node class defines how these nodes work. Generally, a node will contain a member pointer to the data it represents (which is typically an abstract data type rather than a concrete data class), as well as a pointer to the next node in the list, and the previous node if it is doubly-linked. The list itself maintains a member pointer to the head node in the list, and the tail node if doubly-linked. In this way, the list is only concerned with the nodes, not the data within those nodes. By the same token the nodes are only concerned with their nearest neighbouring nodes, not the list, nor the data they contain. Similarly, the data is only concerned with itself and other data, it is not concerned with the fact it may be part of a list or a node. All work concerning nodes can then be delegated to the nodes themselves, while any work relating to the data can be delegated to the data. Thus each class plays a small but clearly defined role within the list; no class does any more than it has to, which greatly simplifies the list interface.


Write an algorithm for the implementation of a circular doubly linked list?

Create a new node, making sure it is not allocated locally in the function and thus will not be destroyed when the function execution finishesFill in dataUse the "last node" pointer in the list and copy the "next" pointer location (pointing to the first node) into the new nodes "next" pointerSet the "last node" "next" pointer to point to the new nodeChange the list's "last node" pointer to point to the new nodeFor an example of implementation see: How_you_insert_a_newnode_in_singly_circular_link_list


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.


How do you implement a doubly linked list using only one reference value instead of the usual next and previous references?

store the exor of the previous node address and next node address in each node of single linked list .further exor the nodes to proceed forward or backward as necessary


How are nodes linked?

That depends on the linking rules.


What is the advantage of doubly linked list over Doubly linked list?

A doubly linked list can be traversed in both directions (forward and backward). A singly linked list can only be traversed in one direction. A node on a doubly linked list may be deleted with little trouble, since we have pointers to the previous and next nodes. A node on a singly linked list cannot be removed unless we have the pointer to its predecessor. On the flip side however, a doubly linked list needs more operations while inserting or deleting and it needs more space (to store the extra pointer).


What is multiple link?

A (singly) linked list is a data structure of nodes where each node contains a value and a pointer to the next node.A doubly linked list is a data structure where each node has two pointers, one to the next and one to the previous node, so you can traverse the chain of links in both directions.A multiple linked list has several pointers in each node. They are used to traverse the nodes in different orders, for example one set of links ordered by date of birth and another ordered alphabetically by name.


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.


How do you implement a doubly linked list by using singly linked list?

To implement a doubly linked list using a singly linked list, you can create two nodes in each element of the singly linked list - one for the next element and another for the previous element. This way, each node will have access to both its previous and next nodes, effectively creating a doubly linked list structure using a singly linked list implementation.


Algorithm in creating insert method in linked list?

You sort a doubly linked list the same way you sort any other kind of list or array. You implement a procedure to sort the list or array, and that procedure calls the appropriate insert, delete, or move methods of the list or array.


Lymph nodes located in the armpits?

Lymph nodes are located throughout the entire body, linked by the lymphatic vessels.