answersLogoWhite

0


Best Answer

void

delete_last (node **head)

{

node *tmp = *head;

node *prev = NULL;

if (!tmp) {

} else if (!tmp->next) {

free(tmp);

*head = NULL;

} else {

while (tmp->next) {

prev = tmp;

tmp = tmp->next;

}

prev->next = NULL;

free(tmp);

}

return;

}

User Avatar

Wiki User

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

Wiki User

14y ago

There are 3 cases to consider when deleting a node from a linked list (a singly-linked-list is assumed to keep things simple)

1. The node is the first node in the list (there are no other nodes pointing to it)

2. The node is somewhere in the middle of the list (there is a node pointing to it and it also points to the node after it)

3. The node is the last node in the list (it does not point to another node)

So consider a list like this: A->B->C

Before deleting any nodes, you must copy their memory location into a temporary pointer, otherwise you will no longer have any way to access them and will not be able to free their memory, creating a memory leak.

Case 1. To delete A, we simply update the pointer to the head of the list so that it now points to B, then delete node A.

Case 2. To delete B, we must have a reference to A, so that we can change A's "next" pointer to point to C instead. This can be done by traversing the list with a "current" and "previous" pointer, such that every time you move down the list with the current pointer you update the previous pointer to the current pointers previous value. When current = B, previous should = A. Then you say that previous(A)->next = current(B)->next, and then delete current(B).

Case 3. This can actually be done in the same manner as case 2. Since the last element in the list should point to null, when you set previous(B)->next = current(C)->next this should result in B pointing to null, then delete current(C)

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Use a while loop to traverse at the end of the list and use the condition as pointer not equal to null.

Something like:

function getLastNode(LinkedList *list)

{

if(list == null)

return null;

while(list->next != null)

{

list = list->next; }

// list now points to the last node

return list;

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you delete last element from link list?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Which function removes last element in a list?

If you are using the doubly-linked list from the STL library, then the function call:name_of_list.push_back();should delete the last element.


Why you use doubly link list?

In a doubly linked list, you can iterate backwards as easily as forwards, as each element contains links to both the prior and the following element. You can also insert or delete an element without needing to iterate and remember the prior element's link. This comes at a cost. You are adding storage to each element for the second link, and you are adding processing overhead to the insert and delete operation. You have to determine the tradeoff.


How do you delete an ith element in singly linked list?

You will need to traverse the list i times to get to the element you want deleted. Each time you go thru the list you will need to remember the forward pointer from the previous element because when you get to the element you want to delete the previous forward pointer needs to be pointed to the I + 1 element of the list.


An algorithm of deleting linked list?

To delete a linked list walk through the list and delete the memory allocated to each element, remembering the next element address, and then iterating or recursing the process using the next element address, until the next element address is null.


Write a program to delete an array element in c?

Enter how many elements you want to enter: 4 Enter the array elements: 10 20 30 40 Enter the location, where you want to delete: 3 Deleted value : 30 The new element list: 10 20 40

Related questions

Assuming an emty list what operations are used to delete and insert a data?

Delete is not possible for an empty list, insert is something like this: Create a new list-element. Register it as the first element of the list. Register it as the last element of the list.


Which function removes last element in a list?

If you are using the doubly-linked list from the STL library, then the function call:name_of_list.push_back();should delete the last element.


Why you use doubly link list?

In a doubly linked list, you can iterate backwards as easily as forwards, as each element contains links to both the prior and the following element. You can also insert or delete an element without needing to iterate and remember the prior element's link. This comes at a cost. You are adding storage to each element for the second link, and you are adding processing overhead to the insert and delete operation. You have to determine the tradeoff.


How do you delete an ith element in singly linked list?

You will need to traverse the list i times to get to the element you want deleted. Each time you go thru the list you will need to remember the forward pointer from the previous element because when you get to the element you want to delete the previous forward pointer needs to be pointed to the I + 1 element of the list.


An algorithm of deleting linked list?

To delete a linked list walk through the list and delete the memory allocated to each element, remembering the next element address, and then iterating or recursing the process using the next element address, until the next element address is null.


Write a program to delete an array element in c?

Enter how many elements you want to enter: 4 Enter the array elements: 10 20 30 40 Enter the location, where you want to delete: 3 Deleted value : 30 The new element list: 10 20 40


What is the difference between linked list and Ordinary list?

A list is an abstract data structure, usually defined as an ordered collection of data. A linked list refers to a specific implementation of a list in which each element in the list is connected (linked) to the next element.


What is list?

Each element has a pointer to the next element, except for the last one.


Write algorithm to delete last node in circular linked list?

This depends on whether the list is singly or doubly(or multiply) linked, and on the actual implementation of the list. For example, you can write a CDLL(circular doubly linked list) without maintaining your beginning or ending nodes, using only a current pointer, thus this question doesn't really apply as there would be no "last" node and thus it would be like deleting any node.A typical implementation of a circular singly-linked list (CSLL) list actually maintains the pointer to the last element (hence it's FIFO nature) and thus there are both last and first nodes.This deletion is a little tricky. Consider that you have situations where the next pointer will point to the current element. On the other hand, you also have a situation where there are n-values that you have to iterate over to find the next-to-last value. Typically you would delete the first node in these lists, again dictated by the FIFO nature of these lists, but deletion of the last node is also not impossible.set struct node *last to list->endif (list->end->next == list->end){set list->end to null (leaving an empty list)} else {while(true){if(last->next == list->end){break}set last to last->next}set link->last to list->end->next (this temporarily sets list's end node to current first node)free last->next (frees the last node)set last->next to list->end (set the new last node next pointer to the first node)set list->end to last (set the list's end node to the new last node)}


What is single list?

Each element has a pointer to the next element, except for the last one.


Adavantage of pointer based of a list over an array?

One advantage of a linked list (with pointers) is that it is fairly cheap to insert or delete an element - once you know where it is. A disadvantage is that getting a specific element - for example, the 1000th. element - is expensive.


Delete a node of the last element in the linked list algorithm in java?

Well if you're using the build in LinkedList class, you can simply call list.removeLast(). But let's assume you're not: class LinkedList { LinkedListNode root; public void removeLast() { // special case for an empty list if( root null ) { root = null; return; } // iterate through list, starting at root LinkedListNode last = null; LinkedListNode current = root; // when current.next is null, current is the last element of this list and // last will become the new last element while( current.next != null ) { last = current; current = current.next; } // cut off current last.next = null; } private class LinkedListNode { Object data; LinkedListNode next = null; } }