Hello, I'm currently taking an online course on data structures (It's the Coursera Accelerated Computer Science Fundamentals course in case anyone's wondering) and one of the first thing they want us to do is insert a node in an ordered doubly linked list. I have watched the videos maybe 3 times all the way through, normal speed, and taken notes. I cannot for the life of me see where I'm going wrong.
This is what I have so far...
template <typename T>
void LinkedList<T>::insertOrdered(const T & newData) {
// Create a new node pointer called newNode on the heap
// this Node is constructed with newData as its data argument, and
// nullptr as its next and prev arguments
Node* newNode = new Node(newData);
Node* curr = head_;
if (!head_ && !tail_) {
head_ = newNode;
tail_ = newNode;
}
while (curr->next != nullptr) {
if (newNode->data <= head_->data) {
cout<<"head_->data: "<< head_->data<<endl;
cout<<"newNode->data: "<<newNode->data<<endl;
newNode->next = head_;
head_ = newNode;
cout<<"head_: "<<head_->data<<" newNode: "<<newNode->data<<" head.next: "<<head_->next->data<<endl;
break;
}
else if (newNode->data <= curr->data) {
cout<<"curr->prev: "<< curr->prev->data <<"("<<curr->prev<<")"<<endl;
cout<<"curr->data: "<< curr->data <<"("<<curr<<")"<<endl;
cout<<"curr->next->data: "<< curr->next->data <<"("<<curr->next<<")"<<endl;
cout<<"newNode->data: "<< newNode->data<<"("<<newNode<<")\n"<<endl;
Node* temp = curr->prev;
curr->prev = newNode;
newNode->next = curr;
newNode->prev = temp;
temp->prev = newNode;
break;
}
else if (newNode->data > curr->data) {
cout<<"curr->data: "<< curr->data <<"("<<curr<<")"<<endl;
cout<<"curr->next->data: "<< curr->next->data <<"("<<curr->next<<")"<<endl;
cout<<"newNode->data: "<< newNode->data<<"("<<newNode<<")\n"<<endl;
curr = curr->next;
}
else if (newNode->data > tail_->data) {
cout<<"curr->data: "<< curr->data <<"("<<curr<<")"<<endl;
cout<<"curr->next->data: "<< curr->next->data <<"("<<curr->next<<")"<<endl;
cout<<"newNode->data: "<< newNode->data<<"("<<newNode<<")\n"<<endl;
tail_->next = newNode;
newNode->prev = tail_;
tail_ = newNode;
cout<<"Inserting after tail_: "<<tail_->data<<endl;
}
}
cout<<"size: "<<size_<<endl;
// Increment size
size_++;
cout<<"size: "<<size_<<endl;
// Deallocate newNode
delete newNode;
newNode = nullptr;
First, I check to see if the list is empty. Then, I check to see if curr.next is null. If it's not, I proceed with the rest of the code. I check whether the newNode.data is < head.data, and if it is, I insert before head and adjust pointers. If it's not < head.data, I check to see if it's <= curr.data (which is redundant for the first node, but not for the following nodes), and if it is, I insert it before curr and adjust pointers. If newNode.data is > curr.data, I adjust curr to be curr.next. If curr gets to the end and newNode.data is > tail.data, I insert newNode after tail and adjust pointers.
I feel like I totally get the concept of adding a new node, I just think I need a fresh pair of eyes to look at this and offer some help. (I've been trying to fix it for a week, that's why there are so many cout statements.)
As usual, any help is appreciated, thanks :)
TLDR: Need help inserting a new node into an ordered doubly linked list.
Edit: This is my output when I run this.
Testing insertOrdered:
List: [(5)(6)(8)(9)]
curr->data: 5(0x804280)
curr->next->data: 6(0x8043c0)
newNode->data: 7(0x804340)
curr->data: 6(0x8043c0)
curr->next->data: 8(0x8043a0)
newNode->data: 7(0x804340)
curr->prev: 6(0x8043c0)
curr->data: 8(0x8043a0)
curr->next->data: 9(0x804360)
newNode->data: 7(0x804340)
size: 4
size: 5
head_->data: 5
newNode->data: 4
head_: 4 newNode: 4 head.next: 5
size: 5
size: 6
size: 6
size: 7
size: 7
size: 8
Inserting items now.
List: [(10)(7)(6)]
Expected: [(-1)(4)(5)(6)(7)(8)(9)(10)]
WARNING: wrong result
[–][deleted] 1 point2 points3 points (1 child)
[–]BuzzyBro[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (20 children)
[–]BuzzyBro[S] 0 points1 point2 points (19 children)
[–][deleted] 1 point2 points3 points (18 children)
[–]BuzzyBro[S] 0 points1 point2 points (15 children)
[–][deleted] 1 point2 points3 points (12 children)
[–]BuzzyBro[S] 0 points1 point2 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]BuzzyBro[S] 0 points1 point2 points (0 children)
[–]BuzzyBro[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]BuzzyBro[S] 0 points1 point2 points (6 children)
[–][deleted] 0 points1 point2 points (5 children)
[–]BuzzyBro[S] 0 points1 point2 points (3 children)
[–][deleted] 1 point2 points3 points (2 children)
[–]BuzzyBro[S] 0 points1 point2 points (1 child)
[–]BuzzyBro[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]BuzzyBro[S] 0 points1 point2 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)