answersLogoWhite

0


Best Answer

Firstly, thanks for trying solve this algorithm.

I will rewrite the wrong way and debug it in the next line, and the causes in th last line , O right?


wrong:void delete(int n).
debug:void delete_node(node* list, int n).
causes:
-You have to include the name of the list from which you want to delete a node. -you used delete as a name of the function, and that is impossible because delete is a predefined function, use delete_node for example.




wrong:you didn't affect the address of the list to the temporary pointer named here P.
debug:p=list.
causes:
-one of the disadvantages of the chained list -unlike contiguous list-is: to arrive to the the required node, we have to traverse all previous nodes to get the required one, therefor we use that temporary pointer, and this latter mast have to start from the first node names here list.


the rest is right, try this code, I think it works::


void delete_node(node *list, int n)
{
node *p;
p=list;
int i=1;
while(i
{
p=p->next;
++i;
}
node *q;
q=p->next;
p->next=q->next;
free(q);
}



Best regards of InfoMan.






User Avatar

Wiki User

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

Wiki User

14y ago

Ya We can Do that actually.

1. Copy the content of the next node in to the node that has to be deleted.

2. Assign the next pointer of the newly copied node of to the next pointer of the node from the content has been copied.

3. Delete the node from the data has copied.


Another way of above problem is do a memcpy on current node from next node. and delete the next node would be more faster and optimized.

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

Let's assume you're using a custom LinkedList (as the one build into Java has this implemented for you).

class LinkedList {

LinkedListNode root;

// Remove the nodes which contain data equal to obj

void deleteNode(Object obj) {

// special case for root

if( root.data.equals(obj) ) {

root = root.next;

}

LinkedListNode current = root;

// iterate through list looking for obj

while( current.next != null ) {

// match found

if( current.next.data.equals(obj) ) {

// cut out the node

current.next = current.next.next;

}

current = current.next;

}

}

private class LinkedListNode {

Object data;

LinkedListNode next;

}

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you you delete a node in singly linked list only pointer to that node is given in C?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Explain the features of doubly linked list?

Well, in a singly linked list you can only move forward, if the pointer you seek is behind your current position you'll have to cross the hole list to get there. In a doubly linked list you can simply move back as well as forward.... hope this helps...


How do you copy all the elements from a given stack implemented in a linked list to a new one and returns the pointer to the new stack?

LIFO


Disadvantages of circular link list as compared to singly link list?

As compared with doubly-linked lists, one disadvantage is that singly-linked lists can only be efficiently traversed in one direction. Finding the (n - 1)th element, given only a pointer to the nth, requires scanning the list from the first element up to the (n - 1)th. Thus (if memory is too limited to permit tricks such as creating a reversed list) to iterate over the entire list in reverse order would require n + (n - 1) + ... + 1 = n(n + 1)/2 link traversals, which grows quadratically with n. Obviously for a doubly-linked list the time merely grows linearly with n.A down-side of all linked lists versus arrays is that random access can be inefficient; the time taken to find an element with a randomly chosen index grows in direct proportion to the list's length (since we must scan from one of the ends). In contrast, an array allows us to index directly to the elements with simple pointer arithmetic, in a time independent of the array's size - at least in an idealised environment.


What are the applications for circular linked lists?

A singly-linked circular list is useful for implementing queue data structures with minimum overhead. Normally we implement a queue with two pointers: one to the tail for insertions and one to the head for extractions. With a circular list we only need to maintain a single pointer to the tail because the tail always points "forwards" to the head (instead of null as it normally would), thus achieving constant-time access to both the head and tail via a single pointer. Circular linked lists are generally useful wherever "wraparound" is necessary. That is, from any given node in the list, we can traverse forwards with the guarantee that we will eventually arrive back at that same node. With doubly-linked circular lists we have the advantage of traversing in either direction (bi-directional traversal).


How do you delete a node with two children in binary tree using c?

Use a recursive function. void delete_node (node* n) { /* ensure the given pointer is valid */ if (n==NULL) return; /* recursively delete the left child */ delete_node (n->left); n->left = NULL; /* recursively delete the right child */ delete_node (n->right); n->right = NULL; /* delete the data (assuming the data is a pointer)*/ free (n->data); n->data = NULL; /* finally, delete the node itself */ free (n); n=NULL; }

Related questions

Explain the features of doubly linked list?

Well, in a singly linked list you can only move forward, if the pointer you seek is behind your current position you'll have to cross the hole list to get there. In a doubly linked list you can simply move back as well as forward.... hope this helps...


How do you copy all the elements from a given stack implemented in a linked list to a new one and returns the pointer to the new stack?

LIFO


Disadvantages of circular link list as compared to singly link list?

As compared with doubly-linked lists, one disadvantage is that singly-linked lists can only be efficiently traversed in one direction. Finding the (n - 1)th element, given only a pointer to the nth, requires scanning the list from the first element up to the (n - 1)th. Thus (if memory is too limited to permit tricks such as creating a reversed list) to iterate over the entire list in reverse order would require n + (n - 1) + ... + 1 = n(n + 1)/2 link traversals, which grows quadratically with n. Obviously for a doubly-linked list the time merely grows linearly with n.A down-side of all linked lists versus arrays is that random access can be inefficient; the time taken to find an element with a randomly chosen index grows in direct proportion to the list's length (since we must scan from one of the ends). In contrast, an array allows us to index directly to the elements with simple pointer arithmetic, in a time independent of the array's size - at least in an idealised environment.


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 the advantage of doubly linked list over singly linked list?

It's not that one is better than the other. They are used in different circumstances. A linear linked list is used like an array, with the added benefits of random insertion/removal of elements, etc. A circular linked list is often used as a buffer where one portion of the program produces data and another consumes it, such as in communications.


What are the applications for circular linked lists?

A singly-linked circular list is useful for implementing queue data structures with minimum overhead. Normally we implement a queue with two pointers: one to the tail for insertions and one to the head for extractions. With a circular list we only need to maintain a single pointer to the tail because the tail always points "forwards" to the head (instead of null as it normally would), thus achieving constant-time access to both the head and tail via a single pointer. Circular linked lists are generally useful wherever "wraparound" is necessary. That is, from any given node in the list, we can traverse forwards with the guarantee that we will eventually arrive back at that same node. With doubly-linked circular lists we have the advantage of traversing in either direction (bi-directional traversal).


How do you delete a node with two children in binary tree using c?

Use a recursive function. void delete_node (node* n) { /* ensure the given pointer is valid */ if (n==NULL) return; /* recursively delete the left child */ delete_node (n->left); n->left = NULL; /* recursively delete the right child */ delete_node (n->right); n->right = NULL; /* delete the data (assuming the data is a pointer)*/ free (n->data); n->data = NULL; /* finally, delete the node itself */ free (n); n=NULL; }


Write down the algorithm to delete a node from doubly linked list whose data item is given?

1. Find the element is the list. Let pointer 'p' point to it. 2. Delete it from the list: if (p->Prev) p->Prev->Next = p->Next; else List->First = p->Next; if (p->Next) p->Next->Prev = p->Prev; else List->Last = p->Prev; 3. Release the memory associated with it.


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); }


C program pointers to pointers examples?

Pointer to Pointer is a double pointer, denoted by (**). Pointer stores the address of the variable and pointer to pointer stores the address of a pointer variable and syntax can be given as int **ptr2ptr;


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.


How do you delete a user profile on your computer when not given the option to delete?

withdraw inboxserver