| | |
Linked List
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2007
Posts: 35
Reputation:
Solved Threads: 0
Hello,
Please i really need your help.
I am trying writing a code that will delete the last element of the list. However, i keep getting 0 or when i tweak it, i get a message asking me to abort what i am doing.
In this code, i append some values to the list and ask it to do some functions like get first, get last, remove first...these functions work fine. However, i cannot seem to get the remLast right.
thank you very much in advance.
Here is the code:
Please i really need your help.
I am trying writing a code that will delete the last element of the list. However, i keep getting 0 or when i tweak it, i get a message asking me to abort what i am doing.
In this code, i append some values to the list and ask it to do some functions like get first, get last, remove first...these functions work fine. However, i cannot seem to get the remLast right.
thank you very much in advance.
Here is the code:
C++ Syntax (Toggle Plain Text)
int LList::remLast()//this does not have parameters. { if(size==0){ cout<<"Empty List --- Removal is not"; cout<<"Possible --- Error"<<endl; return -1; } Lnode *ptemp; Lnode *temp=tail;; if(size==1) head=tail=NULL; ptemp->next = temp->next; size--; if(tail==temp) tail = ptemp; delete temp; }
What makes you think that code will work? Write your code again from scratch and give an explanation of what you're doing and why. If at some point you find yourself at a loss as to what you're doing or why you're doing it, explain how so. Having to put things into words will force you to understand what you're doing.
It looks like your code is a bit foggy on what it ought to be doing.
Get out a piece of paper and pencil and draw yourself a linked list, complete with little arrows pointing from node to node.
Then using the eraser and pencil change the drawing so that the last node is properly unlinked and deleted.
Do that exercise for a list of one node and a list of more than one node.
Hope this helps.
Get out a piece of paper and pencil and draw yourself a linked list, complete with little arrows pointing from node to node.
Then using the eraser and pencil change the drawing so that the last node is properly unlinked and deleted.
Do that exercise for a list of one node and a list of more than one node.
Hope this helps.
•
•
Join Date: Feb 2007
Posts: 35
Reputation:
Solved Threads: 0
Hi,
Thank you for your help. what u wrote down is what i have been trying to do but i am having a problem expressing how to move the pointer to the node before the last node after deleting the actual last node.
I only know how to move forward in a list and not go backwards.
Any assistance is greatly appreciated
Thank you very much.
Thank you for your help. what u wrote down is what i have been trying to do but i am having a problem expressing how to move the pointer to the node before the last node after deleting the actual last node.
I only know how to move forward in a list and not go backwards.
Any assistance is greatly appreciated
Thank you very much.
•
•
Join Date: Jan 2008
Posts: 4
Reputation:
Solved Threads: 0
Hi Olams,
Respectfully there are some fundamental concepts you seem to be struggling with.
You said you needed help moving pointers - that you know how to move forward but not backward.
Break the ideas down into the machine level. Remember, a pointer is just a memory address - it's a number that identifies a place in memory. So a pointer itself does not advance forward or backward as such. You can increment the number (add 1) but that assumes that the memory you're dealing with is all together - contiguous blocks - as what happens when indexing through an array.
Perhaps you're working on a coding exercise (project from school?) and you're supposed to extend a single linked list class into a double linked list class.
If that's the case, your Lnode class should have forward going pointers - that point to the next Lnode in the list (it's really more like a chain) and there should be a backward pointer to the previous node. Using C++ structs this might look like this:
As you can see, each node is really in a two-way chain.
Presumably the remove-last method you refer to is supposed to remove the last node and delete it.
So you first need to remove it from the list (like cutting the links in a chain).
Try using local variables to give you clear signs of what you're dealing with. That is, instead of worrying about tail -> prev why not assign a meaningful local var?
Hope that helps. I'll let you figure out how to deal with the case where there is only one node in the list.
Remember - make your variable names as clear as you can. Names like "temp" aren't that useful. Use a few variables - they're free! Use them for specific purposes and give them names that indicate the purpose.
Respectfully there are some fundamental concepts you seem to be struggling with.
You said you needed help moving pointers - that you know how to move forward but not backward.
Break the ideas down into the machine level. Remember, a pointer is just a memory address - it's a number that identifies a place in memory. So a pointer itself does not advance forward or backward as such. You can increment the number (add 1) but that assumes that the memory you're dealing with is all together - contiguous blocks - as what happens when indexing through an array.
Perhaps you're working on a coding exercise (project from school?) and you're supposed to extend a single linked list class into a double linked list class.
If that's the case, your Lnode class should have forward going pointers - that point to the next Lnode in the list (it's really more like a chain) and there should be a backward pointer to the previous node. Using C++ structs this might look like this:
// A doubly linked-list node
//
struct ListNode {
ListNode * next_node;
ListNode * prev_node;
void * ptr_to_data;
};// A linked list.
//
struct List {
ListNode * head;
ListNode * tail;
int length;
};Presumably the remove-last method you refer to is supposed to remove the last node and delete it.
So you first need to remove it from the list (like cutting the links in a chain).
Try using local variables to give you clear signs of what you're dealing with. That is, instead of worrying about tail -> prev why not assign a meaningful local var?
if (size > 1) {
/////////////////
// This code deals with the case that the tail is not the only one in the list.
/////////////////
//
ListNode * new_tail = tail -> prev;
// Unlink new tail from the old tail.
new_tail -> next = NULL;
// We probably need to delete the data item the tail pointed to??
if ( tail -> ptr_to_data != NULL) {
delete tail -> ptr_to_data;
}
delete tail;
tail = new_tail;
}Hope that helps. I'll let you figure out how to deal with the case where there is only one node in the list.
Remember - make your variable names as clear as you can. Names like "temp" aren't that useful. Use a few variables - they're free! Use them for specific purposes and give them names that indicate the purpose.
Don't tell someone to add complexity. There is no need to add more pointers. People delete the last element of singly-linked lists all the time.
Olams, assuming your list really is a singly-linked list, to delete the last node you will need to start at the head node and traverse the list until the next node is the last node. Then you can delete the last node. Make sense? You have to look ahead a little as you go.
Hope this helps.
Olams, assuming your list really is a singly-linked list, to delete the last node you will need to start at the head node and traverse the list until the next node is the last node. Then you can delete the last node. Make sense? You have to look ahead a little as you go.
Hope this helps.
•
•
Join Date: Jan 2008
Posts: 4
Reputation:
Solved Threads: 0
•
•
•
•
Don't tell someone to add complexity. There is no need to add more pointers. People delete the last element of singly-linked lists all the time.
He did indicate that he wanted to navigate bi-directionally in his list. I was reading between the lines that he was 'supposed' to extend a list because - let's face it - you'd only implement linked-lists in C++ as an exercise. Any commercial dev would use STL.
•
•
•
•
Olams, assuming your list really is a singly-linked list, to delete the last node you will need to start at the head node and traverse the list until the next node is the last node. Then you can delete the last node. Make sense? You have to look ahead a little as you go.
Hope this helps.
•
•
•
•
Originally Posted by wazzam
What? Did you read the original question?
•
•
•
•
Originally Posted by wazzam
He did indicate that he wanted to navigate bi-directionally in his list.
In fact, in post #4 he did say
•
•
•
•
Originally Posted by olams
I only know how to move forward in a list and not go backwards.
•
•
•
•
Originally Posted by wazzam
I was reading between the lines that he was 'supposed' to extend a list because
•
•
•
•
Originally Posted by wazzam
because - let's face it - you'd only implement linked-lists in C++ as an exercise. Any commercial dev would use STL.
•
•
•
•
Originally Posted by wassam
Olam's code fragment did mention 'tail' ergo to remove the last item in a list he does not need to walk the list to find it. Don't be so quick to criticise other people's attempts to help - especially if you don't read the original posting's question.
To delete the last node in a linked list you need to find the penultimate node, not the last.
Assuming his code is a singly-linked list (which, with what we have been given, is all we can safely assume) he will need to walk the list to find it. If, in fact, he does have a doubly-linked list, then by all means use last->previous.
Don't be so quick to return offense where none is given. And don't start personal attacks when you disagree.
I've been teaching people to program for a long time, and one of the most frustrating obstacles to new programmers is people who answer with all kinds of information about how to improve, or add to, or change their project when a more direct answer is available. The most common cause of this problem is that the helper thinks he has a better understanding of the helpee's problem with but a glance than is warranted. The first step is to be sure you understand the OP's constraints, then advise appropriately. Don't assume more than you've been given. Notice how brk235 kindly asked for more information?
![]() |
Similar Threads
- Removing an item from head of linked list (C)
- help implementing singly linked list (C++)
- How to read in a sentence and insert in to linked list? (C++)
- Linked List using pointers (C++ ADT) (C++)
- Why doesn't this remove the last node in a linked list? (C++)
- Cannot figure out how to implement linked list and rbtree for a project! (Java)
- Linked List (C++)
- help by sorting a simply linked list (C)
Other Threads in the C++ Forum
- Previous Thread: sort without camparison?
- Next Thread: Creating a quicksort
| Thread Tools | Search this Thread |
api array based beginner bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project python random read recursion recursive return sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






