| | |
Linked List functions
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
Just trying to get my head around linked lists and I'm trying to all the standard container functions..
For a singly-linked list are these algorithms correct?
Cheers!
For a singly-linked list are these algorithms correct?
C++ Syntax (Toggle Plain Text)
void pop_front(const T& t) { // Deallocate memory pointed to by head node<T>* tempNode = head; delete head; // Get new head head = tempNode->next; } void pop_back(const T& t) { // Deallocate memory pointed to by tail delete tail; // Get new tail node<T>* currentNode = head; for (size_t i=0; i<sizet-2; i++) { currentNode = currentNode->next; } tail = currentNode; tail->next = NULL; }
Cheers!
pop_front is wrong. First you make a copy of head then delete it. When you delete head that also invalidates the copy tempNode. What you want to do is set head then delete tempNode
What are you going to use that parameter for ?
What are you going to use that parameter for ?
C++ Syntax (Toggle Plain Text)
void pop_front(const T& t) { if( head != NULL) { node<T>* tempNode = head; // Get new head head = tempNode->next; // OR just this head = head->next; // Deallocate memory pointed to by head delete tempNode; } }
Last edited by Ancient Dragon; Dec 28th, 2007 at 12:38 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Yeah that makes sense, cheers!
(The parameters were just a careless copy+paste job from push_front() and push_back().)
I'm still not sure about pop_back though.. "size-2" doesn't seem very elegant. I was trying to look through the std::vector header file for some inspiration but it's hard to make sense of it..
(The parameters were just a careless copy+paste job from push_front() and push_back().)
I'm still not sure about pop_back though.. "size-2" doesn't seem very elegant. I was trying to look through the std::vector header file for some inspiration but it's hard to make sense of it..
you could just use a while statement
C++ Syntax (Toggle Plain Text)
void pop_back(const T& t) { // Deallocate memory pointed to by tail delete tail; tail = NULL; // Get new tail node<T>* currentNode = head; while(currentNode->next != NULL) { tail = currentNode; currentNode = currentNove->next; } if( currentNode == head) { head = tail = NULL; } }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- passing linked lists through functions?? (C++)
- Circular linked list (C)
- Linked List (C)
- "doubly linked list" question (C)
- linked list library (C)
- Double Linked Lists and Functions required (C++)
Other Threads in the C++ Forum
- Previous Thread: Initialising
- Next Thread: macro building program help
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline graph homeworkhelper iamthwee ifstream input int integer java lib linux list loop looping loops map math matrix memory multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






